Skip to content

Commit dea0537

Browse files
authored
Feature/rename calculation to optimization (#484)
* Rename Calcualtion to optimization.py # Conflicts: # flixopt/calculation.py * Rename new classes slightly * Also rename results classes * FInalize port to new names * Add old methods * Udpate CHANGELOG.md * Temp * I've successfully renamed all aggregation-related classes and modules to use "clustering" terminology for better consistency with the ClusteredOptimization class name. Changes Made: 1. Module renamed: - aggregation.py → clustering.py (via git mv) 2. Classes renamed: - Aggregation → Clustering - AggregationParameters → ClusteringParameters - AggregationModel → ClusteringModel 3. Updated references in: - optimization.py: All imports, attributes, and method parameters - calculation.py: Import statement (for backward compat type hints) - __init__.py: Exports (includes both old and new names) - Module docstrings updated 4. Attribute updates: - ClusteredOptimization.clustering_parameters - ClusteredOptimization.clustering - ClusteredOptimization.clustering_model - self.durations['clustering'] - Label strings: 'Clustering' instead of 'Aggregation' 5. Backward compatibility: - Added deprecated aliases in clustering.py for all old class names - Old names exported from __init__.py with deprecation warnings - All warnings point to v6.0.0 removal 6. Changelog updated: Added all clustering renames to the breaking changes section * Bugfixes * Use correct deprecation version removal * Import fix * Improve docstrings and remove duplicate check * Remove loguru usage * Bugfix * Move solve to Optimization class * 1. ✅ Added validation for nr_of_previous_values in SegmentedOptimization to prevent silent indexing bugs 2. ✅ Fixed active_timesteps type annotation to include None 3. ✅ Fixed fix_sizes() docstring/implementation inconsistency for optional ds parameter The changelog now documents all the bug fixes that were applied based on the code review feedback. * Update CHANGELOG.md * Update tests * Update tests * Add setting logger level to SUCCESS directly * Remove warnings from examples * Remove warnings from examples * Fix SUCCESS Level setting * 1. Exported SUCCESS_LEVEL Constant ✓ - Added SUCCESS_LEVEL to __all__ in flixopt/config.py:20 - Users can now import and use it: from flixopt.config import SUCCESS_LEVEL 2. Enhanced Documentation ✓ - CONFIG.Logging docstring (flixopt/config.py:227-262): - Added "Log Levels" section explaining all levels including SUCCESS (25) - Added examples showing how to use SUCCESS level as string, numeric, or constant - Shows that SUCCESS is between INFO (20) and WARNING (30) - set_colors() docstring (flixopt/config.py:460): - Already included SUCCESS in color customization examples 3. Comprehensive Test Coverage ✓ Added 5 new tests in tests/test_config.py:81-139: - test_success_level_as_minimum: Verifies SUCCESS level filtering (INFO hidden, SUCCESS shown, WARNING shown) - test_success_level_numeric: Tests using numeric value (25) - test_success_level_constant: Tests using SUCCESS_LEVEL constant - test_success_file_logging: Tests file logging with SUCCESS level - test_success_color_customization: Tests color customization * Removed: - ❌ Monkey-patching logging.Logger.success = ... - ❌ success() wrapper function - ❌ All the complexity and fragility Kept: - ✅ SUCCESS_LEVEL = 25 constant (exported in __all__) - ✅ logging.addLevelName(SUCCESS_LEVEL, 'SUCCESS') - ✅ Clean documentation * Fix singel line coloring * FIx single and multiline coloring * Add proper .modeled, .main_results and .summary to SegmentedOptimization * Updaet CHANGELOG.md * Implement Protocol * Add proper type hints * Remove not needed hints * rename methods * rename calculation variables to optimization * Fixed the _initialize_optimization_common call in SegmentedOptimization.__init_ * Make init more explicit * 1. Fixed outdated docstring in SegmentedResults.setup_colors 2. Fixed misleading objective calculation in SegmentedOptimization.main_result 3. Enhanced docstring warning (flixopt/optimization.py:868-87 * Change assertions to proper Exceptions * Updated Terminology in docs: Key Terminology Changes: | Old | New | |-----------------------|-----------------------| | Calculation | Optimization | | CalculationResults | Results | | FullCalculation | Optimization | | SegmentedCalculation | SegmentedOptimization | | AggregatedCalculation | ClusteredOptimization | | calculation modes | optimization modes | * Add missing type hints and rename files mentioning claculation * renamed all the "aggregation" attributes and methods to "clustering" while maintaining full backward compatibility. * Improve naming in example * Fix remaining mentions of Calculation * Imrpove docstring * Improve conftest.py * Changed x-axis label from "Calculation type" to "Optimization type" for consistency with the renamed terminolog * Fixed xarray boolean comparisons by adding .item() to convert DataArray aggregates to scalar values before use in conditional statements. This prevents ValueError: The truth value of a DataArray is ambiguous errors * Updated TimeSeriesData class docstring from "with aggregation metadata" to "with clustering metadata" to * Fix tests * removed the unused hours_per_timestep attribute from SegmentedResults.__init__: - Issue: This attribute was computed with incorrect semantics (using all_timesteps instead of timesteps_extra, producing N-1 values instead of N) - Fix: Completely removed the line since it was never accessed anywhere in the codebase - Impact: No functionality affected - this was dead code with no consumers * Add early validation * Remove not needed type hint * Add guards against results accessing before modeling * Backward compatibility for legacy JSON key (lines 2128-2133): * Guard against empty segment_results * Fix deprecation of flow_system parameter in results.py * Reuse SUCCESS_LEVEL constant * Typo * Typo * Revert docstring changes * Improve docstring * Update CHANGELOG.md * updated results.py to use the new naming scheme * Further renamings * Further renamings * Rename CalculationResultsPaths to ResultsPaths * Rename CalculationResultsPaths to ResultsPaths * Change folder.mkdir(parents=False to True * Temp * Synchronized folder creation across codebase to always use parents=True and exist_ok=True * added overwrite protection to prevent accidentally overwriting Results and SegmentedResults files. * Use unique names in tests * Improve logging message * Improve CHANGELOG.md * Improve CHANGELOG.md
1 parent 8359c61 commit dea0537

38 files changed

Lines changed: 1805 additions & 1033 deletions

CHANGELOG.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,24 +56,46 @@ If upgrading from v2.x, see the [v3.0.0 release notes](https://github.com/flixOp
5656
If upgrading from v2.x, see the [v3.0.0 release notes](https://github.com/flixOpt/flixOpt/releases/tag/v3.0.0) and [Migration Guide](https://flixopt.github.io/flixopt/latest/user-guide/migration-guide-v3/).
5757
5858
### ✨ Added
59+
- `overwrite` parameter when saving results to file. If True, overwrite existing files.
5960
6061
### 💥 Breaking Changes
6162
6263
### ♻️ Changed
6364
65+
- Now creates the Results folder even i fparents didnt exist
66+
6467
### 🗑️ Deprecated
6568
69+
**Class and module renaming:**
70+
- `FullCalculation` → `Optimization`
71+
- `AggregatedCalculation` → `ClusteredOptimization`
72+
- `SegmentedCalculation` → `SegmentedOptimization`
73+
- `CalculationResults` → `Results`
74+
- `SegmentedCalculationResults` → `SegmentedResults`
75+
- `Aggregation` → `Clustering`
76+
- `AggregationParameters` → `ClusteringParameters`
77+
- `AggregationModel` → `ClusteringModel`
78+
- Module: `calculation.py` → `optimization.py`
79+
- Module: `aggregation.py` → `clustering.py`
80+
81+
Old names remain available with deprecation warnings (removed in v5.0.0).
82+
6683
### 🔥 Removed
6784
6885
### 🐛 Fixed
6986
87+
- Fixed `fix_sizes()` docstring/implementation inconsistency for optional `ds` parameter
88+
7089
### 🔒 Security
7190
7291
### 📦 Dependencies
7392
7493
### 📝 Docs
7594
7695
### 👷 Development
96+
- Fixed `active_timesteps` type annotation to include `None`
97+
- Fixed xarray truth-value ambiguity in `main_results` buses with excess filter
98+
- Added validation for `nr_of_previous_values` in `SegmentedOptimization` to prevent silent indexing bugs
7799
78100
### 🚧 Known Issues
79101

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ flow_system = fx.FlowSystem(timesteps)
4242
flow_system.add_elements(buses, components, effects)
4343

4444
# 2. Create and solve
45-
calculation = fx.FullCalculation("MyModel", flow_system)
46-
calculation.solve()
45+
optimization = fx.Optimization("MyModel", flow_system)
46+
optimization.solve(fx.solvers.HighsSolver())
4747

4848
# 3. Analyze results
49-
calculation.results.solution
49+
optimization.results.solution
5050
```
5151

5252
**Get started with real examples:**
@@ -90,8 +90,8 @@ boiler = fx.Boiler("Boiler", eta=0.9, ...)
9090
**Multi-criteria optimization:** Model costs, emissions, resource use - any custom metric. Optimize single objectives or use weighted combinations and ε-constraints.
9191
[Effects documentation](https://flixopt.github.io/flixopt/latest/user-guide/mathematical-notation/effects-penalty-objective/)
9292

93-
**Performance at any scale:** Choose calculation modes without changing your model - Full, Segmented, or Aggregated (using [TSAM](https://github.com/FZJ-IEK3-VSA/tsam)).
94-
[Calculation modes](https://flixopt.github.io/flixopt/latest/api-reference/calculation/)
93+
**Performance at any scale:** Choose optimization modes without changing your model - Optimization, SegmentedOptimization, or ClusteredOptimization (using [TSAM](https://github.com/FZJ-IEK3-VSA/tsam)).
94+
[Optimization modes](https://flixopt.github.io/flixopt/latest/api-reference/optimization/)
9595

9696
**Built for reproducibility:** Self-contained NetCDF result files with complete model information. Load results months later - everything is preserved.
9797
[Results documentation](https://flixopt.github.io/flixopt/latest/api-reference/results/)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# Calculation Mode comparison
1+
# Optimization Modes Comparison
22
**Note:** This example relies on time series data. You can find it in the `examples` folder of the FlixOpt repository.
33
```python
4-
{! ../examples/03_Calculation_types/example_calculation_types.py !}
4+
{! ../examples/03_Optimization_modes/example_optimization_modes.py !}
55
```

docs/examples/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ We work on improving this gallery. If you have something to share, please contac
99
1. [Minimal Example](00-Minimal Example.md) - The simplest possible FlixOpt model
1010
2. [Simple Example](01-Basic Example.md) - A basic example with more features
1111
3. [Complex Example](02-Complex Example.md) - A comprehensive example with result saving and loading
12-
4. [Calculation Modes](03-Calculation Modes.md) - Comparison of different calculation modes
12+
4. [Optimization Modes](03-Optimization Modes.md) - Comparison of different optimization modes
1313
5. [Scenarios](04-Scenarios.md) - Working with scenarios in FlixOpt
1414
6. [Two-stage Optimization](05-Two-stage-optimization.md) - Two-stage optimization approach

docs/getting-started.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Working with FlixOpt follows a general pattern:
5353
2. **Define [`Effects`][flixopt.effects.Effect]** (costs, emissions, etc.)
5454
3. **Define [`Buses`][flixopt.elements.Bus]** as connection points in your system
5555
4. **Add [`Components`][flixopt.components]** like converters, storage, sources/sinks with their Flows
56-
5. **Run [`Calculations`][flixopt.calculation]** to optimize your system
56+
5. **Run [`Optimizations`][flixopt.optimization]** to optimize your system
5757
6. **Analyze [`Results`][flixopt.results]** using built-in or external visualization tools
5858

5959
## Next Steps

docs/user-guide/core-concepts.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -98,23 +98,23 @@ This approach allows for multi-criteria optimization using both:
9898
- **Weighted Sum Method**: Optimize a theoretical Effect which other Effects crosslink to
9999
- **ε-constraint method**: Constrain effects to specific limits
100100

101-
### Calculation
101+
### Optimization
102102

103-
A [`FlowSystem`][flixopt.flow_system.FlowSystem] can be converted to a Model and optimized by creating a [`Calculation`][flixopt.calculation.Calculation] from it.
103+
A [`FlowSystem`][flixopt.flow_system.FlowSystem] can be converted to a Model and optimized by creating an [`Optimization`][flixopt.optimization.Optimization] from it.
104104

105-
FlixOpt offers different calculation modes:
105+
FlixOpt offers different optimization modes:
106106

107-
- [`FullCalculation`][flixopt.calculation.FullCalculation] - Solves the entire problem at once
108-
- [`SegmentedCalculation`][flixopt.calculation.SegmentedCalculation] - Solves the problem in segments (with optioinal overlap), improving performance for large problems
109-
- [`AggregatedCalculation`][flixopt.calculation.AggregatedCalculation] - Uses typical periods to reduce computational requirements
107+
- [`Optimization`][flixopt.optimization.Optimization] - Solves the entire problem at once
108+
- [`SegmentedOptimization`][flixopt.optimization.SegmentedOptimization] - Solves the problem in segments (with optional overlap), improving performance for large problems
109+
- [`ClusteredOptimization`][flixopt.optimization.ClusteredOptimization] - Uses typical periods to reduce computational requirements
110110

111111
### Results
112112

113-
The results of a calculation are stored in a [`CalculationResults`][flixopt.results.CalculationResults] object.
114-
This object contains the solutions of the optimization as well as all information about the [`Calculation`][flixopt.calculation.Calculation] and the [`FlowSystem`][flixopt.flow_system.FlowSystem] it was created from.
115-
The solution is stored as an `xarray.Dataset`, but can be accessed through their assotiated Component, Bus or Effect.
113+
The results of an optimization are stored in a [`Results`][flixopt.results.Results] object.
114+
This object contains the solutions of the optimization as well as all information about the [`Optimization`][flixopt.optimization.Optimization] and the [`FlowSystem`][flixopt.flow_system.FlowSystem] it was created from.
115+
The solution is stored as an `xarray.Dataset`, but can be accessed through their associated Component, Bus or Effect.
116116

117-
This [`CalculationResults`][flixopt.results.CalculationResults] object can be saved to file and reloaded from file, allowing you to analyze the results anytime after the solve.
117+
This [`Results`][flixopt.results.Results] object can be saved to file and reloaded from file, allowing you to analyze the results anytime after the solve.
118118

119119
## How These Concepts Work Together
120120

@@ -128,12 +128,12 @@ The process of working with FlixOpt can be divided into 3 steps:
128128
- Add
129129
- [`FlowSystems`][flixopt.flow_system.FlowSystem] can also be loaded from a netCDF file*
130130
2. Translate the model to a mathematical optimization problem
131-
- Create a [`Calculation`][flixopt.calculation.Calculation] from your FlowSystem and choose a Solver
132-
- ...The Calculation is translated internally to a mathematical optimization problem...
131+
- Create an [`Optimization`][flixopt.optimization.Optimization] from your FlowSystem and choose a Solver
132+
- ...The Optimization is translated internally to a mathematical optimization problem...
133133
- ...and solved by the chosen solver.
134134
3. Analyze the results
135-
- The results are stored in a [`CalculationResults`][flixopt.results.CalculationResults] object
136-
- This object can be saved to file and reloaded from file, retaining all information about the calculation
135+
- The results are stored in a [`Results`][flixopt.results.Results] object
136+
- This object can be saved to file and reloaded from file, retaining all information about the optimization
137137
- As it contains the used [`FlowSystem`][flixopt.flow_system.FlowSystem], it fully documents all assumptions taken to create the results.
138138

139139
<figure markdown>
@@ -152,4 +152,4 @@ This allows to adjust your model to very specific requirements without loosing t
152152
<!--- [FlowSystem](api/flow_system.md) - Time series and system organization-->
153153
<!--- [Components](api/components.md) - Available component types and how to use them-->
154154
<!--- [Effects](apieffects.md) - Costs, emissions, and other impacts-->
155-
<!--- [Calculation Modes](api/calculation.md) - Different approaches to solving your model-->
155+
<!--- [Optimization Modes](api/optimization.md) - Different approaches to solving your model-->

docs/user-guide/mathematical-notation/dimensions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ flow_system = fx.FlowSystem(
288288
# [6.0, 4.0]] # 2040: 10 × [0.6, 0.4]
289289
```
290290

291-
**Normalization:** Set `normalize_weights=False` in `Calculation` to turn of the normalization.
291+
**Normalization:** Set `normalize_weights=False` in `Optimization` to turn off the normalization.
292292

293293
---
294294

docs/user-guide/migration-guide-v3.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ Terminology changed and sharing system inverted: effects now "pull" shares.
7676

7777
---
7878

79-
### FlowSystem & Calculation
79+
### FlowSystem & Optimization
8080

8181
| Change | Description |
8282
|--------|-------------|
83-
| **FlowSystem copying** | Each `Calculation` gets its own copy (independent) |
84-
| **do_modeling() return** | Returns `Calculation` object (access model via `.model` property) |
83+
| **FlowSystem copying** | Each `Optimization` gets its own copy (independent) |
84+
| **do_modeling() return** | Returns `Optimization` object (access model via `.model` property) |
8585
| **Storage arrays** | Arrays match timestep count (no extra element) |
8686
| **Final charge state** | Use `relative_minimum_final_charge_state` / `relative_maximum_final_charge_state` |
8787

@@ -135,7 +135,7 @@ Terminology changed and sharing system inverted: effects now "pull" shares.
135135
| `agg_group` | `aggregation_group` |
136136
| `agg_weight` | `aggregation_weight` |
137137

138-
??? abstract "Calculation"
138+
??? abstract "Optimization"
139139

140140
| Old (v2.x) | New (v3.0.0) |
141141
|------------|--------------|
@@ -207,7 +207,7 @@ Terminology changed and sharing system inverted: effects now "pull" shares.
207207
| Issue | Solution |
208208
|-------|----------|
209209
| Effect shares not working | See [Effect System Redesign](#effect-system-redesign) |
210-
| Storage dimensions wrong | See [FlowSystem & Calculation](#flowsystem-calculation) |
210+
| Storage dimensions wrong | See [FlowSystem & Optimization](#flowsystem-optimization) |
211211
| Bus assignment error | See [String Labels](#string-labels) |
212212
| KeyError in results | See [Variable Names](#variable-names) |
213213
| `AttributeError: model` | Rename `.model``.submodel` |
@@ -220,7 +220,7 @@ Terminology changed and sharing system inverted: effects now "pull" shares.
220220
| Category | Tasks |
221221
|----------|-------|
222222
| **Install** |`pip install --upgrade flixopt` |
223-
| **Breaking changes** | • Update [effect sharing](#effect-system-redesign)<br>• Update [variable names](#variable-names)<br>• Update [string labels](#string-labels)<br>• Fix [storage arrays](#flowsystem-calculation)<br>• Update [Calculation API](#flowsystem-calculation)<br>• Update [class names](#other-changes) |
223+
| **Breaking changes** | • Update [effect sharing](#effect-system-redesign)<br>• Update [variable names](#variable-names)<br>• Update [string labels](#string-labels)<br>• Fix [storage arrays](#flowsystem-optimization)<br>• Update [Optimization API](#flowsystem-optimization)<br>• Update [class names](#other-changes) |
224224
| **Configuration** | • Enable [logging](#other-changes) if needed |
225225
| **Deprecated** | • Update [deprecated parameters](#deprecated-parameters) (recommended) |
226226
| **Testing** | • Test thoroughly<br>• Validate results match v2.x |

examples/00_Minmal/minimal_example.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
),
3333
)
3434

35-
calculation = fx.FullCalculation('Simulation1', flow_system).do_modeling().solve(fx.solvers.HighsSolver(0.01, 60))
36-
calculation.results['Heat'].plot_node_balance()
35+
optimization = fx.Optimization('Simulation1', flow_system).solve(fx.solvers.HighsSolver(0.01, 60))
36+
optimization.results['Heat'].plot_node_balance()

examples/01_Simple/simple_example.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,24 +104,24 @@
104104

105105
# --- Define and Run Calculation ---
106106
# Create a calculation object to model the Flow System
107-
calculation = fx.FullCalculation(name='Sim1', flow_system=flow_system)
108-
calculation.do_modeling() # Translate the model to a solvable form, creating equations and Variables
107+
optimization = fx.Optimization(name='Sim1', flow_system=flow_system)
108+
optimization.do_modeling() # Translate the model to a solvable form, creating equations and Variables
109109

110110
# --- Solve the Calculation and Save Results ---
111-
calculation.solve(fx.solvers.HighsSolver(mip_gap=0, time_limit_seconds=30))
111+
optimization.solve(fx.solvers.HighsSolver(mip_gap=0, time_limit_seconds=30))
112112

113113
# --- Analyze Results ---
114114
# Colors are automatically assigned using default colormap
115115
# Optional: Configure custom colors with
116-
calculation.results.setup_colors()
117-
calculation.results['Fernwärme'].plot_node_balance_pie()
118-
calculation.results['Fernwärme'].plot_node_balance()
119-
calculation.results['Storage'].plot_charge_state()
120-
calculation.results.plot_heatmap('CHP(Q_th)|flow_rate')
116+
optimization.results.setup_colors()
117+
optimization.results['Fernwärme'].plot_node_balance_pie()
118+
optimization.results['Fernwärme'].plot_node_balance()
119+
optimization.results['Storage'].plot_charge_state()
120+
optimization.results.plot_heatmap('CHP(Q_th)|flow_rate')
121121

122122
# Convert the results for the storage component to a dataframe and display
123-
df = calculation.results['Storage'].node_balance_with_charge_state()
123+
df = optimization.results['Storage'].node_balance_with_charge_state()
124124
print(df)
125125

126126
# Save results to file for later usage
127-
calculation.results.to_file()
127+
optimization.results.to_file()

0 commit comments

Comments
 (0)