Topic
4 replies Latest Post - ‏2013-06-18T20:04:23Z by mrmag
mrmag
mrmag
28 Posts
ACCEPTED ANSWER

Pinned topic Search strategy with ILCGOALn and ILOCPGOALWRAPPERn

‏2013-06-18T13:30:15Z |
Hello!
 
I implemented my own simple search strategy using ILCGOALn and ILOCPGOALWRAPPERn as in the following code. But I found two problems:
 
1 The function main() throws an exception
 
   Concert exception caught: IloCP internal error: cannot replay solution
 
2 In the function main2() the part of while(cp.next()) {  } runs at almost every iteration (as if the CP found a solution) and then after running cp.getValue(...) of course throws an exception:
 
   Concert exception caught: CP Optimizer Error : (10) IlcIntExpI::getValue non-fixed constrained varible 00B3AFF8
 
That I do incorrect?
 

* CPLEX 12.5, VS9

 
 ILCGOAL3(SimpleInstantiate,
         IlcInt,         _n, 
         IlcIntVar *,    _w, 
         IlcFloatVar *,  _obj
        )
{
  int k = 0;
  for (; k < _n; k++)
    if (!_w[k].isFixed())
      break;
 
  if (k >= _n)
    return 0;
 
  IlcInt value = _w[k].getMin();
 
  return IlcOr(       _w[k] == value,
               IlcAnd(_w[k] != value, this)
               );
}
 
 
ILOCPGOALWRAPPER2(WrapperSimpleInstantiate, cp,
                        IloIntVarArray, a,
                        IloNumVar, b
                  ){
 
  IlcInt dim2 = a.getSize();
  IlcIntVar * sa = new (cp.getHeap()) IlcIntVar [dim2];
  for (IlcInt j = 0; j < dim2; j++)
    sa[j] = cp.getIntVar(a[j]);
 
  IlcFloatVar * sb = new (cp.getHeap()) IlcFloatVar;
  *sb = cp.getFloatVar(b);
  
  return SimpleInstantiate(cp, a.getSize(), sa, sb);
}
 
 
 
int main() {
 
  IloIntVarArray x(env);
 
  // initialize
 
  IloNumExpr obj = ...;
 
  model.add(IloMaximize(env, obj));
 
  model.add(MyConstraint(env, x, obj));
 
  IloGoal simple_goal = WrapperSimpleInstantiate(env, x, objvar);
 
  // start solution
 
  cp.solve(simple_goal);
 
}
 
int main2() {
 
  IloIntVarArray x(env);
 
  // initialize
 
  IloNumExpr obj = ...;
 
  model.add(IloMaximize(env, obj));
 
  model.add(MyConstraint(env, x, obj));
 
  IloGoal simple_goal = WrapperSimpleInstantiate(env, x, objvar);
 
  // start solution
 
  cp.startNewSearch(simple_goal);
  while(cp.next()) {
    cp.getValue(...)    // runs almost at every step and throws and exception
  }
}
 
 
Updated on 2013-06-18T13:41:21Z at 2013-06-18T13:41:21Z by mrmag
  • Paul Shaw
    Paul Shaw
    6 Posts
    ACCEPTED ANSWER

    Re: Search strategy with ILCGOALn and ILOCPGOALWRAPPERn

    ‏2013-06-18T14:06:37Z  in response to mrmag

    Hello,

     

    Can you enclose a complete code (which compiles)?  It is quite hard to help on such a specific problem without the full code.

     

    Regards,

    • This reply was deleted by mrmag 2013-06-18T15:52:21Z.
    • mrmag
      mrmag
      28 Posts
      ACCEPTED ANSWER

      Re: Search strategy with ILCGOALn and ILOCPGOALWRAPPERn

      ‏2013-06-18T15:55:49Z  in response to Paul Shaw

      Hello,

      I found the solution. In the goal I have to create two instances with "this":

        return IlcOr(       

                     IlcAnd(_w[k] == value, this),

                     IlcAnd(_w[k]  != value, this)

                     );

       

      I still do not understand why in the CP Optimizer Extensions User's Manual there is an example with a mistake:

       

        CPLEX125/doc/html/en-US/CP_Optimizer/Advanced_user_manual/topics/goals_write_example.html

       

       

      Updated on 2013-06-18T15:56:04Z at 2013-06-18T15:56:04Z by mrmag
      • ChrisBr
        ChrisBr
        47 Posts
        ACCEPTED ANSWER

        Re: Search strategy with ILCGOALn and ILOCPGOALWRAPPERn

        ‏2013-06-18T17:41:39Z  in response to mrmag

        Hello,

        Good, you found the solution by yourself!
        Note that another and more efficient way to write the same goal is:
        return IlcAnd (IlcOr(_w[k] == value, _w[k]  != value), this);

        There isn't any mistake in User's Manual (CPLEX125/doc/html/en-US/CP_Optimizer/Advanced_user_manual/topics/goals_write_example.html)

        ILCGOAL1(MycInstantiate, IlcIntVar, var) {
          if (var.isFixed())
            return 0;
          IlcInt value = var.getMin();
          return IlcOr( var == value,
                        IlcAnd( var != value,
                                this));
        }

        The goal MycInstantiate tries  to assign a value to variable ; if it succeeds, there isn't anything else to do ; but if it fails, it removes the tried and failed value from the domain of the constrained variable and executes the goal again with the aim to choose and try another value to be assigned to this same variable.

        Your goal tries to assign values to several variables. In a first step, it selects a variable and tries to assign its minimum domain-value to it ; even if it succeeds, the goal must be called again to work on the other variables ; that's why in both cases, whenever it succeeds or it fails, your goal must be called again.

        I hope this clarifies.

        Regards,

        Chris.
         

        • mrmag
          mrmag
          28 Posts
          ACCEPTED ANSWER

          Re: Search strategy with ILCGOALn and ILOCPGOALWRAPPERn

          ‏2013-06-18T20:04:23Z  in response to ChrisBr

          Thank you, Chris.