lp_solve 连续调用“solve”;不正确

发布于 2024-12-02 02:18:14 字数 2068 浏览 1 评论 0原文

我需要解决一些简单的线性整数规划问题,我使用了 lp_solve 库。任务是获取线性函数的一些连续值的变量值,并对变量进行可能的简单(线性)约束(实际上,即使没有任何附加约束,我也遇到了问题)。例如,我有线性函数 4a + 5b。我感兴趣的第一个值是(函数值 - 变量值):

0 - (0, 0); 4 - (1, 0); 5 - (0, 1); 8 - (2, 0); 9 - (1, 1)

问题在于,在得到 8 - (2, 0) 后,lp_solve 在求解任务时返回 NUMFAILURE 代码 (5)并将其解析为0 - (0, 0)...

如果我不使用连续调用“解决”函数,而只是从9开始,那么我会得到正确的答案(<代码>9 - (1, 1))。有人可以解释一下吗?代码如下。

#include <iostream>
#include <cstdio>

#include <lpsolve/lp_lib.h>

# if defined ERROR
#   undef ERROR
# endif
# define ERROR() { fprintf(stderr, "Error\n"); exit(1); }

using std::cout;
using std::endl;

void print_res(REAL * vars, int size) {
    cout << "(";
    for (int i = 0; i < size - 1; ++i) {
        cout << round(vars[i]) << ", ";
    }
    cout << round(vars[size - 1]) << ")";
}

int main()
{

    lprec *lp;
    int majorversion, minorversion, release, build, min = 0;

    lp_solve_version(&majorversion, &minorversion, &release, &build);


    const int l = 5;   // number of iterations
    const int dim = 2; // dimension ot current task
    char p_data[] = "4 5"; // objective function: p(a, b) = 4a + 5b


    if ((lp=make_lp(0,  dim)) == NULL)
        ERROR();
    set_verbose(lp, CRITICAL);

    if (!str_add_constraint(lp, p_data, GE, min)) // p(a, b) >= min
      ERROR();

    // objective function - p
    if (!str_set_obj_fn(lp, p_data))
      ERROR();

    // work with integer non-negative variables
    set_int(lp, 1, TRUE);
    set_int(lp, 2, TRUE);
    set_lowbo(lp, 1, 0);
    set_lowbo(lp, 2, 0);

    for (int i = 0; i < l; ++i) {
        cout <<  "Status: " << solve(lp) << endl;
        REAL vars[dim];
        get_variables(lp, vars);
        print_res(vars, dim);

        // increase minimum value for p
        min = round(get_objective(lp));
        cout << ", p = " << min << endl;

        if (!set_rh(lp, 1, min + 1))
          ERROR();
    }

    return 0;
}

I need to solve some simple linear integer programming-like problem, I took lp_solve library. The task is to obtain variables' values for some consequtive values of linear function with possible simple (linear) constraints on variables (actually I encountered a problem even without any additional constraints). E.g. I have linear function 4a + 5b. First values I'm interested in are (function value - variables values):

0 - (0, 0); 4 - (1, 0); 5 - (0, 1); 8 - (2, 0); 9 - (1, 1)

The problem is that after getting 8 - (2, 0), lp_solve returns NUMFAILURE code (5) while solving the task and resolves it to 0 - (0, 0)

If I do not use consequtive calls to 'solve' function and just start from 9 then I got right answer (9 - (1, 1)). Would anyone please explain this? The code is following.

#include <iostream>
#include <cstdio>

#include <lpsolve/lp_lib.h>

# if defined ERROR
#   undef ERROR
# endif
# define ERROR() { fprintf(stderr, "Error\n"); exit(1); }

using std::cout;
using std::endl;

void print_res(REAL * vars, int size) {
    cout << "(";
    for (int i = 0; i < size - 1; ++i) {
        cout << round(vars[i]) << ", ";
    }
    cout << round(vars[size - 1]) << ")";
}

int main()
{

    lprec *lp;
    int majorversion, minorversion, release, build, min = 0;

    lp_solve_version(&majorversion, &minorversion, &release, &build);


    const int l = 5;   // number of iterations
    const int dim = 2; // dimension ot current task
    char p_data[] = "4 5"; // objective function: p(a, b) = 4a + 5b


    if ((lp=make_lp(0,  dim)) == NULL)
        ERROR();
    set_verbose(lp, CRITICAL);

    if (!str_add_constraint(lp, p_data, GE, min)) // p(a, b) >= min
      ERROR();

    // objective function - p
    if (!str_set_obj_fn(lp, p_data))
      ERROR();

    // work with integer non-negative variables
    set_int(lp, 1, TRUE);
    set_int(lp, 2, TRUE);
    set_lowbo(lp, 1, 0);
    set_lowbo(lp, 2, 0);

    for (int i = 0; i < l; ++i) {
        cout <<  "Status: " << solve(lp) << endl;
        REAL vars[dim];
        get_variables(lp, vars);
        print_res(vars, dim);

        // increase minimum value for p
        min = round(get_objective(lp));
        cout << ", p = " << min << endl;

        if (!set_rh(lp, 1, min + 1))
          ERROR();
    }

    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文