Possible to define objective parameters in constraints?
回答済みHi, I have a problem defining some parameters that I want to use in my objective function. I want to define the parameter used in the objective function in the constraints, but it only returns that the parameter u is not defined. An example of what i want to do:
max u11*x1 + u12*x2 + u21*x1 + u22*x2
subject to:
u[i,j] = ....
+ more constraints
-
正式なコメント
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 try Gurobot, our chatbot interface offering instant, expert-level support. -
Hi Tiril!
Could you share a minimal reproducible example of the code which throws the error? We will then be able to give you better advice.
Best regards
Jonasz0 -
import pandas as pd
import gurobipy as gp
from gurobipy import *
import numpy as np
from gurobipy import GRB
import matplotlib.pyplot as plt
import sys
## Sets ##
criteria = [0, 1, 2, 3, 4]
decisionM = [0, 1]
alternative = [0, 1]
## Parameters ##
weight = np.array( [[0.03, 0.09, 0.28, 0.14, 0.46], [0.39, 0.15, 0.08, 0.06, 0.32]])
V1 = np.array([[59800000, 68, 0.5, 0.5, 40], [65622000, 165, 0.58, 0.5, 76]])
V2 = np.array([[59800000, 68, 1, 1, 1100], [65622000, 165, 0.75, 0.5, 450]])
V1_min = []
for c in criteria:
if V1[0,c]<V1[1,c]:
V1_min.append(V1[0,c])
else:
V1_min.append(V1[1,c])
V2_min = []
for c in criteria:
if V2[0,c]<V2[1,c]:
V2_min.append(V1[0,c])
else:
V2_min.append(V2[1,c])
## Model ##
m = gp.Model('Weighted-sum-method')
#Decision variable
x = m.addVars(alternative, decisionM, vtype=GRB.BINARY, name='x')
# Defining U1
for c in criteria:
for a in alternative:
m.addConstr(
(U1[a,c] == V1_min[c]/V1[a,c]), 'Utility1')
# Defining U2
for c in criteria:
for a in alternative:
m.addConstr(
(U2[a,c] == V2_min[c]/V2[a,c]), 'Utility1')
# Only one alternative can be chosen by each decision maker
for a in alternative:
m.addConstr(
(sum(x[a,d] for d in decisionM) == 1), 'onealternative')
# Objective function
obj = m.setObjective(sum(x[a,0]*weight[0,c]*U1[a,c] for a in alternative
for c in criteria) + sum(x[a,1]*weight[1,c]*U2[a,c]
for a in alternative for c in criteria),
GRB.MAXIMIZE)
m.optimize()0 -
Hi Tiril,
1. What are \(U1, U2\)? Are they variables? If so, you need to define them as such upfront, for example by:
U1 = m.addVars(alternative, criteria, vtype="C", name="u1")
U2 = m.addVars(alternative, criteria, vtype="C", name="u2")just after the model definition. Adding these lines solved the problem on my machine.
2. On a side note, you may want to give all constraints different names. Your code names both U1 and U2 constraints 'Utility1'.
Best regards
Jonasz0 -
Thank you, it worked!
0
投稿コメントは受け付けていません。
コメント
5件のコメント