How can I modify the FitnessFunction when doing optimization?

3 Ansichten (letzte 30 Tage)
Tanvir Kaisar
Tanvir Kaisar am 21 Mär. 2017
Kommentiert: Tanvir Kaisar am 21 Mär. 2017
Say my problem is to maximize y= r*x(1)+(x(2))^2 using Genetic Algorithm (with constraints). But my assignment is to do this optimization 100 times with r=1:100.So after the 1st result r will change 1 to 2, then another result and so on.. How can i do this in the most efficient way?
  2 Kommentare
Alan Weiss
Alan Weiss am 21 Mär. 2017
I think that the most efficient way is to write down the solution without using the genetic algorithm. If you really want to maximize that function on an unbounded domain then the answer is infinity. If you have a bounded domain, well, it is again easy to calculate. And if you really have a minimization problem, then again it is easy to calculate the optimum. Have the genetic algorithm find the value of a function that always returns 0, and add it to the calculated solution.
If you meant something else, then please explain what you really want.
Alan Weiss
MATLAB mathematical toolbox documentation
Tanvir Kaisar
Tanvir Kaisar am 21 Mär. 2017
Thanks for your comment... I am sorry that I have not mentioned that the objective function has constraints and bounded. I have to optimize it 100 times (with changing r values)..I am guessing I may have to use some kind of loop...but i dont know how should i approach

Melden Sie sich an, um zu kommentieren.

Antworten (1)

John D'Errico
John D'Errico am 21 Mär. 2017
Bearbeitet: John D'Errico am 21 Mär. 2017
Easy enough.
You could simply create the objective function inside a loops, as such...
for r = 1:100
fun = @(x) r*x(1)+(x(2))^2;
% do optimization here:
...
end
Each time through the loop, the current value of r was used to create the objective function, overwriting the function handle with a new one.
Or, I might have written it like this.
fun = @(x,r) r*x(1)+(x(2))^2;
for r = 1:100
fr = @(x) fun(x,r);
% do optimization here:
...
end
In the second form, I create a two variable function, then inside the loop, I create a second function that calls the first, but with a fixed value of r, set at the current value.
Be careful, as your objective is to maximize. Most optimizers in MATLAB are MINIMIZATION tools. You can convert a maximization problem into a minimization problem by negating the objective function.

Community Treasure Hunt

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

Start Hunting!

Translated by