How to set constraints for outpus of objective function in fmincon

11 views (last 30 days)
Hi, I am new in optimization. I am implementing an algorithm, where inside the optimization function the values of three varaibles are with in certain range, which are set as constraints. how it can be done?
  2 Comments
John D'Errico
John D'Errico on 14 Sep 2017
There is not need to set explicit constraints on the variables. Fmincon allows you to specify bounds on the variables. Why does this not do what you ask for?
Raihan Ul Islam
Raihan Ul Islam on 14 Sep 2017
Edited: Raihan Ul Islam on 14 Sep 2017
i have three variables like x1, x2, x3 inside objective function. From these three values a single value is computed which is output of objective function. But there should be some constraints
  1. 0>=Xi>=1 ; i=1,2,3,
  2. x3>x2>x1.
How can i enforce the above two constraints. Usually objective function gets values of other variables from fmincon. Here i have to check if the values are following above constraints.

Sign in to comment.

Answers (1)

John D'Errico
John D'Errico on 14 Sep 2017
If x1,x2,x3 are optimization parameters, then it is simple.
Case 1 is simply a set of bound constraints. As I said, fmincon explicitly allows upper and lower bound constraints.
Case 2 is a set of TWO linear inequality constraints. Again, fmincon allows linear inequalities, although essentially no optimizer will allow strict inequality constraints. You need to formulate it in the form of
x(3) >= x(2)
x(2) >= x(1)
If you insist on strict inequality constraints, then I suggest using an offset tolerance. Thus
x(3) >= x(2) + mytol
x(2) >= x(1) + mytol
where you set mytol to be some reasonably small positive number.
Again, these are simple linear inequalities, a facility that is explicitly allowed using fmincon. READ THE HELP FOR FMINCON.
If you mean that x1,x2,x3 are computed inside your objective function, then you will need to supply a nonlinear constraint function that computes them independently. Again, this is a facility that is supported by fmincon.
Best of course is if you write one external function that can compute these values. Then you need write the code only once.
Yes, I know that you will next complain that then you will need to compute those parameters twice, once in your objective, and once in your constraint function. WRONG! MATLAB now has a memoization tool that can remember if a function has been called with a given set of parameters, and not need to recompute the values.
help memoize

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!