Free cookie consent management tool by TermsFeed Policy Generator

Changeset 8131 for trunk


Ignore:
Timestamp:
06/27/12 11:33:49 (12 years ago)
Author:
gkronber
Message:

#1876: added support for ADF to lawn mower problem... unfortunately found a bug in architecture manipulating operators or in the grammars.

Location:
trunk/sources/HeuristicLab.Problems.LawnMower/3.3
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.LawnMower/3.3/Interpreter.cs

    r8059 r8131  
    2121
    2222using System;
     23using System.Collections.Generic;
     24using System.Linq;
    2325using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2426
     
    5153      mowerState.Energy = length * width * 2;
    5254      lawn[mowerState.Position.Item1, mowerState.Position.Item2] = true;
    53       EvaluateLawnMowerProgram(tree.Root, ref mowerState, lawn);
     55      EvaluateLawnMowerProgram(tree.Root, ref mowerState, lawn, tree.Root.Subtrees.Skip(1).ToArray());
    5456
    5557      return lawn;
     
    5759
    5860
    59     private static Tuple<int, int> EvaluateLawnMowerProgram(ISymbolicExpressionTreeNode node, ref MowerState mowerState, bool[,] lawn) {
     61    private static Tuple<int, int> EvaluateLawnMowerProgram(ISymbolicExpressionTreeNode node, ref MowerState mowerState, bool[,] lawn, IEnumerable<ISymbolicExpressionTreeNode> adfs) {
    6062      if (mowerState.Energy <= 0) return new Tuple<int, int>(0, 0);
    6163
    6264      if (node.Symbol is ProgramRootSymbol) {
    63         return EvaluateLawnMowerProgram(node.GetSubtree(0), ref mowerState, lawn);
     65        return EvaluateLawnMowerProgram(node.GetSubtree(0), ref mowerState, lawn, adfs);
    6466      } else if (node.Symbol is StartSymbol) {
    65         return EvaluateLawnMowerProgram(node.GetSubtree(0), ref mowerState, lawn);
     67        return EvaluateLawnMowerProgram(node.GetSubtree(0), ref mowerState, lawn, adfs);
    6668      } else if (node.Symbol is Left) {
    6769        switch (mowerState.Heading) {
     
    104106        return constNode.Value;
    105107      } else if (node.Symbol is Sum) {
    106         var p = EvaluateLawnMowerProgram(node.GetSubtree(0), ref mowerState, lawn);
    107         var q = EvaluateLawnMowerProgram(node.GetSubtree(1), ref mowerState, lawn);
     108        var p = EvaluateLawnMowerProgram(node.GetSubtree(0), ref mowerState, lawn, adfs);
     109        var q = EvaluateLawnMowerProgram(node.GetSubtree(1), ref mowerState, lawn, adfs);
    108110        return new Tuple<int, int>(p.Item1 + q.Item1,
    109111          p.Item2 + q.Item2);
    110112      } else if (node.Symbol is Prog) {
    111         EvaluateLawnMowerProgram(node.GetSubtree(0), ref mowerState, lawn);
    112         return EvaluateLawnMowerProgram(node.GetSubtree(1), ref mowerState, lawn);
     113        EvaluateLawnMowerProgram(node.GetSubtree(0), ref mowerState, lawn, adfs);
     114        return EvaluateLawnMowerProgram(node.GetSubtree(1), ref mowerState, lawn, adfs);
    113115      } else if (node.Symbol is Frog) {
    114         var p = EvaluateLawnMowerProgram(node.GetSubtree(0), ref mowerState, lawn);
     116        var p = EvaluateLawnMowerProgram(node.GetSubtree(0), ref mowerState, lawn, adfs);
    115117
    116118        uint newRow = (uint)((mowerState.Position.Item1 + lawn.GetLength(0) + p.Item1 % lawn.GetLength(0)) % lawn.GetLength(0));
     
    120122        lawn[newRow, newCol] = true;
    121123        return new Tuple<int, int>(0, 0);
     124      } else if (node.Symbol is InvokeFunction) {
     125        var invokeNode = node as InvokeFunctionTreeNode;
     126
     127        // find the function definition for the invoke call
     128        var functionDefinition = (from adf in adfs.Cast<DefunTreeNode>()
     129                                  where adf.FunctionName == invokeNode.Symbol.FunctionName
     130                                  select adf).Single();
     131        // clone the function definition because we are replacing the argument nodes
     132        functionDefinition = (DefunTreeNode) functionDefinition.Clone();
     133        // find the argument tree nodes and their parents in the original function definition
     134        // toList is necessary to prevent that newly inserted branches are iterated
     135        var argumentCutPoints = (from parent in functionDefinition.IterateNodesPrefix()
     136                                 from subtree in parent.Subtrees
     137                                 where subtree is ArgumentTreeNode
     138                                 select new { Parent = parent, Argument = subtree.Symbol as Argument, ChildIndex = parent.IndexOfSubtree(subtree) })
     139                                 .ToList();
     140        // replace all argument tree ndoes with the matching argument of the invoke node
     141        foreach (var cutPoint in argumentCutPoints) {
     142          cutPoint.Parent.RemoveSubtree(cutPoint.ChildIndex);
     143          cutPoint.Parent.InsertSubtree(cutPoint.ChildIndex, (SymbolicExpressionTreeNode)invokeNode.GetSubtree(cutPoint.Argument.ArgumentIndex).Clone());
     144        }
     145        return EvaluateLawnMowerProgram(functionDefinition.GetSubtree(0), ref mowerState, lawn, adfs);
    122146      } else {
    123147        throw new ArgumentException("Invalid symbol in the lawn mower program.");
    124148      }
    125149    }
    126 
    127150  }
    128151}
  • trunk/sources/HeuristicLab.Problems.LawnMower/3.3/Problem.cs

    r8059 r8131  
    4242    private const string MaxLawnMowerProgramDepthParameterName = "MaxProgramDepth";
    4343    private const string LawnMowerGrammarParameterName = "Grammar";
     44    private const string MaxFunctionDefinitionsParameterName = "MaxFunctionDefinitions";
     45    private const string MaxArgumentDefinitionsParameterName = "MaxArgumentDefinitions";
    4446
    4547    public IFixedValueParameter<IntValue> LawnWidthParameter {
     
    5860      get { return (IValueParameter<Grammar>)Parameters[LawnMowerGrammarParameterName]; }
    5961    }
     62    public IFixedValueParameter<IntValue> MaxFunctionDefinitionsParameter {
     63      get { return (IFixedValueParameter<IntValue>)Parameters[MaxFunctionDefinitionsParameterName]; }
     64    }
     65    public IFixedValueParameter<IntValue> MaxArgumentDefinitionsParameter {
     66      get { return (IFixedValueParameter<IntValue>)Parameters[MaxArgumentDefinitionsParameterName]; }
     67    }
    6068
    6169    [StorableConstructor]
     
    7381      Parameters.Add(new FixedValueParameter<IntValue>(MaxLawnMowerProgramDepthParameterName, "Maximal depth of the lawn mower program.", new IntValue(13)));
    7482      Parameters.Add(new FixedValueParameter<IntValue>(MaxLawnMowerProgramLengthParameterName, "Maximal length of the lawn mower program.", new IntValue(1000)));
     83      Parameters.Add(new FixedValueParameter<IntValue>(MaxFunctionDefinitionsParameterName, "Maximal number of automatically defined functions (ADF).", new IntValue(3)));
     84      Parameters.Add(new FixedValueParameter<IntValue>(MaxArgumentDefinitionsParameterName, "Maximal number of automatically defined arguments.", new IntValue(3)));
    7585      Parameters.Add(new ValueParameter<Grammar>(LawnMowerGrammarParameterName, "Grammar for the lawn mower program.",
    7686                     new Grammar()));
     
    105115      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged +=
    106116        SymbolicExpressionTreeParameterOnActualNameChanged;
     117      MaxArgumentDefinitionsParameter.ValueChanged += ParameterizeGrammar;
     118      MaxFunctionDefinitionsParameter.ValueChanged += ParameterizeGrammar;
    107119    }
    108 
    109 
    110120
    111121    protected override void OnEvaluatorChanged() {
     
    135145      ParameterizeAnalyzers();
    136146      ParameterizeOperators();
     147    }
     148
     149    private void ParameterizeGrammar(object sender, EventArgs eventArgs) {
     150      GrammarParameter.Value.MaximumFunctionArguments = MaxArgumentDefinitionsParameter.Value.Value;
     151      GrammarParameter.Value.MaximumFunctionDefinitions = MaxFunctionDefinitionsParameter.Value.Value;
    137152    }
    138153
     
    175190        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
    176191      }
     192      foreach (ISymbolicExpressionTreeArchitectureAlteringOperator op in operators.OfType<ISymbolicExpressionTreeArchitectureAlteringOperator>()) {
     193        op.MaximumFunctionDefinitionsParameter.ActualName = MaxFunctionDefinitionsParameter.Name;
     194        op.MaximumFunctionArgumentsParameter.ActualName = MaxArgumentDefinitionsParameter.Name;
     195      }
    177196    }
    178197  }
Note: See TracChangeset for help on using the changeset viewer.