Defining a set dependent on an other set
ユーザーの入力を待っています。Hi,
I'm trying to define a set that is dependent on another set, like this:
K: Set of commodities, denoted k
R^k: Set of feasible routes for transporting commodity k, denoted r
I have two commodities, [0,1], and I want R^1 to be [0,1,2,3] and R^2 to be [0,1,2,3,4,5,6]. I also have a binary variable x_r, how will this be defined when r is dependent on k?
How do I implement this in Gurobi?
-
正式なコメント
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,
you would need two indices per variable for that - one for the commodity and the other one for the route. You implement it just like any other variable:
import gurobipy as gp
m = gp.Model()
R = dict()
R[1] = [0, 1, 2, 3]
R[2] = [0,1,2,3,4,5,6]
K = [1, 2]
x = gp.tupledict()
for k in K:
for r in R[k]:
x[k, r] = m.addVar(vtype="B", name=f"x_{k}_{r}")Best regards
Jonasz0 -
Hi, thank you for the answer! But when running only the above I get the error message "KeyError: 0". It seems to be for the "for r in R[k].
0 -
You're right, I just corrected the error.
Best regards
Jonasz0 -
Thank you so much. Another question arised when implementing the complete model. I have a table of variables that is dependent on both commodity, routes and nodes. When adding constraints I have to sum over these sets, but I'm not sure how to model the matrices. Until now I have defined them for each commodity, like this: M1[r,n], M2[r,n], but I'm not sure how I can also sum over k in commodities.
0 -
I'm not sure I understood your challenge.
If M1 and M2 were corresponding to various nodes, you could instead have them as on dict with a 3-tuples as keys, for example, the following way
# for illustration only, not replicable
M = dict()
for n in nodes:
for r in routes:
for k in commodities:
M[n, r, k] = ...If that's not what you have trouble with, I'd need a better clarification, perhaps with a code example.
Best regards
Jonasz0
投稿コメントは受け付けていません。
コメント
6件のコメント