Custom data types and constraints with genetic algorithms

5 次查看(过去 30 天)
Hello everybody,
I'm trying to implement a genetic algorithm that works with custom data types in MATLAB. Each chromosome of the population is made of two parts: a string and a vector of integers.
The string part is the binary representation of a number and I need to perform admissibility checks over it after crossover and mutation operations in order to avoid that the number represented could exceed a certain value.
I've followed this example for working with custom data types but I still have a question: how do I pass the max value as parameter to crossover and mutation functions? And, more generally, is there a way to pass any extra parameter to these functions?
For now I'm using global variables, but this feels a very bad idea and I'd like to find out which is the right way to do what I need.
Thanks in advance!

回答(1 个)

Shlok
Shlok 2024-8-21
编辑:Shlok 2024-8-21
Hi nc,
I understand that you want to pass additional parameters, to functions such as “crossover” and “mutation” in your genetic algorithm. While using global variables might work, there's a more elegant and safer approach: using anonymous functions.
Anonymous functions allow you to wrap your existing functions and pass extra parameters seamlessly. Let’s assume you want to pass “maxValue” as an additional parameter. Here’s how you can modify your code to include “maxValue” for both the “crossover” and “mutation” functions:
maxValue = 50; % Define your maxValue here
options = optimoptions(options, ...
'CrossoverFcn', @(parents, options, NVARS, FitnessFcn, thisScore, thisPopulation) ...
crossover_permutation(parents, options, NVARS, FitnessFcn, thisScore, thisPopulation, maxValue), ... % passing maxValue here
'MutationFcn', @(parents, options, NVARS, FitnessFcn, state, thisScore, thisPopulation, mutationRate) ...
mutate_permutation(parents, options, NVARS, FitnessFcn, state, thisScore, thisPopulation, mutationRate, maxValue), ... % passing maxValue here
% Other options as needed
);
In this modification:
  • The “CrossoverFcn” and “MutationFcn” are now anonymous functions. They call your existing “crossover_permutation” and “mutate_permutation” functions, respectively, while also passing “maxValue”.
  • Ensure your “crossover_permutation” and “mutate_permutation” functions are updated to accept this new parameter.
For example:
function xoverKids = crossover_permutation(parents,options,NVARS,FitnessFcn,thisScore,thisPopulation,maxValue)
% Your custom crossover function
% Use maxValue here
% ...
end
This approach not only allows you to pass “maxValue”, but also any other additional parameters you might need, in a clean and modular way.
For more details on anonymous functions, you can refer to the following documentation:
Hope it helps.

类别

Help CenterFile Exchange 中查找有关 Genetic Algorithm 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by