IBM ILOG Scheduler User's Manual > Getting Started with Scheduler > Adding Transition Times > Complete Program and Output--Example 5 |
Complete Program and Output--Example 5 |
INDEX
![]() |
You can see the entire program gsTTime.cpp
here or view it online in the standard distribution.
#include <ilsched/iloscheduler.h> ILOSTLBEGIN #if defined(ILO_SDXLOUTPUT) #include "sdxloutput.h" #endif /////////////////////////////////////////////////////////////////////////////// // // PROBLEM DEFINITION // /////////////////////////////////////////////////////////////////////////////// /* THREE DESIGNS OF HOUSES EACH HAVING DIFFERENT TASK DURATIONS */ IloNum durationsDesign1 [] = { 7, 3, 8, 3, 1, 2, 1, 2, 1, 1}; IloNum durationsDesign2 [] = {12, 5, 10, 5, 2, 5, 2, 3, 2, 1}; IloNum durationsDesign3 [] = {15, 3, 10, 6, 2, 3, 2, 3, 2, 1}; const IloInt numberOfTypes = 10; void MakeHouse(IloModel model, const IloNum* dur, const IloNum startMin, const IloNum endMax, const IloUnaryResource worker, const IloNumVar makespan) { IloEnv env = model.getEnv(); /* CREATE THE ACTIVITIES. */ IloActivity masonry(env, dur[0], 1, "masonry "); IloActivity carpentry(env, dur[1], 2, "carpentry "); IloActivity plumbing(env, dur[2], 3, "plumbing "); IloActivity ceiling(env, dur[3], 4, "ceiling "); IloActivity roofing(env, dur[4], 5, "roofing "); IloActivity painting(env, dur[5], 6, "painting "); IloActivity windows(env, dur[6], 7, "windows "); IloActivity facade(env, dur[7], 8, "facade "); IloActivity garden(env, dur[8], 9, "garden "); IloActivity moving(env, dur[9], 0, "moving "); /* SET STARTMIN AND ENDMAX. */ model.add(masonry.startsAfter(startMin)); model.add(moving.endsBefore(endMax)); /* ADD THE TEMPORAL CONSTRAINTS. */ model.add(carpentry.startsAfterEnd(masonry)); model.add(plumbing.startsAfterEnd(masonry)); model.add(ceiling.startsAfterEnd(masonry)); model.add(roofing.startsAfterEnd(carpentry)); model.add(painting.startsAfterEnd(ceiling)); model.add(windows.startsAfterEnd(roofing)); model.add(facade.startsAfterEnd(roofing)); model.add(facade.startsAfterEnd(plumbing)); model.add(garden.startsAfterEnd(roofing)); model.add(garden.startsAfterEnd(plumbing)); model.add(moving.startsAfterEnd(windows)); model.add(moving.startsAfterEnd(facade)); model.add(moving.startsAfterEnd(garden)); model.add(moving.startsAfterEnd(painting)); model.add(moving.endsBefore(makespan)); /* ADD THE RESOURCE CONSTRAINTS. */ model.add(carpentry.requires(worker)); model.add(ceiling.requires(worker)); model.add(roofing.requires(worker)); model.add(windows.requires(worker)); model.add(facade.requires(worker)); model.add(masonry.requires(worker)); model.add(plumbing.requires(worker)); model.add(garden.requires(worker)); model.add(painting.requires(worker)); model.add(moving.requires(worker)); } IloNum GetTransitionTime(IloInt type1, IloInt type2) { if (!type1 || !type2 || type1==type2) return 0; return IloMax(3L, IloAbs((type1 - type2)/2L)); } IloModel DefineModel(const IloEnv env, IloNumVar& makespan) { IloModel model(env); /* CREATE THE MAKESPAN VARIABLE. */ IloNum horizon = 200; makespan = IloNumVar(env, 0, horizon, IloNumVar::Int); /* CREATE THE TRANSITION TABLE */ IloTransitionParam ttParam(env, numberOfTypes, IloTrue); IloInt i, j; for(i = 0; i < numberOfTypes; ++i) { for(j = 0; j <= i; ++j) { ttParam.setValue(i, j, GetTransitionTime(i, j)); } } /* CREATE THE RESOURCE. */ IloUnaryResource worker(env); IloTransitionTime tt(worker, ttParam); worker.setCapacityEnforcement(IloMediumHigh); /* CREATE THE ACTIVITIES AND CONSTRAINTS FOR THE HOUSES. */ MakeHouse(model, durationsDesign1, 0, horizon, worker, makespan); MakeHouse(model, durationsDesign2, 5, 80, worker, makespan); MakeHouse(model, durationsDesign3, 0, horizon, worker, makespan); /* RETURN THE CREATED MODEL. */ return model; } /////////////////////////////////////////////////////////////////////////////// // // PRINTING OF SOLUTIONS // /////////////////////////////////////////////////////////////////////////////// void PrintSolution(const IloSolver solver, const IloNumVar makespan) { IlcScheduler scheduler(solver); IloEnv env = solver.getEnv(); env.out() << "Solution with makespan " << solver.getIntVar(makespan).getMin() << endl; for(IloIterator<IloActivity> act(env); act.ok(); ++act) env.out() << scheduler.getActivity(*act) << endl; solver.printInformation(); } /////////////////////////////////////////////////////////////////////////////// // // MAIN FUNCTION // /////////////////////////////////////////////////////////////////////////////// int main() { try { IloEnv env; IloNumVar makespan; IloModel model = DefineModel(env, makespan); IloSolver solver(model); IloGoal goal = IloRankForward(env, makespan); if (solver.solve(goal)) { PrintSolution(solver,makespan); #if defined(ILO_SDXLOUTPUT) IloSDXLOutput output(env); ofstream outFile("gsTTime.xml"); output.write(IlcScheduler(solver), outFile, solver.getIntVar(makespan)); outFile.close(); #endif } else solver.out() << "No solution !" << endl; env.end(); } catch (IloException& exc) { cout << exc << endl; } return 0; } /////////////////////////////////////////////////////////////////////////////// // // RESULTS // /////////////////////////////////////////////////////////////////////////////// /* Solution with makespan 173 masonry [0 -- 7 --> 7] carpentry [134 -- 3 --> 137] plumbing [108 -- 8 --> 116] ceiling [125 -- 3 --> 128] roofing [142 -- 1 --> 143] painting [149 -- 2 --> 151] windows [170 -- 1 --> 171] facade [157 -- 2 --> 159] garden [164 -- 1 --> 165] moving [172 -- 1 --> 173] masonry [7 -- 12 --> 19] carpentry [43 -- 5 --> 48] plumbing [22 -- 10 --> 32] ceiling [35 -- 5 --> 40] roofing [59 -- 2 --> 61] painting [51 -- 5 --> 56] windows [75 -- 2 --> 77] facade [64 -- 3 --> 67] garden [70 -- 2 --> 72] moving [77..79 -- 1 --> 78..80] masonry [80 -- 15 --> 95] carpentry [131 -- 3 --> 134] plumbing [98 -- 10 --> 108] ceiling [119 -- 6 --> 125] roofing [140 -- 2 --> 142] painting [146 -- 3 --> 149] windows [168 -- 2 --> 170] facade [154 -- 3 --> 157] garden [162 -- 2 --> 164] moving [171 -- 1 --> 172] */
The start and end times of the activities are fixed, except for the moving activity of house Design2, which can start at any time between day 77 and 79 and end between day 78 and 80. The total time to complete the project is 173 days. Notice that our requirement that house Design2 be finished by day 80 has resulted in a longer makespan than would have occurred if we had been able to schedule all activities of the same type together.
Figure 5.1 provides a graphic display of the solution to our problem. The sole activity with variable start and end times is positioned at its earliest start time; its latest end time is indicated with an arrow.
© Copyright IBM Corp. 1987, 2009. Legal terms. | PREVIOUS NEXT |