Define a matrix as a variable and write it in constraint
OngoingHi,
I have a quick question about the matrix variable, when I defined the variable and tried to write it in constraint, I got an error for expr1_3:"numpy.core.multiarray failed to import", I tried to reinstall the numpy, but it didn't work. The constraints I want is: \sum_{i \in ma_1} pi_{ij} = 1 for every j \in ma_1.Could you please help me to check the error? Thank you! Here is my code:
m=15
n=10
var=list(range(m))
var2 = list(range(n))
ma_1=8
ma_2=7
var_a1 = list(range(m_a1))
var_a2 = list(range(m_a2))
vector_a1 = np.array([1,1,1,1,1,1,1,1])
vector_a2 = np.array([1,1,1,1,1,1,1])
m1 = gp.Model("Fair_regression")
x1 = m1.addVars(var2, vtype = GRB.CONTINUOUS, lb=-GRB.INFINITY, name = "x1")
pi_a1 = m1.addMVar(shape=(m_a1, m_a1), vtype=GRB.BINARY, name="pi_a1")
pi_a2 = m1.addMVar(shape=(m_a2, m_a2), vtype=GRB.BINARY, name="pi_a2")
for k in var_a1:
expr1_3 = gp.quicksum(pi_a1[k][l]*vector_a1[l] for l in var_a1)
print(f"{expr1_3 =}")
m1.addConstr(expr1_3 == 1)
m1.write("Fair_"+".lp")
-
Hi Yutian,
I suspect you're using an old version of numpy.
Can you either add run this in a python script:
import numpy as np
import gurobipy as gp
print("numpy:", np.__version__)
print("gurobipy:", gp.gurobi.version())or alternatively the command: pip list (from a terminal)
Then share the output with us.
- Riley
0 -
Thanks for the quick reply. Here is the result.
0 -
Ah, you actually have the opposite problem!
Upgrading to gurobipy 11.0.3 will fix this issue.
- Riley
0 -
Yes, that works! Thank you. And now I have a new question, since I want the constraints \sum_{i \in ma_1} pi_{ij} = 1 for every j \in ma_1. I wrote the constraint like this:
for k in var_a1:
expr1_3 = gp.quicksum(pi_a1[k][l]*vector_a1[l] for l in var_a1)
print(f"{expr1_3 =}")
m1.addConstr(expr1_3 == 1)
But this constraint cannot be written by the m1.write("Fair_"+".lp"), I think there should be some other way to write the sum. And I meet the same question for this code:for s in var_new:
expr1_3=(b[s+1]-b[s])*(eta[s]**2)
print(f"{expr1_3 =}")
m1.addConstr((sum(b[s+1]-b[s])*(eta[s]**2) for s in var_new) <= nu[0])Could you please help me for this? Thank you.
0 -
Hi Yutian,
What is the error you see? Once I take care of the ma_1, ma_2 typo in your code it runs ok and writes the LP file ok. Note though that your code will run slow. The point of the matrix API is to replace this:
for k in var_a1: expr1_3 = gp.quicksum(pi_a1[k][l]*vector_a1[l] for l in var_a1) print(f"{expr1_3 =}") m1.addConstr(expr1_3 == 1)
with this:
m1.addConstr(pi_a1@vector_a1 == 1)
If you are needing to index your MVar, eg pi_a1[k][l], this is a good indication you should not use MVars - just use
pi_a1 = m1.addVars(m_a1, m_a1, vtype=GRB.BINARY, name="pi_a1") pi_a2 = m1.addVars(m_a2, m_a2, vtype=GRB.BINARY, name="pi_a2")
and it will run a lot faster.
- Riley
0 -
Hi Riley,
m1.addConstr(pi_a1@vector_a1 == 1)
This code works very well with the previous definition of the pi_a1, thank you!Now I have two questions:
- When I changed pi to the new definition you wrote, I got this error here:
2. I'm going to set this constraint: \sum_{i from 1 to m-1} ((b_{i+1} - b_i)*eta_i^2) <= nu. When I run the following code:
var_new = list(range(m-1))
var3=list(range(1))
b= array([0. , 0.125 , 0.14285714, 0.25 , 0.28571429, 0.375 , 0.42857143, 0.5 , 0.57142857, 0.625 , 0.71428571, 0.75 , 0.85714286, 0.875 , 1. ])
eta = m1.addVars(var_new, vtype=GRB.CONTINUOUS, name = "eta")
nu = m1.addVars(var3, vtype=GRB.CONTINUOUS, lb=0, name = "v")
for s in var_new:
expr1_3=(b[s+1]-b[s])*(eta[s]**2)
# print(f"{expr1_3 =}")
m1.addConstr((sum(b[s+1]-b[s])*(eta[s]**2) for s in var_new) <= nu[0])I got this error:
I met this error several times, but have no idea how to fix it, could you please also help me to check it? Thank you!
0
Please sign in to leave a comment.
Comments
6 comments