Value Error when solving pypsa model in linopy with changed objective

29 views
Skip to first unread message

gktho...@gmail.com

unread,
Mar 6, 2023, 5:55:39 AM3/6/23
to pypsa
Hi everybody,

I am getting a Value Error if I modify the objective. The model solves, and gives me the solution, but it seems that something goes wrong when unpacking it.

The code I used is for a redisptach optimization, where I want the model to maintain the net positions of each zone as far as possible:

o = r.model
line_s = o["Line-s"]

grouper_lines0 = xr.DataArray(r.lines.bus0.map(r.buses.zone))
grouper_lines1 = xr.DataArray(r.lines.bus1.map(r.buses.zone))
net_positions0 = line_s.T.groupby(grouper_lines0).sum()
net_positions1 = line_s.T.groupby(grouper_lines1).sum()

dev_net_positions = o.add_variables(lower=0, coords=net_positions1.coords)

lhs1 = dev_net_positions + net_positions0 - net_positions1
lhs2 =  net_positions0 - net_positions1 - dev_net_positions

rhs = xr.DataArray((
    r.lines_t.p0.groupby(r.lines.bus0.map(r.buses.zone),axis=1).sum()
    +  r.lines_t.p1.groupby(r.lines.bus1.map(r.buses.zone),axis=1).sum())
)

o.add_constraints(lhs1>=rhs, name="Net-positions1")
o.add_constraints(lhs2<=rhs, name="Net-positions2")

obj = o.objective
o.add_objective(obj + 1000*dev_net_positions.sum().sum(), overwrite=True)

r.optimize.solve_model(solver_name="highs")

This produces the following error code:

ValueError Traceback (most recent call last) Input In [61], in <cell line: 1>() ----> 1 r.optimize.solve_model(solver_name="highs") File ~/.conda/envs/pypsa-eur2/lib/python3.10/site-packages/pypsa/optimization/optimize.py:502, in OptimizationAccessor.solve_model(self, **kwargs) 499 status, condition = m.solve(**kwargs) 501 if status == "ok": --> 502 assign_solution(n) 503 assign_duals(n) 504 post_processing(n) File ~/.conda/envs/pypsa-eur2/lib/python3.10/site-packages/pypsa/optimization/optimize.py:248, in assign_solution(n) 245 if name == "objective_constant": 246 continue --> 248 c, attr = name.split("-", 1) 249 df = sol.to_pandas() 251 if "snapshot" in sol.dims: ValueError: not enough values to unpack (expected 2, got 1)

Maybe this happens when I overwrite the objective? Do I have to pass anything else to the model when doing so, or do you have any other ideas what could solve this?

If I do o.solve(solver_name="highs") it solves fine, so the underlying linopy model seems to be alright.

Best,
Georg
Reply all
Reply to author
Forward
0 new messages