| Conditions | 4 |
| Total Lines | 186 |
| Code Lines | 99 |
| 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 -*- |
||
| 232 | def _create(self, group=None): |
||
| 233 | r"""Creates sets, variables and constraints for FlowBlock |
||
| 234 | with investment attribute of type class:`.Investment`. |
||
| 235 | |||
| 236 | Parameters |
||
| 237 | ---------- |
||
| 238 | group : list |
||
| 239 | List containing tuples containing flow (f) objects that have an |
||
| 240 | attribute investment and the associated source (s) and target (t) |
||
| 241 | of flow e.g. groups=[(s1, t1, f1), (s2, t2, f2),..] |
||
| 242 | """ |
||
| 243 | if group is None: |
||
| 244 | return None |
||
| 245 | |||
| 246 | m = self.parent_block() |
||
| 247 | |||
| 248 | # ######################### SETS ##################################### |
||
| 249 | self.INVESTFLOWS = Set(initialize=[(g[0], g[1]) for g in group]) |
||
| 250 | |||
| 251 | self.CONVEX_INVESTFLOWS = Set( |
||
| 252 | initialize=[ |
||
| 253 | (g[0], g[1]) |
||
| 254 | for g in group |
||
| 255 | if g[2].investment.nonconvex is False |
||
| 256 | ] |
||
| 257 | ) |
||
| 258 | |||
| 259 | self.NON_CONVEX_INVESTFLOWS = Set( |
||
| 260 | initialize=[ |
||
| 261 | (g[0], g[1]) |
||
| 262 | for g in group |
||
| 263 | if g[2].investment.nonconvex is True |
||
| 264 | ] |
||
| 265 | ) |
||
| 266 | |||
| 267 | self.FIXED_INVESTFLOWS = Set( |
||
| 268 | initialize=[(g[0], g[1]) for g in group if g[2].fix[0] is not None] |
||
| 269 | ) |
||
| 270 | |||
| 271 | self.NON_FIXED_INVESTFLOWS = Set( |
||
| 272 | initialize=[(g[0], g[1]) for g in group if g[2].fix[0] is None] |
||
| 273 | ) |
||
| 274 | |||
| 275 | self.MAX_CAPACITY_FACTOR_INVESTFLOWS = Set( |
||
| 276 | initialize=[ |
||
| 277 | (g[0], g[1]) |
||
| 278 | for g in group |
||
| 279 | if g[2].max_capacity_factor is not None |
||
| 280 | ] |
||
| 281 | ) |
||
| 282 | |||
| 283 | self.MIN_CAPACITY_FACTOR_INVESTFLOWS = Set( |
||
| 284 | initialize=[ |
||
| 285 | (g[0], g[1]) |
||
| 286 | for g in group |
||
| 287 | if g[2].min_capacity_factor is not None |
||
| 288 | ] |
||
| 289 | ) |
||
| 290 | |||
| 291 | self.MIN_INVESTFLOWS = Set( |
||
| 292 | initialize=[ |
||
| 293 | (g[0], g[1]) |
||
| 294 | for g in group |
||
| 295 | if (g[2].min[0] != 0 or len(g[2].min) > 1) |
||
| 296 | ] |
||
| 297 | ) |
||
| 298 | |||
| 299 | # ######################### VARIABLES ################################# |
||
| 300 | def _investvar_bound_rule(block, i, o): |
||
| 301 | """Rule definition for bounds of invest variable.""" |
||
| 302 | if (i, o) in self.CONVEX_INVESTFLOWS: |
||
| 303 | return ( |
||
| 304 | m.flows[i, o].investment.minimum, |
||
|
|
|||
| 305 | m.flows[i, o].investment.maximum, |
||
| 306 | ) |
||
| 307 | elif (i, o) in self.NON_CONVEX_INVESTFLOWS: |
||
| 308 | return 0, m.flows[i, o].investment.maximum |
||
| 309 | |||
| 310 | # create invest variable for a investment flow |
||
| 311 | self.invest = Var( |
||
| 312 | self.INVESTFLOWS, |
||
| 313 | within=NonNegativeReals, |
||
| 314 | bounds=_investvar_bound_rule, |
||
| 315 | ) |
||
| 316 | |||
| 317 | # create status variable for a non-convex investment flow |
||
| 318 | self.invest_status = Var(self.NON_CONVEX_INVESTFLOWS, within=Binary) |
||
| 319 | # ######################### CONSTRAINTS ############################### |
||
| 320 | |||
| 321 | def _min_invest_rule(block, i, o): |
||
| 322 | """Rule definition for applying a minimum investment""" |
||
| 323 | expr = ( |
||
| 324 | m.flows[i, o].investment.minimum * self.invest_status[i, o] |
||
| 325 | <= self.invest[i, o] |
||
| 326 | ) |
||
| 327 | return expr |
||
| 328 | |||
| 329 | self.minimum_rule = Constraint( |
||
| 330 | self.NON_CONVEX_INVESTFLOWS, rule=_min_invest_rule |
||
| 331 | ) |
||
| 332 | |||
| 333 | def _max_invest_rule(block, i, o): |
||
| 334 | """Rule definition for applying a minimum investment""" |
||
| 335 | expr = self.invest[i, o] <= ( |
||
| 336 | m.flows[i, o].investment.maximum * self.invest_status[i, o] |
||
| 337 | ) |
||
| 338 | return expr |
||
| 339 | |||
| 340 | self.maximum_rule = Constraint( |
||
| 341 | self.NON_CONVEX_INVESTFLOWS, rule=_max_invest_rule |
||
| 342 | ) |
||
| 343 | |||
| 344 | def _investflow_fixed_rule(block, i, o, t): |
||
| 345 | """Rule definition of constraint to fix flow variable |
||
| 346 | of investment flow to (normed) actual value |
||
| 347 | """ |
||
| 348 | expr = m.flow[i, o, t] == ( |
||
| 349 | (m.flows[i, o].investment.existing + self.invest[i, o]) |
||
| 350 | * m.flows[i, o].fix[t] |
||
| 351 | ) |
||
| 352 | |||
| 353 | return expr |
||
| 354 | |||
| 355 | self.fixed = Constraint( |
||
| 356 | self.FIXED_INVESTFLOWS, m.TIMESTEPS, rule=_investflow_fixed_rule |
||
| 357 | ) |
||
| 358 | |||
| 359 | def _max_investflow_rule(block, i, o, t): |
||
| 360 | """Rule definition of constraint setting an upper bound of flow |
||
| 361 | variable in investment case. |
||
| 362 | """ |
||
| 363 | expr = m.flow[i, o, t] <= ( |
||
| 364 | (m.flows[i, o].investment.existing + self.invest[i, o]) |
||
| 365 | * m.flows[i, o].max[t] |
||
| 366 | ) |
||
| 367 | return expr |
||
| 368 | |||
| 369 | self.max = Constraint( |
||
| 370 | self.NON_FIXED_INVESTFLOWS, m.TIMESTEPS, rule=_max_investflow_rule |
||
| 371 | ) |
||
| 372 | |||
| 373 | def _min_investflow_rule(block, i, o, t): |
||
| 374 | """Rule definition of constraint setting a lower bound on flow |
||
| 375 | variable in investment case. |
||
| 376 | """ |
||
| 377 | expr = m.flow[i, o, t] >= ( |
||
| 378 | (m.flows[i, o].investment.existing + self.invest[i, o]) |
||
| 379 | * m.flows[i, o].min[t] |
||
| 380 | ) |
||
| 381 | return expr |
||
| 382 | |||
| 383 | self.min = Constraint( |
||
| 384 | self.MIN_INVESTFLOWS, m.TIMESTEPS, rule=_min_investflow_rule |
||
| 385 | ) |
||
| 386 | |||
| 387 | def _max_capacity_factor_investflow_rule(block, i, o): |
||
| 388 | """Rule definition for build action of max. sum flow constraint |
||
| 389 | in investment case. |
||
| 390 | """ |
||
| 391 | expr = sum( |
||
| 392 | m.flow[i, o, t] * m.timeincrement[t] for t in m.TIMESTEPS |
||
| 393 | ) <= m.flows[i, o].max_capacity_factor * ( |
||
| 394 | self.invest[i, o] + m.flows[i, o].investment.existing |
||
| 395 | ) |
||
| 396 | return expr |
||
| 397 | |||
| 398 | self.max_capacity_factor = Constraint( |
||
| 399 | self.MAX_CAPACITY_FACTOR_INVESTFLOWS, |
||
| 400 | rule=_max_capacity_factor_investflow_rule, |
||
| 401 | ) |
||
| 402 | |||
| 403 | def _min_capacity_factor_investflow_rule(block, i, o): |
||
| 404 | """Rule definition for build action of min. sum flow constraint |
||
| 405 | in investment case. |
||
| 406 | """ |
||
| 407 | expr = sum( |
||
| 408 | m.flow[i, o, t] * m.timeincrement[t] for t in m.TIMESTEPS |
||
| 409 | ) >= ( |
||
| 410 | (m.flows[i, o].investment.existing + self.invest[i, o]) |
||
| 411 | * m.flows[i, o].min_capacity_factor |
||
| 412 | ) |
||
| 413 | return expr |
||
| 414 | |||
| 415 | self.min_capacity_factor = Constraint( |
||
| 416 | self.MIN_CAPACITY_FACTOR_INVESTFLOWS, |
||
| 417 | rule=_min_capacity_factor_investflow_rule, |
||
| 418 | ) |
||
| 443 |