Skip to content
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

Add function for querying attributes of solver model #247

Merged
merged 4 commits into from
Apr 10, 2024
Merged

Conversation

Irieo
Copy link
Contributor

@Irieo Irieo commented Mar 6, 2024

The new function get_solver_attribute() introduces a mechanism for querying properties of a solver model through the attribute interface. This extends the functionality of Linopy interface by allowing users to retrieve properties from mathematical model, variable attributes and attributes associated with solution quality -> and map this information to dimensions of a linopy problem.

For now the functionality works with Gurobi interface. The list of attributes can be found here. NB some attributes are available at all times (e.g., the number of variables in the model), some are only populated at certain times (e.g., those related to the problems of a specific type).

The functionality can be extended to interfaces of other solvers if Linopy & PyPSA communities finds this useful.

The original idea driving this PR is to create convenient mechanism to retrieve & map to dimensions of an original linopy problem sensitivity information related to variables (SAObjLow, SAObjUp, RC) that is promising to be useful for both debugging and analysis for PyPSA applications.

Toy example:

import pypsa, pandas as pd
n = pypsa.Network()
n.add("Bus", "DE", v_nom=380)
n.add("Generator", "gas", bus="DE", marginal_cost=70, p_nom=50)
n.add("Generator", "coal", bus="DE", marginal_cost=40, p_nom=70)
n.snapshots = pd.date_range("2019-01-01", periods=5, freq="H")
load_series = pd.Series([30, 60, 80, 100, 70], index=n.snapshots)
n.add("Load", "Germany", bus="DE", p_set=load_series)
n.add("StorageUnit", "Battery", bus="DE", p_nom=20, max_hours=5, efficiency_store=1, efficiency_dispatch=0.9, state_of_charge_initial=0)
n.optimize(solver_name="gurobi")
n.model.variables.get_solver_attribute("SAObjUp")

@Irieo Irieo added the enhancement New feature or request label Mar 6, 2024
Copy link

codecov bot commented Apr 10, 2024

Codecov Report

Attention: Patch coverage is 88.88889% with 2 lines in your changes are missing coverage. Please review.

Project coverage is 88.94%. Comparing base (3bd24bc) to head (6c083d6).

Files Patch % Lines
linopy/variables.py 88.23% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #247      +/-   ##
==========================================
- Coverage   88.94%   88.94%   -0.01%     
==========================================
  Files          15       15              
  Lines        3645     3663      +18     
  Branches      856      861       +5     
==========================================
+ Hits         3242     3258      +16     
- Misses        279      281       +2     
  Partials      124      124              

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

@FabianHofmann FabianHofmann marked this pull request as ready for review April 10, 2024 12:34
@Irieo Irieo changed the title Store new var attribute: objective sensitivity information Add function for querying attributes of solver model Apr 10, 2024
@FabianHofmann FabianHofmann merged commit 5d61f95 into master Apr 10, 2024
11 of 13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants