Free cookie consent management tool by TermsFeed Policy Generator

Changeset 10039


Ignore:
Timestamp:
10/11/13 21:41:17 (11 years ago)
Author:
sawinkle
Message:

#2109:

  • Renamed GEEvaluator.cs to GEArtificialAntEvaluator.cs, because a further Evaluator for the Symbolic Regression problem (single objective) is planned to be implemented.
  • Excluded the mapping process from GEArtificialAntEvaluator.cs and created several separated mapper classes. Created stubs for breath-first, depth-first, random and PIGE mappers. These mapper implementations should later be easily usable together with different problems. The mapper can be selected in the GUI, via a Problem parameter. The depth-first mapper is already usable, the others still cause exceptions.
Location:
branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution
Files:
7 added
2 edited
1 moved

Legend:

Unmodified
Added
Removed
  • branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/GEArtificialAntEvaluator.cs

    r10038 r10039  
    3030using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3131using HeuristicLab.Encodings.IntegerVectorEncoding;
    32 using System.Collections.Generic;
    33 using System.Linq;
    34 using HeuristicLab.Random;
     32using HeuristicLab.Problems.GrammaticalEvolution.Mappers;
    3533
    3634namespace HeuristicLab.Problems.GrammaticalEvolution {
    3735  [Item("GEArtificialAntEvaluator", "Evaluates an artificial ant solution, implemented in Grammatical Evolution.")]
    3836  [StorableClass]
    39   public class GEEvaluator : SingleSuccessorOperator,
     37  public class GEArtificialAntEvaluator : SingleSuccessorOperator,
    4038    ISingleObjectiveEvaluator, ISymbolicExpressionTreeGrammarBasedOperator {
    4139
     
    4442      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
    4543    }
     44    // genotype:
    4645    public ILookupParameter<IntegerVector> IntegerVectorParameter {
    4746      get { return (ILookupParameter<IntegerVector>)Parameters["IntegerVector"]; }
    4847    }
     48    // phenotype:
    4949    public ILookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
    5050      get { return (ILookupParameter<SymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]; }
     
    5959      get { return (IValueLookupParameter<ISymbolicExpressionGrammar>)Parameters["SymbolicExpressionTreeGrammar"]; }
    6060    }
     61    // genotype-to-phenotype-mapper:
     62    public ILookupParameter<IGenotypeToPhenotypeMapper> GenotypeToPhenotypeMapperParameter {
     63      get { return (ILookupParameter<IGenotypeToPhenotypeMapper>)Parameters["GenotypeToPhenotypeMapper"]; }
     64    }
    6165    #endregion
    6266 
    6367    [StorableConstructor]
    64     protected GEEvaluator(bool deserializing) : base(deserializing) { }
    65     protected GEEvaluator(GEEvaluator original, Cloner cloner) : base(original, cloner) { }
    66     public override IDeepCloneable Clone(Cloner cloner) { return new GEEvaluator(this, cloner); }
    67     public GEEvaluator()
     68    protected GEArtificialAntEvaluator(bool deserializing) : base(deserializing) { }
     69    protected GEArtificialAntEvaluator(GEArtificialAntEvaluator original, Cloner cloner) : base(original, cloner) { }
     70    public override IDeepCloneable Clone(Cloner cloner) { return new GEArtificialAntEvaluator(this, cloner); }
     71    public GEArtificialAntEvaluator()
    6872      : base() {
    6973      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the evaluated artificial ant solution."));
     
    7377      Parameters.Add(new LookupParameter<IntValue>("MaxTimeSteps", "The maximal number of time steps that the artificial ant should be simulated."));
    7478      Parameters.Add(new ValueLookupParameter<ISymbolicExpressionGrammar>("SymbolicExpressionTreeGrammar", "The tree grammar that defines the correct syntax of symbolic expression trees that should be created."));
     79      Parameters.Add(new LookupParameter<IGenotypeToPhenotypeMapper>("GenotypeToPhenotypeMapper", "Maps the genotype (an integer vector) to the phenotype (a symbolic expression tree)."));
    7580    }
    7681
    7782    public sealed override IOperation Apply() {
    78       SymbolicExpressionTree expression = MapIntegerVectorToSymbolicExpressionTree();
     83      SymbolicExpressionTree expression = GenotypeToPhenotypeMapperParameter.ActualValue.Map (
     84        SymbolicExpressionTreeGrammarParameter.ActualValue,
     85        IntegerVectorParameter.ActualValue       
     86      );
     87      SymbolicExpressionTreeParameter.ActualValue = expression;
    7988      BoolMatrix world = WorldParameter.ActualValue;
    8089      IntValue maxTimeSteps = MaxTimeStepsParameter.ActualValue;
     
    8998      return null;
    9099    }
    91    
    92    
    93     /// <summary>
    94     /// Maps an integer vector to a symbolic expression tree, using a
    95     /// genotype-to-phenotype mapper.
    96     /// </summary>
    97     /// <returns>solution tree</returns>
    98     private SymbolicExpressionTree MapIntegerVectorToSymbolicExpressionTree() {
    99      
    100       ISymbolicExpressionGrammar grammar = SymbolicExpressionTreeGrammarParameter.ActualValue;
    101       SymbolicExpressionTree     tree    = new SymbolicExpressionTree();
    102       IntegerVector integerVectorGenome  = IntegerVectorParameter.ActualValue;
    103       var rootNode  = (SymbolicExpressionTreeTopLevelNode)grammar.ProgramRootSymbol.CreateTreeNode();
    104       var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode();
    105       rootNode.AddSubtree(startNode);
    106       tree.Root = rootNode;
    107      
    108       int genotypeIndex    = 0;
    109       int currSubtreeCount = 1;
    110      
    111       MapGenoToPhenoDepthFirstRec(startNode, integerVectorGenome,
    112                                   grammar, integerVectorGenome.Length,
    113                                   ref genotypeIndex, ref currSubtreeCount);
    114      
    115       SymbolicExpressionTreeParameter.ActualValue = tree;
    116       return tree;
    117     }
    118    
    119    
    120     /// <summary>
    121     /// Genotype-to-Phenotype mapper (recursive depth-first approach).
    122     /// Appends maximum allowed children (non-terminal symbols) to
    123     /// <paramref name="currentNode"/>, as long as <paramref name="currSubtreeCount"/>
    124     /// doesn't exceed <paramref name="maxSubtreeCount"/>.
    125     /// If at most <paramref name="maxSubtreeCount"/> subtrees were created,
    126     /// each non-full node is filled with randomly chosen nodes
    127     /// (non-terminal and terminal), and each non-terminal node is again filled with a terminal node.
    128     /// </summary>
    129     /// <param name="currentNode">current parent node</param>
    130     /// <param name="integerVectorGenome">integer vector used for mapping</param>
    131     /// <param name="grammar">grammar definition to determine the allowed child symbols for currentNode </param>
    132     /// <param name="maxSubtreeCount">maximum allowed subtrees (= number of used genomes)</param>
    133     /// <param name="genotypeIndex">current index in integer vector</param>
    134     /// <param name="currSubtreeCount">number of already determined subtrees (filled or still incomplete)</param>
    135     private void MapGenoToPhenoDepthFirstRec(ISymbolicExpressionTreeNode currentNode,
    136                                              IntegerVector integerVectorGenome,
    137                                              ISymbolicExpressionGrammar grammar,
    138                                              int maxSubtreeCount,
    139                                              ref int genotypeIndex,
    140                                              ref int currSubtreeCount) {
    141       if (currSubtreeCount < maxSubtreeCount) {
    142      
    143         var newNode = GetNewChildNode(currentNode, integerVectorGenome, grammar, genotypeIndex);
    144        
    145         if ((currSubtreeCount + newNode.Symbol.MaximumArity) > maxSubtreeCount) {
    146           // TODO: maybe check, if there is any node, which fits in the tree yet
    147           currentNode.AddSubtree(GetRandomTerminalNode(currentNode, grammar));
    148         } else {
    149           currentNode.AddSubtree(newNode);
    150           genotypeIndex++;
    151           currSubtreeCount += newNode.Symbol.MaximumArity;
    152        
    153           while (newNode.Symbol.MaximumArity > newNode.SubtreeCount) {
    154             MapGenoToPhenoDepthFirstRec(newNode, integerVectorGenome,
    155                                         grammar, maxSubtreeCount,
    156                                         ref genotypeIndex, ref currSubtreeCount);
    157           }
    158         }
    159        
    160       } else {
    161         while (currentNode.Symbol.MaximumArity > currentNode.SubtreeCount) {
    162           var newNode = GetNewChildNode(currentNode, integerVectorGenome, grammar, genotypeIndex);
    163           currentNode.AddSubtree(newNode);
    164           genotypeIndex++;
    165           while (newNode.Symbol.MaximumArity > newNode.SubtreeCount) {
    166             newNode.AddSubtree(GetRandomTerminalNode(newNode, grammar));
    167           }
    168         }
    169       }
    170     }
    171    
    172    
    173     /// <summary>
    174     /// Randomly returns a terminal node for the given <paramref name="parentNode"/>.
    175     /// (A terminal has got a minimum and maximum arity of 0.)
    176     /// </summary>
    177     /// <param name="parentNode">parent node for which a child node is returned randomly</param>
    178     /// <param name="grammar">grammar definition to determine the allowed child symbols for parentNode</param>
    179     /// <returns>randomly chosen terminal node with arity 0</returns>
    180     private ISymbolicExpressionTreeNode GetRandomTerminalNode(ISymbolicExpressionTreeNode parentNode,
    181                                                               ISymbolicExpressionGrammar  grammar) {
    182       var possibleSymbolsList = from   s in grammar.GetAllowedChildSymbols(parentNode.Symbol)
    183                                 where  s.MaximumArity == 0
    184                                 where  s.MinimumArity == 0
    185                                 select s;
    186       // TODO: Check, if symbol list is empty (no terminal nodes found) - what should happen?
    187       return possibleSymbolsList.SelectRandom(new MersenneTwister()).CreateTreeNode();
    188     }
    189    
    190    
    191     /// <summary>
    192     /// Utility method, which returns the number of elements of <paramref name="symbolList"/>.
    193     /// </summary>
    194     /// <param name="symbolList">enumerable symbol list to count the elements for</param>
    195     /// <returns>number of elements in parameter symbolList</returns>
    196     private int GetNumberOfAllowedChildSymbols(IEnumerable<ISymbol> symbolList) {
    197       int count = 0;
    198       using (IEnumerator<ISymbol> enumerator = symbolList.GetEnumerator()) {
    199         while (enumerator.MoveNext()) {
    200           count++;
    201         }
    202       }
    203       return count;
    204     }
    205    
    206    
    207     /// <summary>
    208     /// Returns a randomly chosen child node for the given <paramref name="parentNode"/>.
    209     /// </summary>
    210     /// <param name="parentNode">parent node to find a child node randomly for</param>
    211     /// <param name="integerVectorGenome">integer vector to map to production rules</param>
    212     /// <param name="grammar">grammar definition used to define the allowed child symbols</param>
    213     /// <param name="genotypeIndex">index in the integer vector; can be greater than vector length</param>
    214     /// <returns></returns>
    215     private ISymbolicExpressionTreeNode GetNewChildNode(ISymbolicExpressionTreeNode parentNode,
    216                                                         IntegerVector integerVectorGenome,
    217                                                         ISymbolicExpressionGrammar grammar,
    218                                                         int genotypeIndex) {
    219      
    220       var symbolList    = grammar.GetAllowedChildSymbols(parentNode.Symbol);
    221       int prodRuleCount = GetNumberOfAllowedChildSymbols(symbolList);
    222       int prodRuleIndex = integerVectorGenome[genotypeIndex % integerVectorGenome.Length] % prodRuleCount;
    223       int currentIndex  = 0;
    224      
    225       using (IEnumerator<ISymbol> enumerator = symbolList.GetEnumerator()) {
    226         while (enumerator.MoveNext() && (currentIndex != prodRuleIndex)) {
    227           currentIndex++;
    228         }
    229         return enumerator.Current.CreateTreeNode();
    230       }
    231     }
    232100  }
    233101}
  • branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/GEArtificialAntProblem.cs

    r10022 r10039  
    3434using HeuristicLab.Problems.ArtificialAnt;
    3535using HeuristicLab.Problems.ArtificialAnt.Analyzers;
    36 
    37 namespace HeuristicLab.Problems.GrammaticalEvolution.ArtificialAnt {
     36using HeuristicLab.Problems.GrammaticalEvolution.Mappers;
     37
     38namespace HeuristicLab.Problems.GrammaticalEvolution {
    3839  [Item("Grammatical Evolution Artificial Ant Problem", "Represents the Artificial Ant problem, implemented in Grammatical Evolution.")]
    3940  [Creatable("Problems")]
    4041  [StorableClass]
    41   public sealed class GEArtificialAntProblem : SingleObjectiveHeuristicOptimizationProblem<GEEvaluator, IIntegerVectorCreator>, IStorableContent {
     42  public sealed class GEArtificialAntProblem : SingleObjectiveHeuristicOptimizationProblem<GEArtificialAntEvaluator, IIntegerVectorCreator>, IStorableContent {
    4243    public string Filename { get; set; }
    43 
     44   
    4445    #region constant for default world (Santa Fe)
    4546    private readonly bool[,] santaFeAntTrail = new bool[,] {
     
    101102      get { return (IValueParameter<IntMatrix>)Parameters["Bounds"]; }
    102103    }
     104    public IValueParameter<IGenotypeToPhenotypeMapper> GenotypeToPhenotypeMapperParameter {
     105      get { return (IValueParameter<IGenotypeToPhenotypeMapper>)Parameters["GenotypeToPhenotypeMapper"]; }
     106    }
    103107    #endregion
    104108
     
    164168    }
    165169    public GEArtificialAntProblem()
    166       : base(new GEEvaluator(), new UniformRandomIntegerVectorCreator()) {
     170      : base(new GEArtificialAntEvaluator(), new UniformRandomIntegerVectorCreator()) {
    167171      BoolMatrix world = new BoolMatrix(santaFeAntTrail);
    168172      Parameters.Add(new ValueParameter<IntValue>("MaximumExpressionLength", "Maximal length of the expression to control the artificial ant.", new IntValue(30)));
     
    174178      IntMatrix m = new IntMatrix(new int[,]{{0,100}});
    175179      Parameters.Add(new ValueParameter<IntMatrix>("Bounds", "The integer number range in which the single genomes of a genotype are created.", m));
     180      Parameters.Add(new ValueParameter<IGenotypeToPhenotypeMapper>("GenotypeToPhenotypeMapper", "Maps the genotype (an integer vector) to the phenotype (a symbolic expression tree).", new DepthFirstMapper()));
    176181     
    177182      Maximization.Value = true;
     
    285290        op.SymbolicExpressionTreeGrammarParameter.ActualName = ArtificialAntExpressionGrammarParameter.Name;
    286291      }
    287       foreach (GEEvaluator op in operators.OfType<GEEvaluator>()) {
     292      foreach (GEArtificialAntEvaluator op in operators.OfType<GEArtificialAntEvaluator>()) {
    288293        op.IntegerVectorParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
    289294        op.MaxTimeStepsParameter.ActualName = MaxTimeStepsParameter.Name;
  • branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution-3.3.csproj

    r10022 r10039  
    172172  <ItemGroup>
    173173    <None Include="HeuristicLab.snk" />
     174    <Compile Include="GEArtificialAntEvaluator.cs" />
    174175    <Compile Include="GEArtificialAntProblem.cs" />
    175     <Compile Include="GEEvaluator.cs" />
     176    <Compile Include="Mappers\BreathFirstMapper.cs" />
     177    <Compile Include="Mappers\DepthFirstMapper.cs" />
     178    <Compile Include="Mappers\GenotypeToPhenotypeMapper.cs" />
     179    <Compile Include="Mappers\IGenotypeToPhenotypeMapper.cs" />
     180    <Compile Include="Mappers\PIGEMapper.cs" />
     181    <Compile Include="Mappers\RandomMapper.cs" />
    176182    <Compile Include="Plugin.cs" />
    177183    <Compile Include="Properties\AssemblyInfo.cs" />
     
    196202    </BootstrapperPackage>
    197203  </ItemGroup>
     204  <ItemGroup>
     205    <Folder Include="Mappers" />
     206  </ItemGroup>
    198207  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
    199208  <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Note: See TracChangeset for help on using the changeset viewer.