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..

采纳的回答

Alan Weiss
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

更多回答(1 个)

Geoff Hayes
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
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
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 CenterFile Exchange 中查找有关 Genetic Algorithm 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by