フィルターのクリア

Crossover function in gene expression programming

4 ビュー (過去 30 日間)
LONG TRAN
LONG TRAN 2023 年 4 月 2 日
回答済み: Rishav 2023 年 5 月 3 日
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.

回答 (1 件)

Rishav
Rishav 2023 年 5 月 3 日
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

カテゴリ

Help Center および File ExchangeGraphics Object Programming についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by