-
Notifications
You must be signed in to change notification settings - Fork 437
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
Bug(?) when adding constraints to Links #757
Comments
What is the error message? Do you have an example how we can reproduce the behaviour? |
Hi! Example below:
|
Hey,
the code worked for me. Perhaps you have to update Linopy?
Here are some improvements:
import pypsa
import xarray as xr
import pandas as pd
SNAPSHOTS = pd.date_range("2023-01-01 09:00", "2023-01-01 19:00", freq="H")
LOAD = {
"name": "load_1",
"profile": pd.Series(
[40, 70, 60, 50, 10, 20, 40, 50, 50, 40, 40], index=SNAPSHOTS
),
}
GENERATORS = [
{"name": "gas_1", "p_nom": 100, "p_min": 0, "marginal_cost": 10},
{"name": "gas_2", "p_nom": 100, "p_min": 50, "marginal_cost": 5},
]
############
network = pypsa.Network()
network.set_snapshots(SNAPSHOTS)
main_bus = "bus_grid"
network.add("Bus", name=main_bus)
for gen in GENERATORS:
network.add(
"Generator",
name=gen["name"],
bus=main_bus,
committable=True,
p_nom=gen["p_nom"],
marginal_cost=gen["marginal_cost"],
)
network.generators["p_min"] = [gen["p_min"] for gen in GENERATORS]
network.add("Load", name=LOAD["name"], bus=main_bus, p_set=LOAD["profile"])
network_model = network.optimize.create_model()
generator_power = network_model.variables["Generator-p"]
generator_status = network_model.variables["Generator-status"]
p_min = xr.DataArray(network.generators.p_min, dims=["Generator-com"])
lhs = generator_power - generator_status * p_min
rhs = 0
network_model.add_constraints(lhs >= rhs, name="Generator-p-min")
network.optimize.solve_model(solver_name="highs")
|
I have the following version: Do you have anything different? Regarding the improvement -> since there will be two dims "Generator" and "Generator-com", it will add wrongly some constraints, right? And thank you for the help! |
Same with me, The error is File ~/miniconda3/envs/pypsa/lib/python3.10/site-packages/pypsa/optimization/optimize.py:384, in assign_duals(n, assign_all_duals) File ~/miniconda3/envs/pypsa/lib/python3.10/site-packages/linopy/model.py:189, in Model.dual(self) File ~/miniconda3/envs/pypsa/lib/python3.10/site-packages/linopy/constraints.py:626, in Constraints.dual(self) File ~/miniconda3/envs/pypsa/lib/python3.10/site-packages/linopy/common.py:131, in save_join(integer_dtype, *dataarrays) File ~/miniconda3/envs/pypsa/lib/python3.10/site-packages/xarray/core/dataset.py:696, in Dataset.init(self, data_vars, coords, attrs) File ~/miniconda3/envs/pypsa/lib/python3.10/site-packages/xarray/core/dataset.py:425, in merge_data_and_coords(data_vars, coords) File ~/miniconda3/envs/pypsa/lib/python3.10/site-packages/xarray/core/merge.py:720, in merge_core(objects, compat, join, combine_attrs, priority_arg, explicit_coords, indexes, fill_value, skip_align_args) File ~/miniconda3/envs/pypsa/lib/python3.10/site-packages/xarray/core/merge.py:290, in merge_collected(grouped, prioritized, compat, combine_attrs, equals) File ~/miniconda3/envs/pypsa/lib/python3.10/site-packages/xarray/core/merge.py:144, in unique_variable(name, variables, compat, equals) MergeError: conflicting values for variable 'Link' on objects to be combined. You can skip this check by specifying compat='override'. |
hey @rizky-r-lab, could you confirm you are one the later master branch in pypsa? |
hey @FabianHofmann , i install using conda. However if i install using latest master branch, there are several package installed and the error disappear. |
Hi, We are having a similar issue when we add these variables and constraints. The solver solves, but it also seems in post processing to trough the error below. ` #Obtain variables and attributes for link1 & store1
Error `C:\Users\User\AppData\Roaming\Python\Python39\site-packages\linopy\common.py:124: UserWarning: Coordinates across variables not equal. Perform outer join. File "C:\Users\User\OneDrive\S(Pypsa)\2401_GMNK\Single-Yr_GMNK_v3.2.py", line 186, in File "C:\Users\User\OneDrive\S(Pypsa)\2401_GMNK\Single-Yr_GMNK_v3.2.py", line 165, in expansion File "C:\Users\User.conda\envs\pypsa-root\lib\site-packages\pypsa\optimization\optimize.py", line 616, in solve_model File "C:\Users\User.conda\envs\pypsa-root\lib\site-packages\pypsa\optimization\optimize.py", line 384, in assign_duals File "C:\Users\User\AppData\Roaming\Python\Python39\site-packages\linopy\model.py", line 189, in dual File "C:\Users\User\AppData\Roaming\Python\Python39\site-packages\linopy\constraints.py", line 626, in dual File "C:\Users\User\AppData\Roaming\Python\Python39\site-packages\linopy\common.py", line 131, in save_join File "C:\Users\User.conda\envs\pypsa-root\lib\site-packages\xarray\core\dataset.py", line 696, in init File "C:\Users\User.conda\envs\pypsa-root\lib\site-packages\xarray\core\dataset.py", line 425, in merge_data_and_coords File "C:\Users\User.conda\envs\pypsa-root\lib\site-packages\xarray\core\merge.py", line 720, in merge_core File "C:\Users\User.conda\envs\pypsa-root\lib\site-packages\xarray\core\merge.py", line 290, in merge_collected File "C:\Users\User.conda\envs\pypsa-root\lib\site-packages\xarray\core\merge.py", line 144, in unique_variable MergeError: conflicting values for variable 'Link' on objects to be combined. You can skip this check by specifying compat='override'. None` |
Checklist
master
branch or the latestDescribe the Bug
I think there might me an error when post-processing the network model, when multiple Links have additional constraints. When it is just one, it works fine.
As an example:
Looking at a modelling like:
And adding the following constraint:
Error Message
Xarray will raise an error in post-processing on merge data
Note: The optimization is done correctly, however the post processing raises an error
The text was updated successfully, but these errors were encountered: