how to do the quick sum not for every variable?
回答済みhave already done my objective function as
products, cost = gp.multidict({(1,1) : 0.1, (1,2) : 0.1 (1,4) : 0.15, (2,1) : 0.15,
(2,2) : 0.18, (2,4) : 0.2, (3,1) : 0.25, (3,2) : 0.32,
(3,4) : 0.3, (4,1) : 0.55, (4,2) : 0.5, (5,2) : 0.2,
(5,3) : 0.13, (5,4) : 0.25, (6,2) : 0.3, (6,3) : 0.18,
(6,4) : 0.35, (7,2) : 0.5, (7,3) : 0.28, (7,4) : 0.55,
(8,2): 1, (8,3): 0.6})
product, yearly_requirements = gp.multidict({1 : 250000,
2 : 150000,
3 : 150000,
4 : 80000
5 : 190000
6 : 190000
7 : 190000
8 : 160000
9 : 150000})
I have constraints
x11+x12+x14>=2500000
x21+x22+x24>=1500000
x31+x32+x34>=1500000
x41+x42>=800000
x52+x53+x54>=190000
x62+x63+x64>=190000
x72+x73+x74>=160000
x82+x83>=150000
0.5x11+0.6x21+0.8x31+0.1x41<=360000
0.5x11+0.6x21+0.8x31+0.1x41>=120000
0.5x12+0.6x22+0.6x32+1.0x42+1.0x52+1.2x62+1.6x72+2.0x82>=120000
I want to use the 'quicksum' to do the constraints. but I notice that there is no x13, not i in I=(1,2,3,4,5,6,7,8), not every j in j=(1,2,3,4). how can I do this via quick sum?
-
正式なコメント
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 Qianna,
You can define the \(\texttt{x}\) variables as a tupledict by calling Model.addVars() and then call the method sum(). See the script below for an example of how to implement the first eight constraints:
x = model.addVars(products, name="x")
model.addConstrs((x.sum(i, "*") >= yearly_requirements[i] for i in range(1, 9)))To implement the last three constraints, you can define the \(\texttt{coeff}\) as a new multidict and then use the method prod(). See the snippet below as an example of how to model the constraint \(0.5x_{11}+0.6x_{21}+0.8x_{31}+0.1x_{41} \leq 360000\):
products, coeff = gp.multidict(
{
(1, 1): 0.5,
(2, 1): 0.6,
(3, 1): 0.8,
(4, 1): 0.1,
}
)
model.addConstr(x.prod(coeff, "*", 1) <= 360000)Best regards,
Maliheh
0 -
Hi Maliheh,
Thank you for replying.
I also have three questions
1 I have objective function: min.
0.1x11+0.15x21+0.25x31+0.55x41+0.1x12+0.18x22+0.15x32+0.5x42+0.2x52+0.3x62+0.5x72+1.0x82+0.13x53+0.18x63+0.28x73+0.6x83+0.15x14+0.2x24+0.3x34+0.25x54+0.35x64+0.55x74
which I have made it as tuple: products, cost = gp.multidict({(1,1) : 0.1, (1,2) : 0.1 (1,4) : 0.15, (2,1) : 0.15,
(2,2) : 0.18, (2,4) : 0.2, (3,1) : 0.25, (3,2) : 0.32,
(3,4) : 0.3, (4,1) : 0.55, (4,2) : 0.5, (5,2) : 0.2,
(5,3) : 0.13, (5,4) : 0.25, (6,2) : 0.3, (6,3) : 0.18,
(6,4) : 0.35, (7,2) : 0.5, (7,3) : 0.28, (7,4) : 0.55,
(8,2): 1, (8,3): 0.6})how can I set this obejct?
second,
for this code, if I do "0.5x12+0.6x22+0.6x32+1.0x42+1.0x52+1.2x62+1.6x72+2.0x82>=120000",
should I change 1 to 2 from this' (coeff, "*", 1)'
products, coeff = gp.multidict(
{
(1, 1): 0.5,
(2, 1): 0.6,
(3, 1): 0.8,
(4, 1): 0.1,
}
)
model.addConstr(x.prod(coeff, "*", 1) <= 360000third,, how this works for making x11 + x12+x14.+250000 and other seven constraints. how this code can know which variables are setting for each constraint?
x = model.addVars(products, name="x")
model.addConstrs((x.sum(i, "*") >= yearly_requirements[i] for i in range(1, 9)))0 -
Hi Qianna,
how can I set this obejct?
You can model the objective function as below:
x.prod(cost)
second, for this code, if I do "0.5x12+0.6x22+0.6x32+1.0x42+1.0x52+1.2x62+1.6x72+2.0x82>=120000", should I change 1 to 2 from this' (coeff,"*",1)'
Reading through the documentation of the tupledict.prod(coeff, pattern) method clarifies that this method returns a linear expression containing one term for each key that is present in both \(\texttt{tupledict}\) and the \(\texttt{coeff}\) dict. The \(\texttt{pattern}\) input determines which keys to pull out from \(\texttt{tupledict}\) and \(\texttt{coeff}\). Therefore, if you want to have one term for all variables with any value for the first index and 2 for the second index, the pattern should be passed as \(\texttt{("*", 2)}\).
third,, how this works for making x11 + x12+x14.+250000 and other seven constraints. how this code can know which variables are setting for each constraint?
The code iterates over each \(\texttt{i}\) value in \(\texttt{range(1, 9)}\) and adds one constraint for each index. Let us assume \(\texttt{i=1}\), the LHS expression \(\texttt{x.sum(1, "*")}\) creates a sum over all the \(\texttt{x}\) variables with the first index being equal to 1 and the second index being any value. These variables are \(\texttt{x[1,1]}\), \(\texttt{x[1,2]}\), and \(\texttt{x[1,4]}\) because the \(\texttt{x}\) variables have the same indices as defined by the \(\texttt{products}\) tuplelist. The RHS value equals \(\texttt{yearly_requirements[1]}\) which you have already defined.
I would kindly recommend reading through the documentation of tupledict() and all its methods. I am sure that you would find them very useful and would be able to implement the very similar question you just posted here (we would appreciate it to avoid creating several threads for questions which are so similar). You might also want to read through netflow.py example and netflow.py example details. Of course, feel free to reach out if the current response and the links above do not clarify things completely for you.
Best regards,
Maliheh
0
投稿コメントは受け付けていません。
コメント
4件のコメント