Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to split two nested lists and combine the parts to create two new nested lists
    primarykey
    data
    text
    <p>I'm trying to code a simple genetic programming utility in python. But right now I'm stuck at the crossover/mate function for my trees. The trees are built by nested lists and look something like this:</p> <pre><code># f = internal node (a function), c = leaf node (a constant) tree1 = [f, [f, [f, c, c], [f, c, c]], [f, [f, c, c], [f, c, c]]] tree2 = [f, [f, [f, c, c], c], [f, [f, c, c], c]] </code></pre> <p>I want to randomly select a point in each tree to split at and then I want one part from each tree to be combined into a new tree. There is also a max depth that shouldn't be exceeded so the selects can't really take place anywhere in the tree as it might create a too large tree. Below is an example on how it should work:</p> <pre><code># f:n, where n is the number of arguments the function take # + split here tree1 = [f:2, [f:3, a, a, a], a] # + split here tree2 = [f:2, [f:2, a, a], [f:1, a] tree_child1 = [f:2, [f:1, a], a] tree_child2 = [f:2, [f:2, a, a], [f:3, a, a, a]] </code></pre> <p>I have no idea (at the moment) on how to solve this. Any tips or solutions are more than welcome!</p> <p>(Added my parse function as it might help someone to understand the structure better.)</p> <pre><code># My recursive code to parse the tree. def parse(self, node=None): if not node: node = self.root if isinstance(node, list): function = node[0] res = [] for child in node[1:function.arity+1]: res.append(self.parse(child)) value = function.parse(*res) # function else: value = node.parse() # constant return value </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload