IBM ILOG Solver User's Manual > Developing Solver Applications > Designing Models > Use appropriate search strategies > Example

Let's continue the preceding example. The code presented in the preceding section would search in these ways:

In contrast, we could first try the highest values, since we are interested in the highest possible sum. For that purpose, we define our own selector to choose the variables with the largest maximum values in their domains. In our definition, we use the implementation class IlcIntSelectI. (The IBM ILOG Solver Reference Manual documents IlcIntSelectI.) Here's the code to do this:

class selectMaxI : public IlcIntSelectI {
public:
  selectMaxI():IlcIntSelectI(){}
  virtual IlcInt select(IlcIntVar var){
    return var.getMax();
  }
};

IlcIntSelect selectMax(IloSolver s){
   return new (s.getHeap()) selectMaxI();
}

class IloSelectMaxI : public IloIntValueSelectorI {
public:
  IloSelectMaxI(IloEnvI* env) : IloIntValueSelectorI(env) {}
  IlcIntSelect extract(const IloSolver solver) {
    return selectMax(solver);
  }
  IloIntValueSelectorI* makeClone(IloEnvI* env) {
    return new (env) IloSelectMaxI(env);
  }
};

int main(){
  IloEnv env;
  IloModel m(env);
  IloIntVarArray vars(env, 5, 0, 3);
  IloIntVar sum(env, 0, 100);
  m.add(sum == IloSum(vars));
  m.add(IloMaximize(env, sum));
  IloSolver s(m);
  s.solve(IloGenerate(env, vars, IloChooseMaxMaxInt, IloSelectMax(env)));
  cout << "Sum = " << s.getIntVar(sum) << endl;
  cout << "#tries = " << s.getNumberOfChoicePoints() << endl;
  env.end();
  return 0;
}

This improved version searches in this way:

Here's the output, demonstrating the improvement in the number of tries:

Sum = 15
#tries = 5