| Conditions | 2 |
| Total Lines | 78 |
| Code Lines | 45 |
| 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 -*- |
||
| 118 | def main(): |
||
| 119 | datetimeindex = pd.date_range("1/1/2017", periods=3, freq="H") |
||
| 120 | |||
| 121 | es = EnergySystem(timeindex=datetimeindex, infer_last_interval=False) |
||
| 122 | |||
| 123 | b_0 = Bus(label="b_0") |
||
| 124 | |||
| 125 | b_1 = Bus(label="b_1") |
||
| 126 | |||
| 127 | es.add(b_0, b_1) |
||
| 128 | |||
| 129 | es.add( |
||
| 130 | Link( |
||
| 131 | label="line_0", |
||
| 132 | inputs={b_0: Flow(), b_1: Flow()}, |
||
| 133 | outputs={ |
||
| 134 | b_1: Flow(investment=Investment()), |
||
| 135 | b_0: Flow(investment=Investment()), |
||
| 136 | }, |
||
| 137 | conversion_factors={(b_0, b_1): 0.95, (b_1, b_0): 0.9}, |
||
| 138 | limit_direction=False, |
||
| 139 | ) |
||
| 140 | ) |
||
| 141 | |||
| 142 | es.add( |
||
| 143 | cmp.Source( |
||
| 144 | label="gen_0", |
||
| 145 | outputs={b_0: Flow(nominal_value=100, variable_costs=50)}, |
||
| 146 | ) |
||
| 147 | ) |
||
| 148 | |||
| 149 | es.add( |
||
| 150 | cmp.Source( |
||
| 151 | label="gen_1", |
||
| 152 | outputs={b_1: Flow(nominal_value=100, variable_costs=50)}, |
||
| 153 | ) |
||
| 154 | ) |
||
| 155 | |||
| 156 | es.add( |
||
| 157 | cmp.Sink( |
||
| 158 | label="load_0", inputs={b_0: Flow(nominal_value=150, fix=[0, 1])} |
||
| 159 | ) |
||
| 160 | ) |
||
| 161 | |||
| 162 | es.add( |
||
| 163 | cmp.Sink( |
||
| 164 | label="load_1", inputs={b_1: Flow(nominal_value=150, fix=[1, 0])} |
||
| 165 | ) |
||
| 166 | ) |
||
| 167 | |||
| 168 | m = Model(energysystem=es) |
||
| 169 | |||
| 170 | # m.write('transshipment.lp', io_options={'symbolic_solver_labels': True}) |
||
| 171 | |||
| 172 | m.solve(solver="cbc", solve_kwargs={"tee": True, "keepfiles": False}) |
||
| 173 | |||
| 174 | m.results() |
||
| 175 | |||
| 176 | graph = create_nx_graph(es, m) |
||
| 177 | |||
| 178 | if pygz is not None: |
||
| 179 | draw_graph( |
||
| 180 | graph, |
||
| 181 | plot=True, |
||
| 182 | layout="neato", |
||
| 183 | node_size=3000, |
||
| 184 | node_color={"b_0": "#cd3333", "b_1": "#7EC0EE", "b_2": "#eeac7e"}, |
||
| 185 | ) |
||
| 186 | |||
| 187 | results = processing.results(m) |
||
| 188 | |||
| 189 | print(views.node(results, "gen_0")) |
||
| 190 | print(views.node(results, "gen_1")) |
||
| 191 | |||
| 192 | views.node(results, "line_0")["sequences"].plot(kind="bar") |
||
| 193 | |||
| 194 | # look at constraints of Links in the pyomo model LinkBlock |
||
| 195 | m.LinkBlock.pprint() |
||
| 196 | |||
| 200 |