필터 지우기
필터 지우기

Error parsing function variables

조회 수: 1 (최근 30일)
Ikechi Ndamati
Ikechi Ndamati 2022년 8월 15일
댓글: Ikechi Ndamati 2022년 8월 18일
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
KSSV
KSSV 2022년 8월 16일
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);

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2022년 8월 16일
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
Walter Roberson
Walter Roberson 2022년 8월 17일
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.
Ikechi Ndamati
Ikechi Ndamati 2022년 8월 18일
Thank you @Walter Roberson. It worked.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Historical Contests에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by