필터 지우기
필터 지우기

genetic algorithm coding help error no enough input arguments?

조회 수: 1 (최근 30일)
Arunachalam  D
Arunachalam D 2015년 4월 3일
답변: Alan Weiss 2015년 4월 6일
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