Skip to content

feat: add weighted MILP objectives and voltage-drop controls#5

Open
harrysalmon wants to merge 17 commits intovfinel:developfrom
harrysalmon:harry/milp-weighted-vdrop-docs
Open

feat: add weighted MILP objectives and voltage-drop controls#5
harrysalmon wants to merge 17 commits intovfinel:developfrom
harrysalmon:harry/milp-weighted-vdrop-docs

Conversation

@harrysalmon
Copy link
Copy Markdown

Summary

  • Added a new MILP optimizer module at src/nopywer/optimize_milp.py with a combined formulation for topology, cable tier selection, and power flow.
  • Replaced objective comment-toggles with weighted objective terms:
    • weight_cost
    • weight_length
    • weight_power_distance
    • weight_voltage_drop
    • weight_cumulative_voltage_drop
  • Added optional voltage-drop controls:
    • objective penalties for edge and cumulative node drop
    • hard cap constraints via max_voltage_drop_percent and max_voltage_drop_percent_by_node
  • Added conditional model gating so voltage-drop variables/constraints are only included when voltage features are active, reducing solve complexity for non-voltage runs.
  • Expanded CLI flags for all objective weights and voltage-drop caps.
  • Improved interactive HTML visualization:
    • 3-phase cables shown in dark green
    • edge width proportional to cable diameter
    • improved tooltip details
  • Added extensive documentation and constraint/objective comments, including prominent notes about linearized electrical approximations vs full AC load-flow.
  • Added/expanded tests:
    • tests/optimization/test_optimize_milp.py
    • tests/optimization/test_contracts.py
    • tests/api/test_optimize_endpoint.py
  • Integrated latest MILP lines into colocacion map by backing up and replacing only:
    • colocacion/frontend/public/data/nw25/power-lines.geojson
    • left power-nodes.geojson unchanged.

@vperron
Copy link
Copy Markdown
Collaborator

vperron commented Mar 5, 2026

From afar it seems that this code would benefit from the new model promitives (.to_geojson(), cable tiers, and so on) introduced there

https://github.com/vfinel/nopywer/pull/4/changes

@vperron
Copy link
Copy Markdown
Collaborator

vperron commented Mar 7, 2026

This PR is a single, super heavy commit. I would recommend:

  • rebasing it to make use of the cable tiers in models.py instead of redefining the concept :)
  • splitting into multiple commits: this adds tests that are not completely part of the optimization algorithm
  • try and add code into the current modules (models, optimization, ...) instead of always adding new files -even though for an entirely new test optim module it might make sense

Also I understand if this is just a draft for discussion <3

Comment thread src/nopywer/optimize_milp.py
Comment thread src/nopywer/optimize_milp.py Outdated
Comment thread src/nopywer/optimize_milp.py Outdated
Comment thread src/nopywer/optimize_milp.py Outdated
Comment thread src/nopywer/optimize_milp.py Outdated
Comment thread src/nopywer/optimize_milp.py
Comment thread src/nopywer/optimize_milp.py Outdated
Comment thread tests/api/test_optimize_endpoint.py
Comment thread tests/optimization/test_contracts.py Outdated

assert len(cables) == load_count
assert all(c.from_node != c.to_node for c in cables)
assert all(c.length_m > 0 for c in cables)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks !

@harrysalmon harrysalmon closed this Mar 8, 2026
@harrysalmon harrysalmon force-pushed the harry/milp-weighted-vdrop-docs branch from 0177125 to 1e03e1d Compare March 8, 2026 22:16
@harrysalmon harrysalmon reopened this Mar 8, 2026
@vfinel
Copy link
Copy Markdown
Owner

vfinel commented Mar 9, 2026

awesome work ! I've successfully ran the test on my computer. I could only have a quick look though, i'll let @vperron have a look at the code too :)
Can't wait to compare the different optimisations results. Maybe we could have a button to select the solver to use in the on the server ? Or When running tests, outputing each grids as png, so we can compare them ?

Copy link
Copy Markdown
Collaborator

@vperron vperron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This, friends, is helluva cool PR. 👍

Comment thread src/nopywer/models.py
@classmethod
def capacity_w(cls) -> float:
"""Capacity of this cable type independent of the instance."""
return float(cls.num_phases) * V0 * PF * cls.max_current_a
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

me gusta !

Copy link
Copy Markdown
Owner

@vfinel vfinel Mar 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from an electrical engineering point of view, it is a bit weird to compute a cable capacity in watts (because it depends on the voltage -that can vary because of voltage drop-, and ultimately, cables are rated in amps). It would make more sense to keep capacity in amps, and convert the active power of the load (in watts) to current drawn (in amps).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Helpful, thanks! I've cleared up the name and expanded the docstring to make this clear.

Comment thread src/nopywer/models.py
Comment thread src/nopywer/optimize.py Outdated
@harrysalmon harrysalmon marked this pull request as ready for review March 9, 2026 20:05
@harrysalmon
Copy link
Copy Markdown
Author

awesome work ! I've successfully ran the test on my computer. I could only have a quick look though, i'll let @vperron have a look at the code too :)
Can't wait to compare the different optimisations results. Maybe we could have a button to select the solver to use in the on the server ? Or When running tests, outputing each grids as png, so we can compare them ?

Yeah definitely, as they share the same signiture it should be fairly easy to run each and contrast them visually and numerically.

It's ready to merge now if you're happy with it @vfinel :D

@vfinel
Copy link
Copy Markdown
Owner

vfinel commented Mar 9, 2026

Awesome, thanks for the clarification! Let's merge and continue to build up on this. Thanks for your contribution ! This is going places. Amazing places !

@vfinel
Copy link
Copy Markdown
Owner

vfinel commented Mar 9, 2026

Oups, just a CI error to fix linked to ruff linting, and then we can merge ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants