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:
GA: extra parameters for fitness function

Subject: GA: extra parameters for fitness function

From: SabbeRubbish

Date: 6 Dec, 2008 18:13:32

Message: 1 of 5

Hi,
I'm new to the forums, I hope you can help me.
Is there any way to give ga (or gatool) an extra parameter/argument other than the variables to be optimized?

For example, I have a fitness function that calculates the contrast of an image. I want to maximize the contrast of a grayscale image based on the color image, using the R, G and B factors as multipliers for the red, green and blue subimages respectively.

It works fine if I hardcode the image file into the fitness function, but I'd rather be able to parametrize like e.g.:

ga(@optimizeGrayscale(3, [], [], ones(3,3), [1 1 1], [0 0 0], [1 1 1], 'firstParam', 'fileName')

Where [] is A and b, ones(3,3) is Aeq, [1 1 1] is beq, [0 0 0] is LB and [1 1 1] is UB.

Is it possible to give an extra parameters when referencing functions like @function?

Thanks for any help.

SabbeRubbish

Subject: GA: extra parameters for fitness function

From: SabbeRubbish

Date: 7 Dec, 2008 15:07:29

Message: 2 of 5

I've found a solution to this problem, albeit not a very pretty one: using global variables (I know, it's bad practice but in this particular case I couldn't get around it, on such short notice).

So I just declare my fileName or colorImage as global:
global colorImage;
in both files (calling function and called fitness function).

Subject: GA: extra parameters for fitness function

From: Dave Brackett

Date: 7 Dec, 2008 21:34:02

Message: 3 of 5

SabbeRubbish <SabbeRubbish@gmail.com> wrote in message <19810651.1228662479564.JavaMail.jakarta@nitrogen.mathforum.org>...
> I've found a solution to this problem, albeit not a very pretty one: using global variables (I know, it's bad practice but in this particular case I couldn't get around it, on such short notice).
>
> So I just declare my fileName or colorImage as global:
> global colorImage;
> in both files (calling function and called fitness function).

Try to avoid using global variables where possible as they can cause problems depending on what your code does. To pass variables to a GA the fitness function must be specified as shown in the example below:

FitnessFcn=@(x)m_file_fitness_calc(x,other variables...)

then the ga is called using as: ga(FitnessFcn,other options...) etc.

hope that helps.

Subject: GA: extra parameters for fitness function

From: Alan Weiss

Date: 8 Dec, 2008 02:47:02

Message: 4 of 5

SabbeRubbish wrote:
> I've found a solution to this problem, albeit not a very pretty one: using global variables (I know, it's bad practice but in this particular case I couldn't get around it, on such short notice).
>
> So I just declare my fileName or colorImage as global:
> global colorImage;
> in both files (calling function and called fitness function).

Hi, I'm really sorry you are having this problem. I tried to make it
much more clear how to do this in the function reference page for ga.
There is a big note there that points out a section in the Optimization
Toolbox(TM) User's Guide on how to use anonymous functions or nested
functions to do this without using global variables:
http://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/brhkghv-7.html

Can you please tell me how I might make this information more visible to
customers? It is a common problem, and I struggle with informing people.

Alan Weiss
MATLAB mathematical toolbox documentation

Subject: GA: extra parameters for fitness function

From: SabbeRubbish

Date: 8 Dec, 2008 16:25:32

Message: 5 of 5

Maybe state what davebracket replied to my post.
It helps to see an extra example to show how you can use it in ga. Global variables did do the trick and it's not a very critical application.

Thanks for your concerns :)

SabbeRubbish

Tags for this Thread

No tags are associated with 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