使用 C++ 的双变量 Gurobi
Dual variables Gurobi with C++
我正在使用带有 C++ 接口的 Gurobi811 求解一个线性程序,我想恢复对偶变量。文档上写着对应的属性是Pi,但是语法不清楚。这是一个具有两个变量和三个约束的简单线性规划的示例,问题是获取对应于 c0、c1 和 C2 的对偶变量。有什么建议吗
#include "gurobi_c++.h"
#include <stdlib.h>
using namespace std;
int
main(int argc,
char *argv[])
{
GRBenv *env = NULL;
GRBmodel *model = NULL;
int error = 0;
double sol[2];
int ind[2];
double val[2];
double obj[2];
char vtype[2];
int optimstatus;
double objval;
error = GRBloadenv(&env, "LP.log");
if (error) throw;
error = GRBnewmodel(env, &model, "LP", 0, NULL, NULL, NULL, NULL, NULL);
if (error) throw;
obj[0] = -2; obj[1] = +5;
vtype[0] = GRB_CONTINUOUS; vtype[1] = GRB_CONTINUOUS;
error = GRBaddvars(model, 2, 0, NULL, NULL, NULL, obj, 0, NULL, vtype,
NULL);
if (error) throw;
ind[0] = 0; ind[1] = 1;
val[0] = 2; val[1] = 3;
error = GRBaddconstr(model, 2, ind, val, GRB_LESS_EQUAL, 30.0, "c0");
if (error) throw;
ind[0] = 0; ind[1] = 1;
val[0] = 4; val[1] = -9;
error = GRBaddconstr(model, 2, ind, val, GRB_LESS_EQUAL, 0.0, "c1");
if (error) throw;
ind[0] = 0; ind[1] = 1;
val[0] = 1; val[1] = 1;
error = GRBaddconstr(model, 2, ind, val, GRB_GREATER_EQUAL, 5.0, "c2");
if (error) throw;
error = GRBoptimize(model);
if (error) throw;
/* Capture solution information */
error = GRBgetintattr(model, GRB_INT_ATTR_STATUS, &optimstatus);
if (error) throw;
error = GRBgetdblattr(model, GRB_DBL_ATTR_OBJVAL, &objval);
if (error) throw;
error = GRBgetdblattrarray(model, GRB_DBL_ATTR_X, 0, 2, sol);
if (error) throw;
printf("\nOptimization complete\n");
if (optimstatus == GRB_OPTIMAL) {
printf("Optimal objective: %.4e\n", objval);
printf(" x1=%.4e, x2=%.4e\n", sol[0], sol[1]);
} else if (optimstatus == GRB_INF_OR_UNBD) {
printf("Model is infeasible or unbounded\n");
} else {
printf("Optimization was stopped early\n");
}
if (error) {
printf("ERROR: %s\n", GRBgeterrormsg(env));
//exit(1);
}
GRBfreemodel(model); GRBfreeenv(env);
return 0;
}
对偶变量与线性约束有关,所以你应该使用GRBgetdblattrelement
来获得对偶值。索引可以从函数 GRBgetconstrbyname
中得到。例如
int pIndex0 = 0, pIndex1 = 0, pIndex2 = 0;
GRBgetconstrbyname(model, "c0", &pIndex0);
GRBgetconstrbyname(model, "c1", &pIndex1);
GRBgetconstrbyname(model, "c2", &pIndex2);
double dual0=0, dual1=0, dual2=0;
GRBgetdblattrelement(model, "Pi", pIndex0, &dual0);
GRBgetdblattrelement(model, "Pi", pIndex1, &dual1);
GRBgetdblattrelement(model, "Pi", pIndex2, &dual2);
我正在使用带有 C++ 接口的 Gurobi811 求解一个线性程序,我想恢复对偶变量。文档上写着对应的属性是Pi,但是语法不清楚。这是一个具有两个变量和三个约束的简单线性规划的示例,问题是获取对应于 c0、c1 和 C2 的对偶变量。有什么建议吗
#include "gurobi_c++.h"
#include <stdlib.h>
using namespace std;
int
main(int argc,
char *argv[])
{
GRBenv *env = NULL;
GRBmodel *model = NULL;
int error = 0;
double sol[2];
int ind[2];
double val[2];
double obj[2];
char vtype[2];
int optimstatus;
double objval;
error = GRBloadenv(&env, "LP.log");
if (error) throw;
error = GRBnewmodel(env, &model, "LP", 0, NULL, NULL, NULL, NULL, NULL);
if (error) throw;
obj[0] = -2; obj[1] = +5;
vtype[0] = GRB_CONTINUOUS; vtype[1] = GRB_CONTINUOUS;
error = GRBaddvars(model, 2, 0, NULL, NULL, NULL, obj, 0, NULL, vtype,
NULL);
if (error) throw;
ind[0] = 0; ind[1] = 1;
val[0] = 2; val[1] = 3;
error = GRBaddconstr(model, 2, ind, val, GRB_LESS_EQUAL, 30.0, "c0");
if (error) throw;
ind[0] = 0; ind[1] = 1;
val[0] = 4; val[1] = -9;
error = GRBaddconstr(model, 2, ind, val, GRB_LESS_EQUAL, 0.0, "c1");
if (error) throw;
ind[0] = 0; ind[1] = 1;
val[0] = 1; val[1] = 1;
error = GRBaddconstr(model, 2, ind, val, GRB_GREATER_EQUAL, 5.0, "c2");
if (error) throw;
error = GRBoptimize(model);
if (error) throw;
/* Capture solution information */
error = GRBgetintattr(model, GRB_INT_ATTR_STATUS, &optimstatus);
if (error) throw;
error = GRBgetdblattr(model, GRB_DBL_ATTR_OBJVAL, &objval);
if (error) throw;
error = GRBgetdblattrarray(model, GRB_DBL_ATTR_X, 0, 2, sol);
if (error) throw;
printf("\nOptimization complete\n");
if (optimstatus == GRB_OPTIMAL) {
printf("Optimal objective: %.4e\n", objval);
printf(" x1=%.4e, x2=%.4e\n", sol[0], sol[1]);
} else if (optimstatus == GRB_INF_OR_UNBD) {
printf("Model is infeasible or unbounded\n");
} else {
printf("Optimization was stopped early\n");
}
if (error) {
printf("ERROR: %s\n", GRBgeterrormsg(env));
//exit(1);
}
GRBfreemodel(model); GRBfreeenv(env);
return 0;
}
对偶变量与线性约束有关,所以你应该使用GRBgetdblattrelement
来获得对偶值。索引可以从函数 GRBgetconstrbyname
中得到。例如
int pIndex0 = 0, pIndex1 = 0, pIndex2 = 0;
GRBgetconstrbyname(model, "c0", &pIndex0);
GRBgetconstrbyname(model, "c1", &pIndex1);
GRBgetconstrbyname(model, "c2", &pIndex2);
double dual0=0, dual1=0, dual2=0;
GRBgetdblattrelement(model, "Pi", pIndex0, &dual0);
GRBgetdblattrelement(model, "Pi", pIndex1, &dual1);
GRBgetdblattrelement(model, "Pi", pIndex2, &dual2);