Borrar filtros
Borrar filtros

Crossover function in gene expression programming

3 visualizaciones (últimos 30 días)
LONG TRAN
LONG TRAN el 2 de Abr. de 2023
Respondida: Rishav el 3 de Mayo de 2023
Hi, I want to write a crossover function for GEP, right now i am stuck at the part replacing the subtree into the designated crossover point, I have tried chatgpt numerous times but it keeps on assign old_tree to new_tree which makes the crossover function returns old_tree, here is my crossover code:
function new_tree = set_subtree(old_tree, crossover_point, subtree)
if crossover_point == 1
new_tree = subtree;
return
end
queue = {old_tree};
idx = 1;
while ~isempty(queue)
node = queue{1};
queue = queue(2:end);
if isfield(node, 'children')
for i = 1:length(node.children)
idx = idx + 1;
if idx == crossover_point
node.children{i} = subtree;
new_tree = old_tree;
return
end
queue{end+1} = node.children{i};
end
end
end
error('Crossover point not found in expression tree');
end
Thank you for your time.

Respuestas (1)

Rishav
Rishav el 3 de Mayo de 2023
Hi LONG,
To fix this issue, you can create a copy of old_tree before modifying it, and then assign the modified tree to the new copy.
Here is an updated code by changing the set_subtree function if index equals crossover point:
if idx == crossover_point
% Create a copy of the old tree
new_tree = old_tree;
% Replace the subtree at the crossover point
new_tree.children{i} = subtree;
return
end
In this updated version, new_tree is initialized to a copy of old_tree before the subtree is replaced at the designated crossover point. This ensures that new_tree is not simply a reference to old_tree, but a new copy that includes the modified subtree.
Regards,
Rishav Saha

Categorías

Más información sobre App Building en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by