lp_solve连续调用`solve'是不正确的

时间:2011-08-29 14:27:39

标签: c++ linear-programming

我需要解决一些简单的线性整数编程问题,我拿了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;
}

0 个答案:

没有答案