I do not know how code this model?
AnsweredI want to write a Python code for the mentioned model:

where the p, y, and p_bar are the variables and the others are the parameters and t is:0,1,...,6 and j is 1,2,3.
I wrote the code as:
import numpy as np
import pandas as pd
import scipy.sparse as sp
import gurobipy as gp
from gurobipy import *
T=7
n = 3
m=gp.Model('Initial')
p = {}
for j in range(1,4):
for t in range(T):
p[j,t] = m.addVar(vtype=gp.GRB.BINARY,
name='p'+str(j)+'_'+str(t))
pm = {}
for j in range(1,4):
for t in range(T):
pm[j,t] = m.addVar(vtype=gp.GRB.BINARY,
name='pm'+str(j)+'_'+str(t))
v = {}
for j in range(1,4):
for t in range(T):
v[j,t] = m.addVar(vtype=gp.GRB.BINARY,
name='v'+str(j)+'_'+str(t))
y = {}
for j in range(1,4):
for t in range(T):
y[j,t] = m.addVar(vtype=gp.GRB.BINARY,
name='y'+str(j)+'_'+str(t))
z = {}
for j in range(1,4):
for t in range(T):
z[j,t] = m.addVar(vtype=gp.GRB.BINARY,
name='z'+str(j)+'_'+str(t))
c = np.array([5.0, 15.0, 30.0])
cU = np.array([800.0, 500.0, 250.0])
Pmin = np.array([80.0, 50.0, 30.0])
Pmax = np.array([300.0, 200.0, 100.0])
SU = np.array([100.0, 70.0, 40.0])
SD = np.array([80.0, 50.0, 30.0])
RU = np.array([50.0, 60.0, 70.0])
RD = np.array([30.0, 40.0, 50.0])
TU = np.array([3.0, 2.0, 1.0])
TD = np.array([2.0, 2.0, 2.0])
for t in range(0,7):
R= [0,10,10,10,10,10,10]
D= [0, 240, 250, 200, 170, 230, 190]
DR = list(map(lambda x,y: x+y, DD,R))
m.setObjective((gp.quicksum((5 * p[1,t]+ 15 * p[2,t]+ 30 * p[3,t] + 800 * y[1,t]+ 500 * y[2,t]+ 250 * y[3,t])
for t in range(1,7))), GRB.MINIMIZE)
for t in range(1,7):
m.addConstr(((gp.quicksum(p[j,t]) for j in range(1,4))) == (D[t]))
Questions:
1)
BUT I DO NOT KNOW HOW TO WRITE A CODE FOR THE SECOND CONSTRAINT.
2)
How to write a code for this constraint:

Would you help me?
-
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?. -
These constraints:
for t in range(1,7):
m.addConstr(((gp.quicksum(p[j,t]) for j in range(1,4))) == (D[t]))do not do what you want. You should move the \( \texttt{for j in range(1, 4)} \) into the quicksum() function:
for t in range(1, 7):
m.addConstr(gp.quicksum(p[j, t] for j in range(1, 4)) == D[t])Additionally, \( T = \{0, \ldots, 6\} \), but you only add these constraints for the set \( \{1, \ldots, 6\} \). The summation in your objective is similar. So the mathematical formulation and the code are inconsistent.
The second constraint family can be written in a similar way as the first, assuming \( \texttt{pm} \) corresponds to \( \bar{p} \) in the model formulation:
for t in range(T):
m.addConstr(gp.quicksum(pm[j, t] for j in range(1, 4)) >= D[t] + R[t])For the last constraint family, you should add the constraint for all \( t \in T \setminus \{0\} \) instead of for all \( t \in T \). The constraint for \( t = 0 \) doesn't make sense, because you use \( t - 1 \) to index \( p \) and \( v_j \). Note that you have to be careful how you index \( R_j^U \) and \( S_j^U \) in the code, since Python uses zero-based indexing (\( \{0, 1, 2\} \)), but \( J = \{1, 2, 3\} \):
for t in range(1, T):
for j in range(1, 4):
m.addConstr(p[j, t] - p[j, t-1] <= RU[j-1] * v[j, t-1] + SU[j-1] * y[j, t])You can verify the model is written correctly by writing an LP file to disk for manual inspection:
m.write('mymodel.lp')A couple of comments that could help make your code more concise:
- The code would be easier to read if you define \( \texttt{T = range(7)} \) and \( \texttt{J = range(1, 4)} \) at the beginning. Then, you can just loop over \( \texttt{T} \) and \( \texttt{J} \) in the code. This also matches the formulation better, where \( T \) is a set rather than a number.
- You can use Model.addVars() to add each set of variables in a single line and obtain a tupledict storing variable objects.
E.g.:
T = range(7)
J = range(1, 4)
p = m.addVars(J, T, vtype=gp.GRB.BINARY, name='p')1 -
Thank you very much. It works nicely.
Best Wishes to you Eli.
0
Post is closed for comments.
Comments
3 comments