Reputation: 119
I have a question regarding to pyomo dual variable retrieval. I'm trying to retrieve the dual variable from the constraint "model.market_clearing_const". However, after running these codes folowing, the error I got is:
"Component with id '2886755528520': market_clearing_const[0]"
So basically if I look into the dual component under model, I see suffix=0. So this means there's no dual variables are stored in suffix despite the fact that I already declare it. And my solution is feasible.
Any inputs on this are appreciated! Thank you.
import pandas as pd
from pyomo.environ import *
from pyomo.opt import SolverFactory
import numpy as np
model = ConcreteModel(name="Unit_Commitment")
EGU_temp = pd.read_excel('cost_uc.xlsx','EGUs')
demand_temp = pd.read_excel('cost_uc.xlsx','demand')
wind_temp = pd.read_excel('cost_uc.xlsx','wind')
# Calculate total variable cost cost
EGU_temp['tvc'] = EGU_temp['fuelcost']*EGU_temp['heatrate']/1000 + EGU_temp['vom']
EGU = EGU_temp.to_dict()
demand = demand_temp.to_dict()
wind = wind_temp.to_dict()
# Define set:
U = list(range(10)) # Set of Units
H = list(range(24)) # Set of Hours
# Define parameters:
pmax = EGU['pmax']
pmin = EGU['pmin']
tvc = EGU['tvc']
startup_cost = EGU['startupcost']
load = demand['load']
# Define variables:
model.x = Var(U, H, within=NonNegativeReals)
model.v = Var(U, H, within=Binary)
model.vU = Var(U, H, within=Binary)
model.vD = Var(U, H, within=Binary)
# Define Objective function:
def obj_function(model):
return sum(model.vU[u, h]*startup_cost[u] for u in U for h in H)\
+ sum(model.x[u, h]*tvc[u] for u in U for h in H)
model.obj_func = Objective(rule=obj_function)
# Define constraints:
def upperbound(model,u,h):
return model.x[u,h] <= model.v[u, h]*pmax[u]
model.ub = Constraint(U,H, rule=upperbound)
def lowerbound(model,u,h):
return model.x[u,h] >= model.v[u,h]*pmin[u]
model.lb = Constraint(U,H, rule=lowerbound)
# Market Clearing condition:
def market_clearing(model, h):
return sum(model.x[u, h] for u in U) == load[h]
model.market_clearing_const = Constraint(H, rule=market_clearing)
# Turn on/shut down constraints:
def on_off(model,u,h):
if h == 0:
return Constraint.Skip
else:
return model.v[u, h] == model.v[u, h - 1] + model.vU[u, h] - model.vD[u, h]
model.onoff_const = Constraint(U,H, rule=on_off)
# Solve the model and report results:
model.dual = Suffix(direction=Suffix.IMPORT_EXPORT)
solver = SolverFactory('glpk')
results = solver.solve(model, tee=True)
model.pprint()
if (results.solver.status == SolverStatus.ok) and (results.solver.termination_condition == TerminationCondition.optimal):
print('Solution is feasible')
elif (results.solver.termination_condition == TerminationCondition.infeasible):
print('Solution is infeasible')
else:
# Something else is wrong
print('Solver Status: ', results.solver.status)
print('Total Cost:', value(model.obj_func))
# Print x results:
for u in U:
for h in H:
print('<unit '+str(u+1), 'in hour '+str(h+1)+'>:', value(model.x[u, h]))
# Print shadow price (dual variable):
price = unit_1 = np.zeros(24)
for h in H:
price[h] = model.dual[model.market_clearing_const[h]]
Upvotes: 0
Views: 906
Reputation: 16724
Duals only exist for pure continuous LP models. You have binary variables, making the model a MIP. MIP models don't have duals.
Upvotes: 1