Matlab > Genetic annotator > HELP

HELP > Coding and Minimizing a Fitness Function Using the Genetic Algorithm > Minimizing Using Additional Arguments
Following line has been given, but it don't tell where to define these values? I tried to write in its fitness function and I tried to enter these values at different places in GA in matlab.
a = 100; b = 1; % define constant values
Please let me know, where to enter these constant values?

Answers (1)

Define your function as taking two additional arguments, a, and b. Then when you call ga, instead of just giving a handle to your function, like
ga(@myObjectiveFunction, ....)
call
a = 100;
b = 1;
ga(@(x) myObjectiveFunction(x,a,b), .....)

5 Comments

Program not running: -
I entered this code, it shows no errors: -
function y = parameterized_fitness(x,a,b)
y = a * (x(1)^2 - x(2)) ^2 + (b-x(1))^2;
a=100;
b=1;
ga(@(x) parameterized_fitness(x,a,b));
end
In output I entered
Fitness function: @(x) parameterized_fitness(x,a,b)
Number of variables: 2
I get the following output: -
Optimization running.
Undefined function or variable 'a'.
I am new to matlab, please explain me how to do it right?
You put your ga call in your objective function. Don't do that. Do this instead:
function y = parameterized_fitness(x,a,b)
y = a * (x(1)^2 - x(2)) ^2 + (b-x(1))^2;
end
Then in MATLAB, do this:
a = 100;
b = 1;
[x,fval,exitflag] = ga(@(x)parameterized_fitness(x,a,b),2);
Alan Weiss
MATLAB mathematical toolbox documentation
Inderjeet Singh
Inderjeet Singh on 21 Jan 2016
Edited: Inderjeet Singh on 21 Jan 2016
I have inserted an image. I wrote it in fitness function and it still gives error: -
Error in fitnessfcn: Error: The expression to the left of the equals sign is not a valid target for an assignment.
I tried to enter value of a and b in linear inequalities, it still gave same error.
I tried to write it in matlab as shown in image and it gave errors too.
You saw that the first time you ran the code it worked fine. You saw that the second time there was an error. What was the difference between these two times? Look hard at the end of the ga call, you will see that the time it worked was when you had a ,2 toward the end of the line. What does this 2 mean? Look at the syntax for ga and see if you can figure it out.
Alan Weiss
MATLAB mathematical toolbox documentation

Sign in to comment.

Asked:

on 21 Jan 2016

Commented:

on 21 Jan 2016

Community Treasure Hunt

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

Start Hunting!