Skip to content

Adding binary (binary_big_M) decomposition type#50

Draft
dalyw wants to merge 10 commits into
mainfrom
feature/binary_decompose
Draft

Adding binary (binary_big_M) decomposition type#50
dalyw wants to merge 10 commits into
mainfrom
feature/binary_decompose

Conversation

@dalyw

@dalyw dalyw commented Jan 2, 2026

Copy link
Copy Markdown
Contributor

Updating error to note that absolute_value not valid for cvxpy, as neither is DCP-compliant

Allowing binary_big_M for cvxpy with gurobi

Addresses #45 and #46

Also added a helper function get_prev_demand_dict that can be called by external codebases by passing in charge data and a charge dict

Pull request recommendations:

  • Name your pull request your-development-type/short-description. Ex: feature/gui
  • Link to any relevant issue in the PR description. Ex: Resolves [ENH: Support optimization of flexibility metrics #12]
  • Provide context of changes.
  • Provide relevant tests for your feature or bug fix.
  • Provide or update documentation for any feature added by your pull request.

Thanks for contributing!

Updating error to note that  absolute_value not valid for cvxpy, as neither is DCP-compliant

Allowing binary_big_M for cvxpy with gurobi
@codecov-commenter

codecov-commenter commented Jan 2, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.63%. Comparing base (d044d34) to head (b2dcf7a).

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #50      +/-   ##
==========================================
+ Coverage   94.38%   94.63%   +0.24%     
==========================================
  Files          10       10              
  Lines        1603     1659      +56     
==========================================
+ Hits         1513     1570      +57     
+ Misses         90       89       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@fletchapin fletchapin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks great to me! @arao53 might have comments since he's more familiar with MILP

Comment thread docs/how_to_advanced.rst
.. code-block:: python

from electric_emission_cost import costs
from eeco import costs

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

good catch!

Comment thread eeco/costs.py Outdated
- "absolute_value": Uses max(x, 0) constraints. Creates nonlinear problem
for Pyomo due to abs() constraint.
- "binary_big_M": Uses binary indicator with Big-M constraints.
Creates a MILP (mixed-integer linear program) for Pyomo.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should this be "for Pyomo or CVXPY" since binary_big_M is supported for both?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes!

Comment thread eeco/costs.py Outdated
- "absolute_value": Uses max(x, 0) constraints. Creates nonlinear problem
for Pyomo due to abs() constraint.
- "binary_big_M": Uses binary indicator with Big-M constraints.
Creates a MILP (mixed-integer linear program) for Pyomo.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should this be "for Pyomo or CVXPY" since binary_big_M is supported for both?

@dalyw

dalyw commented Feb 5, 2026

Copy link
Copy Markdown
Contributor Author

(Still working on this PR, it can be merged AFTER #51 if that's approved)

- check if charge_array is cp.Expression and access charge_array.size rather than len(charge_array) if so

-Don't check for consumption_max when building in lines 641-644 if cp.Expression

- Update CVXPY branch gate conditions lines 694-711. When demand is below the tier limit, consumption_data - limit < 0, so cp.max(demand_charged) < 0, and cp.maximum(... -prev_demand_cost, 0) = 0.

- in calculate_cost lines 1291 - 1298, get n by which to divide demand consumption estimate based on .size if cp.Expression
… tuple from decompose_consumption. Previously was using it in the "model" entry for pyomo, but constraints weren't properly getting added.

Now there will be a separate "_" in the return tuple from calculate_cost and calculate_itemized cost. Alternatively could try to group both items under "model_or_constraints" to not change compatibility with previous uses of eeco? Or add backwards compatibility?
Comment thread eeco/costs.py Outdated
)

return results_dict, model
return results_dict, model, cvxpy_constraints

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@fletchapin when using the binary_big_M decomposition method for cvxpy, it has to create 3 new constraints in _decompose_binary_cvx that get passed back up to calculate_cost. Do you think it's better to:

  1. Return cvxpy_constraints as a separate item in this tuple (current implementation here)
  2. Return results_dict, model_objects where "model_objects" could either be a pyomo model or a cvxpy constraint list?

Option 2 would prevent this update breaking older code that calls calculate_cost, but will take a little more work to change some of the checks for model being "None"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated to option 2 which should avoid any issues with existing calls to calculate_cost!

Comment thread eeco/utils.py

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@arao53 Interested in any thoughts you have on the MILP functions here _decompose_binary_cvx and _decompose_binary_pyo. No rush on this since I'll make any further updates when I get back mid-April

dalyw added 5 commits May 1, 2026 14:42
Adding codecov for new lines

Allowing big_m as optional argument for decomposition_type
Removing outdated binary_variable references
…ict from prior data. Helper for use in other repositories that need to pass in a prev_demand_dict to calculate_cost
…cts instead of model. (model_objects can be Pyomo model m OR a list of cvxpy constraints)

update calculate_itemized_cost to preserve the model_objects returned by calculate_cost
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