Changeset 3338 for trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3
- Timestamp:
- 04/13/10 20:44:31 (15 years ago)
- 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 29 29 namespace HeuristicLab.Problems.ArtificialAnt { 30 30 public class AntInterpreter { 31 31 32 public int MaxTimeSteps { get; set; } 32 33 public int FoodEaten { get; set; } … … 48 49 } 49 50 } 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 50 61 public int ElapsedTime { get; set; } 51 62 private int currentDirection; … … 81 92 public void Step() { 82 93 // expression evaluated completly => start at root again 83 if (nodeStack.Count == 0) 94 if (nodeStack.Count == 0) { 84 95 nodeStack.Push(Expression.ResultProducingExpression); 96 } 97 85 98 var currentNode = nodeStack.Pop(); 86 99 if (currentNode.Symbol is Left) { … … 116 129 } else if (currentNode.Symbol is InvokeFunction) { 117 130 var invokeNode = currentNode as InvokeFunctionTreeNode; 118 var fun Branch = (from node in expression.Root.SubTrees119 let funNode = node as DefunTreeNode120 where funNode != null121 where funNode.Name == invokeNode.InvokedFunctionName122 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 nothing131 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]); 129 142 } else { 130 143 throw new InvalidOperationException(currentNode.Symbol.ToString()); -
trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/ArtificialAntExpressionGrammar.cs
r3294 r3338 33 33 34 34 public ArtificialAntExpressionGrammar() 35 : base( 0, 3, 0, 3) {35 : base() { 36 36 Initialize(); 37 37 } … … 45 45 var right = new Right(); 46 46 var defun = new Defun(); 47 var invoke = new InvokeFunction();48 47 var allSymbols = new List<Symbol>() { ifFoodAhead, prog2, prog3, move, left, right }; 49 48 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 49 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); 79 72 } 80 73 } 81 74 } 82 83 75 } 84 76 } -
trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/ArtificialAntProblem.cs
r3294 r3338 151 151 set { MaxExpressionDepthParameter.Value = value; } 152 152 } 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 } 153 161 public SymbolicExpressionTreeCreator SolutionCreator { 154 162 get { return SolutionCreatorParameter.Value; } … … 168 176 get { return EvaluatorParameter.Value; } 169 177 } 170 public ISymbolicExpressionGrammar ArtificialAntExpressionGrammar {171 get { return ArtificialAntExpressionGrammarParameter.Value; }178 public GlobalSymbolicExpressionGrammar ArtificialAntExpressionGrammar { 179 get { return (GlobalSymbolicExpressionGrammar)ArtificialAntExpressionGrammarParameter.Value; } 172 180 } 173 181 public ISingleObjectiveSolutionsVisualizer Visualizer { … … 191 199 SymbolicExpressionTreeCreator creator = new ProbabilisticTreeCreator(); 192 200 Evaluator evaluator = new Evaluator(); 193 ArtificialAntExpressionGrammar grammar = new ArtificialAntExpressionGrammar();194 201 BestAntTrailVisualizer visualizer = new BestAntTrailVisualizer(); 195 202 BoolMatrix world = new BoolMatrix(santaFeAntTrail); 203 ISymbolicExpressionGrammar grammar = new GlobalSymbolicExpressionGrammar(new ArtificialAntExpressionGrammar()); 196 204 Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to true as the Artificial Ant Problem is a maximization problem.", new BoolValue(true))); 197 205 Parameters.Add(new ValueParameter<SymbolicExpressionTreeCreator>("SolutionCreator", "The operator which should be used to create new artificial ant solutions.", creator)); 198 206 Parameters.Add(new ValueParameter<Evaluator>("Evaluator", "The operator which should be used to evaluate artificial ant solutions.", evaluator)); 199 207 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));201 208 Parameters.Add(new ValueParameter<IntValue>("MaxExpressionLength", "Maximal length of the expression to control the artificial ant.", new IntValue(100))); 202 209 Parameters.Add(new ValueParameter<IntValue>("MaxExpressionDepth", "Maximal depth of the expression to control the artificial ant.", new IntValue(10))); 203 210 Parameters.Add(new ValueParameter<IntValue>("MaxFunctionDefinitions", "Maximal number of automatically defined functions in the expression to control the artificial ant.", new IntValue(3))); 204 211 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)); 205 213 Parameters.Add(new ValueParameter<BoolMatrix>("World", "The world for the artificial ant with scattered food items.", world)); 206 214 Parameters.Add(new ValueParameter<IntValue>("MaxTimeSteps", "The number of time steps the artificial ant has available to collect all food items.", new IntValue(600))); … … 292 300 Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged); 293 301 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; 294 314 } 295 315 … … 348 368 op.MaxFunctionArgumentsParameter.ActualName = MaxFunctionArgumentsParameter.Name; 349 369 } 370 foreach (SymbolicExpressionTreeCreator op in Operators.OfType<SymbolicExpressionTreeCreator>()) { 371 op.MaxFunctionArgumentsParameter.ActualName = MaxFunctionArgumentsParameter.Name; 372 op.MaxFunctionDefinitionsParameter.ActualName = MaxFunctionDefinitionsParameter.Name; 373 } 350 374 } 351 375
Note: See TracChangeset
for help on using the changeset viewer.