Invalid argument to Model.addConstr
AnsweredDear,
I am getting the following error: Invalid argument to Model.addConstr.
I put my code downstairs, could you please help me to fix this error? Thank you!
Here is my code:
import random
import numpy as np
import gurobipy as gp
from gurobipy import GRB
from gurobipy import *
from gurobipy import quicksum
m=15
n=10
m_a1 = (m + 1) // 2
m_a2 = m - (m + 1) // 2
np.random.seed(1)
#Define x^0
x0_first_part = np.random.uniform(-1, 0, n // 2)
# Generate the next ⌊n/2⌋ - 1 components from Unif(0, 10)
x0_second_part = np.random.uniform(0, 10, n // 2 -1)
# Set the last component to zero
x0_last_part = np.array([0])
# Combine all parts to create the vector x^0
x0 = np.concatenate([x0_first_part, x0_second_part, x0_last_part])
x0
xi = np.zeros((m, n))
# Loop through the first ceil(m/2) data points
for i in range((m + 1) // 2):
# Assign the sensitive attribute as -1
xi[i, -1] = -1
# Assign the first (n-1) features from Unif(0, j)
for j in range(1, n):
xi[i, j-1] = np.random.uniform(0, j)
# Loop through the remaining data points
for i in range((m + 1) // 2, m):
# Assign the sensitive attribute as 1
xi[i, -1] = 1
# Assign the first (n-1) features from Unif(0, j+2)
for j in range(1, n):
xi[i, j-1] = np.random.uniform(0, j + 2)
E_xi = np.mean(xi, axis=0)
# Calculate the dot product of E[xi]^T and x0
dot_product = np.dot(E_xi, x0)
# Generate the noise from Unif(-0.1, 0.1)
uniform_noise = np.random.uniform(-0.1, 0.1, size=m)
# Final noise for each data point is uniform_noise * dot_product
noise = uniform_noise * dot_product
y_tilde = np.dot(xi, x0) + noise
var=list(range(m))
var2 = list(range(n))
m0 = gp.Model("Knapsack")
x = m0.addVars(var, vtype = GRB.CONTINUOUS, name = "x")
z = m0.addVars(var, vtype=GRB.CONTINUOUS, name="z")
obj_expr1 = gp.quicksum(z[i] for i in var)
# First half: sum of z[i] for i from 0 to 7 (indices 1 to 8)
obj_expr2 = gp.quicksum(z[i] for i in range(8))
# Second half: sum of z[i] for i from 8 to 14 (indices 9 to 15)
obj_expr3 = gp.quicksum(z[i] for i in range(8, 15))
objective = (obj_expr1 / m) + (obj_expr2 * m_a2 / (m_a1 * m)) + (obj_expr3 * m_a1 / (m_a2 * m))
for i in var:
expr = sum(xi[i] * x[i] for i in var) - y_tilde[i] # This creates a LinExpr using Gurobi's variable x[i]
m0.addConstr(z[i] >= expr)
m0.addConstr(z[i] >= -expr)
m0.setObjective(objective, sense=GRB.MINIMIZE)
m0.optimize()
-
Official comment
This post is more than two 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 try Gurobot, our chatbot interface offering instant, expert-level support. -
Hi Yutian,
I'll try and help you debug, rather than give you the answer.
The full error gives you the exact place where the error occurs:
GurobiError Traceback (most recent call last)
Cell In[4], line 89
85 for i in var:
86 expr = sum(xi[i] * x[i] for i in var) - y_tilde[i] # This creates a LinExpr using Gurobi's variable x[i]
---> 89 m0.addConstr(z[i] >= expr)
90 m0.addConstr(z[i] >= -expr)
92 m0.setObjective(objective, sense=GRB.MINIMIZE)
File src/gurobipy/model.pxi:3675, in gurobipy.Model.addConstr()
File src/gurobipy/model.pxi:3467, in gurobipy.Model.__addConstr()
GurobiError: Invalid argument to Model.addConstrThe error is saying the addConstr method does not like something about z[i] >= expr. If you put the following lines of code just before the line which causes the error, then it will print the value of z[i] and expr to screen.
print(f"{z[i]=}")
print(f"{expr=}")Does the output look like what you would expect?
- Riley
0 -
Hi Riley,
Thank you for the two-line code! You are right, looks like it is not what I want, I will check it later and give you more feedback. Thank you again!
0
Post is closed for comments.
Comments
3 comments