IBM ILOG Dispatcher User's Manual > The Basics > Minimizing the Number of Vehicles > Solve > Define the main function

After you finish creating the RoutingModel and RoutingSolver classes and the printInformation function, you use them in the main function. You can use command line syntax to pass the names of input files to the model. If you do not specify input files, the defaults will be used. In the main function, you first create an environment. Then you create an instance of the RoutingModel class, which takes the environment and input files as parameters. You create an instance of the RoutingSolver class. This takes one parameter, the model. You use an if loop to find a solution. If Dispatcher finds a first solution, you print the information and then improve the solution. Then you close all empty vehicles. You use the function reduceActiveVehicles to search for the vehicle with the fewest number of visits and empty it. You try to find a new solution without using this vehicle. If you find a new solution, you improve it. After solution improvement, you empty the vehicle that now has the fewest number of visits. When you cannot empty any more vehicles and still find a solution, you have minimized the number of vehicles. This solution is then printed. The following code is provided for you:

int main(int argc, char * argv[]) {
  IloEnv env;
  try {
    RoutingModel mdl(env, argc, argv);
    RoutingSolver solver(mdl);
    if (solver.findFirstSolution()) {
      solver.printInformation("***First Solution***");
      solver.improveWithNhood();
      solver.printInformation("***Solution after improvements with nhood***");
      solver.closeEmptyVehicles();
      solver.reduceActiveVehicles();
      solver.printInformation("***Solution after reducing active vehicles***");
    }
  } catch(IloException& ex) {
    cerr << "Error: " << ex << endl;
  }
  env.end();
  return 0;
}

Step 15   -  

Compile and run the program

Compile and run the program. You will get results that show the routing plan and information for the first solution, the initial improved solution, and the solution found after reducing the number of active vehicles. The first solution uses 20 vehicles. The initial improved solution uses 17 vehicles. The solution found after reducing the number of active vehicles uses 16 vehicles.

First solution information

The first solution phase finds a solution using 20 vehicles with a total cost of 2554.03 units:

***First Solution***
Number of fails               : 50659
Number of choice points       : 1077
Number of variables           : 5504
Number of constraints         : 881
Reversible stack (bytes)      : 578904
Solver heap (bytes)           : 2504836
Solver global heap (bytes)    : 1187724
And stack (bytes)             : 20124
Or stack (bytes)              : 44244
Search Stack (bytes)          : 4044
Constraint queue (bytes)      : 13164
Total memory used (bytes)     : 4353040
Elapsed time since creation   : 9.483
Number of nodes               : 201
Number of visits              : 300
Number of vehicles            : 50
Number of dimensions          : 2
Number of accepted moves      : 0
===============
Cost         : 2554.03
Number of vehicles used : 20

Improved Solution Information

The solution improvement phase finds a solution using 17 vehicles with a total cost of 2073.11 units-:

***Solution after improvements with nhood***
Number of fails               : 0
Number of choice points       : 0
Number of variables           : 5504
Number of constraints         : 878
Reversible stack (bytes)      : 578904
Solver heap (bytes)           : 2089268
Solver global heap (bytes)    : 1203804
And stack (bytes)             : 20124
Or stack (bytes)              : 44244
Search Stack (bytes)          : 4044
Constraint queue (bytes)      : 35196
Total memory used (bytes)     : 3975584
Elapsed time since creation   : 0.07
Number of nodes               : 201
Number of visits              : 300
Number of vehicles            : 50
Number of dimensions          : 2
Number of accepted moves      : 222
===============
Cost         : 2073.11
Number of vehicles used : 17

Solution information after reducing active vehicles

After solution improvement, vehicle 20 has the fewest number of visits. (See the section "Complete Output".) The reduceActiveVehicles function empties vehicle 20, assigns its visits to other vehicles, and improves the solution. Now, vehicle 7 has the fewest number of visits. The reduceActiveVehicles function empties vehicle 7, tries to assign its visits to other vehicles, but is not able to do so. The last solution found is restored. The solution after reducing active vehicles uses 16 vehicles with a total cost of 2072.93 units:

Emptying vehicle20 ...
Improving solution
Emptying vehicle7 ...
Improving solution
***Solution after reducing active vehicles***
Number of fails               : 0
Number of choice points       : 0
Number of variables           : 5504
Number of constraints         : 917
Reversible stack (bytes)      : 578904
Solver heap (bytes)           : 2089592
Solver global heap (bytes)    : 1207824
And stack (bytes)             : 20124
Or stack (bytes)              : 44244
Search Stack (bytes)          : 4044
Constraint queue (bytes)      : 35196
Total memory used (bytes)     : 3979928
Elapsed time since creation   : 0.061
Number of nodes               : 201
Number of visits              : 300
Number of vehicles            : 50
Number of dimensions          : 2
Number of accepted moves      : 238
===============
Cost         : 2072.93
Number of vehicles used : 16

The complete program and output are listed in "Complete Program". You can also view it online in the YourDispatcherHome/examples/src/minvehcl.cpp file.