簡體   English   中英

如何在gecode中打印變量的值

[英]How to print values of variables in gecode

我正在嘗試使用 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;
}

我得到 output 為 [1..6] [10..85] [1..100]。 但我期待一個有效的解決方案,如 1 83 5 分別作為 xyz 值的答案。有人能解釋一下嗎?

您忘記為變量添加分支指令。 如果沒有任何分支,執行將在傳播后停止,如果沒有傳播者失敗,則假定它已達到解決方案。

變量數組分支器的一個簡單示例是branch(*this, x, INT_VAR_SIZE_MIN(), INT_VAL_MIN()); . 更多關於分支的信息可以在MPG中找到。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM