Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Matlab Function Handle for GA

Subject: Matlab Function Handle for GA

From: Burak Ozsoy

Date: 17 Jan, 2012 17:01:08

Message: 1 of 2

Hi all,

Currently i am trying to run genetic algorithm optimization. I want to calculate fitness function and constraints in the same function. Let me summarize my problem

1- I have a main file which defines the main variables.
2- I have evalobjcon function to calculate my objectives and constraints
(I need to calculate them in the same file because of the computational effort issues)

i.e [fitness c ceq]=evalobj(variable1,variable2,......)

3- I have an optimization.m script to call evalobjcon, also i am using function handles to assign fitness function

[f c ceq] = @(x) evalobj(variable1, variable2, ...)
ObjectiveFunction = f;
ConstraintFunction = [c ceq];

Then i'm trying to run

[x,fval] = ga(ObjectiveFunction,nvars,[],[],[],[],LB,UB,ConstraintFunction,[],options);


The problem is when i run ga code after defining bound and options when i run the code it gives an error like

Error : Only functions can return multiple values.

What is the solution for this issue.

Thanks for your time and help.

Subject: Matlab Function Handle for GA

From: Alan Weiss

Date: 17 Jan, 2012 19:40:02

Message: 2 of 2

On 1/17/2012 12:01 PM, Burak Ozsoy wrote:
> Hi all,
>
> Currently i am trying to run genetic algorithm optimization. I want to
> calculate fitness function and constraints in the same function. Let me
> summarize my problem
>
> 1- I have a main file which defines the main variables. 2- I have
> evalobjcon function to calculate my objectives and constraints
> (I need to calculate them in the same file because of the computational
> effort issues)
>
> i.e [fitness c ceq]=evalobj(variable1,variable2,......)
>
> 3- I have an optimization.m script to call evalobjcon, also i am using
> function handles to assign fitness function
> [f c ceq] = @(x) evalobj(variable1, variable2, ...)
> ObjectiveFunction = f;
> ConstraintFunction = [c ceq];
>
> Then i'm trying to run
>
> [x,fval] =
> ga(ObjectiveFunction,nvars,[],[],[],[],LB,UB,ConstraintFunction,[],options);
>
>
>
> The problem is when i run ga code after defining bound and options when
> i run the code it gives an error like
>
> Error : Only functions can return multiple values.
>
> What is the solution for this issue.
>
> Thanks for your time and help.

See this thread:
http://www.mathworks.com/matlabcentral/newsreader/view_thread/269936#707304

Alan Weiss
MATLAB mathematical toolbox documentation

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us