MATLAB: Setting boundaries on fmincon output

fminconMATLABoptimization

Hi all,
I am trying to optimize for the best efficiency for a given set of inputs, so I would like to use fmincon to output an optimal value that lies only between the possible range of between 0 and 100% (0 to 1). I am using several input variables and setting upper and lower boundaries for those. How could implement this so I dont get solutions which would not be possible?
Many thanks.

Best Answer

If your constraints can be expressed as upper and lower bounds on individual variables, then use the ub and lb parameters to express those -- this is the best way to do constraints. All optimizers should always obey these kinds of constraints.
If you have constraints that do not fit as plain upper and lower bounds, but can be expressed as linear inequalities, then use the A matrix and b vector to express these. These are a pretty good way to do constraints. Some optimizer algorithms always obey these kinds of constraints, and other optimizer algorithms might violate them during the initial evaluations to estimate the gradients.
If you have constraints that can be expressed as linear equalities, then use the Aeq matrix and b vector to express these. These kinds of constraints are harder to deal with. In theory it is possible to express linear equalities as the pair of entries A(K,:) * x = b with -A(K,:) * x = -b but let the algorithm decide whether to implement them internally that way (some of the algorithms do), as some algorithms have more direct ways of dealing with such constraints. Avoid these if you can use ub/lb or linear inequalities, but use them if needed.
If you have constraints that cannot be expressed linearly, use the nonlinear constraint function handle parameter. The function handle must accept the vector of x values and return two parameters, the nonlinear inequalties an the nonlinear equalities. These kinds of constraints are harder to deal with, and it might not be possible for the optimizer to find any position that satisfies the constraints. Only use these if you must do so.
In your situation, you should use constraints in priority from top to bottom in the list I gave above. If you must then you can have your nonlinear constraint function calculate pretty much the same thing as your optimizer does (hint: use a helper function to avoid duplicate code). (hint: use memoize() if calculation of the helper function is expensive.)
Related Question