Changeset 10290 for branches/GrammaticalEvolution
- Timestamp:
- 01/06/14 20:41:57 (11 years ago)
- Location:
- branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/ArtificialAnt/GEArtificialAntEvaluator.cs
r10280 r10290 66 66 get { return (ILookupParameter<IRandom>)Parameters["Random"]; } 67 67 } 68 public ILookupParameter<IntMatrix> BoundsParameter { 69 get { return (ILookupParameter<IntMatrix>)Parameters["Bounds"]; } 70 } 71 public ILookupParameter<IntValue> MaxExpressionLengthParameter { 72 get { return (ILookupParameter<IntValue>)Parameters["MaximumExpressionLength"]; } 73 } 68 74 #endregion 69 75 … … 82 88 Parameters.Add(new LookupParameter<IGenotypeToPhenotypeMapper>("GenotypeToPhenotypeMapper", "Maps the genotype (an integer vector) to the phenotype (a symbolic expression tree).")); 83 89 Parameters.Add(new LookupParameter<IRandom>("Random", "Random number generator for the genotype creation and the genotype-to-phenotype mapping.")); 90 91 Parameters.Add(new LookupParameter<IntMatrix>("Bounds", "The integer number range in which the single genomes of a genotype are created.")); 92 Parameters.Add(new LookupParameter<IntValue>("MaximumExpressionLength", "Maximal length of the expression to control the artificial ant.")); 84 93 } 85 94 … … 87 96 SymbolicExpressionTree tree = GenotypeToPhenotypeMapperParameter.ActualValue.Map( 88 97 RandomParameter.ActualValue, 98 BoundsParameter.ActualValue, 99 MaxExpressionLengthParameter.ActualValue.Value, 89 100 SymbolicExpressionTreeGrammarParameter.ActualValue, 90 101 IntegerVectorParameter.ActualValue -
branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/Mappers/BreathFirstMapper.cs
r10280 r10290 24 24 using HeuristicLab.Common; 25 25 using HeuristicLab.Core; 26 using HeuristicLab.Data; 26 27 using HeuristicLab.Encodings.IntegerVectorEncoding; 27 28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; … … 54 55 /// <param name="genotype">integer vector, which should be mapped to a tree</param> 55 56 /// <returns>phenotype (a symbolic expression tree)</returns> 56 public override SymbolicExpressionTree Map(IRandom random, 57 public override SymbolicExpressionTree Map(IRandom random, IntMatrix bounds, int length, 57 58 ISymbolicExpressionGrammar grammar, 58 59 IntegerVector genotype) { … … 87 88 88 89 int genotypeIndex = 0; 89 int currSubtreeCount = 1;90 91 90 queue.Enqueue(new Tuple<ISymbolicExpressionTreeNode, int>(startNode, 1)); 92 91 93 while ( (currSubtreeCount < maxSubtreeCount) && (queue.Count > 0)) {92 while (queue.Count > 0) { 94 93 95 94 Tuple<ISymbolicExpressionTreeNode, int> current = queue.Dequeue(); … … 98 97 for (int i = 0; i < current.Item2; ++i) { 99 98 100 var newNode = GetNewChildNode(current.Item1, genotype, grammar, genotypeIndex, random); 101 int arity = SampleArity(random, newNode, maxSubtreeCount - currSubtreeCount, grammar); 102 103 if (arity < 0) { 99 if (genotypeIndex >= maxSubtreeCount) { 100 // if all genomes were used, only add terminal nodes to the remaining subtrees 104 101 current.Item1.AddSubtree(GetRandomTerminalNode(current.Item1, grammar, random)); 105 102 } else { 103 var newNode = GetNewChildNode(current.Item1, genotype, grammar, genotypeIndex, random); 104 int arity = SampleArity(random, newNode, grammar); 105 106 106 current.Item1.AddSubtree(newNode); 107 107 genotypeIndex++; 108 currSubtreeCount += arity;109 108 if (arity > 0) { 110 109 // new node has subtrees so enqueue the node … … 114 113 } 115 114 } 116 117 // maximum allowed subtree count was already reached, but there are still118 // incomplete subtrees (non-terminal symbols) in the tree119 // -> fill them with terminal symbols120 while (queue.Count > 0) {121 Tuple<ISymbolicExpressionTreeNode, int> current = queue.Dequeue();122 for (int i = 0; i < current.Item2; ++i) {123 current.Item1.AddSubtree(GetRandomTerminalNode(current.Item1, grammar, random));124 }125 }126 115 } 127 116 } -
branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/Mappers/DepthFirstMapper.cs
r10280 r10290 24 24 using HeuristicLab.Common; 25 25 using HeuristicLab.Core; 26 using HeuristicLab.Data; 26 27 using HeuristicLab.Encodings.IntegerVectorEncoding; 27 28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; … … 54 55 /// <param name="genotype">integer vector, which should be mapped to a tree</param> 55 56 /// <returns>phenotype (a symbolic expression tree)</returns> 56 public override SymbolicExpressionTree Map(IRandom random, 57 public override SymbolicExpressionTree Map(IRandom random, IntMatrix bounds, int length, 57 58 ISymbolicExpressionGrammar grammar, 58 59 IntegerVector genotype) { … … 89 90 90 91 int genotypeIndex = 0; 91 int currSubtreeCount = 1;92 93 92 stack.Push(new Tuple<ISymbolicExpressionTreeNode, int>(startNode, 1)); 94 93 95 while ( (currSubtreeCount < maxSubtreeCount) && (stack.Count > 0)) {94 while (stack.Count > 0) { 96 95 97 96 // get next node from stack and re-push it, if this node still has unhandled subtrees ... … … 101 100 } 102 101 103 var newNode = GetNewChildNode(current.Item1, genotype, grammar, genotypeIndex, random); 104 int arity = SampleArity(random, newNode, maxSubtreeCount - currSubtreeCount, grammar); 105 106 if (arity < 0) { 102 if (genotypeIndex >= maxSubtreeCount) { 103 // if all genomes were used, only add terminal nodes to the remaining subtrees 107 104 current.Item1.AddSubtree(GetRandomTerminalNode(current.Item1, grammar, random)); 108 105 } else { 106 var newNode = GetNewChildNode(current.Item1, genotype, grammar, genotypeIndex, random); 107 int arity = SampleArity(random, newNode, grammar); 108 109 109 current.Item1.AddSubtree(newNode); 110 110 genotypeIndex++; 111 currSubtreeCount += arity;112 111 if (arity > 0) { 113 112 // new node has subtrees so push it onto the stack … … 116 115 } 117 116 } 118 119 // maximum allowed subtree count was already reached, but there are still120 // incomplete subtrees (non-terminal symbols) in the tree121 // -> fill them with terminal symbols122 while (stack.Count > 0) {123 Tuple<ISymbolicExpressionTreeNode, int> current = stack.Pop();124 if (current.Item2 > 1) {125 stack.Push(new Tuple<ISymbolicExpressionTreeNode, int>(current.Item1, current.Item2 - 1));126 }127 current.Item1.AddSubtree(GetRandomTerminalNode(current.Item1, grammar, random));128 }129 117 } 130 118 } -
branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/Mappers/GenotypeToPhenotypeMapper.cs
r10280 r10290 24 24 using HeuristicLab.Common; 25 25 using HeuristicLab.Core; 26 using HeuristicLab.Data; 26 27 using HeuristicLab.Encodings.IntegerVectorEncoding; 27 28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; … … 40 41 protected GenotypeToPhenotypeMapper() : base() { } 41 42 42 public abstract SymbolicExpressionTree Map(IRandom random, 43 public abstract SymbolicExpressionTree Map(IRandom random, IntMatrix bounds, int length, 43 44 ISymbolicExpressionGrammar grammar, 44 45 IntegerVector genotype); … … 96 97 if (prodRuleCount < 1) return null; 97 98 98 int prodRuleIndex = genotype[genotypeIndex % genotype.Length] % prodRuleCount; 99 // genotypeIndex % genotype.Length 100 int prodRuleIndex = genotype[genotypeIndex] % prodRuleCount; 99 101 100 102 var newNode = symbolList.ElementAt(prodRuleIndex).CreateTreeNode(); … … 109 111 /// <param name="random">random number generator</param> 110 112 /// <param name="node">node, for which a random arity is determined</param> 111 /// <param name="maxAllowedArity">the resulting arity must not exceed this maximum arity</param>112 113 /// <param name="grammar">symbolic expression grammar to use</param> 113 /// <returns>random arity in the interval [minArity, maxArity] of the node or 114 /// -1, if minArity exceeds maxAllowedArity</returns> 114 /// <returns>random arity in the interval [minArity, maxArity]</returns> 115 115 protected int SampleArity(IRandom random, 116 116 ISymbolicExpressionTreeNode node, 117 int maxAllowedArity,118 117 ISymbolicExpressionGrammar grammar) { 119 118 120 119 int minArity = grammar.GetMinimumSubtreeCount(node.Symbol); 121 120 int maxArity = grammar.GetMaximumSubtreeCount(node.Symbol); 122 123 if (maxAllowedArity < maxArity) {124 maxArity = maxAllowedArity;125 }126 127 if (maxAllowedArity < minArity) {128 return -1;129 }130 121 131 122 if (minArity == maxArity) { -
branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/Mappers/IGenotypeToPhenotypeMapper.cs
r10280 r10290 21 21 22 22 using HeuristicLab.Core; 23 using HeuristicLab.Data; 23 24 using HeuristicLab.Encodings.IntegerVectorEncoding; 24 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; … … 29 30 /// </summary> 30 31 public interface IGenotypeToPhenotypeMapper : IIntegerVectorOperator { 31 SymbolicExpressionTree Map(IRandom random, 32 SymbolicExpressionTree Map(IRandom random, IntMatrix bounds, int length, 32 33 ISymbolicExpressionGrammar grammar, 33 34 IntegerVector genotype); -
branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/Mappers/PIGEMapper.cs
r10280 r10290 20 20 #endregion 21 21 22 using System.Collections.Generic; 22 23 using HeuristicLab.Common; 23 24 using HeuristicLab.Core; 25 using HeuristicLab.Data; 24 26 using HeuristicLab.Encodings.IntegerVectorEncoding; 25 27 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; … … 27 29 28 30 namespace HeuristicLab.Problems.GrammaticalEvolution { 31 29 32 /// <summary> 30 33 /// Position Independent (PI) Grammatical Evolution Mapper 34 /// ----------------------------------------------------------------------------------- 35 /// Standard GE mappers: 36 /// Rule = Codon Value % Number Of Rules 37 /// 38 /// 𝜋GE: 39 /// 𝜋GE codons consist of (nont, rule) tuples, where nont may be one value from an "order" 40 /// integer vector and rule may be one value from a "content" integer vector. 41 /// 42 /// Order: NT = nont % Num. NT ... determines, which non-terminal to expand next 43 /// Content: Rule = rule % Num. Rules ... rule determination as with standard GE mappers 44 /// 45 /// Four mutation and crossover strategies possible: 46 /// * Order-only: only "order" vector is modified, "content" vector is fixed (1:0), 47 /// worst result according to [2] 48 /// * Content-only: only "content" vector is modified, "order" vector is fixed (0:1), 49 /// best result according to [2] 50 /// * 𝜋GE: genetic operators are applied equally (1:1), 51 /// * Content:Order: genetic operators are applied unequally (e.g. 2:1 or 1:2), 52 /// 53 /// Here, the "content-only" strategy is implemented, as it is the best solution according to [2] 54 /// and it does not require much to change in the problem and evaluator classes. 55 /// 31 56 /// </summary> 32 [Item("PIGEMapper", "")] 57 /// <remarks> 58 /// Described in 59 /// 60 /// [1] Michael O’Neill et al. 𝜋Grammatical Evolution. In: GECCO 2004. 61 /// Vol. 3103. LNCS. Heidelberg: Springer-Verlag Berlin, 2004, pp. 617–629. 62 /// url: http://dynamics.org/Altenberg/UH_ICS/EC_REFS/GP_REFS/GECCO/2004/31030617.pdf. 63 /// 64 /// [2] David Fagan et al. Investigating Mapping Order in πGE. IEEE, 2010 65 /// url: http://ncra.ucd.ie/papers/pigeWCCI2010.pdf 66 /// </remarks> 67 [Item("PIGEMapper", "Position Independent (PI) Grammatical Evolution Mapper")] 33 68 [StorableClass] 34 69 public class PIGEMapper : GenotypeToPhenotypeMapper { 70 71 private IntegerVector nontVector; 72 73 public IntegerVector NontVector { 74 get { return nontVector; } 75 set { nontVector = value; } 76 } 77 78 private IntegerVector GetNontVector(IRandom random, IntMatrix bounds, int length) { 79 IntegerVector v = new IntegerVector(length); 80 v.Randomize(random, bounds); 81 return v; 82 } 35 83 36 84 [StorableConstructor] … … 52 100 /// <param name="genotype">integer vector, which should be mapped to a tree</param> 53 101 /// <returns>phenotype (a symbolic expression tree)</returns> 54 public override SymbolicExpressionTree Map(IRandom random, 102 public override SymbolicExpressionTree Map(IRandom random, IntMatrix bounds, int length, 55 103 ISymbolicExpressionGrammar grammar, 56 104 IntegerVector genotype) { … … 62 110 tree.Root = rootNode; 63 111 64 // TODO 112 if (NontVector == null) { 113 NontVector = GetNontVector(random, bounds, length); 114 } 115 116 MapPIGEIteratively(startNode, genotype, grammar, 117 genotype.Length, random); 65 118 66 119 return tree; 67 120 } 121 122 123 private void MapPIGEIteratively(ISymbolicExpressionTreeNode startNode, 124 IntegerVector genotype, 125 ISymbolicExpressionGrammar grammar, 126 int maxSubtreeCount, IRandom random) { 127 128 List<ISymbolicExpressionTreeNode> nonTerminals = new List<ISymbolicExpressionTreeNode>(); 129 130 int genotypeIndex = 0; 131 nonTerminals.Add(startNode); 132 133 while (nonTerminals.Count > 0) { 134 135 if (genotypeIndex >= maxSubtreeCount) { 136 // if all genomes were used, only add terminal nodes to the remaining subtrees 137 ISymbolicExpressionTreeNode current = nonTerminals[0]; 138 nonTerminals.RemoveAt(0); 139 current.AddSubtree(GetRandomTerminalNode(current, grammar, random)); 140 } else { 141 // Order: NT = nont % Num. NT 142 int nt = NontVector[genotypeIndex] % nonTerminals.Count; 143 ISymbolicExpressionTreeNode current = nonTerminals[nt]; 144 nonTerminals.RemoveAt(nt); 145 146 // Content: Rule = rule % Num. Rules 147 ISymbolicExpressionTreeNode newNode = GetNewChildNode(current, genotype, grammar, genotypeIndex, random); 148 int arity = SampleArity(random, newNode, grammar); 149 150 current.AddSubtree(newNode); 151 genotypeIndex++; 152 // new node has subtrees, so add "arity" number of copies of this node to the nonTerminals list 153 for (int i = 0; i < arity; ++i) { 154 nonTerminals.Add(newNode); 155 } 156 } 157 } 158 } 68 159 } 69 160 } -
branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/Mappers/RandomMapper.cs
r10280 r10290 22 22 using HeuristicLab.Common; 23 23 using HeuristicLab.Core; 24 using HeuristicLab.Data; 24 25 using HeuristicLab.Encodings.IntegerVectorEncoding; 25 26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; … … 52 53 /// <param name="genotype">integer vector, which should be mapped to a tree</param> 53 54 /// <returns>phenotype (a symbolic expression tree)</returns> 54 public override SymbolicExpressionTree Map(IRandom random, 55 public override SymbolicExpressionTree Map(IRandom random, IntMatrix bounds, int length, 55 56 ISymbolicExpressionGrammar grammar, 56 57 IntegerVector genotype) { -
branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/Symbolic/GESymbolicRegressionSingleObjectiveEvaluator.cs
r10280 r10290 22 22 using HeuristicLab.Common; 23 23 using HeuristicLab.Core; 24 using HeuristicLab.Data; 24 25 using HeuristicLab.Parameters; 25 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 34 35 public const string EvaluatorParameterName = "Evaluator"; 35 36 public const string RandomParameterName = "Random"; 37 public const string BoundsParameterName = "Bounds"; 38 public const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength"; 36 39 37 40 public IValueParameter<ISymbolicRegressionSingleObjectiveEvaluator> EvaluatorParameter { … … 40 43 public ILookupParameter<IRandom> RandomParameter { 41 44 get { return (ILookupParameter<IRandom>)Parameters[RandomParameterName]; } 45 } 46 public ILookupParameter<IntMatrix> BoundsParameter { 47 get { return (ILookupParameter<IntMatrix>)Parameters[BoundsParameterName]; } 48 } 49 public ILookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter { 50 get { return (ILookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; } 42 51 } 43 52 … … 53 62 : base() { 54 63 Parameters.Add(new ValueParameter<ISymbolicRegressionSingleObjectiveEvaluator>(EvaluatorParameterName, "The symbolic regression evaluator that should be used to assess the quality of trees.", new SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator())); 64 Parameters.Add(new LookupParameter<IntMatrix>(BoundsParameterName, "The integer number range in which the single genomes of a genotype are created.")); 65 Parameters.Add(new LookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "Genotype length.")); 55 66 } 56 67 … … 69 80 var tree = GenotypeToPhenotypeMapperParameter.ActualValue.Map( 70 81 RandomParameter.ActualValue, 82 BoundsParameter.ActualValue, 83 MaximumSymbolicExpressionTreeLengthParameter.ActualValue.Value, 71 84 SymbolicExpressionTreeGrammarParameter.ActualValue, 72 85 genotype
Note: See TracChangeset
for help on using the changeset viewer.