IBM ILOG Dispatcher User's Manual > Transportation Industry Solutions > Adding Vehicle Breaks > Model

Once you have written a description of your problem, you can use Dispatcher classes to model it.

Step 2   -  

Open the example file

Open the example file YourDispatcherHome/examples/src/tutorial/breaks_partial.cpp in your development environment.

As in the previous examples, you will use a RoutingModel class to call the functions that create the dimensions, nodes, vehicles, and visits. In this example, the RoutingModel class also calls functions that create and constrain the breaks.

Step 3   -  

Declare the RoutingModel class

Add the following code after the comment //Declare the RoutingModel class

class RoutingModel {
  IloEnv _env;
  IloModel _mdl;

  void createDimensions();
  void createNodes(char* nodeFileName);
  void createVehicles(char* vehicleFileName);
  void createVisits(char* visitsFileName);
  void createVehicleBreaks(char* vehcileBreaksFileName);
  void createBreaksRelation(char* breaksRelationFileName);

Two new functions appear in RoutingModel to model breaks: createVehicleBreaks and createBreaksRelation.

The function createVehicles is very similar to previous examples, but introduces a new member function.

Step 4   -  

Create the vehicles

l

Add the following code after the comment //Create the vehicles.

    IloVisit first(node1, "Depot");
    _mdl.add(first.getCumulVar(weight) == 0);
    _mdl.add(first.getCumulVar(time) >= line.getFloatByHeader("open"));

    IloVisit last(node2, "Depot");
    _mdl.add(last.getCumulVar(time) <= line.getFloatByHeader("close"));
    IloVehicle vehicle(first, last, name);
    vehicle.setCost(length, 1.0);
    vehicle.setSpeed(time, 30.0);
    vehicle.setCapacity(weight, capacity);
    vehicle.setKey(name);
    _mdl.add(vehicle);

The member function IloVehicle::setSpeed sets the speed of vehicle over the dimension time to be 30.0.

Next, you will create the vehicle breaks.

Step 5   -  

Create the vehicle breaks

l

Add the following code after the comment //Create the vehicle breaks.

    IloVehicle vehicle = IloVehicle::Find(_env, vehicleName);
    for ( IloInt i = 0; i < nbDays; i++ ) {
      char name[100];
      IloNumVar breakStart(_env,(i * 24) + startLB, (i * 24) + startUB);
      IloNumVar breakDuration(_env, durationLB, durationUB);
      sprintf(name,"%s-day%ld",breakName,i + 1);
      IloVehicleBreakCon breaks(vehicle, time, breakStart, breakDuration, 
name);
      breaks.setKey(name);
      _mdl.add(breaks);

This section of code iterates over all vehicle breaks, regardless of the break name (coffee, lunch, and so on), and adds them to the model. startLB, startUB, durationLB, and durationUB refer to the lower and upper bounds on the start and duration of each break. The various break starts and durations (with their associated bounds) are calculated over the 24 hours of the delivery days, and the resulting vehicle break constraint is then applied to vehicle with the constructor IloVehicleBreakCon.

The following code is the first section of RoutingModel::createVehicleBreaks, and is provided for you. This shows how the data is read and identified from the vehicle breaks input file.

void RoutingModel::createVehicleBreaks(char* vehicleBreaksFileName) {
  IloDimension2 time = IloDimension2::Find(_env, "Time");

  IloCsvReader csvVehicleBreaksReader(_env, vehicleBreaksFileName);
  IloCsvReader::LineIterator it(csvVehicleBreaksReader);
  while ( it.ok() ) {
    IloCsvLine line = *it;
    char* vehicleName = line.getStringByHeader("vehicle");
    char* breakName = line.getStringByHeader("breakName");
    IloNum startLB = line.getFloatByHeader("startLB");
    IloNum startUB = line.getFloatByHeader("startUB");
    IloNum durationLB = line.getFloatByHeader("durationLB");
    IloNum durationUB = line.getFloatByHeader("durationUB");
    IloInt nbDays = line.getIntByHeader("nbDays");

Step 6   -  

Create the break constraints

l

Add the following code after the comment //Create the break constraints

    IloNumVar breakDuration(_env);
    IloVehicleBreakCon vehicleBreakCon1 = IloVehicleBreakCon::Find(_env, 
break1);
    if ( strlen(break2) != 0 ) {
      IloVehicleBreakCon vehicleBreakCon2 = IloVehicleBreakCon::Find(_env, 
break2);
      _mdl.add(breakDuration == vehicleBreakCon1.getDurationVar()
                                + vehicleBreakCon2.getDurationVar());
    }
    else {
      _mdl.add(breakDuration == vehicleBreakCon1.getStartVar()
                                + vehicleBreakCon1.getDurationVar());
    }
    if ( strcmp(constraint,"equal") == 0 )
      _mdl.add(breakDuration == totalDuration);
    if ( strcmp(constraint,"at least") == 0 )
      _mdl.add(breakDuration >= totalDuration);
    if ( strcmp(constraint,"at most") == 0 )
      _mdl.add(breakDuration <= totalDuration);

This code is a section of the function RoutingModel::createBreaksRelation. break1, break2, totalDuration, and constraint are read in from the breaks relation input file. break1 and break2 are added together, and the totalDuration is constrained to be equal, at least, or at most the amount of break time specified.