Conditions | 32 |
Total Lines | 478 |
Code Lines | 164 |
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:
Complex classes like solph.flows._investment_flow_block.InvestmentFlowBlock._create_constraints() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | # -*- coding: utf-8 -*- |
||
266 | def _create_constraints(self): |
||
267 | r"""Creates all constraints for standard flows. |
||
268 | |||
269 | Depending on the attributes of the *InvestmentFlowBlock* |
||
270 | and *SimpleFlowBlock*, different constraints are created. |
||
271 | The following constraints are created |
||
272 | for all *InvestmentFlowBlock* objects:\ |
||
273 | |||
274 | Total capacity / energy |
||
275 | |||
276 | .. math:: |
||
277 | & |
||
278 | if \quad p=0:\\ |
||
279 | & |
||
280 | P_{total}(p) = P_{invest}(p) + P_{exist}(p) \\ |
||
281 | &\\ |
||
282 | & |
||
283 | else:\\ |
||
284 | & |
||
285 | P_{total}(p) = P_{total}(p-1) + P_{invest}(p) - P_{old}(p) \\ |
||
286 | &\\ |
||
287 | & |
||
288 | \forall p \in \textrm{PERIODS} |
||
289 | |||
290 | Upper bound for the flow value |
||
291 | |||
292 | .. math:: |
||
293 | & |
||
294 | P(p, t) \le ( P_{total}(p) ) \cdot f_{max}(t) \\ |
||
295 | & |
||
296 | \forall p, t \in \textrm{TIMEINDEX} |
||
297 | |||
298 | For a multi-period model, the old capacity is defined as follows: |
||
299 | |||
300 | .. math:: |
||
301 | & |
||
302 | P_{old}(p) = P_{old,exo}(p) + P_{old,end}(p)\\ |
||
303 | &\\ |
||
304 | & |
||
305 | if \quad p=0:\\ |
||
306 | & |
||
307 | P_{old,end}(p) = 0\\ |
||
308 | &\\ |
||
309 | & |
||
310 | else \quad if \quad l \leq year(p):\\ |
||
311 | & |
||
312 | P_{old,end}(p) = P_{invest}(p_{comm})\\ |
||
313 | &\\ |
||
314 | & |
||
315 | else:\\ |
||
316 | & |
||
317 | P_{old,end}(p) = 0\\ |
||
318 | &\\ |
||
319 | & |
||
320 | if \quad p=0:\\ |
||
321 | & |
||
322 | P_{old,exo}(p) = 0\\ |
||
323 | &\\ |
||
324 | & |
||
325 | else \quad if \quad l - a \leq year(p):\\ |
||
326 | & |
||
327 | P_{old,exo}(p) = P_{exist} (*)\\ |
||
328 | &\\ |
||
329 | & |
||
330 | else:\\ |
||
331 | & |
||
332 | P_{old,exo}(p) = 0\\ |
||
333 | &\\ |
||
334 | & |
||
335 | \forall p \in \textrm{PERIODS} |
||
336 | |||
337 | where: |
||
338 | |||
339 | * (*) is only performed for the first period the condition |
||
340 | is True. A decommissioning flag is then set to True |
||
341 | to prevent having falsely added old capacity in future periods. |
||
342 | * :math:`year(p)` is the year corresponding to period p |
||
343 | * :math:`p_{comm}` is the commissioning period of the flow |
||
344 | (which is determined by the model itself) |
||
345 | |||
346 | Depending on the attribute :attr:`nonconvex`, the constraints for the |
||
347 | bounds of the decision variable :math:`P_{invest}(p)` are different:\ |
||
348 | |||
349 | * :attr:`nonconvex = False` |
||
350 | |||
351 | .. math:: |
||
352 | & |
||
353 | P_{invest, min}(p) \le P_{invest}(p) \le P_{invest, max}(p) \\ |
||
354 | & |
||
355 | \forall p \in \textrm{PERIODS} |
||
356 | |||
357 | * :attr:`nonconvex = True` |
||
358 | |||
359 | .. math:: |
||
360 | & |
||
361 | P_{invest, min}(p) \cdot Y_{invest}(p) \le P_{invest}(p)\\ |
||
362 | & |
||
363 | P_{invest}(p) \le P_{invest, max}(p) \cdot Y_{invest}(p)\\ |
||
364 | &\\ |
||
365 | & |
||
366 | \forall p \in \textrm{PERIODS} |
||
367 | |||
368 | For all *InvestmentFlowBlock* objects |
||
369 | (independent of the attribute :attr:`nonconvex`), |
||
370 | the following additional constraints are created, if the appropriate |
||
371 | attribute of the *SimpleFlowBlock* |
||
372 | (see :class:`oemof.solph.flows._simple_flow_block.SimpleFlowBlock`) |
||
373 | is set: |
||
374 | |||
375 | * :attr:`fix` is not None |
||
376 | |||
377 | Actual value constraint for investments with fixed flow values |
||
378 | |||
379 | .. math:: |
||
380 | & |
||
381 | P(p, t) = P_{total}(p) \cdot f_{fix}(t) \\ |
||
382 | &\\ |
||
383 | & |
||
384 | \forall p, t \in \textrm{TIMEINDEX} |
||
385 | |||
386 | * :attr:`min != 0` |
||
387 | |||
388 | Lower bound for the flow values |
||
389 | |||
390 | .. math:: |
||
391 | & |
||
392 | P(p, t) \geq P_{total}(p) \cdot f_{min}(t) \\ |
||
393 | &\\ |
||
394 | & |
||
395 | \forall p, t \in \textrm{TIMEINDEX} |
||
396 | |||
397 | * :attr:`full_load_time_max is not None` |
||
398 | |||
399 | Upper bound for the sum of all flow values |
||
400 | (e.g. maximum full load hours) |
||
401 | |||
402 | .. math:: |
||
403 | \sum_{p, t} P(p, t) \cdot \tau(t) \leq P_{total}(p) |
||
404 | \cdot t_{full\_load, min} |
||
405 | |||
406 | * :attr:`full_load_time_min is not None` |
||
407 | |||
408 | Lower bound for the sum of all flow values |
||
409 | (e.g. minimum full load hours) |
||
410 | |||
411 | .. math:: |
||
412 | \sum_{p, t} P(t) \cdot \tau(t) \geq P_{total} |
||
413 | \cdot t_{full\_load, min} |
||
414 | |||
415 | * :attr:`overall_maximum` is not None |
||
416 | (for multi-period model only) |
||
417 | |||
418 | Overall maximum of total installed capacity / energy for flow |
||
419 | |||
420 | .. math:: |
||
421 | & |
||
422 | P_{total}(p) \leq P_{overall,max} \\ |
||
423 | &\\ |
||
424 | & |
||
425 | \forall p \in \textrm{PERIODS} |
||
426 | |||
427 | * :attr:`overall_minimum` is not None |
||
428 | (for multi-period model only) |
||
429 | |||
430 | Overall minimum of total installed capacity / energy for flow; |
||
431 | applicable only in last period |
||
432 | |||
433 | .. math:: |
||
434 | P_{total}(p_{last}) \geq P_{overall,min} |
||
435 | """ |
||
436 | m = self.parent_block() |
||
437 | |||
438 | self.minimum_rule = self._minimum_investment_constraint() |
||
439 | self.maximum_rule = self._maximum_investment_constraint() |
||
440 | |||
441 | # Handle unit lifetimes |
||
442 | def _total_capacity_rule(block): |
||
443 | """Rule definition for determining total installed |
||
444 | capacity (taking decommissioning into account) |
||
445 | """ |
||
446 | for i, o in self.INVESTFLOWS: |
||
447 | for p in m.PERIODS: |
||
448 | if p == 0: |
||
449 | expr = ( |
||
450 | self.total[i, o, p] |
||
451 | == self.invest[i, o, p] |
||
452 | + m.flows[i, o].investment.existing |
||
453 | ) |
||
454 | self.total_rule.add((i, o, p), expr) |
||
455 | # applicable for multi-period model only |
||
456 | else: |
||
457 | expr = ( |
||
458 | self.total[i, o, p] |
||
459 | == self.invest[i, o, p] |
||
460 | + self.total[i, o, p - 1] |
||
461 | - self.old[i, o, p] |
||
462 | ) |
||
463 | self.total_rule.add((i, o, p), expr) |
||
464 | |||
465 | self.total_rule = Constraint( |
||
466 | self.INVESTFLOWS, m.PERIODS, noruleinit=True |
||
467 | ) |
||
468 | self.total_rule_build = BuildAction(rule=_total_capacity_rule) |
||
469 | |||
470 | if m.es.periods is not None: |
||
471 | |||
472 | def _old_capacity_rule_end(block): |
||
473 | """Rule definition for determining old endogenously installed |
||
474 | capacity to be decommissioned due to reaching its lifetime. |
||
475 | Investment and decommissioning periods are linked within |
||
476 | the constraint. The respective decommissioning period is |
||
477 | determined for every investment period based on the components |
||
478 | lifetime and a matrix describing its age of each endogenous |
||
479 | investment. Decommissioning can only occur at the beginning of |
||
480 | each period. |
||
481 | |||
482 | Note |
||
483 | ---- |
||
484 | For further information on the implementation check |
||
485 | PR#957 https://github.com/oemof/oemof-solph/pull/957 |
||
486 | """ |
||
487 | for i, o in self.INVESTFLOWS: |
||
488 | lifetime = m.flows[i, o].investment.lifetime |
||
489 | if lifetime is None: |
||
490 | msg = ( |
||
491 | "You have to specify a lifetime " |
||
492 | "for a Flow with an associated " |
||
493 | "investment object in " |
||
494 | f"a multi-period model! Value for {(i, o)} " |
||
495 | "is missing." |
||
496 | ) |
||
497 | raise ValueError(msg) |
||
498 | |||
499 | # get the period matrix describing the temporal distance |
||
500 | # between all period combinations. |
||
501 | periods_matrix = m.es.periods_matrix |
||
502 | |||
503 | # get the index of the minimum value in each row greater |
||
504 | # equal than the lifetime. This value equals the |
||
505 | # decommissioning period if not zero. The index of this |
||
506 | # value represents the investment period. If np.where |
||
507 | # condition is not met in any row, min value will be zero |
||
508 | decomm_periods = np.argmin( |
||
509 | np.where( |
||
510 | (periods_matrix >= lifetime), |
||
511 | periods_matrix, |
||
512 | np.inf, |
||
513 | ), |
||
514 | axis=1, |
||
515 | ) |
||
516 | |||
517 | # no decommissioning in first period |
||
518 | expr = self.old_end[i, o, 0] == 0 |
||
519 | self.old_rule_end.add((i, o, 0), expr) |
||
520 | |||
521 | # all periods not in decomm_periods have no decommissioning |
||
522 | # zero is excluded |
||
523 | for p in m.PERIODS: |
||
524 | if p not in decomm_periods and p != 0: |
||
525 | expr = self.old_end[i, o, p] == 0 |
||
526 | self.old_rule_end.add((i, o, p), expr) |
||
527 | |||
528 | # multiple invests can be decommissioned in the same period |
||
529 | # but only sequential ones, thus a bookkeeping is |
||
530 | # introduced and constraints are added to equation one |
||
531 | # iteration later. |
||
532 | last_decomm_p = np.nan |
||
533 | # loop over invest periods (values are decomm_periods) |
||
534 | for invest_p, decomm_p in enumerate(decomm_periods): |
||
535 | # Add constraint of iteration before |
||
536 | # (skipped in first iteration by last_decomm_p = nan) |
||
537 | if (decomm_p != last_decomm_p) and ( |
||
538 | last_decomm_p is not np.nan |
||
539 | ): |
||
540 | expr = self.old_end[i, o, last_decomm_p] == expr |
||
541 | self.old_rule_end.add((i, o, last_decomm_p), expr) |
||
542 | |||
543 | # no decommissioning if decomm_p is zero |
||
544 | if decomm_p == 0: |
||
545 | # overwrite decomm_p with zero to avoid |
||
546 | # chaining invest periods in next iteration |
||
547 | last_decomm_p = 0 |
||
548 | |||
549 | # if decomm_p is the same as the last one chain invest |
||
550 | # period |
||
551 | elif decomm_p == last_decomm_p: |
||
552 | expr += self.invest[i, o, invest_p] |
||
553 | # overwrite decomm_p |
||
554 | last_decomm_p = decomm_p |
||
555 | |||
556 | # if decomm_p is not zero, not the same as the last one |
||
557 | # and it's not the first period |
||
558 | else: |
||
559 | expr = self.invest[i, o, invest_p] |
||
560 | # overwrite decomm_p |
||
561 | last_decomm_p = decomm_p |
||
562 | |||
563 | # Add constraint of very last iteration |
||
564 | if last_decomm_p != 0: |
||
565 | expr = self.old_end[i, o, last_decomm_p] == expr |
||
566 | self.old_rule_end.add((i, o, last_decomm_p), expr) |
||
567 | |||
568 | self.old_rule_end = Constraint( |
||
569 | self.INVESTFLOWS, m.PERIODS, noruleinit=True |
||
570 | ) |
||
571 | self.old_rule_end_build = BuildAction(rule=_old_capacity_rule_end) |
||
572 | |||
573 | def _old_capacity_rule_exo(block): |
||
574 | """Rule definition for determining old exogenously given |
||
575 | capacity to be decommissioned due to reaching its lifetime |
||
576 | """ |
||
577 | for i, o in self.INVESTFLOWS: |
||
578 | age = m.flows[i, o].investment.age |
||
579 | lifetime = m.flows[i, o].investment.lifetime |
||
580 | is_decommissioned = False |
||
581 | for p in m.PERIODS: |
||
582 | # No shutdown in first period |
||
583 | if p == 0: |
||
584 | expr = self.old_exo[i, o, p] == 0 |
||
585 | self.old_rule_exo.add((i, o, p), expr) |
||
586 | elif lifetime - age <= m.es.periods_years[p]: |
||
587 | # Track decommissioning status |
||
588 | if not is_decommissioned: |
||
589 | expr = ( |
||
590 | self.old_exo[i, o, p] |
||
591 | == m.flows[i, o].investment.existing |
||
592 | ) |
||
593 | is_decommissioned = True |
||
594 | else: |
||
595 | expr = self.old_exo[i, o, p] == 0 |
||
596 | self.old_rule_exo.add((i, o, p), expr) |
||
597 | else: |
||
598 | expr = self.old_exo[i, o, p] == 0 |
||
599 | self.old_rule_exo.add((i, o, p), expr) |
||
600 | |||
601 | self.old_rule_exo = Constraint( |
||
602 | self.INVESTFLOWS, m.PERIODS, noruleinit=True |
||
603 | ) |
||
604 | self.old_rule_exo_build = BuildAction(rule=_old_capacity_rule_exo) |
||
605 | |||
606 | def _old_capacity_rule(block): |
||
607 | """Rule definition for determining (overall) old capacity |
||
608 | to be decommissioned due to reaching its lifetime |
||
609 | """ |
||
610 | for i, o in self.INVESTFLOWS: |
||
611 | for p in m.PERIODS: |
||
612 | expr = ( |
||
613 | self.old[i, o, p] |
||
614 | == self.old_end[i, o, p] + self.old_exo[i, o, p] |
||
615 | ) |
||
616 | self.old_rule.add((i, o, p), expr) |
||
617 | |||
618 | self.old_rule = Constraint( |
||
619 | self.INVESTFLOWS, m.PERIODS, noruleinit=True |
||
620 | ) |
||
621 | self.old_rule_build = BuildAction(rule=_old_capacity_rule) |
||
622 | |||
623 | def _investflow_fixed_rule(block): |
||
624 | """Rule definition of constraint to fix flow variable |
||
625 | of investment flow to (normed) actual value |
||
626 | """ |
||
627 | for i, o in self.FIXED_INVESTFLOWS: |
||
628 | for p, t in m.TIMEINDEX: |
||
629 | expr = ( |
||
630 | m.flow[i, o, t] |
||
631 | == self.total[i, o, p] * m.flows[i, o].fix[t] |
||
632 | ) |
||
633 | self.fixed.add((i, o, p, t), expr) |
||
634 | |||
635 | self.fixed = Constraint( |
||
636 | self.FIXED_INVESTFLOWS, m.TIMEINDEX, noruleinit=True |
||
637 | ) |
||
638 | self.fixed_build = BuildAction(rule=_investflow_fixed_rule) |
||
639 | |||
640 | def _max_investflow_rule(block): |
||
641 | """Rule definition of constraint setting an upper bound of flow |
||
642 | variable in investment case. |
||
643 | """ |
||
644 | for i, o in self.NON_FIXED_INVESTFLOWS: |
||
645 | for p, t in m.TIMEINDEX: |
||
646 | expr = ( |
||
647 | m.flow[i, o, t] |
||
648 | <= self.total[i, o, p] * m.flows[i, o].max[t] |
||
649 | ) |
||
650 | self.max.add((i, o, p, t), expr) |
||
651 | |||
652 | self.max = Constraint( |
||
653 | self.NON_FIXED_INVESTFLOWS, m.TIMEINDEX, noruleinit=True |
||
654 | ) |
||
655 | self.max_build = BuildAction(rule=_max_investflow_rule) |
||
656 | |||
657 | def _min_investflow_rule(block): |
||
658 | """Rule definition of constraint setting a lower bound on flow |
||
659 | variable in investment case. |
||
660 | """ |
||
661 | for i, o in self.MIN_INVESTFLOWS: |
||
662 | for p, t in m.TIMEINDEX: |
||
663 | expr = ( |
||
664 | m.flow[i, o, t] |
||
665 | >= self.total[i, o, p] * m.flows[i, o].min[t] |
||
666 | ) |
||
667 | self.min.add((i, o, p, t), expr) |
||
668 | |||
669 | self.min = Constraint( |
||
670 | self.MIN_INVESTFLOWS, m.TIMEINDEX, noruleinit=True |
||
671 | ) |
||
672 | self.min_build = BuildAction(rule=_min_investflow_rule) |
||
673 | |||
674 | def _full_load_time_max_investflow_rule(_, i, o): |
||
675 | """Rule definition for build action of max. sum flow constraint |
||
676 | in investment case. |
||
677 | """ |
||
678 | expr = sum( |
||
679 | m.flow[i, o, t] * m.timeincrement[t] for t in m.TIMESTEPS |
||
680 | ) <= ( |
||
681 | m.flows[i, o].full_load_time_max |
||
682 | * sum(self.total[i, o, p] for p in m.PERIODS) |
||
683 | ) |
||
684 | return expr |
||
685 | |||
686 | self.full_load_time_max = Constraint( |
||
687 | self.FULL_LOAD_TIME_MAX_INVESTFLOWS, |
||
688 | rule=_full_load_time_max_investflow_rule, |
||
689 | ) |
||
690 | |||
691 | def _full_load_time_min_investflow_rule(_, i, o): |
||
692 | """Rule definition for build action of min. sum flow constraint |
||
693 | in investment case. |
||
694 | """ |
||
695 | expr = sum( |
||
696 | m.flow[i, o, t] * m.timeincrement[t] for t in m.TIMESTEPS |
||
697 | ) >= ( |
||
698 | sum(self.total[i, o, p] for p in m.PERIODS) |
||
699 | * m.flows[i, o].full_load_time_min |
||
700 | ) |
||
701 | return expr |
||
702 | |||
703 | self.full_load_time_min = Constraint( |
||
704 | self.FULL_LOAD_TIME_MIN_INVESTFLOWS, |
||
705 | rule=_full_load_time_min_investflow_rule, |
||
706 | ) |
||
707 | |||
708 | if m.es.periods is not None: |
||
709 | |||
710 | def _overall_maximum_investflow_rule(block): |
||
711 | """Rule definition for maximum overall investment |
||
712 | in investment case. |
||
713 | """ |
||
714 | for i, o in self.OVERALL_MAXIMUM_INVESTFLOWS: |
||
715 | for p in m.PERIODS: |
||
716 | expr = ( |
||
717 | self.total[i, o, p] |
||
718 | <= m.flows[i, o].investment.overall_maximum |
||
719 | ) |
||
720 | self.overall_maximum.add((i, o, p), expr) |
||
721 | |||
722 | self.overall_maximum = Constraint( |
||
723 | self.OVERALL_MAXIMUM_INVESTFLOWS, m.PERIODS, noruleinit=True |
||
724 | ) |
||
725 | self.overall_maximum_build = BuildAction( |
||
726 | rule=_overall_maximum_investflow_rule |
||
727 | ) |
||
728 | |||
729 | def _overall_minimum_investflow_rule(block, i, o): |
||
730 | """Rule definition for minimum overall investment |
||
731 | in investment case. |
||
732 | |||
733 | Note: This is only applicable for the last period |
||
734 | """ |
||
735 | expr = ( |
||
736 | m.flows[i, o].investment.overall_minimum |
||
737 | <= self.total[i, o, m.PERIODS[-1]] |
||
738 | ) |
||
739 | return expr |
||
740 | |||
741 | self.overall_minimum = Constraint( |
||
742 | self.OVERALL_MINIMUM_INVESTFLOWS, |
||
743 | rule=_overall_minimum_investflow_rule, |
||
744 | ) |
||
1171 |