public static void Shake(IRandom random, ISymbolicExpressionTree tree, double shakingFactor) {
   List<ISymbolicExpressionTreeNode> parametricNodes = new List<ISymbolicExpressionTreeNode>();
   tree.Root.ForEachNodePostfix(n => {
     if (n.HasLocalParameters) parametricNodes.Add(n);
   });
   if (parametricNodes.Count > 0) {
     var selectedPoint = parametricNodes.SampleRandom(random);
     selectedPoint.ShakeLocalParameters(random, shakingFactor);
   }
 }
Example #2
0
    /// <summary>
    /// Genotype-to-Phenotype mapper (iterative random approach, where the next non-terminal
    /// symbol to expand is randomly determined).
    /// </summary>
    /// <param name="startNode">first node of the tree with arity 1</param>
    /// <param name="genotype">integer vector, which should be mapped to a tree</param>
    /// <param name="grammar">grammar to determine the allowed child symbols for each node</param>
    /// <param name="maxSubtreeCount">maximum allowed subtrees (= number of used genomes)</param>
    /// <param name="random">random number generator</param>
    private void MapRandomIteratively(ISymbolicExpressionTreeNode startNode,
                                     IntegerVector genotype,
                                     ISymbolicExpressionGrammar grammar,
                                     int maxSubtreeCount, IRandom random) {

      List<ISymbolicExpressionTreeNode> nonTerminals = new List<ISymbolicExpressionTreeNode>();

      int genotypeIndex = 0;
      nonTerminals.Add(startNode);

      while (nonTerminals.Count > 0) {
        if (genotypeIndex >= maxSubtreeCount) {
          // if all genomes were used, only add terminal nodes to the remaining subtrees
          ISymbolicExpressionTreeNode current = nonTerminals[0];
          nonTerminals.RemoveAt(0);
          current.AddSubtree(GetRandomTerminalNode(current, grammar, random));
        } else {
          // similar to PIGEMapper, but here the current node is determined randomly ...
          ISymbolicExpressionTreeNode current = nonTerminals.SampleRandom(random);
          nonTerminals.Remove(current);

          ISymbolicExpressionTreeNode newNode = GetNewChildNode(current, genotype, grammar, genotypeIndex, random);
          int arity = SampleArity(random, newNode, grammar);

          current.AddSubtree(newNode);
          genotypeIndex++;
          // new node has subtrees, so add "arity" number of copies of this node to the nonTerminals list
          for (int i = 0; i < arity; ++i) {
            nonTerminals.Add(newNode);
          }
        }
      }
    }