genetic algorithm coding help error no enough input arguments?
1 回表示 (過去 30 日間)
古いコメントを表示
Objective function
function z=my_fun(a,b,c)
z=a+2*b+56*c+100;
constraint function
function [c]=const(a,b,c)
c=[6<=a<=100;2<=b<=4;2<=c<=4];
Main program
clear all
clc
nvars=3;
lb=[6;2;2];
ub=[100;4;4];
[population_1 fval]=ga(my_fun,nvars,[],[],[],[],[],[],lb,ub,const)
Error when running
Error using my_fun (line 2)
Not enough input arguments.
Error in start (line 6)
[population_1 fval]=ga(my_fun,3,[],[],[],[],[],[],lb,ub,const)
please help me to code sir..
0 件のコメント
採用された回答
Alan Weiss
2015 年 4 月 6 日
In addition to what Geoff said, do NOT use your nonlinear constraint function. It appears that you are trying to enforce bounds on your variables. Use bounds to do that, NOT poorly-written nonlinear constraints. I say poorly-written because you do not obey the syntax of nonlinear constraints.
Alan Weiss
MATLAB mathematical toolbox documentation
0 件のコメント
その他の回答 (1 件)
Geoff Hayes
2015 年 4 月 3 日
Arunachalam - your function has to many input arguments. Look at the description for the fitness function. It requires a single row vector whose size is determined by the number of variables that you are optimizing over (your function has three inputs). Try changing it to the following
function z=my_fun(chromosomes)
a = chromosomes(1);
b = chromosomes(2);
c = chromosomes(3);
z=a+2*b+56*c+100;
2 件のコメント
Geoff Hayes
2015 年 4 月 4 日
編集済み: Geoff Hayes
2015 年 4 月 4 日
Arunchalam's answer moved here
Geoff Hayes getting error
Error using my_fun (line 2)
Not enough input arguments.
Error in start (line 6)
[x fval]=ga(my_fun,nvars,[],[],[],[],[],[],lb,ub,const)
Geoff Hayes
2015 年 4 月 4 日
Arunachalam - yes, you are observing that error message because your my_fun fitness function has too many input arguments. The genetic algorithm calls your fitness function and tries to pass just one array of chromosomes (one element for each variable that you are optimizing over). Your function expects three, and so the error occurs.
Change your fitness function to that which I described in my answer and you should be able to proceed.
参考
カテゴリ
Help Center および File Exchange で Genetic Algorithm についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!