Not Enough Input Argument Error
显示 更早的评论
Hello everyone, I am trying to solve a problem using optimization toolbox. The solver i am using is Genetic algorithm solver. I do not understand but i am keep getting this error in my objective function section. I am using live editor and there is a local function section and i wrote my objective function there but keep getting this error. My data is showing in the workspace but objective function seem to be not recgonizing any of the data i have. Here is the objective function i wrote. Can you help me to solve it please?
thank you in advance!
PS: I wrote temp becasue it gave me index error before so tried to solve it this way. Now i am having input argument error.
function z = objectivefunct(ai,rij,xij)
temp=reshape(xij,36,36);
for i = 1:36
for j = 1:36
z = (-1)*(ai(i,1)*temp(i,j)*rij(i,j));
end
end
end
采纳的回答
Andreas Apostolatos
2022-3-16
编辑:Andreas Apostolatos
2022-3-16
Hi,
It is yet not clear what kind of error you are receiving, since you have not shared it with us. However I understand that you are trying to use the 'ga' function in MATLAB and, that you pass as first argument a handle to the function 'objectivefunct' you defined above. Please note, that the function handle that represents the objective function to be minimized should "accept a row vector of length nvars and return a scalar value." Please see the following link for more information,
In your case you are trying to pass three arguments to your objective function 'ai', 'rij', 'xij' which is obviously not allowed. If you want to pass additional arguments to your objective function which are defined in your base workspace, then you can use a wrapper, namely,
x = ga(@(ai) objectivefunct(ai,rij,xij), nvars)
What happens here is, that function 'ga' "sees" only a function handle that receives one input argument 'ai' as input (assuming of course that this input argument contains your design variables) while the rest of the arguments are taken by their definitions in the base workspace.
Please note, that this question is already addressed in different threads, among which also in the following one,
I hope this helps.
Kind regards,
Andreas
8 个评论
Thank you for your answer. I guess i am understanding what you are trying to say. ıf you dont mind can you have a look into my code? Because i am very new to matlab and i am having trouble with the optimization toolbox.
I will attach the file. If you can have a look i would be so happy!
Thank you again.
Yes, this exactly what I meant.
Kind regards,
Andreas
thank you very much!
Best regards,
Beyza.
Hello Andreas,
Sorry for again taking your time but i couldn't solve the problem. The reason is that i couldn't quite get my head around it. Now i have several questions about how to wrap.
1: Should i change my function? (I mean if the code is right just the way it is? Or the code is totally wrong?)
2: where should i write my objective function? (in an another script or in the local function section?)
3: How should i implement the wrapper ? ( Where should i add it?)
At this point I really do not know what to do. I have read the additional websites you recommended i understand where my mistake is but need a little assistance to solve it.
If you can help i will be appreciated.
Thank you.
Best,
Beyza.
Hi Beyza,
1.) I ran the m-file you shared, and the optimization seems to be going through, meaning that there is no error asserted. However, I cannot interpret or comment on the results, since I do not know what kind of problem you are trying to solve.
2.) You can write your objective function either as a local function at the end of your script or as a separate function in an extra m-file. Both methods should work, you need to decide which of the two approaches suits you best.
3.) You already implemented the wrapper in your script in the following lines of code,
% Pass fixed parameters to objfun
objfun = @(xij)objectivefunct(ai,rij,xij);
% Set nondefault solver options
options = optimoptions("ga","CrossoverFcn","crossoverscattered",...
"MutationFcn","mutationadaptfeasible","SelectionFcn",...
"selectionstochunif","EliteCount",2,"PopulationSize",37,"MaxGenerations",...
100,"MaxStallGenerations",100,"Display","iter","PlotFcn","gaplotbestf");
% Solve
[solution,z] = ga(objfun,V,[],[],[],[],zeros(V,1),ones(V,1),[],...
[],options);
Here objfun is the the wrapper function whereas objectivefunct is your actual objective function. You need this wrapper function because the first argument of the optimization functions in MATLAB need to be a function handle pointing to a function that only receives one input argument, in your case xij.
I hope this helps you further.
Kind regards,
Andreas
Azime Beyza Ari
2022-3-18
编辑:Azime Beyza Ari
2022-3-18
thank you again for taking your time to help me!
here is the updated file attached.
If you run you can see that it gives 'index in position 1 exceeds array bounds. Index must not exceed 1' error. You said this earlier on the comments that this toolbox takes vectors and gives a output as a scalar value. I know this error is caused by matrixes i have (probably). Further comments on this error and how to solve it would be so good!
( If i convert my matrices into row vectors would i be able to solve this without any errors? Is this the solution to my problem? )
,P.S: My desired output is the summation of ai's if xij(Decision variable) and rij (xij is dependent to this parameter) is 1. So the answer should be around 5k-6k. I am trying to maximize the covered population. So here my ai's represents the population and the summation of ai's is around 6k. If ı do not get error I keep getting an output as 334 which is not acceptable.
Thank you,
Best,
Beyza.
hey Andreas,
So i tried writing my matrices as a row vector but still getting an error! If you have time can you have a look in to the file i attahced in the last comment i wrote in 18 march.
sorry if i am taking your time. But really need assistance! I am stuck and can not do it without any assistance!
Thank you,
Best,
Beyza.
Hi Beyza,
Your design variables is a vector of nvars variables. which is set by the second input argument to function ga, see the following lines of code:
[solution,z] = ga(objfun,nvars,[],[],[],[],zeros(nvars,1),...
ones(nvars,1),[],[],options);
According to the documentation, the array of the design variables is a row vector with its dimension being equal to nvars, see the following documentation page for more information,
where it is mentioned:
"Number of variables, specified as a positive integer. The solver passes row vectors of length nvars to fun."
Thus, this is obviously a column-vector, but you are trying to access some elements in the second row within your function handle representing your objective function, see the following lines of code,
function z = objectivefunc(vars,rij,ai)
xij = vars(1);
for i = 1:36
for j = 1:36
z = (-1)*(vars(i,j)*rij(i,j)*ai(i,1));
end
end
end
I would recommend you to revise the reason why you are trying to access more rows of array vars, namely vars(i,j), since your design variable vector has only one vector as said above.
You need to first understand the error that MATLAB asserts and then based on that try to debug to better understand the code and how your code corresponds to your mathematical modeling. The best way to do this, is by adding break points, see the following screenshot,

By placing a breakpoint at line 148 and then hovering the mouse pointer over the variable vars I can see, that this is row-vector. In your nested loop however the index i runs from 1 to 36, which triggers the error, as no other value for index i than 1 can be accessed.
If you have further questions please consider creating a new MATLAB Answers thread.
I hope this information helps you.
Kind regards,
Andreas
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
