| Conditions | 13 |
| Total Lines | 327 |
| Code Lines | 94 |
| 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 -*- |
||
| 252 | def _create_constraints(self): |
||
| 253 | r"""Creates all constraints for standard flows. |
||
| 254 | |||
| 255 | Depending on the attributes of the *InvestmentFlowBlock* |
||
| 256 | and *SimpleFlowBlock*, different constraints are created. |
||
| 257 | The following constraints are created |
||
| 258 | for all *InvestmentFlowBlock* objects:\ |
||
| 259 | |||
| 260 | Total capacity / energy |
||
| 261 | |||
| 262 | .. math:: |
||
| 263 | & |
||
| 264 | if \quad p=0:\\ |
||
| 265 | & |
||
| 266 | P_{total}(p) = P_{invest}(p) + P_{exist}(p) \\ |
||
| 267 | &\\ |
||
| 268 | & |
||
| 269 | else:\\ |
||
| 270 | & |
||
| 271 | P_{total}(p) = P_{total}(p-1) + P_{invest}(p) - P_{old}(p) \\ |
||
| 272 | &\\ |
||
| 273 | & |
||
| 274 | \forall p \in \textrm{CAPACITY_PERIODS} |
||
| 275 | |||
| 276 | Upper bound for the flow value |
||
| 277 | |||
| 278 | .. math:: |
||
| 279 | & |
||
| 280 | P(p, t) \le ( P_{total}(p) ) \cdot f_{max}(t) \\ |
||
| 281 | & |
||
| 282 | \forall p, t \in \textrm{TIMEINDEX} |
||
| 283 | |||
| 284 | For a multi-period model, the old capacity is defined as follows: |
||
| 285 | |||
| 286 | .. math:: |
||
| 287 | & |
||
| 288 | P_{old}(p) = P_{old,exo}(p) + P_{old,end}(p)\\ |
||
| 289 | &\\ |
||
| 290 | & |
||
| 291 | if \quad p=0:\\ |
||
| 292 | & |
||
| 293 | P_{old,end}(p) = 0\\ |
||
| 294 | &\\ |
||
| 295 | & |
||
| 296 | else \quad if \quad l \leq year(p):\\ |
||
| 297 | & |
||
| 298 | P_{old,end}(p) = P_{invest}(p_{comm})\\ |
||
| 299 | &\\ |
||
| 300 | & |
||
| 301 | else:\\ |
||
| 302 | & |
||
| 303 | P_{old,end}(p) = 0\\ |
||
| 304 | &\\ |
||
| 305 | & |
||
| 306 | if \quad p=0:\\ |
||
| 307 | & |
||
| 308 | P_{old,exo}(p) = 0\\ |
||
| 309 | &\\ |
||
| 310 | & |
||
| 311 | else \quad if \quad l - a \leq year(p):\\ |
||
| 312 | & |
||
| 313 | P_{old,exo}(p) = P_{exist} (*)\\ |
||
| 314 | &\\ |
||
| 315 | & |
||
| 316 | else:\\ |
||
| 317 | & |
||
| 318 | P_{old,exo}(p) = 0\\ |
||
| 319 | &\\ |
||
| 320 | & |
||
| 321 | \forall p \in \textrm{CAPACITY_PERIODS} |
||
| 322 | |||
| 323 | where: |
||
| 324 | |||
| 325 | * (*) is only performed for the first period the condition |
||
| 326 | is True. A decommissioning flag is then set to True |
||
| 327 | to prevent having falsely added old capacity in future periods. |
||
| 328 | * :math:`year(p)` is the year corresponding to period p |
||
| 329 | * :math:`p_{comm}` is the commissioning period of the flow |
||
| 330 | (which is determined by the model itself) |
||
| 331 | |||
| 332 | Depending on the attribute :attr:`nonconvex`, the constraints for the |
||
| 333 | bounds of the decision variable :math:`P_{invest}(p)` are different:\ |
||
| 334 | |||
| 335 | * :attr:`nonconvex = False` |
||
| 336 | |||
| 337 | .. math:: |
||
| 338 | & |
||
| 339 | P_{invest, min}(p) \le P_{invest}(p) \le P_{invest, max}(p) \\ |
||
| 340 | & |
||
| 341 | \forall p \in \textrm{CAPACITY_PERIODS} |
||
| 342 | |||
| 343 | * :attr:`nonconvex = True` |
||
| 344 | |||
| 345 | .. math:: |
||
| 346 | & |
||
| 347 | P_{invest, min}(p) \cdot Y_{invest}(p) \le P_{invest}(p)\\ |
||
| 348 | & |
||
| 349 | P_{invest}(p) \le P_{invest, max}(p) \cdot Y_{invest}(p)\\ |
||
| 350 | &\\ |
||
| 351 | & |
||
| 352 | \forall p \in \textrm{CAPACITY_PERIODS} |
||
| 353 | |||
| 354 | For all *InvestmentFlowBlock* objects |
||
| 355 | (independent of the attribute :attr:`nonconvex`), |
||
| 356 | the following additional constraints are created, if the appropriate |
||
| 357 | attribute of the *SimpleFlowBlock* |
||
| 358 | (see :class:`oemof.solph.flows._simple_flow_block.SimpleFlowBlock`) |
||
| 359 | is set: |
||
| 360 | |||
| 361 | * :attr:`fix` is not None |
||
| 362 | |||
| 363 | Actual value constraint for investments with fixed flow values |
||
| 364 | |||
| 365 | .. math:: |
||
| 366 | & |
||
| 367 | P(p, t) = P_{total}(p) \cdot f_{fix}(t) \\ |
||
| 368 | &\\ |
||
| 369 | & |
||
| 370 | \forall p, t \in \textrm{TIMEINDEX} |
||
| 371 | |||
| 372 | * :attr:`min != 0` |
||
| 373 | |||
| 374 | Lower bound for the flow values |
||
| 375 | |||
| 376 | .. math:: |
||
| 377 | & |
||
| 378 | P(p, t) \geq P_{total}(p) \cdot f_{min}(t) \\ |
||
| 379 | &\\ |
||
| 380 | & |
||
| 381 | \forall p, t \in \textrm{TIMEINDEX} |
||
| 382 | |||
| 383 | * :attr:`full_load_time_max is not None` |
||
| 384 | |||
| 385 | Upper bound for the sum of all flow values |
||
| 386 | (e.g. maximum full load hours) |
||
| 387 | |||
| 388 | .. math:: |
||
| 389 | \sum_{p, t} P(p, t) \cdot \tau(t) \leq P_{total}(p) |
||
| 390 | \cdot t_{full\_load, min} |
||
| 391 | |||
| 392 | * :attr:`full_load_time_min is not None` |
||
| 393 | |||
| 394 | Lower bound for the sum of all flow values |
||
| 395 | (e.g. minimum full load hours) |
||
| 396 | |||
| 397 | .. math:: |
||
| 398 | \sum_{p, t} P(t) \cdot \tau(t) \geq P_{total} |
||
| 399 | \cdot t_{full\_load, min} |
||
| 400 | |||
| 401 | * :attr:`overall_maximum` is not None |
||
| 402 | (for multi-period model only) |
||
| 403 | |||
| 404 | Overall maximum of total installed capacity / energy for flow |
||
| 405 | |||
| 406 | .. math:: |
||
| 407 | & |
||
| 408 | P_{total}(p) \leq P_{overall,max} \\ |
||
| 409 | &\\ |
||
| 410 | & |
||
| 411 | \forall p \in \textrm{CAPACITY_PERIODS} |
||
| 412 | |||
| 413 | * :attr:`overall_minimum` is not None |
||
| 414 | (for multi-period model only) |
||
| 415 | |||
| 416 | Overall minimum of total installed capacity / energy for flow; |
||
| 417 | applicable only in last period |
||
| 418 | |||
| 419 | .. math:: |
||
| 420 | P_{total}(p_{last}) \geq P_{overall,min} |
||
| 421 | """ |
||
| 422 | m = self.parent_block() |
||
| 423 | |||
| 424 | self.minimum_rule = self._minimum_investment_constraint() |
||
| 425 | self.maximum_rule = self._maximum_investment_constraint() |
||
| 426 | |||
| 427 | # Handle unit lifetimes |
||
| 428 | def _total_capacity_rule(block): |
||
| 429 | """Rule definition for determining total installed |
||
| 430 | capacity (taking decommissioning into account) |
||
| 431 | """ |
||
| 432 | for i, o in self.INVESTFLOWS: |
||
| 433 | for p in m.CAPACITY_PERIODS: |
||
| 434 | if p == 0: |
||
| 435 | expr = ( |
||
| 436 | self.total[i, o, p] |
||
| 437 | == self.invest[i, o, p] |
||
| 438 | + m.flows[i, o].investment.existing |
||
| 439 | ) |
||
| 440 | self.total_rule.add((i, o, p), expr) |
||
| 441 | # applicable for multi-period model only |
||
| 442 | else: |
||
| 443 | expr = ( |
||
| 444 | self.total[i, o, p] |
||
| 445 | == self.invest[i, o, p] |
||
| 446 | + self.total[i, o, p - 1] |
||
| 447 | - self.old[i, o, p] |
||
| 448 | ) |
||
| 449 | self.total_rule.add((i, o, p), expr) |
||
| 450 | |||
| 451 | self.total_rule = Constraint( |
||
| 452 | self.INVESTFLOWS, m.CAPACITY_PERIODS, noruleinit=True |
||
| 453 | ) |
||
| 454 | self.total_rule_build = BuildAction(rule=_total_capacity_rule) |
||
| 455 | |||
| 456 | def _investflow_fixed_rule(block): |
||
| 457 | """Rule definition of constraint to fix flow variable |
||
| 458 | of investment flow to (normed) actual value |
||
| 459 | """ |
||
| 460 | for i, o in self.FIXED_INVESTFLOWS: |
||
| 461 | for p, t in m.TIMEINDEX: |
||
| 462 | expr = ( |
||
| 463 | m.flow[i, o, t] |
||
| 464 | == self.total[i, o, p] * m.flows[i, o].fix[t] |
||
| 465 | ) |
||
| 466 | self.fixed.add((i, o, p, t), expr) |
||
| 467 | |||
| 468 | self.fixed = Constraint( |
||
| 469 | self.FIXED_INVESTFLOWS, m.TIMEINDEX, noruleinit=True |
||
| 470 | ) |
||
| 471 | self.fixed_build = BuildAction(rule=_investflow_fixed_rule) |
||
| 472 | |||
| 473 | def _max_investflow_rule(block): |
||
| 474 | """Rule definition of constraint setting an upper bound of flow |
||
| 475 | variable in investment case. |
||
| 476 | """ |
||
| 477 | for i, o in self.NON_FIXED_INVESTFLOWS: |
||
| 478 | for p, t in m.TIMEINDEX: |
||
| 479 | expr = ( |
||
| 480 | m.flow[i, o, t] |
||
| 481 | <= self.total[i, o, p] * m.flows[i, o].max[t] |
||
| 482 | ) |
||
| 483 | self.max.add((i, o, p, t), expr) |
||
| 484 | |||
| 485 | self.max = Constraint( |
||
| 486 | self.NON_FIXED_INVESTFLOWS, m.TIMEINDEX, noruleinit=True |
||
| 487 | ) |
||
| 488 | self.max_build = BuildAction(rule=_max_investflow_rule) |
||
| 489 | |||
| 490 | def _min_investflow_rule(block): |
||
| 491 | """Rule definition of constraint setting a lower bound on flow |
||
| 492 | variable in investment case. |
||
| 493 | """ |
||
| 494 | for i, o in self.MIN_INVESTFLOWS: |
||
| 495 | for p, t in m.TIMEINDEX: |
||
| 496 | expr = ( |
||
| 497 | m.flow[i, o, t] |
||
| 498 | >= self.total[i, o, p] * m.flows[i, o].min[t] |
||
| 499 | ) |
||
| 500 | self.min.add((i, o, p, t), expr) |
||
| 501 | |||
| 502 | self.min = Constraint( |
||
| 503 | self.MIN_INVESTFLOWS, m.TIMEINDEX, noruleinit=True |
||
| 504 | ) |
||
| 505 | self.min_build = BuildAction(rule=_min_investflow_rule) |
||
| 506 | |||
| 507 | def _full_load_time_max_investflow_rule(_, i, o): |
||
| 508 | """Rule definition for build action of max. sum flow constraint |
||
| 509 | in investment case. |
||
| 510 | """ |
||
| 511 | expr = sum( |
||
| 512 | m.flow[i, o, t] * m.timeincrement[t] for t in m.TIMESTEPS |
||
| 513 | ) <= ( |
||
| 514 | m.flows[i, o].full_load_time_max |
||
| 515 | * sum(self.total[i, o, p] for p in m.CAPACITY_PERIODS) |
||
| 516 | ) |
||
| 517 | return expr |
||
| 518 | |||
| 519 | self.full_load_time_max = Constraint( |
||
| 520 | self.FULL_LOAD_TIME_MAX_INVESTFLOWS, |
||
| 521 | rule=_full_load_time_max_investflow_rule, |
||
| 522 | ) |
||
| 523 | |||
| 524 | def _full_load_time_min_investflow_rule(_, i, o): |
||
| 525 | """Rule definition for build action of min. sum flow constraint |
||
| 526 | in investment case. |
||
| 527 | """ |
||
| 528 | expr = sum( |
||
| 529 | m.flow[i, o, t] * m.timeincrement[t] for t in m.TIMESTEPS |
||
| 530 | ) >= ( |
||
| 531 | sum(self.total[i, o, p] for p in m.CAPACITY_PERIODS) |
||
| 532 | * m.flows[i, o].full_load_time_min |
||
| 533 | ) |
||
| 534 | return expr |
||
| 535 | |||
| 536 | self.full_load_time_min = Constraint( |
||
| 537 | self.FULL_LOAD_TIME_MIN_INVESTFLOWS, |
||
| 538 | rule=_full_load_time_min_investflow_rule, |
||
| 539 | ) |
||
| 540 | |||
| 541 | if m.es.investment_times is not None: |
||
| 542 | |||
| 543 | def _overall_maximum_investflow_rule(block): |
||
| 544 | """Rule definition for maximum overall investment |
||
| 545 | in investment case. |
||
| 546 | """ |
||
| 547 | for i, o in self.OVERALL_MAXIMUM_INVESTFLOWS: |
||
| 548 | for p in m.CAPACITY_PERIODS: |
||
| 549 | expr = ( |
||
| 550 | self.total[i, o, p] |
||
| 551 | <= m.flows[i, o].investment.overall_maximum |
||
| 552 | ) |
||
| 553 | self.overall_maximum.add((i, o, p), expr) |
||
| 554 | |||
| 555 | self.overall_maximum = Constraint( |
||
| 556 | self.OVERALL_MAXIMUM_INVESTFLOWS, |
||
| 557 | m.CAPACITY_PERIODS, |
||
| 558 | noruleinit=True, |
||
| 559 | ) |
||
| 560 | self.overall_maximum_build = BuildAction( |
||
| 561 | rule=_overall_maximum_investflow_rule |
||
| 562 | ) |
||
| 563 | |||
| 564 | def _overall_minimum_investflow_rule(block, i, o): |
||
| 565 | """Rule definition for minimum overall investment |
||
| 566 | in investment case. |
||
| 567 | |||
| 568 | Note: This is only applicable for the last period |
||
| 569 | """ |
||
| 570 | expr = ( |
||
| 571 | m.flows[i, o].investment.overall_minimum |
||
| 572 | <= self.total[i, o, m.CAPACITY_PERIODS[-1]] |
||
| 573 | ) |
||
| 574 | return expr |
||
| 575 | |||
| 576 | self.overall_minimum = Constraint( |
||
| 577 | self.OVERALL_MINIMUM_INVESTFLOWS, |
||
| 578 | rule=_overall_minimum_investflow_rule, |
||
| 579 | ) |
||
| 675 |