If-Else condition for GUROBI C++ is not working
回答済みHi,
I want to apply the If-Else condition to calculate the tardiness of each job and then, calculate the total tardiness for all jobs in a scheduling problem. The expression for calculating tardiness for each job can be represented as follows.
Tardiness of job i = maximum (0, completion time of job i - due date of job i)
From the above expression it is clear that the tardiness value can not be less than zero, i.e., if the completion time of a job is less than its due date then the its tardiness value is zero.
Now, I use the Indicator constraint to express this relationship and used an additional binary variable b and Big M constraint to express this relationship. However, the indicator constraint isn't working as the value of tardiness for a job is 'zero' even though its completing time is greater than its due date. Hence, could you please help me to identify this issue?
Note that, I have followed the similar concept from this post: https://support.gurobi.com/hc/en-us/articles/4414392016529-How-do-I-model-conditional-statements-in-Gurobi-
// Set objective: minimize total tardiness
double bigM = 10000, eps_value = 0.0001;
GRBQuadExpr totalTardiness = 0;
for (int local_i = 0; local_i < job; local_i++)
{
GRBVar job_tardy = model.addVar(0.0, GRB_INFINITY, 0.0, GRB_INTEGER);
GRBVar b = model.addVar(0, 1, 1, GRB_BINARY);
job_comtime = 0.0;
for (int local_k = 0; local_k < mac; local_k++)
{
for (int local_l = 0; local_l < sequence; local_l++)
{
job_comtime += jmalloc[local_i][local_k][local_l] * process_time[local_k][local_i];
}
}
model.addConstr (job_comtime >= duedate[local_i]+ eps_value - bigM * (1-b));
model.addConstr(job_comtime <= bigM * (1 - b));
GRBQuadExpr test = model.addVar(0, INFINITY, 0, GRB_INTEGER);
model.addGenConstrIndicator(b, true, (test == job_comtime));
model.addGenConstrIndicator(b, false, (test == 0));
totalTardiness += test ;
}
model.setObjective(totalTardiness, GRB_MINIMIZE);
model.optimize();
-
正式なコメント
This post is more than two 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 Mohammad,
Your code snippet is a bit confusing for me, e.g., variable job_tardy is never used after creating it.
Additionally, you are using GRBQuadExpr, does your model contain quadratic terms?
I guess jmalloc[][][] are variables, right?But I would model it in a different way, without BigM and indicator constraints:
The variable job_tardy has lower bound 0 so it cannot get negative.
Then, you push up job_tardy by the difference of completion time and due date:job_comtime - duedate <= job_tardy
In the minimization objective you sum up all job_tardy variables, so in any optimal solution the job_tardy values will correspond exactly to the tardiness of a job since it does not make sense to set it to a higher value due to minimization.
Best regards,
Mario0
投稿コメントは受け付けていません。
コメント
2件のコメント