Passed
Pull Request — dev (#850)
by Uwe
08:28 queued 07:10
created

minimal_invest   A

Complexity

Total Complexity 0

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 0
eloc 89
dl 0
loc 163
rs 10
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
"""
3
Example that shows how to use "Offset-Invest".
4
5
Installation requirements
6
-------------------------
7
This example requires oemof.solph (v0.5.x), install by:
8
9
    pip install oemof.solph[examples]
10
11
License
12
-------
13
Johannes Röder <https://www.uni-bremen.de/en/res/team/johannes-roeder-m-sc>
14
15
`MIT license <https://github.com/oemof/oemof-solph/blob/dev/LICENSE>`_
16
17
"""
18
19
import logging
20
import os
21
22
import pandas as pd
23
from matplotlib import pyplot as plt
24
25
from oemof import solph
26
27
data = [0, 15, 30, 35, 20, 25, 27, 10, 5, 2, 15, 40, 20, 0, 0]
28
29
# create an energy system
30
idx = solph.create_time_index(2017, number=len(data))
31
es = solph.EnergySystem(timeindex=idx, infer_last_interval=False)
32
33
bus_0 = solph.Bus(label="bus_0")
34
bus_1 = solph.Bus(label="bus_1")
35
es.add(bus_0, bus_1)
36
37
c_0 = 10
38
c_1 = 100
39
40
es.add(
41
    solph.components.Source(
42
        label="source_0", outputs={bus_0: solph.Flow(variable_costs=c_0)}
43
    )
44
)
45
46
es.add(
47
    solph.components.Source(
48
        label="source_1", outputs={bus_1: solph.Flow(variable_costs=c_1)}
49
    )
50
)
51
52
es.add(
53
    solph.components.Sink(
54
        label="demand", inputs={bus_1: solph.Flow(fix=data, nominal_value=1)}
55
    )
56
)
57
58
# solph.Sink(label='excess_1', inputs={
59
#     bus_1: solph.Flow()})
60
61
# parameter
62
p_install_min = 20
63
p_install_max = 35000
64
c_fix = 2000
65
c_var = 180
66
eta = 0.8
67
68
# non offset invest
69
trafo = solph.components.Transformer(
70
    label="transformer",
71
    inputs={bus_0: solph.Flow()},
72
    outputs={
73
        bus_1: solph.Flow(
74
            nominal_value=None,
75
            investment=solph.Investment(
76
                ep_costs=c_var,
77
                maximum=p_install_max,
78
                minimum=p_install_min,
79
                # existing=10,
80
                nonconvex=True,
81
                offset=c_fix,
82
            ),
83
            # min=0.1,
84
            # fixed=True,
85
            # actual_value=[0.5, 0.5, 0.5, 0.5, 0.5],
86
        )
87
    },
88
    conversion_factors={bus_1: eta},
89
)
90
es.add(trafo)
91
# create an optimization problem and solve it
92
om = solph.Model(es)
93
94
# export lp file
95
filename = os.path.join(
96
    solph.helpers.extend_basic_path("lp_files"), "OffsetInvestor.lp"
97
)
98
logging.info("Store lp-file in {0}.".format(filename))
99
om.write(filename, io_options={"symbolic_solver_labels": True})
100
101
# solve model
102
om.solve(solver="cbc", solve_kwargs={"tee": True})
103
104
# create result object
105
results = solph.processing.results(om)
106
107
bus1 = solph.views.node(results, "bus_1")["sequences"]
108
109
# plot the time series (sequences) of a specific component/bus
110
if plt is not None:
111
    bus1.plot(kind="line", drawstyle="steps-mid")
112
    plt.legend()
113
    plt.show()
114
115
# Nachvollziehen der Berechnung
116
# Kosten Invest
117
p_invest = solph.views.node(results, "transformer")["scalars"][
118
    (("transformer", "bus_1"), "invest")
119
]
120
invest_binary = solph.views.node(results, "transformer")["scalars"][
121
    (("transformer", "bus_1"), "invest_status")
122
]
123
c_invest = p_invest * c_var + c_fix * invest_binary
124
125
# costs analysis
126
e_source_0 = solph.views.node(results, "source_0")["sequences"][
127
    (("source_0", "bus_0"), "flow")
128
].sum()
129
c_source_0 = c_0 * e_source_0
130
e_source_1 = solph.views.node(results, "source_1")["sequences"][
131
    (("source_1", "bus_1"), "flow")
132
].sum()
133
c_source_1 = c_1 * e_source_1
134
135
c_total = c_invest + c_source_0 + c_source_1
136
137
es.results["meta"] = solph.processing.meta_results(om)
138
objective = pd.DataFrame.from_dict(es.results["meta"]).at[
139
    "Lower bound", "objective"
140
]
141
142
print("  objective:", objective)
143
print("  berechnet:", c_total)
144
145
print("")
146
print("Max. zulässige Investleistung", p_install_max)
147
print("Erforderlicher Mindest-Invest", p_install_min)
148
print("Installierte Leistung:", p_invest)
149
print(
150
    "Maximale Leistung - demand:",
151
    solph.views.node(results, "bus_1")["sequences"][
152
        (("bus_1", "demand"), "flow")
153
    ].max(),
154
)
155
print(
156
    "Maximale Leistung im Einsatz",
157
    solph.views.node(results, "transformer")["sequences"][
158
        (("transformer", "bus_1"), "flow")
159
    ].max(),
160
)
161
if p_invest > max(data):
162
    print("Anlage wurde überdimensioniert")
163