Topic
2 replies Latest Post - ‏2014-03-25T11:39:55Z by Trust20092009_01
Trust20092009_01
Trust20092009_01
6 Posts
ACCEPTED ANSWER

Pinned topic Why is the constructor being called three times?

‏2014-03-17T16:19:38Z |

I just want to better understand how does ILOCPCONSTRAINTWRAPPER work so I built this small example and I really do not understand why  the constructor being called three times?

#include<ilcp/cpext.h>

class IlcAloneI : public IlcConstraintI {
  public:
    IlcAloneI(IloCP cp, IlcIntVar x);
    virtual void propagate ();
    virtual void post();

};
IlcAloneI::IlcAloneI(IloCP cp,IlcIntVar x) : IlcConstraintI(cp){  std::cout << " ** THE CONSTRUCTOR IS CALLED ** " << std::endl;}

void IlcAloneI::propagate () {}
void IlcAloneI::post(){}

IlcConstraint IlcAlone(IlcIntExp x){
  IloCP cp = x.getCP();
    return new (cp.getHeap()) IlcAloneI(cp, x);
}

ILOCPCONSTRAINTWRAPPER1(IloAlone, cp , IloIntVar, x)
{
use(cp,x);
return IlcAlone(cp.getIntVar(x));
}

int main(int , const char * []){
  IloEnv env;
  try {
    IloModel model(env);
    IloIntVar x(env,0,1);
    model.add(IloAlone(env,x));
    IloCP cp(model);
    cp.setIntParameter(IloCP::Workers, 1);
    cp.solve();

  }
  catch (IloException& ex) {
    env.out() << "Error: " << ex << std::endl;
  }
  env.end();
  return 0;
}

-------------------------------

 ** THE CONSTRUCTOR IS CALLED **
 ** THE CONSTRUCTOR IS CALLED **
 ** THE CONSTRUCTOR IS CALLED **
 ! ----------------------------------------------------------------------------
 ! Satisfiability problem - 1 variable, 1 constraint
 ! Workers              = 1
 ! Initial process time : 0.00s (0.00s extraction + 0.00s propagation)
 !  . Log search space  : 1.0 (before), 1.0 (after)
 !  . Memory usage      : 467.8 kB (before), 467.8 kB (after)
 ! Using sequential search.
 ! ----------------------------------------------------------------------------
 !               Branches  Non-fixed            Branch decision
 *                      1 0.00s                   0  = _int0
 ! ----------------------------------------------------------------------------
 ! Search terminated normally, 1 solution found.
 ! Number of branches     : 1
 ! Number of fails        : 0
 ! Total memory usage     : 570.8 kB (531.8 kB CP Optimizer + 39.0 kB Concert)
 ! Time spent in solve    : 0.00s (0.00s engine + 0.00s extraction)
 ! Search speed (br. / s) : 100.0
 ! ----------------------------------------------------------------------------
 

Best,

S.S
 

Updated on 2014-03-22T17:05:39Z at 2014-03-22T17:05:39Z by Trust20092009_01
  • Philippe_Refalo
    Philippe_Refalo
    48 Posts
    ACCEPTED ANSWER

    Re: Why is the constructor being called three times?

    ‏2014-03-25T08:43:17Z  in response to Trust20092009_01

    The wrapping code as well as the constructor of the IlcConstraitn can be called up to three times. This is normal. This is necessary to see the variables that are involved in a constraint in order to presolve or to compute conflict sets.  But you will not end up having 3 instances of the constraint, just one will be created for the search. 

    As a consequence of these multiple calls, the wrapping of constraints should be limited to usage variable declarations and to the construction of the IlcConstraint. 

    Regards 

    Philippe