Clear Filters
Clear Filters

Error parsing function variables

1 view (last 30 days)
Hello, please I need your help in debugging my code. I have added a snippet of the code because is too cumbersome to include here, but I believe that with this snippet, you may be able to advise me accordingly. Given the error, where could my problem arise from?
PS: water_pipe_length, sewer_pipe_length are arrays of over 10000 elements
The error displayed is:
Not enough input arguments.
Error in Optimization>@(r,p)[AG_LCC_MUT(r,p),AG_S(r,p)] (line 272)
MultiObj.fun = @(r,p) [AG_LCC_MUT(r,p), AG_S(r,p)];
Error in NSGAII (line 57)
Pfit = fun(P);
Error in Optimization (line 283)
NSGAII(params,MultiObj);
N = 50; %Planning period (years)
r = linspace(0.3,0.6,4); %discount rate
p = [5,6]; %frequency of maintenance
no_of_comp = [1,2,3];
OMC = @(r,p)compound_t(p,r,C4,Cr,C3,water_pipe_length(n),sewer_pipe_length(n),N);
MC = @(r,p)compound_Syn(p,r,C4,Cr,C3,assets_category(n),water_pipe_length(n),sewer_pipe_length(n),pavement_area(n),N); %Maintenance cost
AG_LCC_MUT = @(r,p) ICC + MUT_Req + C_rep + C_ist + OMC;
AG_S = @(r,p) MC/(1+r).^-N;
MultiObj.fun = @(r,p) [AG_LCC_MUT(r,p), AG_S(r,p)];
MultiObj.nVar = 2;
MultiObj.var_min = -pi.*ones(1,MultiObj.nVar);
MultiObj.var_max = pi.*ones(1,MultiObj.nVar);
params.Np = 200; % Population size
params.pc = 0.9; % Probability of crossover
params.pm = 0.5; % Probability of mutation
params.maxgen = 100; % Maximum number of generations
params.ms = 0.05; % Mutation strength
% NSGA-II algorithm
NSGAII(params,MultiObj);
%% Functions
% Function to calculate (1+r)^50 for any utility
function [U] = compound_t(p,r,C4,Cr,C3,water_pipe_length,sewer_pipe_length,N)
%AA/p +
d5 = 0; %damage state after 5 years
detr = 0; %deterioration rate
U = 0;
for ii = 1:N
d = unifrnd(0:1/p,1);%select damage state per year
dr = unifrnd(0:1/N,1);%select deterioration rate per year
detr = detr + dr; %update deterioration rate
d5 = detr*(d5+d); %add the damage per year for p years
U = U+((C4*water_pipe_length)./p + d5*Cr*water_pipe_length+(C3*sewer_pipe_length)./p + d5*Cr*sewer_pipe_length)./((1+r).^ii);
end
end
function [U] = compound_Syn(p,r,C4,Cr,C3,assets_category,water_pipe_length,sewer_pipe_length,pavement_area,N)
%AA/p +
d5 = 0; %damage state after 5 years
detr = 0; %deterioration rate
U = 0;
if strcmp(assets_category,'E')%assets_category == 'E'
pave_cons_cost = 125; %Pavement construction cost
pave_off_facil_cost = 210; %Pavement off facility cost
else
pave_cons_cost = 150;
pave_off_facil_cost = 245;
end
for ii = 1:N
d = unifrnd(0:1/p,1);%select damage state per year
dr = unifrnd(0:1/N,1);%select deterioration rate per year
detr = detr + dr; %update deterioration rate
d5 = detr*(d5+d); %add the damage per year for p years
U = U+((C4*water_pipe_length)./p + d5*Cr*water_pipe_length+(C3*sewer_pipe_length)./p + d5*Cr*sewer_pipe_length+...
(pavement_area*pave_cons_cost)+(pavement_area*pave_off_facil_cost))./((1+r).^ii);
end
end
  2 Comments
KSSV
KSSV on 16 Aug 2022
You need to attach the function: NSGAII. When we run the code the error we get is:
Unrecognized function or variable 'NSGAII'.
Error in Junk (line 21)
NSGAII(params,MultiObj);

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 16 Aug 2022
If you are using https://www.mathworks.com/matlabcentral/fileexchange/65494-non-sorting-genetic-algorithm-ii-nsga-ii (which appears to be the case), then notice near the top of the executable code:
% Initialization
gen = 1;
P = repmat((var_max-var_min)',Np,1).*rand(Np,nVar) + repmat(var_min',Np,1);
Pfit = fun(P);
That is, the function is only going to be passed one parameter, not two. The optimizer cannot handle separated variables. You need to use something like
MultiObj.fun = @(rp) [AG_LCC_MUT(rp(1),rp(2)), AG_S(rp(1),rp(2))];
  3 Comments
Walter Roberson
Walter Roberson on 17 Aug 2022
AG_LCC_MUT = @(r,p) ICC + MUT_Req + C_rep + C_ist + OMC(r,p);
AG_S = @(r,p) MC(r,p)/(1+r).^(-N);
If ICC or others are function handles, you will need to pass appropriate parameters to them as well.

Sign in to comment.

More Answers (0)

Categories

Find more on Historical Contests in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!