Adding Constraint
回答済みHello,
I would like to add new constraint to my model but no success. The constraint Xlt + Xkt = 0, YPYN in code," means that there is a sublist for each book and these books cannot be placed at the same time.
Index L is used for this reason. k1 cannot be placed with k5 or k7 at the same time. k2 also cannot be placed with k3 at the same time." Would you please help me ?
import gurobipy as gp
from gurobipy import GRB
T = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
K = ["k1","k2","k3","k4","k5","k6","k7","k8","k9"]
L = {“k1” : [“k5”, “k7”], “k2” : “k3” }
z = gp.Model("MP")
x = z.addVars (T, K, vtype =GRB.BINARY, name = "jf")
Job_assigns = z.addConstrs ((x.sum("*", k) == 1 for k in K), name = "job_assign")
# Books not ordered in a shelf at the same time
YPYN = z.addConstrs (gp.quicksum(x(t, k) + x(t, l) == 0 for k in K for l in L), name = "YPYN")
# Objective
Sonuc = gp.quicksum (x[t, k] * t for t in T for k in K)
z.setObjective(Result, GRB.MINIMIZE)
-
正式なコメント
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?. -
Hi Musa,
Could you write what the constraints \(\texttt{YPYN}\) shall look like when written down as a mathematical term, e.g., \(x_{1, k1} + x_{1, k5} = 0\) ?
Moreover, The term \(\texttt{Result}\) in not defined. I assume that \(\texttt{Sonuc}\) is meant to be your \(\texttt{Result}\)?
Best regards,
Jaromił0 -
Hi Jaromil,
Ypyn refers jobs must not be applied at the same time period t. Yes, constraint you wrote is correct. Job1 and job 5 must not be applied at the same t.
I also would like to have a constraint for job ordering. Sorry for result and sonuc conflicts. No problem for that.0 -
Hi Musa,
You can construct the \(\texttt{YPYN}\) via
T = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
L = {"k1" : ["k5", "k7"], "k2" : ["k3"] }
z = gp.Model("MP")
x = z.addVars (T, K, vtype =GRB.BINARY, name = "jf")
# Books not ordered in a shelf at the same time
YPYN = z.addConstrs( (x[t, k] + x[t, l] == 0 for t in T for k in L for l in L[k]), name = "YPYN")Note that I made all non-key elements of the dictionary a list to make accessing it easier.
You can always control what you just modeled by using the Model.write() function in order to write an LP file and analyzing it.
A job ordering constraint, depending on what exactly you have in mind, can be written in a similar manner.
Best regards,
Jaromił0 -
Thank you for your help. This subjec is very beneficial for me. Now, I need job priority constraint. I tries k1 < k2 basically. It means k2 must be performed before application of k1. How can I write code for this constraint. When the constraint is written error code comes back. Less than or greater than inequalities not supported by Gurobi information is mentioned inside one of other question block. Would you please help me how to write code for less than equalities.
0 -
Hi Musa,
Strict inequalities are not allowed in (Mixed-Integer) Linear Programming, since then the (relaxed) feasible set would not be closed.
You might want to look up some literature on modeling of Job (Shop) Scheduling Problems via MIPs to get a better understanding of the problem structure and the ordering constraint you mentioned.
Best regards,
Jaromił0 -
Thank you for information. Is it possible to add new part to objective like this;
Sum t for only values Xkt=1.
I mean T (time line) is from 1 to 11, but all jobs are completed within 7 time periods. In other words; x values for times period 8, 9, 10, 11 are zero. So, in this case sum from 1 to 7 should be result. Would you please help me to write code for this objective.
Objective : Sonuc = gp.quicksum (x[t, k] * t for t in T for k in K) + new part0 -
Hi Musa,
Could you elaborate more on the new part as I can't fully understand what you are trying to implement? Do you want to sum over the indices \(t\) for any binary \(x_{tk}\) set to 1? This can be simply done by \(\sum t \cdot x_{kt}\), since \(x_{kt}\) are binary variables but you are already doing this.
Best regards,
Jaromił0 -
Hi Jaromil,
My aim is to get best total time. Only two constraints are added to try model behaviour. One is for job (kart) assignment. One is for the total number of available worker.
1st constraint says all jobs must be assigned only one time period t.
2st constraint says the number of available workers in each time is limited, 5 in this example, and all jobs must be assigned for this rule.
When the code is run in gurobi result is as follows;
Solution count 3: 25 25 68
Variable x
-------------------------
job_finish[1,kart3] 1
job_finish[1,kart6] 1
job_finish[1,kart9] 1
job_finish[2,kart1] 1
job_finish[2,kart2] 1
job_finish[3,kart5] 1
job_finish[4,kart4] 1
job_finish[5,kart7] 1
job_finish[6,kart8] 1Total time is 6. I tried to figure out if better assignment is possible and realized that there is a better solution (better total time) although objective values is the same. Like this;
Kart 1 and Kart 2 are assigned at t=1
Kart 3 and Kart 4 are assigned at t=2
Kart 5 and Kart 9 are assigned at t =3
Kart 6 and Kart 8 are assigned at t =4
Kart 7 is assigned at t=5
but total time is 5.
So, that's why I would like to add new part for objective.
Result would be 31 (25+6 (last t value)) or 46 (25 + 21 (sum of time periods which covers job assignment) in existing code and would be 30 or 40 for new objective. Finally this new part forces model to find better total time.
My code;
T = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
K = [ “kart1”, “kart2”, “kart3”, “kart4”, “kart5”, “kart6”, “kart7”, “kart8”, “kart9” ]
A = {“kart1”: [3], “kart2”: [2], “kart3”: [1], “kart4”: [4], “kart5”: [3], “kart6”: [1], “kart7”: [5], “kart8”: [4], “kart9”: [2]}
At = 5
z = gp.Model(“MaintenancePlan”)
x = z.addVars (T, K, vtype =GRB.BINARY, name = “job_finish”)
Job_assigns = z.addConstrs ((x.sum(“*”, k) == 1 for k in K), name = “job_assign”)
AdamSay = z.addConstrs (((gp.quicksum(x[t, k]*r for k in K for r in A[k]) <= 5) for t in T), name = "AdamSay")
Sonuc = gp.quicksum (x[t, k] * t for t in T for k in K)
z.setObjective(Sonuc, GRB.MINIMIZE)0 -
Hi Musa,
One way to model this would be to introduce an additional integer variable to capture the time costs and add inequality constraints controlling its value, e.g.,
time = z.addVar(vtype=GRB.INTEGER, lb=1, ub=11, name="time")
z.addConstrs( ( time >= x[t,k] * t for t in T for k in K ), name = "time_constraint")
Sonuc = gp.quicksum( x[t, k] * t for t in T for k in K ) + timeBest regards,
Jaromił0
投稿コメントは受け付けていません。
コメント
10件のコメント