1

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?

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

First, you are my hero: it was failing because getValue cannot be a part of the model, and re-writing f(x) as a "cplex" version is the way to go, thanks! I think that is a pain, because f is nontrivial, and now I have to rewrite it using all of their notation, but O well. As far as linearity is concerned, CPLEX (atleast as of 12.4) now supports MILP programming, with integer and binary constraints. The constraint may have to be integer linear or integer quadratic, but I don't think it has to be convex because it may not even be continuous if x is binary or integer constrained.
You mentioned "unless I solve it iteratively". Is there a way I can solve it iteratively and not have to rewrite f? Perhaps a pointer to using getValue() in a model that is solved iteratively?
@Tommy You would solve a problem iteratively if you wanted to use the results of an optimization to make a different solution. It would not help in your case. To avoid multiple version of the code, you can use C++ templates.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.