2

我正在尝试使用 gecode 求解线性方程 15 * x + y + 0.4*z == 100。我想打印 x,y,z 的值。但是,当我运行以下代码时,

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;
}

我得到的输出为 [1..6] [10..85] [1..100]。但我期待一个有效的解决方案,如 1 83 5 分别作为 xyz 值的答案。有人能解释一下吗?

4

1 回答 1

1

您忘记为变量添加分支指令。如果没有任何分支,执行将在传播后停止,如果没有传播者失败,则假定它已达到解决方案。

变量数组的分支器的一个简单示例是branch(*this, x, INT_VAR_SIZE_MIN(), INT_VAL_MIN());. 更多关于分支的信息可以在MPG中找到。

于 2020-04-23T05:55:36.090 回答