genetic algorithm coding help error no enough input arguments?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Arunachalam D
le 3 Avr 2015
Réponse apportée : Alan Weiss
le 6 Avr 2015
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 commentaires
Réponse acceptée
Alan Weiss
le 6 Avr 2015
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 commentaires
Plus de réponses (1)
Geoff Hayes
le 3 Avr 2015
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 commentaires
Geoff Hayes
le 4 Avr 2015
Modifié(e) : Geoff Hayes
le 4 Avr 2015
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
le 4 Avr 2015
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.
Voir également
Catégories
En savoir plus sur Genetic Algorithm dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!