- Timestamp:
- 02/01/15 19:49:25 (10 years ago)
- Location:
- branches/HeuristicLab.Problems.GrammaticalOptimization
- Files:
-
- 1 added
- 18 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.Bandits/HeuristicLab.Algorithms.Bandits.csproj
r11842 r11847 34 34 <HintPath>..\..\..\trunk\sources\bin\ALGLIB-3.7.0.dll</HintPath> 35 35 </Reference> 36 <Reference Include="HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm-3.3"> 37 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm-3.3.dll</HintPath> 38 </Reference> 36 39 <Reference Include="System" /> 37 40 <Reference Include="System.Core" /> 38 <Reference Include="System.Xml.Linq" />39 <Reference Include="System.Data.DataSetExtensions" />40 <Reference Include="Microsoft.CSharp" />41 <Reference Include="System.Data" />42 <Reference Include="System.Xml" />43 41 </ItemGroup> 44 42 <ItemGroup> -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GeneticProgramming
-
Property
svn:ignore
set to
bin
obj
-
Property
svn:ignore
set to
-
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GeneticProgramming/HeuristicLab.Algorithms.GeneticProgramming.csproj
r11846 r11847 34 34 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Algorithms.GeneticAlgorithm-3.3.dll</HintPath> 35 35 </Reference> 36 <Reference Include="HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm-3.3"> 37 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm-3.3.dll</HintPath> 38 </Reference> 36 39 <Reference Include="HeuristicLab.Collections-3.3"> 37 40 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Collections-3.3.dll</HintPath> … … 54 57 <Reference Include="HeuristicLab.Optimization-3.3"> 55 58 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Optimization-3.3.dll</HintPath> 59 </Reference> 60 <Reference Include="HeuristicLab.ParallelEngine-3.3"> 61 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.ParallelEngine-3.3.dll</HintPath> 56 62 </Reference> 57 63 <Reference Include="HeuristicLab.Parameters-3.3"> -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GeneticProgramming/OffspringSelectionGP.cs
r11846 r11847 25 25 this.random = random; 26 26 // default parameter values 27 PopulationSize = 300;27 PopulationSize = 100; 28 28 MutationRate = 0.15; 29 29 MaxSolutionSize = 100; … … 33 33 public override void Run(int maxEvaluations) { 34 34 var hlProblem = new GenericSymbExprProblem(problem); 35 hlProblem.Evaluator.SolutionEvaluated += OnSolutionEvaluated; // raise solution evaluated event for each GP solution 35 hlProblem.Evaluator.SolutionEvaluated += OnSolutionEvaluated; // raise solution evaluated event for each GP solution, don't scale quality to 0..1 36 hlProblem.MaximumSymbolicExpressionTreeLength.Value = MaxSolutionSize; 37 hlProblem.MaximumSymbolicExpressionTreeDepth.Value = MaxSolutionDepth; 36 38 37 39 using (var wh = new AutoResetEvent(false)) { 38 var ga = new GeneticAlgorithm.GeneticAlgorithm();39 ga.Engine = new SequentialEngine.SequentialEngine();40 ga.ExceptionOccurred += (sender, args) => { Console.WriteLine(args.Value.Message); wh.Set(); };41 ga.Stopped += (sender, args) => { wh.Set(); };40 var osga = new OffspringSelectionGeneticAlgorithm.OffspringSelectionGeneticAlgorithm(); 41 osga.Engine = new ParallelEngine.ParallelEngine(); 42 osga.ExceptionOccurred += (sender, args) => { Console.WriteLine(args.Value.Message); wh.Set(); }; 43 osga.Stopped += (sender, args) => { wh.Set(); }; 42 44 43 ga.Problem = hlProblem;44 var mutator = (MultiSymbolicExpressionTreeManipulator) ga.MutatorParameter.ValidValues.Single(op => op.Name == "MultiSymbolicExpressionTreeManipulator");45 osga.Problem = hlProblem; 46 var mutator = (MultiSymbolicExpressionTreeManipulator)osga.MutatorParameter.ValidValues.Single(op => op.Name == "MultiSymbolicExpressionTreeManipulator"); 45 47 foreach (var op in mutator.Operators) { 46 48 if (op.Name == "ChangeNodeTypeManipulation" … … 48 50 else mutator.Operators.SetItemCheckedState(op, false); 49 51 } 50 ga.Mutator = mutator;51 ga.Crossover =ga.CrossoverParameter.ValidValues.Single(op => op.Name == "SubtreeSwappingCrossover");52 ga.Selector = ga.SelectorParameter.ValidValues.Single(op => op.Name == "GenderSpecificSelector");52 osga.Mutator = mutator; 53 osga.Crossover = osga.CrossoverParameter.ValidValues.Single(op => op.Name == "SubtreeSwappingCrossover"); 54 osga.Selector = osga.SelectorParameter.ValidValues.Single(op => op.Name == "GenderSpecificSelection"); 53 55 54 ga.PopulationSize.Value = PopulationSize; 55 ga.MaximumGenerations.Value = maxEvaluations / PopulationSize + 1; // one extra generation in case maxEvaluations is not a multiple of PopulationSize 56 ga.MutationProbability.Value = MutationRate; 56 osga.PopulationSize.Value = PopulationSize; 57 osga.MaximumGenerations.Value = 100000; // some very large value (we stop based on evaluations) 58 osga.MaximumSelectionPressure.Value = 1000; 59 osga.MaximumEvaluatedSolutions.Value = maxEvaluations; 60 osga.MutationProbability.Value = MutationRate; 57 61 58 ga.SetSeedRandomly = new BoolValue(false);59 ga.Seed = new IntValue(random.Next());62 osga.SetSeedRandomly = new BoolValue(false); 63 osga.Seed = new IntValue(random.Next()); 60 64 61 ga.Prepare();62 ga.Start();65 osga.Prepare(); 66 osga.Start(); 63 67 64 68 wh.WaitOne(); -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GeneticProgramming/StandardGP.cs
r11846 r11847 35 35 public override void Run(int maxEvaluations) { 36 36 var hlProblem = new GenericSymbExprProblem(problem); 37 hlProblem.Evaluator.SolutionEvaluated += OnSolutionEvaluated; // raise solution evaluated event for each GP solution 37 hlProblem.Evaluator.SolutionEvaluated += OnSolutionEvaluated; // raise solution evaluated event for each GP solution, don't scale quality to 0..1 38 hlProblem.MaximumSymbolicExpressionTreeLength.Value = MaxSolutionSize; 39 hlProblem.MaximumSymbolicExpressionTreeDepth.Value = MaxSolutionDepth; 40 38 41 39 42 using (var wh = new AutoResetEvent(false)) { 40 43 var ga = new GeneticAlgorithm.GeneticAlgorithm(); 41 ga.Engine = new SequentialEngine.SequentialEngine();44 ga.Engine = new ParallelEngine.ParallelEngine(); 42 45 ga.ExceptionOccurred += (sender, args) => { Console.WriteLine(args.Value.Message); wh.Set(); }; 43 46 ga.Stopped += (sender, args) => { wh.Set(); }; -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GrammaticalOptimization/HeuristicLab.Algorithms.GrammaticalOptimization.csproj
r11846 r11847 36 36 <Reference Include="System" /> 37 37 <Reference Include="System.Core" /> 38 <Reference Include="System.Xml.Linq" />39 <Reference Include="System.Data.DataSetExtensions" />40 <Reference Include="Microsoft.CSharp" />41 <Reference Include="System.Data" />42 <Reference Include="System.Xml" />43 38 </ItemGroup> 44 39 <ItemGroup> -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Common/HeuristicLab.Common.csproj
r11799 r11847 33 33 <Reference Include="System" /> 34 34 <Reference Include="System.Core" /> 35 <Reference Include="System.Drawing" />36 <Reference Include="System.Xml.Linq" />37 <Reference Include="System.Data.DataSetExtensions" />38 <Reference Include="Microsoft.CSharp" />39 <Reference Include="System.Data" />40 <Reference Include="System.Xml" />41 35 </ItemGroup> 42 36 <ItemGroup> -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization.csproj
r11846 r11847 69 69 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.PluginInfrastructure-3.3.dll</HintPath> 70 70 </Reference> 71 <Reference Include="HeuristicLab.SequentialEngine-3.3">72 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.SequentialEngine-3.3.dll</HintPath>73 </Reference>74 71 <Reference Include="System" /> 75 72 <Reference Include="System.Core" /> … … 77 74 <Reference Include="System.Xml.Linq" /> 78 75 <Reference Include="System.Data.DataSetExtensions" /> 79 <Reference Include="Microsoft.CSharp" />80 76 <Reference Include="System.Data" /> 81 77 <Reference Include="System.Xml" /> … … 115 111 </ProjectReference> 116 112 </ItemGroup> 113 <ItemGroup> 114 <Folder Include="TreeRepresentation\" /> 115 </ItemGroup> 117 116 <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> 118 117 <!-- To modify your build process, add your task inside one of the targets below and uncomment it. -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/EvenParityProblem.cs
r11832 r11847 5 5 using System.Text; 6 6 using System.Text.RegularExpressions; 7 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 7 8 8 9 namespace HeuristicLab.Problems.GrammaticalOptimization { 9 10 // 4-bit even parity 10 public class EvenParityProblem : I Problem {11 public class EvenParityProblem : ISymbolicExpressionTreeProblem { 11 12 // + == OR 12 13 // * == AND … … 15 16 S -> a | b | c | d | a*S | b*S | c*S | d*S | a+S | b+S | c+S | d+S | !S | (S) 16 17 "; 18 // A = AND, O = OR, N = NOT, C = Clause 19 private const string hlGrammarString = @" 20 G(E): 21 E -> A | O | N | C | a | b | c | d 22 A -> EE | EEE 23 O -> EE | EEE 24 N -> E 25 C -> E 26 "; 17 27 18 28 private readonly IGrammar grammar; 19 private readonly ExpressionInterpreter interpreter = new ExpressionInterpreter();20 29 public EvenParityProblem() { 21 30 this.grammar = new Grammar(grammarString); 31 this.SymbolicExpressionGrammar = new GenericSymbExprGrammar(new Grammar(hlGrammarString)); 22 32 } 23 33 24 34 public double BestKnownQuality(int maxLen) { 25 35 // for now only an upper bound is returned, ideally all fitness cases are predicted correctly 26 return Math.Pow(2, 4);36 return 16; 27 37 } 28 38 … … 32 42 33 43 public double Evaluate(string sentence) { 44 var interpreter = new ExpressionInterpreter(); // for concurrent evaluation 34 45 var vars = new bool[4]; 35 46 var nCorrect = 0; … … 55 66 } 56 67 57 public IEnumerable<Feature> GetFeatures(string phrase) 58 { 68 public IEnumerable<Feature> GetFeatures(string phrase) { 59 69 throw new NotImplementedException(); 70 } 71 72 public ISymbolicExpressionGrammar SymbolicExpressionGrammar { get; private set; } 73 public string ConvertTreeToSentence(ISymbolicExpressionTree tree) { 74 var sb = new StringBuilder(); 75 76 TreeToSentence(tree.Root.GetSubtree(0).GetSubtree(0), sb); 77 78 return sb.ToString(); 79 } 80 81 private void TreeToSentence(ISymbolicExpressionTreeNode treeNode, StringBuilder sb) { 82 if (treeNode.SubtreeCount == 0) { 83 // terminal 84 sb.Append(treeNode.Symbol.Name); 85 } else { 86 switch (treeNode.Symbol.Name) { 87 case "O": { 88 sb.Append("("); 89 TreeToSentence(treeNode.Subtrees.First(), sb); 90 foreach (var subTree in treeNode.Subtrees.Skip(1)) { 91 sb.Append("+"); 92 TreeToSentence(subTree, sb); 93 } 94 sb.Append(")"); 95 break; 96 } 97 case "A": { 98 TreeToSentence(treeNode.Subtrees.First(), sb); 99 foreach (var subTree in treeNode.Subtrees.Skip(1)) { 100 sb.Append("*"); 101 TreeToSentence(subTree, sb); 102 } 103 break; 104 } 105 case "N": { 106 Debug.Assert(treeNode.SubtreeCount == 1); 107 sb.Append("!("); 108 TreeToSentence(treeNode.Subtrees.Single(), sb); 109 sb.Append(")"); 110 break; 111 } 112 case "C": { 113 Debug.Assert(treeNode.SubtreeCount == 1); 114 sb.Append("("); 115 TreeToSentence(treeNode.Subtrees.Single(), sb); 116 sb.Append(")"); 117 break; 118 } 119 default: { 120 Debug.Assert(treeNode.SubtreeCount == 1); 121 TreeToSentence(treeNode.Subtrees.Single(), sb); 122 break; 123 } 124 } 125 } 60 126 } 61 127 } -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/GrammaticalOptimizationEvaluator.cs
r11846 r11847 1 1 using System; 2 using System.Collections.Generic;3 using System.Diagnostics;4 using System.Linq;5 using System.Text;6 using System.Text.RegularExpressions;7 2 using HeuristicLab.Common; 8 3 using HeuristicLab.Core; … … 10 5 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 11 6 using HeuristicLab.Operators; 12 using HeuristicLab.Optimization;13 7 using HeuristicLab.Parameters; 14 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;15 8 using HeuristicLab.PluginInfrastructure; 16 9 -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/HardPalindromeProblem.cs
r11832 r11847 3 3 using System.Linq; 4 4 using System.Text; 5 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 5 6 6 7 namespace HeuristicLab.Problems.GrammaticalOptimization { 7 public class HardPalindromeProblem : I Problem {8 public class HardPalindromeProblem : ISymbolicExpressionTreeProblem { 8 9 // length of the longest palindrome in the sentence + number of different symbols occurring in the palindrome 9 10 private const string grammarString = @" … … 12 13 T -> a .. z 13 14 "; 15 private const string hlGrammarString = @" 16 G(S): 17 S -> a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z | SS 18 "; 14 19 15 20 private readonly IGrammar grammar; 16 21 public HardPalindromeProblem() { 17 22 this.grammar = new Grammar(grammarString); 23 this.SymbolicExpressionGrammar = new GenericSymbExprGrammar(new Grammar(hlGrammarString)); 18 24 } 19 25 … … 47 53 throw new NotImplementedException(); 48 54 } 55 56 public ISymbolicExpressionGrammar SymbolicExpressionGrammar { get; private set; } 57 public string ConvertTreeToSentence(ISymbolicExpressionTree tree) { 58 var sb = new StringBuilder(); 59 foreach (var s in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix()) { 60 if (s.Symbol.Name == "S") continue; 61 sb.Append(s.Symbol.Name); 62 } 63 return sb.ToString(); 64 } 49 65 } 50 66 } -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/PalindromeProblem.cs
r11832 r11847 3 3 using System.Linq; 4 4 using System.Text; 5 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 5 6 6 7 namespace HeuristicLab.Problems.GrammaticalOptimization { 7 public class PalindromeProblem : I Problem {8 public class PalindromeProblem : ISymbolicExpressionTreeProblem { 8 9 // length of the longest palindrome in the sentence 9 10 private const string grammarString = @" … … 13 14 "; 14 15 16 17 private const string hlGrammarString = @" 18 G(S): 19 S -> a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z | SS 20 "; 21 15 22 private readonly IGrammar grammar; 16 23 public PalindromeProblem() { 17 24 this.grammar = new Grammar(grammarString); 25 this.SymbolicExpressionGrammar = new GenericSymbExprGrammar(new Grammar(hlGrammarString)); 18 26 } 19 27 … … 84 92 } 85 93 86 public IEnumerable<Feature> GetFeatures(string phrase) 87 { 94 public IEnumerable<Feature> GetFeatures(string phrase) { 88 95 throw new NotImplementedException(); 96 } 97 98 public ISymbolicExpressionGrammar SymbolicExpressionGrammar { get; private set; } 99 public string ConvertTreeToSentence(ISymbolicExpressionTree tree) { 100 var sb = new StringBuilder(); 101 foreach (var s in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix()) { 102 if (s.Symbol.Name == "S") continue; 103 sb.Append(s.Symbol.Name); 104 } 105 return sb.ToString(); 89 106 } 90 107 } -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/RoyalPairProblem.cs
r11832 r11847 5 5 using System.Text; 6 6 using System.Text.RegularExpressions; 7 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 7 8 8 9 namespace HeuristicLab.Problems.GrammaticalOptimization { 9 10 // counts the number of times a pair of symbols occurs in a sentence 10 public class RoyalPairProblem : I Problem {11 public class RoyalPairProblem : ISymbolicExpressionTreeProblem { 11 12 private const string grammarString = @" 12 13 G(S): … … 14 15 "; 15 16 17 private const string hlGrammarString = @" 18 G(S): 19 S -> a | b | SS 20 "; 21 16 22 private readonly IGrammar grammar; 17 23 public RoyalPairProblem() { 18 24 this.grammar = new Grammar(grammarString); 25 this.SymbolicExpressionGrammar = new GenericSymbExprGrammar(new Grammar(hlGrammarString)); 19 26 // TODO: allow configuration of the number of symbols 20 27 } … … 36 43 37 44 public string CanonicalRepresentation(string phrase) { 38 //throw new NotImplementedException();39 45 return phrase; 40 46 } … … 44 50 throw new NotImplementedException(); 45 51 } 52 public ISymbolicExpressionGrammar SymbolicExpressionGrammar { get; private set; } 53 public string ConvertTreeToSentence(ISymbolicExpressionTree tree) { 54 var sb = new StringBuilder(); 55 foreach (var s in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix()) { 56 if (s.Symbol.Name == "S") continue; 57 sb.Append(s.Symbol.Name); 58 } 59 return sb.ToString(); 60 } 46 61 } 47 62 } -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/RoyalSymbolProblem.cs
r11832 r11847 3 3 using System.Diagnostics; 4 4 using System.Linq; 5 using System.Text; 5 6 using System.Text.RegularExpressions; 7 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 6 8 7 9 namespace HeuristicLab.Problems.GrammaticalOptimization { 8 10 // counts the number of times a symbol occurs in a sentence 9 public class RoyalSymbolProblem : I Problem {11 public class RoyalSymbolProblem : ISymbolicExpressionTreeProblem { 10 12 private const string grammarString = @" 11 13 G(S): 12 14 S -> a | aS | b | bS 13 15 "; 16 17 private const string hlGrammarString = @" 18 G(S): 19 S -> a | b | SS 20 "; 21 14 22 // Obj(s \in L(G(S))) = Anzahl "a"-Symbole in s 15 23 … … 17 25 public RoyalSymbolProblem() { 18 26 this.grammar = new Grammar(grammarString); 27 this.SymbolicExpressionGrammar = new GenericSymbExprGrammar(new Grammar(hlGrammarString)); 19 28 //TODO: allow configuration of the number of symbols 20 29 } … … 35 44 } 36 45 public string CanonicalRepresentation(string phrase) { 37 throw new NotImplementedException();38 46 return phrase; 39 47 } 40 48 41 public IEnumerable<Feature> GetFeatures(string phrase) 42 { 49 public IEnumerable<Feature> GetFeatures(string phrase) { 43 50 throw new NotImplementedException(); 51 } 52 53 public ISymbolicExpressionGrammar SymbolicExpressionGrammar { get; private set; } 54 public string ConvertTreeToSentence(ISymbolicExpressionTree tree) { 55 var sb = new StringBuilder(); 56 foreach (var s in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix()) { 57 if (s.Symbol.Name == "S") continue; 58 sb.Append(s.Symbol.Name); 59 } 60 return sb.ToString(); 44 61 } 45 62 } -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/SantaFeAntProblem.cs
r11832 r11847 1 1 using System; 2 2 using System.Collections.Generic; 3 using System.Diagnostics; 3 4 using System.Linq; 4 5 using System.Runtime.InteropServices; … … 6 7 using System.Text; 7 8 using System.Text.RegularExpressions; 9 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 8 10 9 11 namespace HeuristicLab.Problems.GrammaticalOptimization { 10 public class SantaFeAntProblem : I Problem {12 public class SantaFeAntProblem : ISymbolicExpressionTreeProblem { 11 13 private const string grammarString = @" 12 14 G(A): 13 15 A -> l | r | m | ?(A)(A) | lA | rA | mA 14 16 "; 17 // for tree-based GP in HL we need a different grammar for the same language 18 // A = Ant, P = Prog2 and Prog3, I = IfFoodAhead 19 private const string hlGrammarString = @" 20 G(A): 21 A -> l | r | m | P | I 22 P -> AA | AAA 23 I -> AA 24 "; 15 25 16 26 … … 25 35 public SantaFeAntProblem() { 26 36 this.grammar = new Grammar(grammarString); 37 this.SymbolicExpressionGrammar = new GenericSymbExprGrammar(new Grammar(hlGrammarString)); 27 38 } 28 39 … … 116 127 public IEnumerable<Feature> GetFeatures(string phrase) { 117 128 return Enumerable.Repeat(new Feature(CanonicalRepresentation(phrase), 1.0), 1); 129 } 130 131 public ISymbolicExpressionGrammar SymbolicExpressionGrammar { get; private set; } 132 public string ConvertTreeToSentence(ISymbolicExpressionTree tree) { 133 var sb = new StringBuilder(); 134 ConvertTreeToSentence(tree.Root.GetSubtree(0).GetSubtree(0), sb); 135 return sb.ToString(); 136 } 137 private void ConvertTreeToSentence(ISymbolicExpressionTreeNode node, StringBuilder sb) { 138 if (node.SubtreeCount == 0) { 139 // terminal 140 sb.Append(node.Symbol.Name); 141 } else if (node.Symbol.Name == "I") { 142 Debug.Assert(node.SubtreeCount == 2); 143 sb.Append("?("); 144 ConvertTreeToSentence(node.GetSubtree(0), sb); 145 sb.Append(")("); 146 ConvertTreeToSentence(node.GetSubtree(1), sb); 147 sb.Append(")"); 148 } else { 149 foreach (var subTree in node.Subtrees) { ConvertTreeToSentence(subTree, sb); } 150 } 118 151 } 119 152 } -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/SentenceSetStatistics.cs
r11846 r11847 19 19 // public double MedianQuality { get; private set; } 20 20 private double sumQualities; 21 22 private readonly double bestKnownQuality; 23 public SentenceSetStatistics(double bestKnownQuality = 1.0) { 24 this.bestKnownQuality = bestKnownQuality; 25 } 21 26 22 27 public void AddSentence(string sentence, double quality) { … … 43 48 string.Format("Sentences: {0,10} avg.-quality {1,7:F5} best {2,7:F5} {3,2} {4,10} {5,30} first {6,7:F5} {7,20} last {8,7:F5} {9,20}", 44 49 NumberOfSentences, AverageQuality, 45 BestSentenceQuality, DoubleExtensions.IsAlmost(BestSentenceQuality, 1.0) ? 1.0 : 0.0,50 BestSentenceQuality, DoubleExtensions.IsAlmost(BestSentenceQuality, bestKnownQuality) ? 1.0 : 0.0, 46 51 BestSentenceIndex, TrimToSize(BestSentence, 30), 47 52 FirstSentenceQuality, TrimToSize(FirstSentence, 20), -
branches/HeuristicLab.Problems.GrammaticalOptimization/Main/Main.csproj
r11846 r11847 36 36 </PropertyGroup> 37 37 <ItemGroup> 38 <Reference Include="HeuristicLab.Algorithms.GeneticAlgorithm-3.3">39 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Algorithms.GeneticAlgorithm-3.3.dll</HintPath>40 </Reference>41 38 <Reference Include="System" /> 42 39 <Reference Include="System.Core" /> -
branches/HeuristicLab.Problems.GrammaticalOptimization/Main/Program.cs
r11846 r11847 303 303 const int maxIterations = 100000; 304 304 const int seed = 31415; 305 var globalStatistics = new SentenceSetStatistics( );306 307 var gp = new StandardGP(new SymbolicRegressionPoly10Problem(), new Random(seed));305 var globalStatistics = new SentenceSetStatistics(512); 306 307 var gp = new StandardGP(new HardPalindromeProblem(), new Random(seed)); 308 308 gp.FoundNewBestSolution += (sentence, quality) => { 309 309 Console.WriteLine("{0}", globalStatistics); … … 317 317 } 318 318 }; 319 320 gp.PopulationSize = 1000; 321 gp.MaxSolutionSize = 31 + 2; 322 gp.MaxSolutionDepth = 20; 319 323 320 324 var sw = new Stopwatch();
Note: See TracChangeset
for help on using the changeset viewer.