Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/13/10 20:44:31 (14 years ago)
Author:
gkronber
Message:

Fixed bugs related to dynamic symbol constraints with ADFs. #290 (Implement ADFs)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/AntInterpreter.cs

    r3294 r3338  
    2929namespace HeuristicLab.Problems.ArtificialAnt {
    3030  public class AntInterpreter {
     31
    3132    public int MaxTimeSteps { get; set; }
    3233    public int FoodEaten { get; set; }
     
    4849      }
    4950    }
     51
     52    private SymbolicExpressionTreeNode FindMatchingFunction(string name) {
     53      foreach (var defunBranch in expression.Root.SubTrees.OfType<DefunTreeNode>()) {
     54        if (defunBranch.FunctionName == name) return defunBranch;
     55      }
     56      throw new ArgumentException("Function definition for " + name + " not found.");
     57    }
     58
     59
     60
    5061    public int ElapsedTime { get; set; }
    5162    private int currentDirection;
     
    8192    public void Step() {
    8293      // expression evaluated completly => start at root again
    83       if (nodeStack.Count == 0)
     94      if (nodeStack.Count == 0) {
    8495        nodeStack.Push(Expression.ResultProducingExpression);
     96      }
     97
    8598      var currentNode = nodeStack.Pop();
    8699      if (currentNode.Symbol is Left) {
     
    116129      } else if (currentNode.Symbol is InvokeFunction) {
    117130        var invokeNode = currentNode as InvokeFunctionTreeNode;
    118         var funBranch = (from node in expression.Root.SubTrees
    119                          let funNode = node as DefunTreeNode
    120                          where funNode != null
    121                          where funNode.Name == invokeNode.InvokedFunctionName
    122                          select funNode).FirstOrDefault();
    123         if (funBranch == null) throw new InvalidOperationException("Can't find definition of function " + invokeNode.InvokedFunctionName);
    124         nodeStack.Push(funBranch.SubTrees[0]);
    125         foreach (var subTree in invokeNode.SubTrees)
    126           nodeStack.Push(subTree);
    127       } else if(currentNode.Symbol is Argument) {
    128         // do nothing
     131        var functionDefinition = (SymbolicExpressionTreeNode)FindMatchingFunction(invokeNode.InvokedFunctionName).Clone();
     132        var argumentCutPoints = (from node in functionDefinition.IterateNodesPrefix()
     133                                 where node.SubTrees.Count > 0
     134                                 from subtree in node.SubTrees
     135                                 where subtree is ArgumentTreeNode
     136                                 select new { Parent = node, Argument = subtree.Symbol as Argument, ChildIndex = node.SubTrees.IndexOf(subtree) }).ToList();
     137        foreach (var cutPoint in argumentCutPoints) {
     138          cutPoint.Parent.RemoveSubTree(cutPoint.ChildIndex);
     139          cutPoint.Parent.InsertSubTree(cutPoint.ChildIndex, (SymbolicExpressionTreeNode)invokeNode.SubTrees[cutPoint.Argument.ArgumentIndex].Clone());
     140        }
     141        nodeStack.Push(functionDefinition.SubTrees[0]);
    129142      } else {
    130143        throw new InvalidOperationException(currentNode.Symbol.ToString());
Note: See TracChangeset for help on using the changeset viewer.