Can I build a model using at the same time a matrix approach and a dictionary approach?
回答済みHi Gurobi Community,
I would like to know if it is possible to mix in the same model variables which have been defined with m.addMVar and variables which has been defined with m.addVars?
Both types of variables are not together in the same constraints, but they are together in the objective function.
I tried to implement this, but I get an error when I define the objective function and I obtained this error: Error code-1: Incompatible dimensions.
"""
Created on Mon Mar 7 14:30:11 2022
@author: calle
Gurobi Matrix Example Modified
max x +2y +z + w1 + w2
st
x+2y+3z <= 4
x+2y >= 1
x+z >= 1
w1+w2 <= 10
3w1 + 2w2 <= 15
x,y,z, binary
w1, w2, continuous
Restrictions of w can be rewritten as:
\sum_{i \in I} D_{i,r} w_i <= C_r \forall r \in R
Objective function = sum_i w_i
As an extension to the gurobi example I added the thrid constraint
and dictionary variables
"""
#import libraries
import gurobipy as gp
from gurobipy import GRB
import numpy as np
import scipy.sparse as sp
try:
#Create model
m = gp.Model("Matrix1")
#read data
#read array objective function
obj = np.array([1.0, 2.0, 1.0])
#read array right hand side
rhs1 = np.array([4.0, -1.0])
#read technological coefficient matrix
#non zer values of the matrix
#the negative comes from the transformation of the equation in <=
val1 = np.array([1.0, 2.0, 3.0, -1.0, -2.0])
#rows index of the non zero values of the matrix
row1 = np.array([0, 0, 0, 1, 1])
#col index of the non zero values of the matrix
col1 = np.array([0, 1, 2, 0, 1])
#define matrix
A1 = sp.csr_matrix((val1, (row1, col1)), shape=(2, 3))
#elements of third constraint
rhs2 = np.array([1.0])
val2 = np.array([1.0,1.0])
row2 = np.array([0, 0])
col2 = np.array([0, 2])
A2 = sp.csr_matrix((val2, (row2, col2)), shape = (1,3))
#Create sets
I = np.array(['i1', 'i2'])
R = np.array(['r1', 'r2'])
#Create parameters
D = {('i1', 'r1'): 1, ('i1', 'r2'): 3, ('i2', 'r1'): 1, ('i2', 'r2'): 2}
C = {'r1': 10, 'r2': 15}
#define variables
x = m.addMVar(shape = 3, vtype = GRB.BINARY, name = "x")
w = m.addVars(I, name = 'w')
#define constraints
m.addConstr(A1 @ x <= rhs1, name = "c")
#Third constraint
m.addConstr(A2 @ x >= rhs2, name = "c3")
#w constraints
m.addConstrs((gp.quicksum(D[i,r] * w[i] for i in I)
<= C[r] for r in R), name = 'c_resource_capacity')
#define objective function
term1 = obj @ x
term2 = sum(w[i] for i in I)
of = term1 + term2
m.setObjective(of, GRB.MAXIMIZE)
#write model
m.write("matrix_dict_gurobi.lp")
#optimise
m.optimize()
#print
print(x.X)
x_sol = x.X
of_sol = m.objVal
m.write("matrix_sol.sol")
except gp. GurobiError as e:
print ('Error code' + str(e. errno ) + ": " + str(e))
except AttributeError :
print ('Encountered an attribute error ')
-
Hi Juan,
Which Gurobi version you are using? In Gurobi versions <= 9.5.0, it is not possible to add an instance of LinExpr(your \(\texttt{term2}\) objective) to an instance of MLinExpr (your \(\texttt{term1}\) objective). However, in the latest Gurobi version 9.5.1, this operation can be done.
The Gurobi Matrix API has some limitations, but our development team is continually working to improve the usability of the Matrix API. Therefore, I would suggest updating to the latest version to have access to the latest improvement.
Best regards,
Maliheh
0 -
Hi Maliheh,
Thanks for your reply.
I noticed that I was using Gurobi 9.1.1
I am going to update to the latest version.
Best,
Juan
0
サインインしてコメントを残してください。
コメント
2件のコメント