Azzera filtri
Azzera filtri

genetic algorithm coding help error no enough input arguments?

1 visualizzazione (ultimi 30 giorni)
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..

Risposta accettata

Alan Weiss
Alan Weiss il 6 Apr 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

Più risposte (1)

Geoff Hayes
Geoff Hayes il 3 Apr 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 Commenti
Geoff Hayes
Geoff Hayes il 4 Apr 2015
Modificato: Geoff Hayes il 4 Apr 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
Geoff Hayes il 4 Apr 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.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by