3. Getting comfortable

This chapter provides an overview of some functionality in Gecode that makes modeling and execution of models more convenient.

Overview. Expressions constructed from standard arithmetic operators for posting linear constraints are discussed in Posting linear constraints de-mystified, cost functions for best solution search are discussed in Using a cost function, and a script commandline driver that supports the most common options for running models from the commandline is discussed in Using the script commandline driver.

3.1. Posting linear constraints de-mystified

Program 3.1 A Gecode model for Send More Money using modeling support
#include <gecode/int.hh>
#include <gecode/minimodel.hh>
#include <gecode/search.hh>

...
class SendMoreMoney : public Space {
protected:
  IntVarArray l;
public:
  SendMoreMoney(void) : l(*this, 8, 0, 9) {
    ...
    rel(*this,             1000*s + 100*e + 10*n + d
                         + 1000*m + 100*o + 10*r + e
              == 10000*m + 1000*o + 100*n + 10*e + y);
    ...
  }
  ...
};

...

As mentioned in the previous chapter, Gecode comes with simple modeling support for posting constraints defined by linear expressions and relations. The parts of the program for Send More Money from Program 2.1 that change are shown in Program 3.1. In order to use the modeling support, we have to include the MiniModel header.

The MiniModel module also supports Boolean expressions and relations, and much more, see Modeling convenience: MiniModel for more information. The module in itself does not implement any constraints. The function rel takes the description of the linear constraint, analyzes it, and posts a linear constraint by using the same linear function we have been using in A first Gecode model.

3.2. Using a cost function

Program 3.2 A Gecode model for Send Most Money using a cost function
...
class SendMostMoney : public IntMaximizeSpace {
protected:
  IntVarArray l;
  IntVar money;
public:
  SendMostMoney(void)
    : l(*this, 8, 0, 9), money(*this,0,100000) {
    ...
    rel(*this, money == 10000*m + 1000*o + 100*n + 10*e + y);
    ...
  }
  SendMostMoney(SendMostMoney& s) : IntMaximizeSpace(s) {
    l.update(*this, s.l);
    money.update(*this, s.money);
  }
  ...
  // [send most money with cost:cost function]
};

...

Program 3.2 uses the class IntMaximizeSpace for cost-based optimization for Send Most Money. The class is also included in Gecode’s MiniModel module (see Support for cost-based optimization and Support for cost-based optimization).

The IntMaximizeSpace class is a sub-class of Space that defines a constrain() member function based on the cost of a space. Our model must implement a virtual cost() function that returns an integer variable defining the cost (the function must be const). In our example, we extend the model to maintain the cost (the amount of money) in a dedicated variable money (note that this variable must also be updated during cloning).

The cost function then just returns the amount of money as follows:

  virtual IntVar cost(void) const {
    return money;
  }

3.3. Using the script commandline driver

In order to experiment from the commandline with different model variants, different search engines, and so on, it is convenient to have support for passing different option values on the commandline that then can be used by a model. Gecode comes with a simple commandline driver that defines Script as a subclass of Space for modeling and support for commandline options.

Defining a script class. Suppose that we want to experiment with two different variants of Send Most Money with a cost function: the first variant uses the model from Using a cost function and the second variant models the equation \(SEND+MOST=MONEY\) by using carry variables.

Using carry variables with several linear equations instead of a single linear equation is straightforward. A carry variable is an integer variable with value 0 or 1 and each column in the equation \(SEND+MOST=MONEY\) is modeled by a linear equation involving the appropriate carry variable as follows:

      {
        IntVar c0(*this, 0, 1), c1(*this, 0, 1), 
               c2(*this, 0, 1), c3(*this, 0, 1);
        rel(*this,      d + t == y + 10 * c0);
        rel(*this, c0 + n + s == e + 10 * c1);
        rel(*this, c1 + e + o == n + 10 * c2);
        rel(*this, c2 + s + m == o + 10 * c3);
        rel(*this, c3         == m);
      }
Program 3.3 A Gecode model for Send Most Money using the script commandline driver
#include <gecode/driver.hh>
#include <gecode/int.hh>
#include <gecode/minimodel.hh>

using namespace Gecode;

class SendMostMoney : public IntMaximizeScript {
...
public:
  enum {
    MODEL_SINGLE, MODEL_CARRY
  };
  SendMostMoney(const Options& opt)
    : IntMaximizeScript(opt),
      l(*this, 8, 0, 9), money(*this,0,100000) {
    ...
    switch (opt.model()) {
    case MODEL_SINGLE:
      ...
      break;
    case MODEL_CARRY:
      // [send most money with driver:using carries]
      break;
    }
    ...
  }
  ...
  virtual void print(std::ostream& os) const {
    os << l << std::endl;
  }
};

int main(int argc, char* argv[]) {
  // [send most money with driver:commandline options]
  // [send most money with driver:run script]
  return 0;
}

Program 3.3 shows a model for Send Most Money that uses the IntMaximizeScript class as base class (see Script classes) rather than IntMaximizeSpace (likewise, the driver module also offers a Script class to be used instead of Space). There are three main differences between IntMaximizeScript and IntMaximizeSpace (Script and Space):

  1. The constructor must accept a constant argument of type Options (actually, it must accept a constant argument of the type that is specified for the run member function to be explained below) that is used to pass values computed from options passed on the commandline.

  2. The constructor of a subclass of IntMaximizeScript or any other script class must call the constructor IntMaximizeScript with an argument of type Options.

  3. A subclass of IntMaximizeScript must define a virtual print function that accepts a standard output stream as argument.

Note that one has to include <gecode/driver.hh> for a model that uses the script commandline driver. However, neither <gecode/search.hh> nor <gecode/gist.hh> need to be included as search is handled by the commandline driver.

The class SendMostMoney defines an enumeration type with values MODEL_SINGLE (for a model where a single linear equation is posted for \(SEND+MOST=MONEY\)) and MODEL_CARRY (for a model where several linear equations using carry variables are posted for \(SEND+MOST=MONEY\)). The options object opt provides a member function model that returns an enumeration value and posts the constraints accordingly.

Defining commandline options. The mapping between strings passed on the commandline and the values MODEL_SINGLE and MODEL_CARRY is established by configuring an object of class Options accordingly as follows:

  Options opt("SEND + MOST = MONEY");
  opt.model(SendMostMoney::MODEL_SINGLE, 
            "single", "use single linear equation");
  opt.model(SendMostMoney::MODEL_CARRY, 
            "carry", "use carry");
  opt.model(SendMostMoney::MODEL_SINGLE);
  opt.solutions(0);
  opt.parse(argc,argv);

This code creates a new Option object opt where the string "SEND + MOST = MONEY" serves as identification (such as when requesting to print help about the available commandline options).

The first call to opt.model() defines that single is a legal value for the option switch -model, that the string use single linear equation is the help text for the option value, and that the corresponding value (that is, the value returned by opt.model()) is SendMostMoney::MODEL_SINGLE. The last call to opt.model defines the default value.

As we are performing best solution search, we want to compute all possible solutions. This is done by setting opt.solutions to 0 (the default value is 1 for searching for the first solution). Note that this default value can be changed on the commandline by passing an integer as value for the -solutions commandline option. Parsing the commandline by parse now takes the configured values for the commandline options -model and -solutions into account.

The Options class supports most options that are useful for propagation, search, and so on similar to the model option. The full details are explained in Script commandline driver.

The last piece of our model is calling the static run method of the script class by passing as template arguments our script class type SendMostMoney, BAB as the search engine we would like to use, and the type of options Options (as mentioned before, the constructor of the script must accept a single argument of this type).

  Script::run<SendMostMoney,BAB,Options>(opt);

Running the script from the commandline. Suppose that we have compiled our script example as the file send-most-money-with-driver.exe (for some platforms, just drop .exe). Then

send-most-money-with-driver.exe

prints something along the lines

SEND + MOST = MONEY
{9, 3, 4, 2, 1, 0, 5, 7}
{9, 3, 4, 2, 1, 0, 6, 8}
{9, 4, 5, 2, 1, 0, 6, 8}
{9, 5, 6, 3, 1, 0, 4, 7}
{9, 6, 7, 2, 1, 0, 3, 5}
{9, 6, 7, 3, 1, 0, 5, 8}
{9, 7, 8, 2, 1, 0, 3, 5}
{9, 7, 8, 2, 1, 0, 4, 6}

Initial
        propagators: 3
        branchers:   1

Summary
        runtime:      0.024 (24.000000 ms)
        solutions:    8
        propagations: 109
        nodes:        33
        failures:     9
        restarts:     0
        no-goods:     0
        peak depth:   8

If we want to try the model with carry variables we can do that by running

send-most-money-with-driver.exe -model carry

We can also use Gist by giving a different mode of execution:

send-most-money-with-driver.exe -mode gist

As we call run with BAB as type of search, Gist will automatically start in branch-and-bound mode. Other supported modes are solution (the default mode shown above), time for printing average runtimes, and stat for just printing an execution statistics but no solutions.

Another important commandline option is -help which prints the options supported by the script together with some configuration information. The full details are explained in Script commandline driver.