This is connected posting of recent question.
There is an objective function including a formula which is actually so complicated to convert into gurobi library type or slack using. And the input of the formula is index of decision variable which have value of 0 or positive integer. As per previous comment, I constructed a following sample code using indicator constraint which contains constant value as t and i. But it has some error like
TypeError: unsupported operand type(s) for -=: 'NoneType' and 'float'
Could you please advise on the programming difficulty.
import numpy as np
import gurobipy as gp
from gurobipy import GRB
k = np.array([1 for i in range(10)])
for j in x:
k[j] = 0
m = gp.Model("qp")
x = m.addVars(10 ,lb=0, ub=10, vtype=GRB.INTEGER, name="x")
y = m.addVars(10, lb=0, ub=10, vtype=GRB.INTEGER, name="y")
b = m.addVars(10, vtype=GRB.BINARY, name="b")
# Basic constraints
m.addConstrs(x[i] + 2 * y[i] <= 21 for i in range(10))
m.addConstrs(x[i] - y[i] >= 0 for i in range(10))
m.addConstr(x.sum('*') == 9)
# big-M approach
eps = 0.000001
M = 100 + eps
# Model if x > 0 then b = 1, otherwise b = 0
m.addConstrs(x[i] >= eps - M * (1 - b[i]) for i in range(10))
m.addConstrs(x[i] <= M * b[i] for i in range(10))
# Indicator constraints
z =  # ← ← ← Initialization of an input for nonlinear-type formulation
t = -1
for i in range(10):
m.addConstr((b[i] == 1) >> (t == i))
# Objective function
obj = gp.quicksum(x[i] + y[i] for i in range(10))
m.setObjective(obj + tmp_f(z), GRB.MAXIMIZE)
Please sign in to leave a comment.