| Conditions | 2 |
| Total Lines | 88 |
| Code Lines | 56 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | # -*- coding: utf-8 -*- |
||
| 116 | def main(): |
||
| 117 | datetimeindex = pd.date_range("1/1/2017", periods=2, freq="H") |
||
| 118 | |||
| 119 | es = EnergySystem(timeindex=datetimeindex) |
||
| 120 | |||
| 121 | b_el0 = ElectricalBus(label="b_0", v_min=-1, v_max=1) |
||
| 122 | |||
| 123 | b_el1 = ElectricalBus(label="b_1", v_min=-1, v_max=1) |
||
| 124 | |||
| 125 | b_el2 = ElectricalBus(label="b_2", v_min=-1, v_max=1) |
||
| 126 | |||
| 127 | es.add(b_el0, b_el1, b_el2) |
||
| 128 | |||
| 129 | es.add( |
||
| 130 | ElectricalLine( |
||
| 131 | input=b_el0, |
||
| 132 | output=b_el1, |
||
| 133 | reactance=0.0001, |
||
| 134 | investment=Investment(ep_costs=10), |
||
| 135 | min=-1, |
||
| 136 | max=1, |
||
| 137 | ) |
||
| 138 | ) |
||
| 139 | |||
| 140 | es.add( |
||
| 141 | ElectricalLine( |
||
| 142 | input=b_el1, |
||
| 143 | output=b_el2, |
||
| 144 | reactance=0.0001, |
||
| 145 | nominal_value=60, |
||
| 146 | min=-1, |
||
| 147 | max=1, |
||
| 148 | ) |
||
| 149 | ) |
||
| 150 | |||
| 151 | es.add( |
||
| 152 | ElectricalLine( |
||
| 153 | input=b_el2, |
||
| 154 | output=b_el0, |
||
| 155 | reactance=0.0001, |
||
| 156 | nominal_value=60, |
||
| 157 | min=-1, |
||
| 158 | max=1, |
||
| 159 | ) |
||
| 160 | ) |
||
| 161 | |||
| 162 | es.add( |
||
| 163 | Source( |
||
| 164 | label="gen_0", |
||
| 165 | outputs={b_el0: Flow(nominal_value=100, variable_costs=50)}, |
||
| 166 | ) |
||
| 167 | ) |
||
| 168 | |||
| 169 | es.add( |
||
| 170 | Source( |
||
| 171 | label="gen_1", |
||
| 172 | outputs={b_el1: Flow(nominal_value=100, variable_costs=25)}, |
||
| 173 | ) |
||
| 174 | ) |
||
| 175 | |||
| 176 | es.add( |
||
| 177 | Sink(label="load", inputs={b_el2: Flow(nominal_value=100, fix=[1, 1])}) |
||
| 178 | ) |
||
| 179 | |||
| 180 | m = Model(energysystem=es) |
||
| 181 | |||
| 182 | # m.write('lopf.lp', io_options={'symbolic_solver_labels': True}) |
||
| 183 | |||
| 184 | m.solve(solver="cbc", solve_kwargs={"tee": True, "keepfiles": False}) |
||
| 185 | |||
| 186 | m.results() |
||
| 187 | |||
| 188 | graph = create_nx_graph(es) |
||
| 189 | |||
| 190 | if pygz is not None: |
||
| 191 | draw_graph( |
||
| 192 | graph, |
||
| 193 | plot=True, |
||
| 194 | layout="neato", |
||
| 195 | node_size=3000, |
||
| 196 | node_color={"b_0": "#cd3333", "b_1": "#7EC0EE", "b_2": "#eeac7e"}, |
||
| 197 | ) |
||
| 198 | |||
| 199 | results = processing.results(m) |
||
| 200 | |||
| 201 | print(views.node(results, "gen_0")["sequences"]) |
||
| 202 | print(views.node(results, "gen_1")["sequences"]) |
||
| 203 | print(views.node(results, "load")["sequences"]) |
||
| 204 | |||
| 208 |