- Define the Step Size: Decide the step size for each variable. For example, if you want a1 to vary in steps of 0.1 and a2 in steps of 0.05, define these step sizes.
- Transform the Problem: Scale the bounds so that the GA operates in an integer space. For example, if your range is [2.5, 3.9] and the step size is 0.1, this can be transformed to [25, 39] in integer space.
- Modify the Fitness Function: Scale the variables back to their original range inside your fitness function.
- Set GA Options: Use 'PopulationType', 'doubleVector' and 'UseInteger', false options.
How do I set interval values for a Population Range in GA?
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I'm using a genetic algorithm to reach the best config for my project. For that, I've defined the limits of my functions using LB and UB, where they are the Lower Bound and the Upper Bound, respectively. Here's the example:
a1 = [2.5 3.9]
a2 = [0.1 0.4]
LB = [a1(1) a2(1)]
UB = [a1(2) a2(2)]
In my options tab for the GA, I'm using the setting:
options = gaoptimset(options,'PopInitRange', [LB;UB;]);
However, when I run the GA, it uses a ridiculous amount of data to calculate, as it runs for every 0.0001 value within the set range. For example, instead of using 2.5, then 2.6 and 2.7, it uses 2.5000, then 2.5001 and 2.5002, making it extremely slow and using a precision that's not going to be used in real life.
I would like to know if there's a way for me to set the intervals in which the values will variate so that I can run my code faster, as it is taking too long.
0 comentarios
Respuestas (1)
Shubh
el 22 de En. de 2024
Hi Pedro,
In MATLAB's Genetic Algorithm (GA) toolbox, you can control the precision of the variables by setting the 'PopulationType' and 'UseInteger' options in 'gaoptimset'. However, these settings might not provide the exact level of control you're looking for, such as specifying the exact step size (e.g., 0.1) for the variation of values. The GA in MATLAB typically works with floating-point numbers and doesn't natively support specifying step sizes for the variables.
A workaround for this issue is to scale and discretize your problem. Essentially, you transform your continuous variables into discrete ones by defining a step size and then scale them back during the evaluation of the fitness function. Here’s how you can do it:
Here's an example of how you can implement this in MATLAB:
% Original bounds
a1 = [2.5, 3.9];
a2 = [0.1, 0.4];
% Step sizes
stepSizeA1 = 0.1;
stepSizeA2 = 0.05;
% Transform bounds to integer space
LB = [a1(1)/stepSizeA1, a2(1)/stepSizeA2];
UB = [a1(2)/stepSizeA1, a2(2)/stepSizeA2];
% GA options
options = gaoptimset('PopulationType', 'doubleVector', 'UseInteger', false);
% Fitness function
fitnessFunction = @(x) myFitnessFunction(x(1)*stepSizeA1, x(2)*stepSizeA2);
% Running GA
[x, fval] = ga(fitnessFunction, 2, [], [], [], [], LB, UB, [], options);
function score = myFitnessFunction(a1, a2)
% Your fitness function here, using the scaled variables
% ...
end
In this code, 'myFitnessFunction' should be your original fitness function but it now takes the scaled variables as input. Inside this function, you'll use 'a1' and 'a2' as they are, since they're already scaled back to their original range.
This method allows the GA to operate in a discretized space with your specified step sizes, potentially speeding up the optimization process. However, keep in mind that discretizing the problem can sometimes affect the ability of the GA to find the global optimum, depending on the nature of the fitness landscape.
Hope this helps!
1 comentario
Sam Chak
el 22 de En. de 2024
I'm unsure about how to apply the step size. Could you please modify your code for this simple problem? It will help enhance the value of your answer and increase the likelihood of it being accepted by the OP.
% Original bounds
a1 = [2.5, 3.9];
a2 = [0.1, 0.4];
% Step sizes
stepSizeA1 = 0.1;
stepSizeA2 = 0.05;
% Transform bounds to integer space
% LB = [a1(1)/stepSizeA1, a2(1)/stepSizeA2] % [25, 2]??
% UB = [a1(2)/stepSizeA1, a2(2)/stepSizeA2] % [39, 8]??
LB = [a1(1), a2(1)];
UB = [a1(2), a2(2)];
% GA options
% options = gaoptimset('PopulationType', 'doubleVector', 'UseInteger', false);
% Fitness function
fitnessFunction = @(x) myFitnessFunction(x(1)*stepSizeA1, x(2)*stepSizeA2);
% Running GA
% [x, fval] = ga(fitnessFunction, 2, [], [], [], [], LB, UB, [], options);
[x, fval] = ga(@myFitnessFunction, 2, [], [], [], [], LB, UB, [])
function score = myFitnessFunction(param)
x1 = param(1);
x2 = param(2);
score = (x1 - 3.1)^2 + (x2 - 0.25)^2;
end
Ver también
Categorías
Más información sobre Genetic Algorithm en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!