-
Notifications
You must be signed in to change notification settings - Fork 9
Add mathematical correctness test suite #596
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
…0 to eta=0.5. Now the expected cost is 40 (fuel=20/0.5). If investment logic were broken and allowed free investment, the optimizer would use the high-efficiency invest boiler instead, yielding cost=20 — a clearly different value. test_bus_balance_exact → test_merit_order_dispatch: Renamed to reflect what it actually tests. Added explicit per-source flow assertions (src1=[20,20], src2=[10,10]) instead of just checking their sum, which was tautologically true given bus balance is a fundamental model constraint.
Test: test_storage_soc_bounds Category: Storage What it proves: relative_maximum_charge_state=0.5 limits usable capacity to 50/100 kWh, forcing 10 kWh from expensive source (cost 1050 vs 60 without limit) ──────────────────────────────────────── Test: test_min_uptime_forces_operation Category: Status What it proves: min_uptime=2, max_uptime=2 forces boiler into blocks of exactly 2 hours, dictating the on/off pattern [1,1,0,1,1] and total cost 190 ──────────────────────────────────────── Test: test_min_downtime_prevents_restart Category: Status What it proves: min_downtime=3 prevents cheap boiler from restarting at t=2 after shutting down at t=1, forcing expensive backup (cost 60 vs 40 without constraint) ──────────────────────────────────────── Test: test_piecewise_selects_cheap_segment Category: Piecewise What it proves: Optimizer correctly interpolates within the efficient segment of a 2-piece conversion (cost ≈153.33) ──────────────────────────────────────── Test: test_piecewise_conversion_at_breakpoint Category: Piecewise What it proves: At segment boundary, fuel=30 exactly matches both segments' definition; verifies breakpoint continuity
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the
📝 WalkthroughWalkthroughThis PR introduces a comprehensive test suite for a flow-based optimization framework, covering bus dispatch logic, conversion efficiency (boilers and CHP), effects and constraints, investment sizing, piecewise conversions, temporal status constraints, and storage arbitrage, along with reusable test helpers. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
…─────────────────────────────────────────────────────────────────────────────┐ │ File │ New tests │ Features covered │ ├──────────────────────────┼──────────────┼──────────────────────────────────────────────────────────────────────────────────────────────────────┤ │ test_flow_constraints.py │ 6 (new file) │ relative_minimum, relative_maximum, flow_hours_max, flow_hours_min, load_factor_max, load_factor_min │ ├──────────────────────────┼──────────────┼──────────────────────────────────────────────────────────────────────────────────────────────────────┤ │ test_effects.py │ +3 │ minimum_total, maximum_per_hour, minimum_per_hour │ ├──────────────────────────┼──────────────┼──────────────────────────────────────────────────────────────────────────────────────────────────────┤ │ test_storage.py │ +3 │ initial_charge_state='equals_final', minimal_final_charge_state, capacity_in_flow_hours as invest │ ├──────────────────────────┼──────────────┼──────────────────────────────────────────────────────────────────────────────────────────────────────┤ │ test_investment.py │ +1 │ fixed_size │ ├──────────────────────────┼──────────────┼──────────────────────────────────────────────────────────────────────────────────────────────────────┤ │ test_status.py │ +4 │ effects_per_active_hour, active_hours_min, max_downtime, startup_limit │ ├──────────────────────────┼──────────────┼──────────────────────────────────────────────────────────────────────────────────────────────────────┤ │ test_bus.py │ +1 │ prevent_simultaneous_flow_rates │
Final: 44 tests across 8 files (~19s) File: test_conversion.py Tests: 3 Features: boiler eta, variable eta, CHP dual output ──────────────────────────────────────── File: test_bus.py Tests: 3 Features: merit order, imbalance penalty, prevent_simultaneous_flow_rates ──────────────────────────────────────── File: test_effects.py Tests: 9 Features: effects_per_flow_hour, share_from_temporal, share_from_periodic, maximum/minimum_total, maximum/minimum_per_hour, maximum/minimum_temporal ──────────────────────────────────────── File: test_flow_constraints.py Tests: 6 Features: relative_minimum/maximum, flow_hours_min/max, load_factor_min/max ──────────────────────────────────────── File: test_investment.py Tests: 5 Features: optimal sizing, optional skip, minimum_size, fixed_size, piecewise_effects_of_investment ──────────────────────────────────────── File: test_status.py Tests: 8 Features: startup cost, active_hours_min/max, min/max_uptime, min/max_downtime, startup_limit, effects_per_active_hour ──────────────────────────────────────── File: test_storage.py Tests: 8 Features: arbitrage, losses, eta charge/discharge, SOC bounds, cyclic, final charge state, invest capacity, prevent simultaneous ──────────────────────────────────────── File: test_piecewise.py Tests: 2 Features: segment selection, breakpoint consistency
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
- mandatory=True/False - forces vs optional investment - effects_of_retirement - penalty for NOT investing - InvestParameters + StatusParameters - combined sizing with on/off behavior - previous_flow_rate - scalar and array forms for uptime/downtime carry-over - Piecewise with gaps - forbidden operating regions All tests assert on cost differences to ensure they fail on regression.
…tatusParameters tests added: ┌───────────────────────────────────────────────┬────────────────────────────┬──────────────────────────────────────────────┐ │ Test │ StatusParameter │ What it proves │ ├───────────────────────────────────────────────┼────────────────────────────┼──────────────────────────────────────────────┤ │ test_component_status_effects_per_active_hour │ effects_per_active_hour=50 │ Adds 50€/hour cost when component is ON │ ├───────────────────────────────────────────────┼────────────────────────────┼──────────────────────────────────────────────┤ │ test_component_status_active_hours_min │ active_hours_min=2 │ Forces component to run minimum 2 hours │ ├───────────────────────────────────────────────┼────────────────────────────┼──────────────────────────────────────────────┤ │ test_component_status_max_uptime │ max_uptime=2, min_uptime=2 │ Limits continuous operation to 2-hour blocks │ ├───────────────────────────────────────────────┼────────────────────────────┼──────────────────────────────────────────────┤ │ test_component_status_min_downtime │ min_downtime=3 │ Prevents restart for 3 hours after shutdown │ ├───────────────────────────────────────────────┼────────────────────────────┼──────────────────────────────────────────────┤ │ test_component_status_max_downtime │ max_downtime=1 │ Forces restart after maximum 1 hour off │ ├───────────────────────────────────────────────┼────────────────────────────┼──────────────────────────────────────────────┤ │ test_component_status_startup_limit │ startup_limit=1 │ Caps number of startups to 1 │ └───────────────────────────────────────────────┴────────────────────────────┴──────────────────────────────────────────────┘ Previously we had 3 component-level status tests (startup_cost, min_uptime, active_hours_max). Now we have 9 tests covering all StatusParameters options at the component level.
Summary
tests/test_math/directory with 21 small, analytically verifiable optimization testsTest Coverage
Covered (21 tests across 7 files)
test_conversion.pytest_storage.pytest_status.pytest_piecewise.pytest_investment.pytest_effects.pytest_bus.pyNot Yet Covered (planned)
Flow:
relative_minimum/relative_maximumflow_hours_max/flow_hours_minload_factor_min/load_factor_maxEffect:
share_from_periodicminimum_totalmaximum_temporal/minimum_temporalmaximum_per_hour/minimum_per_hourStorage:
initial_charge_state='equals_final'(cyclic)minimal_final_charge_statecapacity_in_flow_hoursas InvestParametersprevent_simultaneous_charge_and_dischargeInvestParameters:
fixed_sizepiecewise_effects_of_investmentStatusParameters:
effects_per_active_houractive_hours_minmax_downtimestartup_limitSource/Sink:
prevent_simultaneous_flow_ratesTest Plan
Summary by CodeRabbit