Tommy
Tommy

Reputation: 13672

cplex constraint function of a variable

I have an optimization variable x and a constant y.

I want to express a constraint

f(x) <= y. 

I tried doing

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

But I get the error

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

Can someone help me write a constraint of this form?

Upvotes: 1

Views: 2738

Answers (1)

David Nehme
David Nehme

Reputation: 21597

First, strict inequality is not possible with linear programming. You can however express

f(x) <= y

cplex->getValue(x) is a double so f(x) <= y is a boolean. At any rate, cplex->getValue() is only available after you have a solution so it should never be part of your model, unless you are solving it iteratively. To get a IloRange, you need to rewrite f(x) to accept an IloNumVar as it's parameter and to return an IloExpr. For example, if you have something like

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

You need a version

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

Then you can write

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

If you are using cplex (or any linear programming solver), f(x) can only be a linear function or convex quadratic function.

Upvotes: 2

Related Questions