How to print values of variables in gecode

169 Views Asked by At

I am trying to solve linear equation 15 * x + y + 0.4*z == 100 using gecode. I want to print the values of x,y,z. But, when i run following code,

class LinearEq1 : public Space {
protected:
    IntVar x;
    IntVar y;
    IntVar z;
public:
    LinearEq1(void) :x(*this,-100,100), y(*this, -100, 100), z(*this, -100,100) {
        // no leading zeros
        rel(*this, x, IRT_GR, 0);
        rel(*this, y, IRT_GR, 0);
        rel(*this, z, IRT_GR, 0);
        rel(*this, 15 * x + y + 0.4*z == 100);
    }
    // search support
    LinearEq1(LinearEq1& s) : Space(s) {
        x.update(*this, s.x);
        y.update(*this, s.y);
        z.update(*this, s.z);
    }
    virtual Space* copy(void) {
        return new LinearEq1(*this);
    }
    // print solution
    void print(void) const {
        std::cout << x<<"  "<<y<< " "<<z<<std::endl;
    }
};

// main function
int main(int argc, char* argv[]) {
    // create model and search engine
    LinearEq1* m = new LinearEq1;
    DFS<LinearEq1> e(m);
    delete m;
    // search and print all solutions
    LinearEq1* s = e.next();
    s->print();
    return 0;
}

I get the output as [1..6] [10..85] [1..100]. But I am expecting one valid solution such as 1 83 5 as the answer for the values of x y z respectively.Can someone explain??

1

There are 1 best solutions below

0
Dekker1 On

You forgot to add the branching instruction for the variables. Without any branchers, the execution will stop after propagation and assume that it has reached a solution if no propagator has failed.

A simple example of a brancher for a array of variables is branch(*this, x, INT_VAR_SIZE_MIN(), INT_VAL_MIN());. More information about branchers can be found in the MPG.