How to set constraints for outpus of objective function in fmincon
11 views (last 30 days)
Show older comments
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
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?
Answers (1)
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
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!