1

我有一个优化变量x 和一个常数 y。

我想表达一个约束

f(x) <= y. 

我试着做

 27: IloRange rng = (f(cplex->getValue(x)) <= y);
 28: model.add(rng);

但我得到了错误

cplex.cpp:27: error: conversion from 'bool' to non-scalar type 'IloRange' requested

有人可以帮我写一个这种形式的约束吗?

4

1 回答 1

2

首先,线性规划不可能实现严格的不等式。但是你可以表达

f(x) <= y

cplex->getValue(x) 是一个双精度所以 f(x) <= y 是一个布尔值。无论如何, cplex->getValue() 仅在您有解决方案后才可用,因此它永远不应该成为您模型的一部分,除非您正在迭代解决它。要获得 IloRange,您需要重写 f(x) 以接受 IloNumVar 作为其参数并返回 IloExpr。例如,如果你有类似的东西

double f(double x) {return 2*x;}

你需要一个版本

IloExpr f(IloNumVarx) {return 2*x;}

然后你可以写

IloRange rng = (f(x) <= y);

如果您使用 cplex(或任何线性规划求解器),f(x) 只能是线性函数或凸二次函数。

于 2012-07-18T03:34:21.433 回答