How to retrieve 2D variables in multi-objective solutions?
回答済みI am running a multi-objective model and got 3 solutions. The list "solutions" I used can retrievee all the decision variables as 3 lists, but how do I get the exact 2D decision variables defined in this problem instead of using for-loops to extract them in the list "solutions"?
I tried x.x, y.x, and z.x, but it shows the values for only one solution...
Here is my code:
import gurobipy as gp
from gurobipy import GRB
import numpy as np
m = gp.Model("Practice")
x = m.addMVar(shape=((3,3)),lb=0,ub=10000, vtype=GRB.CONTINUOUS,name ="x")
y = m.addMVar(shape=(3),lb=0,vtype=GRB.CONTINUOUS,name ="y")
z = m.addMVar(shape=((3,3)),lb=0,vtype=GRB.BINARY,name ="z")
D=np.ones((3,3))*9
for j in range(3):
m.addConstr(y[j] <= x[:,j]@z[:,j], "c2");
m.addConstr(sum(z[:,j]) <= 1, "c3")
TD=0
for i in range(3):
for j in range(3):
TD += z[i,j]*D[i,j]
R = sum(y)
m.setObjectiveN(R,0,2, GRB.MAXIMIZE);
m.setObjectiveN(TD,1,1, GRB.MINIMIZE);
m.optimize();
# get the set of variables
v = m.getVars()
# Query number of multiple objectives, and number of solutions
nSolutions = m.SolCount
nObjectives = m.NumObj
solutions = []
for s in range(nSolutions):
# Set which solution we will query from now on
m.params.SolutionNumber = s
# Print objective value of this solution in each objective
print('Solution', s, ':', end='')
for o in range(nObjectives):
# Set which objective we will query
m.params.ObjNumber = o
# Query the o-th objective value
print(' ', 'Objective',o+1, '=', m.ObjNVal , end='')
print('\n')
print('x=\n',x.x,'\n y=\n',y.x,'\n z=\n',z.x)
print('\n')
solutions.append(m.getAttr('Xn',v))
-
正式なコメント
This post is more than three 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 why not try our AI Gurobot?. -
The Xn variable attribute (not X) is used in conjunction with the SolutionNumber parameter to query suboptimal solutions. So \( \texttt{x.Xn} \), \( \texttt{y.Xn} \), and \( \texttt{z.Xn} \) together will give you the solution corresponding to the current value of the SolutionNumber parameter. The X attribute queries only variable values from Gurobi's best solution.
To create a list containing all of the different solutions (like your \( \texttt{solutions} \) list), you must loop over the different solutions via the SolutionNumber parameter and query each solution individually. This is true regardless of whether or not your problem has multiple objectives. Is there a reason you don't want to use a \( \texttt{for} \) loop to query all of the solutions?
1 -
Thank you so much for your comments Eli.
x.Xn, y.Xn, and z.Xn are exactly what I want corresponding to the SolutionNumber parameter.
I was looking for a way to retrieve variables in the same dimension as I defined in the model.
My solutions list do give the values of all decision variables, but it converts the dimension from 3 by 3 into 1 by 9... and I think a for loop for each variable will take too much time to run once the dimension increases.
But with Xn, I only have to use a for loop over all solutions which takes much less time.
Thank you again for your help.
0
投稿コメントは受け付けていません。
コメント
3件のコメント