2

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??

  • Isn't 2 66 10 a valid solution as well? (Hopefully, I didn't make a mistake - I solved in my head.) ;-) As it is, your equation _15 * x + y + 0.4*z == 100_ has a set of solutions and the allowed ranges for x, y, and z define how large this set is (even if x, y, z have to be integral values). So, I believe you cannot expect to get one specific element if the solution is a set of elements. For this, you had to give more constraints. (I must admit I heard about Gecode in your Q the first time and read the first few pages of the doc. - out of curiosity.) – Scheff's Cat Apr 23 '20 at 05:36
  • ... However, the example shown on page 19 of the [Modeling and Programming with Gecode](https://www.gecode.org/doc-latest/MPG.pdf) looks very similar. So, I would expect (without deeper knowledge), if you want one solution then you have to choose one value for x, adjust the constraint resp. (e.g. by tighten the range of x) and solve again, repeating that for y until you got a solution with 1 (or 0) elements. – Scheff's Cat Apr 23 '20 at 05:40
  • Maybe, this is what you're looking for... In the above linked doc. on page 27: 2.5 Best solution search. – Scheff's Cat Apr 23 '20 at 05:44

1 Answers1

1

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.

Dekker1
  • 5,565
  • 25
  • 33