Reputation: 31
I try to impose a nonlinear constraint in the fmincon optimizer. The problem is that the nonlinear constraint should be relevant only when one of the parameters is negative. The code is like this:
function [c, ceq] = confun_Model11(param)
% Nonlinear inequality constraints: c(x)<=0
if param(6)<0
c = (-4)*param(5)*param(7) + param(6)^2+eps;
else
c = [];
end
%Nonlinear equality constraints: ceq(x)=0
ceq = [];
end
The problem is that for example when using the diagnostics option Matlab says that there are no nonlinear constrains:
Constraints
Number of nonlinear inequality constraints: 0
Number of nonlinear equality constraints: 0
and also during the search for the optimum this nonlinear constraint is violated. Can somebody please indicate if I have not properly defined the nonlinear conditioned constraint?
Upvotes: 0
Views: 794
Reputation: 3450
I'm not sure that fmincon can cope with the number of constraints changing during runtime.
Rather than trying to switch off the constraint within your code evaluation, it would be better to return a value that satisfies the constraint c(x)<=0 when you don't want the constraint to be active.
function [c, ceq] = confun_Model11(param)
% Nonlinear inequality constraints: c(x)<=0
c = (-4)*param(5)*param(7) + param(6)^2+eps;
% Ensure constraint isn't violated if param(6) is positive
if param(6) >= 0
c = -abs(c);
end
%Nonlinear equality constraints: ceq(x)=0
ceq = [];
end
Now, this still isn't great, because depending on other (linear?) constraints on the parameters, the constraint function could be discontinuous. In general you're much more likely to succeed with the optimisation if the constraint functions are continuous and smooth - so try and redefine your constraint function into something without 'if' statements or 'abs' functions... ie. something that just crosses zero where you want your constraint to apply.
It might make sense to use more than one non-linear constraint to achieve this goal.
Upvotes: 2