nonlinear constrain error must return real value

17 views (last 30 days)
Hello! When trying to solve an optimization problem( linear and nonlinear constrains) using the ga function it returns the following error:
>> [x,fval] = ga(ObjectiveFunction,nvars,[],[],Aeq,Beq,lb,ub,ConstraintFunction)
Error using constrValidate (line 59)
Constraint function must return real value.
Error in gacommon (line 125)
[LinearConstr, Iterate,nineqcstr,neqcstr,ncstr] = constrValidate(NonconFcn, ...
Error in ga (line 363)
NonconFcn,options,Iterate,type] = gacommon(nvars,fun,Aineq,bineq,Aeq,beq,lb,ub, ...
My non linear constrains are:
function [C,Ceq] = simple_constraint(x)
Ceq=[x(1)*70+x(8)*x(12)-x(3)*x(9);
x(2)*70+x(7)*x(11)-x(4)*x(10);
120-x(3)*(x(11)-x(9));
120-x(16)*0.2*x(13);
17.5-x(4)*(x(12)-x(10));
17.5-x(17)*0.5*x(14);
90-x(18)*0.5*x(15);
x(13)-((90-x(9))-(150-x(11)))/(log(abs((90-x(9))/(150-x(11)))));
x(14)-((264-x(10))-(264-x(12)))/(log(abs((264-x(10))/(264-x(12)))));
x(15)-((264-70)-(264-100))/(log(abs((264-70)/(264-100))))];
C=[];
end
And my function for minimizing is :
function y = simple_fitness(x)
y=5750*(x(16)^0.59+x(17)^0.59+x(18)^0.59);
end
We tried using the dbstop if error and in the c vector there was a NaN value (we think that is a problem in the 9th nonlean equality constrain, or do the c and ceq equality vectors don't connect?).
Thanks beforehand!
  2 Comments
madhan ravi
madhan ravi on 12 Dec 2018
upload the full code where was simple_constraint used?
Maria Lepouri
Maria Lepouri on 12 Dec 2018
It was used here:
ObjectiveFunction = @simple_fitness;
nvars = 2; % Number of variables
LB = [0 0]; % Lower bound
UB = [1 13]; % Upper bound
ConstraintFunction = @simple_constraint;
[x,fval] = ga(ObjectiveFunction,nvars,[],[],[],[],LB,UB, ...
ConstraintFunction)

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 12 Dec 2018
The expression beginning with ‘x(13)’ contains a log argument that could become negative if ‘x(11)’ is greater than 150, and similarly, ‘x(14)’ contains a log argument that could become negative if ‘x(12)’ is greater than 264. Logarithms of negative numbers are complex. Constraining these parameters in the ‘ub’ vector could be one solution, although since I do not know in what order the constraints are checked, might not prevent the error.
To illustrate:
log(abs((90-x(9))/(150-x(11))))
log(abs((264-x(10))/(264-x(12))))
  1 Comment
Maria Lepouri
Maria Lepouri on 12 Dec 2018
I understand what you say but I've already used the abs function to prevent a negative log. I will check it thought and add an abs to both numerator and denominator.

Sign in to comment.

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!