hi, i have a problem with ga function. the problem is: Caused by: Failure in initial user-supplied nonlinear constraint function evaluation.

1 view (last 30 days)
my function is:
function f = obj_fun2(x)
MTTR = [0.04166666 0.041666667 0.083333333 0.041666667 0.041666667 0.083333333 0.041666667];
tic
S = zeros(1,320);
for j=1:320
S(j) = max([MTTR(1)*x(1,j), MTTR(2)*x(2,j), MTTR(3)*x(3,j), MTTR(4)*x(4,j), MTTR(5)*x(5,j), MTTR(6)*x(6,j), MTTR(7)*x(7,j)]);
end
toc
f = -((320 - sum(S))/320);
end
my constraint is:
function [c, ceq] = nlc(x)
for j=1:320
c(1) = -sum(x(1,:)) +(320/43.65);
c(2) = -sum(x(2,:)) +(320/43.71);
c(3) = -sum(x(3,:)) +(320/47.78);
c(4) = -sum(x(4,:)) +(320/89.66);
c(5) = -sum(x(5,:)) +(320/68.70);
c(6) = -sum(x(6,:)) +(320/29.22);
c(7) = -sum(x(7,:)) +(320/73.88);
end
ceq = [];
end
my model is:
nvars= 1;
intcon= 1:nvars;
LB = zeros(1,nvars);
UB = ones(1,nvars);
options = optimoptions(@ga,'SelectionFcn',@selectiontournament, ...
'FitnessScalingFcn',@fitscalingprop);
[x , fval] = ga(@obj_fun2 , nvars ,[],[],[],[], LB, UB , @nlc, intcon ) ;

Accepted Answer

Alan Weiss
Alan Weiss on 19 Oct 2021
I believe that the error is here:
function [c, ceq] = nlc(x)
for j=1:320
c(1) = -sum(x(1,:)) +(320/43.65);
c(2) = -sum(x(2,:)) +(320/43.71);
% ...
end
end
You specify nvars= 1. However, your constraint function is trying to access x(2). Your x is a scalar. Error.
Alan Weiss
MATLAB mathematical toolbox documentation

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!