Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/09/10 17:28:32 (14 years ago)
Author:
gkronber
Message:

Added first version of architecture altering operators for ADFs. #290 (Implement ADFs)

Location:
trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3
Files:
9 edited

Legend:

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

    r3239 r3294  
    2121
    2222using System;
     23using System.Linq;
    2324using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2425using HeuristicLab.Data;
    2526using System.Collections.Generic;
     27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.GeneralSymbols;
    2628
    2729namespace HeuristicLab.Problems.ArtificialAnt {
     
    8082      // expression evaluated completly => start at root again
    8183      if (nodeStack.Count == 0)
    82         nodeStack.Push(Expression.Root.SubTrees[0]);
     84        nodeStack.Push(Expression.ResultProducingExpression);
    8385      var currentNode = nodeStack.Pop();
    8486      if (currentNode.Symbol is Left) {
     
    112114        nodeStack.Push(currentNode.SubTrees[0]);
    113115        return;
     116      } else if (currentNode.Symbol is InvokeFunction) {
     117        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
    114129      } else {
    115130        throw new InvalidOperationException(currentNode.Symbol.ToString());
  • trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/ArtificialAntExpressionGrammar.cs

    r3257 r3294  
    3030namespace HeuristicLab.Problems.ArtificialAnt {
    3131  [StorableClass]
    32   public class ArtificialAntExpressionGrammar : Item, ISymbolicExpressionGrammar {
     32  public class ArtificialAntExpressionGrammar : DefaultSymbolicExpressionGrammar {
    3333
    3434    public ArtificialAntExpressionGrammar()
    35       : base() {
    36     }
    37     #region ISymbolicExpressionGrammar Members
    38 
    39     [Storable]
    40     private StartSymbol startSymbol = new StartSymbol();
    41     public Symbol StartSymbol {
    42       get { return startSymbol; }
     35      : base(0, 3, 0, 3) {
     36      Initialize();
    4337    }
    4438
    45     [Storable]
    46     private static List<Symbol> allSymbols = new List<Symbol>() {
    47       new IfFoodAhead(),
    48       new Prog2(),
    49       new Prog3(),
    50       new Move(),
    51       new Left(),
    52       new Right()
    53     };
    54     [Storable]
    55     private Dictionary<Type, Dictionary<int, IEnumerable<Symbol>>> allowedSymbols = new Dictionary<Type, Dictionary<int, IEnumerable<Symbol>>>() {
    56       {
    57         typeof(StartSymbol),
    58         new Dictionary<int, IEnumerable<Symbol>>()
    59         {
    60           { 0, allSymbols},
     39    private void Initialize() {
     40      var ifFoodAhead = new IfFoodAhead();
     41      var prog2 = new Prog2();
     42      var prog3 = new Prog3();
     43      var move = new Move();
     44      var left = new Left();
     45      var right = new Right();
     46      var defun = new Defun();
     47      var invoke = new InvokeFunction();
     48      var allSymbols = new List<Symbol>() { ifFoodAhead, prog2, prog3, move, left, right };
     49      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);
     65
     66        for (int i = 0; i < GetMaxSubTreeCount(invoke); i++) {
     67          AddAllowedSymbols(invoke, i, sym);
    6168        }
    62       },      {
    63         typeof(IfFoodAhead),
    64         new Dictionary<int, IEnumerable<Symbol>>()
    65         {
    66           { 0, allSymbols},
    67           { 1, allSymbols}
     69      }
     70      foreach (var sym in nonTerminalSymbols) {
     71        for (int argIndex = 0; argIndex < GetMaxSubTreeCount(sym); argIndex++) {
     72          AddAllowedSymbols(sym, argIndex, invoke);
    6873        }
    69       },
    70       {
    71         typeof(Prog2),
    72         new Dictionary<int, IEnumerable<Symbol>>()
    73         {
    74           { 0, allSymbols},
    75           { 1, allSymbols}
     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);
     79          }
    7680        }
    77       },
    78       {
    79         typeof(Prog3),
    80         new Dictionary<int, IEnumerable<Symbol>>()
    81         {
    82           { 0, allSymbols},
    83           { 1, allSymbols},
    84           { 2, allSymbols}
    85         }
    86       },
    87     };
    88     public IEnumerable<Symbol> AllowedSymbols(Symbol parent, int argumentIndex) {
    89       return allowedSymbols[parent.GetType()][argumentIndex];
    90     }
     81      }
    9182
    92     [Storable]
    93     private Dictionary<Type, int> minLength = new Dictionary<Type, int>() {
    94       {typeof(StartSymbol), 1},
    95       {typeof(IfFoodAhead), 3},
    96       {typeof(Prog2), 3},
    97       {typeof(Prog3), 4},
    98       {typeof(Move), 1},
    99       {typeof(Left), 1},
    100       {typeof(Right), 1}
    101     };
    102     public int MinimalExpressionLength(Symbol start) {
    103       return minLength[start.GetType()];
    104     }
    105 
    106     [Storable]
    107     private Dictionary<Type, int> maxLength = new Dictionary<Type, int>() {
    108       {typeof(StartSymbol), int.MaxValue},
    109       {typeof(IfFoodAhead), int.MaxValue},
    110       {typeof(Prog2), int.MaxValue},
    111       {typeof(Prog3), int.MaxValue},
    112       {typeof(Move), 1},
    113       {typeof(Left), 1},
    114       {typeof(Right), 1}
    115     };
    116     public int MaximalExpressionLength(Symbol start) {
    117       return maxLength[start.GetType()];
    118     }
    119 
    120     [Storable]
    121     private Dictionary<Type, int> minDepth = new Dictionary<Type, int>() {
    122       {typeof(StartSymbol), 1},
    123       {typeof(IfFoodAhead), 1},
    124       {typeof(Prog2), 1},
    125       {typeof(Prog3), 1},
    126       {typeof(Move), 0},
    127       {typeof(Left), 0},
    128       {typeof(Right), 0}
    129     };
    130     public int MinimalExpressionDepth(Symbol start) {
    131       return minDepth[start.GetType()];
    132     }
    133 
    134 
    135     [Storable]
    136     private Dictionary<Type, int> subTrees = new Dictionary<Type, int>() {
    137       {typeof(StartSymbol), 1},
    138       {typeof(IfFoodAhead), 2},
    139       {typeof(Prog2), 2},
    140       {typeof(Prog3), 3},
    141       {typeof(Move), 0},
    142       {typeof(Left), 0},
    143       {typeof(Right), 0}
    144     };
    145     public int MinSubTrees(Symbol start) {
    146       return subTrees[start.GetType()];
    147     }
    148     public int MaxSubTrees(Symbol start) {
    149       return subTrees[start.GetType()];
    150     }
    151 
    152     #endregion
    153 
    154     #region ISymbolicExpressionGrammar Members
    155 
    156 
    157     public bool IsValidExpression(SymbolicExpressionTree expression) {
    158       if (expression.Root.Symbol != StartSymbol) return false;
    159       return IsValidExpression(expression.Root);
    160     }
    161 
    162     #endregion
    163 
    164     private bool IsValidExpression(SymbolicExpressionTreeNode root) {
    165       if (root.SubTrees.Count < MinSubTrees(root.Symbol)) return false;
    166       if (root.SubTrees.Count > MaxSubTrees(root.Symbol)) return false;
    167       for (int i = 0; i < root.SubTrees.Count; i++) {
    168         if (!AllowedSymbols(root.Symbol, i).Contains(root.SubTrees[i].Symbol)) return false;
    169         if (!IsValidExpression(root.SubTrees[i])) return false;
    170       }
    171       return true;
    17283    }
    17384  }
  • trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/ArtificialAntProblem.cs

    r3251 r3294  
    107107      get { return (ValueParameter<IntValue>)Parameters["MaxExpressionDepth"]; }
    108108    }
     109    public ValueParameter<IntValue> MaxFunctionDefinitionsParameter {
     110      get { return (ValueParameter<IntValue>)Parameters["MaxFunctionDefinitions"]; }
     111    }
     112    public ValueParameter<IntValue> MaxFunctionArgumentsParameter {
     113      get { return (ValueParameter<IntValue>)Parameters["MaxFunctionArguments"]; }
     114    }
    109115    public ValueParameter<BoolMatrix> WorldParameter {
    110116      get { return (ValueParameter<BoolMatrix>)Parameters["World"]; }
     
    195201      Parameters.Add(new ValueParameter<IntValue>("MaxExpressionLength", "Maximal length of the expression to control the artificial ant.", new IntValue(100)));
    196202      Parameters.Add(new ValueParameter<IntValue>("MaxExpressionDepth", "Maximal depth of the expression to control the artificial ant.", new IntValue(10)));
     203      Parameters.Add(new ValueParameter<IntValue>("MaxFunctionDefinitions", "Maximal number of automatically defined functions in the expression to control the artificial ant.", new IntValue(3)));
     204      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)));
    197205      Parameters.Add(new ValueParameter<BoolMatrix>("World", "The world for the artificial ant with scattered food items.", world));
    198206      Parameters.Add(new ValueParameter<IntValue>("MaxTimeSteps", "The number of time steps the artificial ant has available to collect all food items.", new IntValue(600)));
     
    333341        op.ChildParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
    334342      }
     343      foreach (SymbolicExpressionTreeManipulator op in Operators.OfType<SymbolicExpressionTreeManipulator>()) {
     344        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
     345      }
     346      foreach (SymbolicExpressionTreeArchitectureAlteringOperator op in Operators.OfType<SymbolicExpressionTreeArchitectureAlteringOperator>()) {
     347        op.MaxFunctionDefiningBranchesParameter.ActualName = MaxFunctionDefinitionsParameter.Name;
     348        op.MaxFunctionArgumentsParameter.ActualName = MaxFunctionArgumentsParameter.Name;
     349      }
    335350    }
    336351
  • trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/Symbols/IfFoodAhead.cs

    r3223 r3294  
    2121
    2222using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     23using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     24using HeuristicLab.Core;
    2325namespace HeuristicLab.Problems.ArtificialAnt {
     26  [StorableClass]
     27  [Item("IfFoodAhead", "Represents the if-food-ahead symbol in a artificial ant expression.")]
    2428  public sealed class IfFoodAhead : Symbol {
    2529  }
  • trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/Symbols/Left.cs

    r3223 r3294  
    2121
    2222using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     23using HeuristicLab.Core;
     24using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2325namespace HeuristicLab.Problems.ArtificialAnt {
     26  [StorableClass]
     27  [Item("Left", "Represents the turn-left symbol in a artificial ant expression.")]
    2428  public sealed class Left : Symbol {
    2529  }
  • trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/Symbols/Move.cs

    r3223 r3294  
    2121
    2222using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     23using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     24using HeuristicLab.Core;
    2325namespace HeuristicLab.Problems.ArtificialAnt {
     26  [StorableClass]
     27  [Item("Move", "Represents the move-forward symbol in a artificial ant expression.")]
    2428  public sealed class Move : Symbol {
    2529  }
  • trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/Symbols/Prog2.cs

    r3223 r3294  
    2222
    2323using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     24using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     25using HeuristicLab.Core;
    2426namespace HeuristicLab.Problems.ArtificialAnt {
     27  [StorableClass]
     28  [Item("Prog2", "Represents the sequence symbol with 2 sub-trees in a artificial ant expression.")]
    2529  public sealed class Prog2 : Symbol {
    2630  }
  • trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/Symbols/Prog3.cs

    r3269 r3294  
    2121
    2222using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     23using HeuristicLab.Core;
     24using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2325namespace HeuristicLab.Problems.ArtificialAnt {
     26  [StorableClass]
     27  [Item("Prog3", "Represents the sequence symbol with 3 sub-trees in a artificial ant expression.")]
    2428  public sealed class Prog3 : Symbol {
    25     public Prog3()
    26       : base() {
    27     }
    2829  }
    2930}
  • trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/Symbols/Right.cs

    r3223 r3294  
    2121
    2222using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     23using HeuristicLab.Core;
     24using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2325namespace HeuristicLab.Problems.ArtificialAnt {
     26  [StorableClass]
     27  [Item("Right", "Represents the turn-right symbol in a artificial ant expression.")]
    2428  public sealed class Right : Symbol {
    2529  }
Note: See TracChangeset for help on using the changeset viewer.