unsupported operand type(s) for >>: 'bool' and 'TempConstr'
Answeredmodel.addConstrs((dx[k+1] == 1) >> (x[i,j,k+1] >= 1) for k in range(4))
dx is defined as: dx = model.addVars(9, vtype = GRB.BINARY, name="dx")
X (j, I, k) is defined as: x (j, I, k) = model. AddVar (vtype = GRBS. INTEGER, lb = 0, ub = 1, name = "x () % s, % s, % s" % (j, I, k))
The code I borrowed:
Created on Fri Apr 21 16:28:06 2023
@author: help@gurobi.cn
Problem description: The index is a variable processing method
(1) There is a set of random integers
(2) You need to find an optimal window that maximizes the sum of the numbers in the window
(3) The number of digits in the window cannot exceed 4
import gurobipy as gp
from gurobipy import GRB
# Random integer
numlist = [32, 90, 91, 18, 57, 12, 24, 62, 83, 56, 75, 4, 9, 82, 36, 65, 94, 44, 89, 43, 17, 45, 27, 85, 5, 98, 60, 59, 28, 93]
list_len = len(numlist)
model=gp.Model("VarIndex")
# Window lower bound
x=model.addVar(vtype=GRB.INTEGER,lb=0,ub=list_len-1)
# Window upper bound
y=model.addVar(vtype=GRB.INTEGER,lb=0,ub=list_len-1)
# The upper bound of the window cannot be smaller than the lower bound of the window
model.addConstr(y >= x)
# The window cannot contain more than 4 digits
model.addConstr(y-x <=3)
# Get the sum of values in the window
# Can not directly write gp.quicksum(numlist[i] for i in range(list_len) if i>= x and i<=y) because x,y are variables
# We need to define a new variable for each i newValue[i] = numlist[i] if i>=x and i <=y; newValue[i] = 0 if the condition is not met
The introduced if condition is usually implemented through the indicator constraint.
# First, it is necessary to introduce list_len 0-1 variables to represent whether the i>=x and i <=y conditions are satisfied
z=model.addVars(list_len,vtype=GRB.BINARY)
model.addConstrs((z[i] == 1) >> (x <= i) for i in range(list_len))
model.addConstrs((z[i] == 1) >> (y >= i) for i in range(list_len))
# Add list_len newValue variable. If z[i] satisfies the condition, newValue[i]== numlist[i], otherwise newValue[i]== 0
newValue = model.addVars(list_len)
model.addConstrs(newValue[i] == z[i] * numlist[i] for i in range(list_len))
# Sum all newValue[i] so that x and y do not need to appear in the subscript
obj = newValue.sum()
# Maximize the sum of values in the window
model.setObjective(obj, GRB.MAXIMIZE)
model.optimize()
# Print results
print(' Window start: '+ str(x.X))
print(' End of window: '+ str(y.X))
print(' Sum of values in window: '+ str(obj.getValue()))
print('z identifier: ')
print([z[i].x for i in range(list_len)])
-
The code works fine. You must have done something wrong with defining the variables. Here is a minimal working snippet that uses exactly your formulation, except for a correct way to define the x variables:
import gurobipy as gp
from gurobipy import GRB
model = gp.Model()
x = model.addVars(1, 1, 9, vtype=GRB.BINARY, name="dx")
dx = model.addVars(9, vtype=GRB.BINARY, name="dx")
i = 0
j = 0
model.addConstrs((dx[k + 1] == 1) >> (x[i, j, k + 1] >= 1) for k in range(4))0
Please sign in to leave a comment.
Comments
1 comment