FRAMES NO FRAMES

Macro IlcChooseIndex1

Definition file: ilsolver/critmac.h
IlcChooseIndex1

This macro defines a new choice function (a criterion) in Solver for setting parameters on the search for a solution, you use this macro if you have one integer criterion.

This macro defines a choice function for constrained variables of type varType. The name of the function will be name. The second argument, criterion, should be a C++ expression of type IlcInt. In that expression, the constrained variable to evaluate must be denoted by var. The index of the variable in the array is varIndex. The function named name returns the index of the constrained variable of type varType that minimizes the expression criterion. If all the constrained variables have already been bound, then this function returns -1.

Example

As an example of how to use IlcChooseIndex1, the predefined criteria for constrained integer variables could be defined in the following way with this macro:

 IlcChooseIndex1(IlcChooseMinSizeInt,  var.getSize(), IlcIntVar);
 IlcChooseIndex1(IlcChooseMaxSizeInt, -var.getSize(), IlcIntVar);
 IlcChooseIndex1(IlcChooseMinMinInt,   var.getMin(),  IlcIntVar);
 IlcChooseIndex1(IlcChooseMinMaxInt,   var.getMax(),  IlcIntVar);
 IlcChooseIndex1(IlcChooseMaxMinInt,  -var.getMin(),  IlcIntVar);
 IlcChooseIndex1(IlcChooseMaxMaxInt,  -var.getMax(),  IlcIntVar);

Other classes of variables can also be used:

 IlcChooseIndex1(IlcChooseMinSizeAny,  var.getSize(), IlcAnyVar);

Implementation

Here's how this macro might be implemented.

#define IlcChooseIndex1(name, criterion, type) 
 IlcInt       name (IlcInt theIlcChooseIndexSize, 
            name2(type,I**) vars) { 
     IlcInt varIndex; 
     type var; 
     name2(type,I**) tmp=vars;
     IlcInt indexBest=-1; 
     IlcInt value, min = IlcIntMax; 
     for (varIndex=0; 
        varIndex<theIlcChooseIndexSize; 
        varIndex++, tmp++) {
      var = type(*tmp); 
      if (!var.isBound()) { 
          value = criterion; 
          if (min > value) { 
              indexBest = varIndex; 
              min = value; 
          } 
      }
     } 
     return indexBest; 
 } 
 IlcInt name (const name2(type,Array) array){ 
    return name (array.getSize(), array.getImpl()->getArray()); 
 }

See Also: