code(logic) works with CVXPY using the Gurobi solver, but not in Gurobi himself
OngoingHi, I have an optimization problem and when I run it in CVXPY with Gurobi SOLVER, I am getting results; but when I run it with Gurobi, it says " Model is infeasible"
Here is the code:
"import pandas as pd
import numpy as np
import gurobipy as gp
from gurobipy import *
n = 96
S = pd.read_csv("\..case2.csv")
S = S.drop(S.columns[[0]], axis=1)
load = S['Load'][1:n].to_numpy()
PV = S['PV'][1:n].to_numpy()
TOU = S['TOU'][1:n].to_numpy()
TOU = TOU*0.01
load = list(load)
PV = list(PV)
TOU = list (TOU)
#10kWh is the actual capacity
BatCap1 = 10
BatCap2 = 10
BatCap3 = 10
BatCap4 = 10
Cexport = .1
# Create a new model
m = Model()
n = 96
# Create variables
#x is charging, discharging variable for each battery
x1 = m.addVars(n-1,lb=-5.5,ub=5.5,vtype=GRB.INTEGER, name="x1")
x2 = m.addVars(n-1,lb=-5.5,ub=5.5,vtype=GRB.INTEGER, name="x2")
x3 = m.addVars(n-1,lb=-5.5,ub=5.5,vtype=GRB.INTEGER, name="x3")
x4 = m.addVars(n-1,lb=-5.5,ub=5.5,vtype=GRB.INTEGER, name="x4")
#SOC
SOC1 = m.addVars(n,vtype=GRB.CONTINUOUS, name="SOC1")
SOC2 = m.addVars(n,vtype=GRB.CONTINUOUS, name="SOC2")
SOC3 = m.addVars(n,vtype=GRB.CONTINUOUS, name="SOC3")
SOC4 = m.addVars(n,vtype=GRB.CONTINUOUS, name="SOC4")
# Add constraint: SOC[start]=50, initial SOC
m.addConstr(SOC1[0] == 50)
m.addConstrs(0 <= SOC1[i] <= 100 for i in range(n))
m.addConstr(SOC2[0] == 50)
m.addConstrs(0 <= SOC2[i] <= 100 for i in range(n))
m.addConstr(SOC3[0] == 50)
m.addConstrs(0 <= SOC3[i] <= 100 for i in range(n))
m.addConstr(SOC4[0] == 50)
m.addConstrs(0 <= SOC4[i] <= 100 for i in range(n))
m.addConstrs ( (SOC1[i+1]-SOC1[i] == (x1[i]/(4*BatCap1)*100)) for i in range(n-1))
m.addConstrs ( (SOC2[i+1]-SOC2[i] == (x2[i]/(4*BatCap2)*100)) for i in range(n-1))
m.addConstrs ( (SOC3[i+1]-SOC3[i] == (x3[i]/(4*BatCap3)*100)) for i in range(n-1))
m.addConstrs ( (SOC4[i+1]-SOC4[i] == (x4[i]/(4*BatCap4)*100)) for i in range(n-1))
Deltat=.25
Pimport = m.addVars(n,vtype=GRB.CONTINUOUS, name = "Pimport")
Pimportmax = m.addVars(n,vtype=GRB.CONTINUOUS, name = "Pimportmax")
for i in range(n-1):
Pimport[i] == (load[i]+(x1[i]+x2[i]+x3[i]+x4[i])-PV[i])
for i in range(n-1):
Pimportmax[i] == gp.max_([Pimport[i],0.0])
obj1 = sum( (Pimportmax[i]*TOU[i]*Deltat) for i in range(n-1))
m.addConstrs (Pexport[i] == (PV[i]-load[i]-(x1[i]+x2[i]+x3[i]+x4[i])) for i in range(n-1))
m.setObjective(obj1,GRB.MINIMIZE)
m.optimize()
"
Can any one please help me with this issue?
-
Official comment
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?. -
Hi Saeed,
Would it be possible to send us the csv file or a minimal working example such that we can reproduce the issue? You can find the instruction for sharing files in our Posting to the Community Forum article.
Two points to consider:- Can the variables \(\texttt{Pimport}\) be negative? The constraints below imply that the variables \(\texttt{Pimport}\) can be negative otherwise there is no need for such constraints. However, \(\texttt{Pimport}\) variables are defined with the default lower bound of 0.
Pimportmax[i] == gp.max_([Pimport[i],0.0])
- Furthermore, the above constraints as well as the constraints defining \(\texttt{Pimport}\) need to be explicitly added to the model by the model.addConstrs() method to be in effect.
Best regards,Maliheh
1
Post is closed for comments.
Comments
2 comments