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)

Location:
trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3
Files:
3 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());
  • trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/ArtificialAntExpressionGrammar.cs

    r3294 r3338  
    3333
    3434    public ArtificialAntExpressionGrammar()
    35       : base(0, 3, 0, 3) {
     35      : base() {
    3636      Initialize();
    3737    }
     
    4545      var right = new Right();
    4646      var defun = new Defun();
    47       var invoke = new InvokeFunction();
    4847      var allSymbols = new List<Symbol>() { ifFoodAhead, prog2, prog3, move, left, right };
    4948      var nonTerminalSymbols = new List<Symbol>() { ifFoodAhead, prog2, prog3 };
    50       SetMinSubTreeCount(ifFoodAhead, 2);
    51       SetMaxSubTreeCount(ifFoodAhead, 2);
    52       SetMinSubTreeCount(prog2, 2);
    53       SetMaxSubTreeCount(prog2, 2);
    54       SetMinSubTreeCount(prog3, 3);
    55       SetMaxSubTreeCount(prog3, 3);
    56       SetMinSubTreeCount(move, 0);
    57       SetMaxSubTreeCount(move, 0);
    58       SetMinSubTreeCount(left, 0);
    59       SetMaxSubTreeCount(left, 0);
    60       SetMinSubTreeCount(right, 0);
    61       SetMaxSubTreeCount(right, 0);
    62       foreach (var sym in allSymbols) {
    63         AddAllowedSymbols(StartSymbol, 0, sym);
    64         AddAllowedSymbols(defun, 0, sym);
    6549
    66         for (int i = 0; i < GetMaxSubTreeCount(invoke); i++) {
    67           AddAllowedSymbols(invoke, i, sym);
    68         }
    69       }
    70       foreach (var sym in nonTerminalSymbols) {
    71         for (int argIndex = 0; argIndex < GetMaxSubTreeCount(sym); argIndex++) {
    72           AddAllowedSymbols(sym, argIndex, invoke);
    73         }
    74       }
    75       foreach (var nonTerminal in nonTerminalSymbols) {
    76         foreach (var child in allSymbols) {
    77           for (int argIndex = 0; argIndex < GetMaxSubTreeCount(nonTerminal); argIndex++) {
    78             AddAllowedSymbols(nonTerminal, argIndex, child);
     50      allSymbols.ForEach(s => AddSymbol(s));
     51      SetMinSubtreeCount(ifFoodAhead, 2);
     52      SetMaxSubtreeCount(ifFoodAhead, 2);
     53      SetMinSubtreeCount(prog2, 2);
     54      SetMaxSubtreeCount(prog2, 2);
     55      SetMinSubtreeCount(prog3, 3);
     56      SetMaxSubtreeCount(prog3, 3);
     57      SetMinSubtreeCount(move, 0);
     58      SetMaxSubtreeCount(move, 0);
     59      SetMinSubtreeCount(left, 0);
     60      SetMaxSubtreeCount(left, 0);
     61      SetMinSubtreeCount(right, 0);
     62      SetMaxSubtreeCount(right, 0);
     63
     64      // each symbols is allowed as child of the start symbol
     65      allSymbols.ForEach(s => SetAllowedChild(StartSymbol, s, 0));
     66
     67      // each symbol is allowed as child of all other symbols (except for terminals that have MaxSubtreeCount == 0
     68      foreach (var parent in allSymbols) {
     69        for (int argIndex = 0; argIndex < GetMaxSubtreeCount(parent); argIndex++) {
     70          foreach (var child in allSymbols) {
     71            SetAllowedChild(parent, child, argIndex);
    7972          }
    8073        }
    8174      }
    82 
    8375    }
    8476  }
  • trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/ArtificialAntProblem.cs

    r3294 r3338  
    151151      set { MaxExpressionDepthParameter.Value = value; }
    152152    }
     153    public IntValue MaxFunctionDefinitions {
     154      get { return MaxFunctionDefinitionsParameter.Value; }
     155      set { MaxFunctionDefinitionsParameter.Value = value; }
     156    }
     157    public IntValue MaxFunctionArguments {
     158      get { return MaxFunctionArgumentsParameter.Value; }
     159      set { MaxFunctionArgumentsParameter.Value = value; }
     160    }
    153161    public SymbolicExpressionTreeCreator SolutionCreator {
    154162      get { return SolutionCreatorParameter.Value; }
     
    168176      get { return EvaluatorParameter.Value; }
    169177    }
    170     public ISymbolicExpressionGrammar ArtificialAntExpressionGrammar {
    171       get { return ArtificialAntExpressionGrammarParameter.Value; }
     178    public GlobalSymbolicExpressionGrammar ArtificialAntExpressionGrammar {
     179      get { return (GlobalSymbolicExpressionGrammar)ArtificialAntExpressionGrammarParameter.Value; }
    172180    }
    173181    public ISingleObjectiveSolutionsVisualizer Visualizer {
     
    191199      SymbolicExpressionTreeCreator creator = new ProbabilisticTreeCreator();
    192200      Evaluator evaluator = new Evaluator();
    193       ArtificialAntExpressionGrammar grammar = new ArtificialAntExpressionGrammar();
    194201      BestAntTrailVisualizer visualizer = new BestAntTrailVisualizer();
    195202      BoolMatrix world = new BoolMatrix(santaFeAntTrail);
     203      ISymbolicExpressionGrammar grammar = new GlobalSymbolicExpressionGrammar(new ArtificialAntExpressionGrammar());
    196204      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to true as the Artificial Ant Problem is a maximization problem.", new BoolValue(true)));
    197205      Parameters.Add(new ValueParameter<SymbolicExpressionTreeCreator>("SolutionCreator", "The operator which should be used to create new artificial ant solutions.", creator));
    198206      Parameters.Add(new ValueParameter<Evaluator>("Evaluator", "The operator which should be used to evaluate artificial ant solutions.", evaluator));
    199207      Parameters.Add(new ValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this artificial ant instance.", new DoubleValue(89)));
    200       Parameters.Add(new ValueParameter<ISymbolicExpressionGrammar>("ArtificialAntExpressionGrammar", "The grammar that should be used for artificial ant expressions.", grammar));
    201208      Parameters.Add(new ValueParameter<IntValue>("MaxExpressionLength", "Maximal length of the expression to control the artificial ant.", new IntValue(100)));
    202209      Parameters.Add(new ValueParameter<IntValue>("MaxExpressionDepth", "Maximal depth of the expression to control the artificial ant.", new IntValue(10)));
    203210      Parameters.Add(new ValueParameter<IntValue>("MaxFunctionDefinitions", "Maximal number of automatically defined functions in the expression to control the artificial ant.", new IntValue(3)));
    204211      Parameters.Add(new ValueParameter<IntValue>("MaxFunctionArguments", "Maximal number of arguments of automatically defined functions in the expression to control the artificial ant.", new IntValue(3)));
     212      Parameters.Add(new ValueParameter<ISymbolicExpressionGrammar>("ArtificialAntExpressionGrammar", "The grammar that should be used for artificial ant expressions.", grammar));
    205213      Parameters.Add(new ValueParameter<BoolMatrix>("World", "The world for the artificial ant with scattered food items.", world));
    206214      Parameters.Add(new ValueParameter<IntValue>("MaxTimeSteps", "The number of time steps the artificial ant has available to collect all food items.", new IntValue(600)));
     
    292300      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
    293301      VisualizerParameter.ValueChanged += new EventHandler(VisualizerParameter_ValueChanged);
     302      MaxFunctionArgumentsParameter.ValueChanged += new EventHandler(MaxFunctionArgumentsParameter_ValueChanged);
     303      MaxFunctionArguments.ValueChanged += new EventHandler(MaxFunctionArgumentsParameter_ValueChanged);
     304      MaxFunctionDefinitionsParameter.ValueChanged += new EventHandler(MaxFunctionDefinitionsParameter_ValueChanged);
     305      MaxFunctionDefinitions.ValueChanged += new EventHandler(MaxFunctionDefinitionsParameter_ValueChanged);
     306    }
     307
     308    void MaxFunctionDefinitionsParameter_ValueChanged(object sender, EventArgs e) {
     309      ArtificialAntExpressionGrammar.MaxFunctionDefinitions = MaxFunctionDefinitions.Value;
     310    }
     311
     312    void MaxFunctionArgumentsParameter_ValueChanged(object sender, EventArgs e) {
     313      ArtificialAntExpressionGrammar.MaxFunctionArguments = MaxFunctionArguments.Value;
    294314    }
    295315
     
    348368        op.MaxFunctionArgumentsParameter.ActualName = MaxFunctionArgumentsParameter.Name;
    349369      }
     370      foreach (SymbolicExpressionTreeCreator op in Operators.OfType<SymbolicExpressionTreeCreator>()) {
     371        op.MaxFunctionArgumentsParameter.ActualName = MaxFunctionArgumentsParameter.Name;
     372        op.MaxFunctionDefinitionsParameter.ActualName = MaxFunctionDefinitionsParameter.Name;
     373      }
    350374    }
    351375
Note: See TracChangeset for help on using the changeset viewer.