Skip to content

[BUG] Penalities in Aggregation lead to Exception #487

@FBumann

Description

@FBumann

What's broken?

Setting AggregationParameters.penalty_of_period_freedom leads to Exception

Code to reproduce

import pandas as pd
import numpy as np
import flixopt as fx
"""
This script shows how to use the flixopt framework to model a super minimalistic energy system in the most concise way possible.
THis can also be used to create proposals for new features, bug reports etc
"""

import numpy as np
import pandas as pd

import flixopt as fx

if __name__ == '__main__':
    fx.CONFIG.silent()
    flow_system = fx.FlowSystem(pd.date_range('2020-01-01', periods=9, freq='h'))

    flow_system.add_elements(
        fx.Bus('Heat'),
        fx.Bus('Gas'),
        fx.Effect('Costs', '€', 'Cost', is_standard=True, is_objective=True),
        fx.linear_converters.Boiler(
            'Boiler',
            thermal_efficiency=0.5,
            thermal_flow=fx.Flow(label='Heat', bus='Heat', size=50),
            fuel_flow=fx.Flow(label='Gas', bus='Gas'),
        ),
        fx.Sink(
            'Sink',
            inputs=[fx.Flow(label='Demand', bus='Heat', size=1, fixed_relative_profile=np.array([30, 0, 20, 30, 0, 20, 30, 0, 20]))],
        ),
        fx.Source(
            'Source',
            outputs=[fx.Flow(label='Gas', bus='Gas', size=1000, effects_per_flow_hour=0.04)],
        ),
    )

    calculation = fx.AggregatedCalculation(
        'Simulation1',
        flow_system,
        aggregation_parameters=fx.AggregationParameters(
            hours_per_period=2,
            nr_of_periods=1,
            fix_storage_flows=False,
            aggregate_data_and_fix_non_binary_vars=True,
            percentage_of_period_freedom=10,
            penalty_of_period_freedom=2,
        )
    ).solve(fx.solvers.HighsSolver(0.01, 60))

fx.CONFIG.Logging.enable_console('DEBUG')
flow_system = fx.FlowSystem(pd.date_range('2020-01-01', periods=3, freq='h'))

flow_system.add_elements(
    fx.Bus('Heat'),
    fx.Bus('Gas'),
    fx.Effect('Costs', '€', 'Cost', is_standard=True, is_objective=True),
    fx.linear_converters.Boiler(
        'Boiler',
        eta=0.5,
        Q_th=fx.Flow(label='Heat', bus='Heat', size=50),
        Q_fu=fx.Flow(label='Gas', bus='Gas'),
    ),
    fx.Sink(
        'Sink',
        inputs=[
            fx.Flow(label='Demand', bus='Heat', size=1, fixed_relative_profile=np.array([30, 0, 20]))
        ],
    ),
    fx.Source(
        'Source',
        outputs=[fx.Flow(label='Gas', bus='Gas', size=1000, effects_per_flow_hour=0.04)],
    ),
)

calculation = fx.FullCalculation('Simulation1', flow_system).do_modeling().solve(fx.solvers.HighsSolver(0.01, 60))

Error message

Traceback (most recent call last):
  File "/Users/felix/PycharmProjects/flixopt_719231/.venv/lib/python3.13/site-packages/IPython/core/interactiveshell.py", line 3699, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
    ~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<ipython-input-2-dd7d80826155>", line 1, in <module>
    runfile('/Users/felix/PycharmProjects/flixopt_719231/examples/00_Minmal/minimal_example.py', wdir='/Users/felix/PycharmProjects/flixopt_719231/examples/00_Minmal')
    ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Applications/PyCharm.app/Contents/plugins/python-ce/helpers/pydev/_pydev_bundle/pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
    ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Applications/PyCharm.app/Contents/plugins/python-ce/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
    ~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/felix/PycharmProjects/flixopt_719231/examples/00_Minmal/minimal_example.py", line 46, in <module>
    ).solve(fx.solvers.HighsSolver(0.01, 60))
      ~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/felix/PycharmProjects/flixopt_719231/flixopt/calculation.py", line 235, in solve
    self.do_modeling()
    ~~~~~~~~~~~~~~~~^^
  File "/Users/felix/PycharmProjects/flixopt_719231/flixopt/calculation.py", line 329, in do_modeling
    self.aggregation_model.do_modeling()
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/Users/felix/PycharmProjects/flixopt_719231/flixopt/aggregation.py", line 349, in do_modeling
    for variable in self.variables_direct.values():
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/felix/PycharmProjects/flixopt_719231/.venv/lib/python3.13/site-packages/linopy/variables.py", line 1199, in __getattr__
    raise AttributeError(
        f"Variables has no attribute `{name}` or the attribute is not accessible / raises an error."
    )
AttributeError: Variables has no attribute `values` or the attribute is not accessible / raises an error.

Additional context

flixopt==4.1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions