genetic algorithm error not enough input arguments

26 次查看(过去 30 天)
function deltaH = curvefit_H_2modes(w1,w2,m1,m2,c1,c2)
load('file.mat');
z = -(x.^2) .* ((1 ./ ((w1.^2 * m1) - (x.^2 * m1) + (1i * x * c1))) + ...
(1 ./ ((w2.^2 * m2) - (x.^2 * m2) + (1i * x * c2))));
deltaH = sum(abs(z - y).^2);
end
Ok, now I use the Genetic Algorithm in the Global Optimization Toolbox and input @curvefit_H_2modes as my fitness function and I input 6 as the number of variables. Then I click on Start to run the GA solver. I get an error message saying "Not enough input arguments" ... WHY?! My function takes 6 input arguments. The function loads a file which has two vectors, x and y (experimental data).
I can run the function myself using estimates for the 6 input variables and it runs perfectly. Why does the GA have a problem with it?
  1 个评论
Craig Cowled
Craig Cowled 2013-5-23
I should add that this is my first time using the Optimization Toolbox, so it's possible I have made a rookie error. Would be grateful if anyone has an answer for me.

请先登录,再进行评论。

采纳的回答

Alan Weiss
Alan Weiss 2013-5-23
There are several problems with your syntax. The main problem is that all optimization solvers expect the decision variables (the variables you want to change to find an optimum) to be in a single vector, typically called x. For example,
function deltaH = curvefit_H_2modes(x)
w1 = x(1);
w2 = x(2);
m1 = x(3);
m2 = x(4);
c1 = x(5);
c2 = x(6);
load('file.mat');
...
Furthermore, you will have much better performance if you get the load statement out of your function definition, and simply pass in the values you need (I think it is just y) in a nested function or anonymous function. See the documentation for details.
Finally, you will probably get better results if you use the appropriate solver. For this type of problem (minimizing a sum of squares), the Optimization Decision Table recommends using lsqnonlin or lsqcurvefit. And do you really need to sum the squares, or can you use the norm function?
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation

更多回答(3 个)

Craig Cowled
Craig Cowled 2013-5-23
Alan,
Thank you for taking the time to answer this in so much detail!
Point taken regarding the choice of optimization tool. I think MattJ recommended I use lsqnonlin for this problem as well. Good point about the load command. Will probably slow down the computation. Will look at the norm function.
Actually, I chose the GA because I need to learn how to use this for another problem (updating a finite element model), so I am still very interested in understanding how the function works.
Thank you again, Alan. It's guys like you that make this forum awesome.
Craig.

Craig Cowled
Craig Cowled 2013-5-24
Hey, what do you know, it worked!!
Btw, your recommendation of using LSQNONLIN was the right call too.
Although the GA worked, it took a few minutes of computation and the residual was an order of magnitude worse than what I got out of LSQNONLIN. Also, I discovered that I had to tweak some of the GA options to get better results.
If anyone is interested, the fitness function I used for my GA optimization was similar to the code in http://www.mathworks.com.au/matlabcentral/answers/60638-curve-fitting-a-complex-function-using-cftool with the only difference being the last line.
When using LSQNONLIN, I used ...
deltaH = z1 - y1;
because it outputs a vector which the LSQNONLIN function uses to find the least squares solution.
When using the GA, I changed this to ...
deltaH = norm(z1 - y1);
because the GA needs a scalar value to calculate the fitness of individuals.

Ali Meghdadi
Ali Meghdadi 2013-12-1
hi, I also have somehow the same problem. My constraint function has 8 parameters as input and gives an output and GA doesn't accept it. what can I do?
  1 个评论
Alan Weiss
Alan Weiss 2013-12-2
Please look at the accepted answer for the solution to your problem.
If that doesn't work, then provide more detail, such as the syntax you are using, and the error GA returns.
Alan Weiss
MATLAB mathematical toolbox documentation

请先登录,再进行评论。

类别

Help CenterFile 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!

Translated by