Hi,
I have tried to use clones to reason with models.
I got some execution errors which I do not understand. May I show it to you with a little example?

I create a class Model with an IntVarArray "vars" as its own attribute.
The default constructor initializes "vars" with two IntVar, vars[0] = 0, and vars[1] = [0..1].
The function addVar() adds a new IntVar to "vars". The function postEQ() constraints two IntVars of "vars" to be equal.

In the main program we create a first model "m" with the default constructor.
We add two new vars "vars[2]" and "vars[3]", and constraint vars[2] = vars[0] and vars[3] = vars[1].
We request propagation and show the solution.

Then we create "m2" by using "m" and the copy constructor.
We add two new vars "vars[4]" and "vars[5]" and constraints vars[4] = vars[0] and vars[5] = vars[1].
While it succeeds posting vars[4] = vars[0] it fails trying to post vars[5] = vars[1].

If (always in "m2") we try with vars[4] = vars[2] and vars[5] = vars[3] it fails too.
It succeeds posting vars[4] = vars[2], but it fails trying to post vars[5] = vars[3].
If we try with vars[4]= vars[5] it also fails.

It seems that the error appears when we try to constraint a non-asigned "new variable" (created in the current Space "m2")
with a non-asigned "old variable" (created in the Space "m", from which "m2" has been cloned from).
An error also appears when we try to constraint two non-asigned variables in the clonated space. In "m2", both vars[4] = vars[5] and vars[2] = vars[3] fails, while in "m" vars[2] = vars[3] succeeds.

Do you know what is the problem? Am I doing something wrong with the copy constructor?

Gecode Version: Gecode 3.2.2
Compiler tools: Microsoft Visual Studio 2008
OS: Microsoft Windows XP SP3

Thank you very much in advance,
Nacho

/* EXAMPLE */


#include <gecode/int.hh>
#include <gecode/search.hh>

using namespace Gecode;

class Model : public Space {
protected:
  IntVarArray vars;
public:
  Model(void) : Space(), vars(*this, 2, 0, 1){
      rel(*this, vars[0], IRT_EQ, 0);
  };
 
  Model(bool share, Model& s) : Space(share, s) {
    vars.update(*this, share, s.vars);
  }

  virtual Space* copy(bool share) {
    return new Model(share,*this);
  }

  void print(void) const {
    std::cout << vars << std::endl;
  }

  void newVar(){
     vars.add(*this, IntVar(*this, 0, 1));
  }

  void postEQ(int i, int j){
    rel(*this, vars[i], IRT_EQ, vars[j]);
  }

};


int main(int argc, char* argv[]) {
  Model* m = new Model;
  m->newVar();
  m->newVar();
  m->postEQ(2,0);
  m->postEQ(3,1);
  m->status();
  m->print();

 //------------------//

  Model* m2 = dynamic_cast<Model*>(m->copy(true)); 
  m2->newVar();
  m2->newVar();
  m2->postEQ(4,0);
  m2->postEQ(5,1);
  m2->status();
  m2->print();

  return 0;
}




#include <gecode/int.hh>
#include <gecode/search.hh>

using namespace Gecode;

class Model : public Space {
protected:
  IntVarArray vars;
public:
  Model(void) : Space(), vars(*this, 2, 0, 1){
	  rel(*this, vars[0], IRT_EQ, 0);
  };
  
  Model(bool share, Model& s) : Space(share, s) {
    vars.update(*this, share, s.vars);
  }

  virtual Space* copy(bool share) {
    return new Model(share,*this);
  }

  void print(void) const {
    std::cout << vars << std::endl;
  }

  void newVar(){
	 vars.add(*this, IntVar(*this, 0, 1));
  }

  void postEQ(int i, int j){
	rel(*this, vars[i], IRT_EQ, vars[j]);
  }

};


int main(int argc, char* argv[]) {
  Model* m = new Model;
  m->newVar();
  m->newVar();
  m->postEQ(2,0);
  m->postEQ(3,1);
  m->status();
  m->print();

 //------------------//

  Model* m2 = dynamic_cast<Model*>(m->copy(true));  
  m2->newVar();
  m2->newVar();
  m2->postEQ(4,0);
  m2->postEQ(5,1);
  m2->status();
  m2->print();

  return 0;
}


_______________________________________________
Gecode users mailing list
[email protected]
https://www.gecode.org/mailman/listinfo/gecode-users

Reply via email to