MATLAB: Constraining Dependent Variables in ga Optimization

constrained optimizationgenetic algorithmGlobal Optimization ToolboxMATLAB

Hi,
I am trying to constrain my optimization for min f(x) by restricting the range allowed for a dependent variable h(x). I am using a the ga optimizer from the Optimization Toolbox. Do I need to write a penalty or barrier function into by objective function, or is there another simpler way that I can apply the constraint? I tried using the nonlcon input in the ga function, however have been unable to get it to converge. The documentation suggests that nonlcon can only take x (independent variable vector) as an input and so I think that reading in dependent variables may be a misuse of the function.
Please help! Any guidance would be greatly appreciated.

Best Answer

The documentation suggests that nonlcon can only take x (independent variable vector) as an input and so I think that reading in dependent variables may be a misuse of the function.
Well, nonlcon should be written to accept a vector in the space of x as input, not a vector in the space of h. However, since h is a function of x, that should be straightforward. Something like this,
function [c,ceq] = nonlcon(x)
ub=... %upper bounds
lb=... %lower bounds
hx=h(x);
c=[hx(:)-ub(:); lb(:)-hx(:) ];
ceq=[];
end
Are you sure it's not converging, or might it just be converging to something you don't like? With nonlinear constraints, it can be difficult to find a good initial population, especially if the constraints define a set with several disconnected regions.