You can see the entire program assign.cpp
here or view it online in the standard distribution.
#include <ilsched/iloscheduler.h>
ILOSTLBEGIN
#if defined(ILO_SDXLOUTPUT)
#include "sdxloutput.h"
#endif
IloInt NumberOfActivities = 10;
IloInt NumberOfWorkers = 3;
const char* ActivityNames [] = {
"masonry ",
"carpentry ",
"plumbing ",
"ceiling ",
"roofing ",
"painting ",
"windows ",
"facade ",
"garden ",
"moving "};
const char* WorkerNames [] = {
"joe",
"jack",
"jim"};
IloInt StartTimes [] = { 0, 7, 7, 7, 10, 10, 11, 15, 15, 17 };
IloInt ProcessingTimes [] = { 7, 3, 8, 3, 1, 2, 1, 2, 1, 1 };
///////////////////////////////////////////////////////////////////////////////
//
// MODEL DEFINITION
//
//////////////////////////////////////////////////////////////////////////////
IloModel DefineModel(IloEnv env,
IloInt numberOfActivities,
IloInt numberOfWorkers,
const char** activityNames,
const char** workerNames,
IloInt* startTimes,
IloInt* processingTimes)
{
IloModel model(env);
/* CREATE THE WORKERS. */
IloInt i;
for (i = 0; i < numberOfWorkers; i++) {
IloUnaryResource resource(env, workerNames[i]);
}
/* CREATE THE ACTIVITIES. */
for (i = 0; i < numberOfActivities; i++) {
IloActivity activity(env, processingTimes[i], activityNames[i]);
activity.setStartTime(startTimes[i]);
}
return model;
}
///////////////////////////////////////////////////////////////////////////////
//
// PRINTING OF SOLUTIONS
//
///////////////////////////////////////////////////////////////////////////////
void
PrintSolution(IloSolver solver)
{
IlcScheduler scheduler(solver);
for (IloIterator<IloActivity> iterator(solver.getEnv());
iterator.ok();
++iterator) {
IloActivity act(*iterator);
IloUnaryResource res =
IloUnaryResource( (IloUnaryResourceI*)act.getObject() );
solver.out() << scheduler.getActivity( act )
<< "\tassigned to " << res.getName()
<< endl;
}
}
///////////////////////////////////////////////////////////////////////////////
//
// PROBLEM SOLVING
//
///////////////////////////////////////////////////////////////////////////////
// selector for non-assigned activity
ILOVISITOR0(ForAllActivitiesInEnv,
IloActivity,
IloEnv, env) {
for(IloIterator<IloActivity> ite(env); ite.ok(); ++ite) {
visit(*ite);
}
}
ILOPREDICATE0(IsAllocatedPredicate,
IloActivity, act) {
if (act.getObject() == 0)
return IloFalse;
else
return IloTrue;
}
ILOEVALUATOR0(StartMinEvaluator,
IloActivity, act) {
return act.getStartMin();
}
IloBool
SolveProblem(IloSolver solver, IloModel model) {
IloEnv env = model.getEnv();
IlcScheduler scheduler = IlcScheduler(solver);
IloActivity act;
IloPredicate<IloActivity> notAllocated = !IsAllocatedPredicate(env);
IloSelector<IloActivity,IloEnv> selector =
IloBestSelector<IloActivity,IloEnv>
(ForAllActivitiesInEnv(env),
notAllocated ,
StartMinEvaluator(env).makeLessThanComparator());
while ( selector.select(act, env) ) {
for (IloIterator<IloUnaryResource> iterator(model.getEnv());
notAllocated(act);
++iterator) {
if (iterator.ok()) {
IloUnaryResource res = *iterator;
IloConstraint ct = act.requires(res);
model.add(ct);
if(solver.solve())
{
// store the assigned resource for the given activity
act.setObject( (void*)(*iterator).getImpl() );
}
else {
model.remove(ct);
}
}
else
return IloFalse;
}
}
return IloTrue;
}
///////////////////////////////////////////////////////////////////////////////
//
// MAIN FUNCTION
//
///////////////////////////////////////////////////////////////////////////////
int main()
{
try {
IloEnv env;
IloModel model;
// Model
model = DefineModel(env,
NumberOfActivities,
NumberOfWorkers,
ActivityNames,
WorkerNames,
StartTimes,
ProcessingTimes);
// Algorithm
IloSolver solver(model);
if (SolveProblem(solver, model)) {
PrintSolution(solver);
#if defined(ILO_SDXLOUTPUT)
IloSDXLOutput output(env);
ofstream outFile("assign.xml");
output.write(IlcScheduler(solver), outFile);
outFile.close();
#endif
}
else
solver.out() << "No Solution!" << endl;
env.end();
} catch (IloException& exc) {
cout << exc << endl;
}
return 0;
}
///////////////////////////////////////////////////////////////////////////////
//
// RESULTS
//
///////////////////////////////////////////////////////////////////////////////
/* assign
masonry [0 -- 7 --> 7] assigned to joe
carpentry [7 -- 3 --> 10] assigned to joe
plumbing [7 -- 8 --> 15] assigned to jack
ceiling [7 -- 3 --> 10] assigned to jim
roofing [10 -- 1 --> 11] assigned to joe
painting [10 -- 2 --> 12] assigned to jim
windows [11 -- 1 --> 12] assigned to joe
facade [15 -- 2 --> 17] assigned to joe
garden [15 -- 1 --> 16] assigned to jack
moving [17 -- 1 --> 18] assigned to joe
*/