Changeset 3294 for trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3
- Timestamp:
- 04/09/10 17:28:32 (14 years ago)
- 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 21 21 22 22 using System; 23 using System.Linq; 23 24 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 24 25 using HeuristicLab.Data; 25 26 using System.Collections.Generic; 27 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.GeneralSymbols; 26 28 27 29 namespace HeuristicLab.Problems.ArtificialAnt { … … 80 82 // expression evaluated completly => start at root again 81 83 if (nodeStack.Count == 0) 82 nodeStack.Push(Expression.R oot.SubTrees[0]);84 nodeStack.Push(Expression.ResultProducingExpression); 83 85 var currentNode = nodeStack.Pop(); 84 86 if (currentNode.Symbol is Left) { … … 112 114 nodeStack.Push(currentNode.SubTrees[0]); 113 115 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 114 129 } else { 115 130 throw new InvalidOperationException(currentNode.Symbol.ToString()); -
trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/ArtificialAntExpressionGrammar.cs
r3257 r3294 30 30 namespace HeuristicLab.Problems.ArtificialAnt { 31 31 [StorableClass] 32 public class ArtificialAntExpressionGrammar : Item, ISymbolicExpressionGrammar {32 public class ArtificialAntExpressionGrammar : DefaultSymbolicExpressionGrammar { 33 33 34 34 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(); 43 37 } 44 38 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); 61 68 } 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); 68 73 } 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 } 76 80 } 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 } 91 82 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 #endregion153 154 #region ISymbolicExpressionGrammar Members155 156 157 public bool IsValidExpression(SymbolicExpressionTree expression) {158 if (expression.Root.Symbol != StartSymbol) return false;159 return IsValidExpression(expression.Root);160 }161 162 #endregion163 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;172 83 } 173 84 } -
trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/ArtificialAntProblem.cs
r3251 r3294 107 107 get { return (ValueParameter<IntValue>)Parameters["MaxExpressionDepth"]; } 108 108 } 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 } 109 115 public ValueParameter<BoolMatrix> WorldParameter { 110 116 get { return (ValueParameter<BoolMatrix>)Parameters["World"]; } … … 195 201 Parameters.Add(new ValueParameter<IntValue>("MaxExpressionLength", "Maximal length of the expression to control the artificial ant.", new IntValue(100))); 196 202 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))); 197 205 Parameters.Add(new ValueParameter<BoolMatrix>("World", "The world for the artificial ant with scattered food items.", world)); 198 206 Parameters.Add(new ValueParameter<IntValue>("MaxTimeSteps", "The number of time steps the artificial ant has available to collect all food items.", new IntValue(600))); … … 333 341 op.ChildParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName; 334 342 } 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 } 335 350 } 336 351 -
trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/Symbols/IfFoodAhead.cs
r3223 r3294 21 21 22 22 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 23 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 24 using HeuristicLab.Core; 23 25 namespace HeuristicLab.Problems.ArtificialAnt { 26 [StorableClass] 27 [Item("IfFoodAhead", "Represents the if-food-ahead symbol in a artificial ant expression.")] 24 28 public sealed class IfFoodAhead : Symbol { 25 29 } -
trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/Symbols/Left.cs
r3223 r3294 21 21 22 22 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 23 using HeuristicLab.Core; 24 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 23 25 namespace HeuristicLab.Problems.ArtificialAnt { 26 [StorableClass] 27 [Item("Left", "Represents the turn-left symbol in a artificial ant expression.")] 24 28 public sealed class Left : Symbol { 25 29 } -
trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/Symbols/Move.cs
r3223 r3294 21 21 22 22 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 23 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 24 using HeuristicLab.Core; 23 25 namespace HeuristicLab.Problems.ArtificialAnt { 26 [StorableClass] 27 [Item("Move", "Represents the move-forward symbol in a artificial ant expression.")] 24 28 public sealed class Move : Symbol { 25 29 } -
trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/Symbols/Prog2.cs
r3223 r3294 22 22 23 23 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 24 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 25 using HeuristicLab.Core; 24 26 namespace HeuristicLab.Problems.ArtificialAnt { 27 [StorableClass] 28 [Item("Prog2", "Represents the sequence symbol with 2 sub-trees in a artificial ant expression.")] 25 29 public sealed class Prog2 : Symbol { 26 30 } -
trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/Symbols/Prog3.cs
r3269 r3294 21 21 22 22 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 23 using HeuristicLab.Core; 24 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 23 25 namespace HeuristicLab.Problems.ArtificialAnt { 26 [StorableClass] 27 [Item("Prog3", "Represents the sequence symbol with 3 sub-trees in a artificial ant expression.")] 24 28 public sealed class Prog3 : Symbol { 25 public Prog3()26 : base() {27 }28 29 } 29 30 } -
trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/Symbols/Right.cs
r3223 r3294 21 21 22 22 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 23 using HeuristicLab.Core; 24 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 23 25 namespace HeuristicLab.Problems.ArtificialAnt { 26 [StorableClass] 27 [Item("Right", "Represents the turn-right symbol in a artificial ant expression.")] 24 28 public sealed class Right : Symbol { 25 29 }
Note: See TracChangeset
for help on using the changeset viewer.