Changeset 11846
- Timestamp:
- 02/01/15 16:32:26 (10 years ago)
- Location:
- branches/HeuristicLab.Problems.GrammaticalOptimization
- Files:
-
- 13 added
- 13 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Problems.GrammaticalOptimization/GrammaticalOptimization.sln
r11732 r11846 15 15 EndProject 16 16 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HeuristicLab.Problems.GrammaticalOptimization.SymbReg", "HeuristicLab.Problems.GrammaticalOptimization.SymbReg\HeuristicLab.Problems.GrammaticalOptimization.SymbReg.csproj", "{17A7A380-86CE-482D-8D22-CBD70CC97F0D}" 17 EndProject 18 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HeuristicLab.Algorithms.GeneticProgramming", "HeuristicLab.Algorithms.GeneticProgramming\HeuristicLab.Algorithms.GeneticProgramming.csproj", "{14BEC23F-63FD-4954-B8AE-E2F4962E9B57}" 17 19 EndProject 18 20 Global … … 50 52 {17A7A380-86CE-482D-8D22-CBD70CC97F0D}.Release|Any CPU.ActiveCfg = Release|Any CPU 51 53 {17A7A380-86CE-482D-8D22-CBD70CC97F0D}.Release|Any CPU.Build.0 = Release|Any CPU 54 {14BEC23F-63FD-4954-B8AE-E2F4962E9B57}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 55 {14BEC23F-63FD-4954-B8AE-E2F4962E9B57}.Debug|Any CPU.Build.0 = Debug|Any CPU 56 {14BEC23F-63FD-4954-B8AE-E2F4962E9B57}.Release|Any CPU.ActiveCfg = Release|Any CPU 57 {14BEC23F-63FD-4954-B8AE-E2F4962E9B57}.Release|Any CPU.Build.0 = Release|Any CPU 52 58 EndGlobalSection 53 59 GlobalSection(SolutionProperties) = preSolution -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GrammaticalOptimization/HeuristicLab.Algorithms.GrammaticalOptimization.csproj
r11806 r11846 45 45 <Compile Include="AlternativesSampler.cs" /> 46 46 <Compile Include="AlternativesContextSampler.cs" /> 47 <Compile Include="SolverBase.cs" /> 48 <Compile Include="ISolver.cs" /> 47 49 <Compile Include="SequentialSearch.cs" /> 48 50 <Compile Include="ExhaustiveRandomFirstSearch.cs" /> -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GrammaticalOptimization/RandomSearch.cs
r11793 r11846 3 3 4 4 namespace HeuristicLab.Algorithms.GrammaticalOptimization { 5 public class RandomSearch { 6 public event Action<string, double> FoundNewBestSolution; 7 public event Action<string, double> SolutionEvaluated; 8 5 public class RandomSearch : SolverBase { 9 6 private readonly int maxLen; 10 7 private readonly Random random; … … 17 14 } 18 15 19 public void Run(int maxIterations) { 20 double bestQuality = double.MinValue; 16 public override void Run(int maxIterations) { 21 17 for (int i = 0; i < maxIterations; i++) { 22 18 var sentence = CreateSentence(problem.Grammar).ToString(); 23 19 var quality = problem.Evaluate(sentence) / problem.BestKnownQuality(maxLen); 24 RaiseSolutionEvaluated(sentence, quality); 25 26 if (quality > bestQuality) { 27 bestQuality = quality; 28 RaiseFoundNewBestSolution(sentence, quality); 29 } 20 OnSolutionEvaluated(sentence, quality); 30 21 } 31 22 } … … 35 26 return grammar.CompleteSentenceRandomly(random, sentence, maxLen); 36 27 } 37 38 private void RaiseSolutionEvaluated(string sentence, double quality) {39 var handler = SolutionEvaluated;40 if (handler != null) handler(sentence, quality);41 }42 private void RaiseFoundNewBestSolution(string sentence, double quality) {43 var handler = FoundNewBestSolution;44 if (handler != null) handler(sentence, quality);45 }46 28 } 47 29 } -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GrammaticalOptimization/SequentialSearch.cs
r11806 r11846 23 23 // ... until phrase is terminal 24 24 // 3) Collect reward and update policy (feedback: state of visited rewards from step 2) 25 public class SequentialSearch {25 public class SequentialSearch : SolverBase { 26 26 // only for storing states so that it is not necessary to allocate new state strings whenever we select a follow state using the policy 27 27 private class TreeNode { … … 37 37 } 38 38 39 40 public event Action<string, double> FoundNewBestSolution;41 public event Action<string, double> SolutionEvaluated;42 39 43 40 private readonly int maxLen; … … 52 49 private int maxSearchDepth; 53 50 54 private double bestQuality;55 51 private string bestPhrase; 56 52 private readonly List<string> stateChain; … … 66 62 } 67 63 68 public void Run(int maxIterations) { 69 bestQuality = double.MinValue; 64 public override void Run(int maxIterations) { 70 65 Reset(); 71 66 … … 79 74 if (double.IsNaN(quality)) quality = 0.0; 80 75 Debug.Assert(quality >= 0 && quality <= 1.0); 76 77 if (quality > bestQuality) { 78 bestPhrase = sentence; 79 } 80 81 OnSolutionEvaluated(sentence, quality); 81 82 DistributeReward(quality); 82 83 83 RaiseSolutionEvaluated(sentence, quality);84 85 if (quality > bestQuality) {86 bestQuality = quality;87 bestPhrase = sentence;88 RaiseFoundNewBestSolution(sentence, quality);89 }90 84 } 91 85 } … … 244 238 } 245 239 #endregion 246 247 private void RaiseSolutionEvaluated(string sentence, double quality) { 248 var handler = SolutionEvaluated; 249 if (handler != null) handler(sentence, quality); 250 } 251 private void RaiseFoundNewBestSolution(string sentence, double quality) { 252 var handler = FoundNewBestSolution; 253 if (handler != null) handler(sentence, quality); 254 } 240 255 241 } 256 242 } -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Common/Extensions.cs
r11799 r11846 7 7 namespace HeuristicLab.Common { 8 8 public static class Extensions { 9 9 10 public static bool IsAlmost(this double x, double y) { 10 11 return Math.Abs(x - y) < 1.0e-12; -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization.Test/HeuristicLab.Problems.GrammaticalOptimization.Test.csproj
r11732 r11846 39 39 </PropertyGroup> 40 40 <ItemGroup> 41 <Reference Include="HeuristicLab.Common-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 42 <SpecificVersion>False</SpecificVersion> 43 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Common-3.3.dll</HintPath> 44 </Reference> 45 <Reference Include="HeuristicLab.Core-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 46 <SpecificVersion>False</SpecificVersion> 47 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Core-3.3.dll</HintPath> 48 </Reference> 49 <Reference Include="HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4, Version=3.4.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 50 <SpecificVersion>False</SpecificVersion> 51 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.dll</HintPath> 52 </Reference> 41 53 <Reference Include="HeuristicLab.Problems.Instances-3.3"> 42 54 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Problems.Instances-3.3.dll</HintPath> -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization.csproj
r11842 r11846 34 34 </PropertyGroup> 35 35 <ItemGroup> 36 <Reference Include="HeuristicLab.Collections-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 37 <SpecificVersion>False</SpecificVersion> 38 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Collections-3.3.dll</HintPath> 39 </Reference> 40 <Reference Include="HeuristicLab.Common-3.3"> 41 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Common-3.3.dll</HintPath> 42 </Reference> 43 <Reference Include="HeuristicLab.Core-3.3"> 44 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Core-3.3.dll</HintPath> 45 </Reference> 46 <Reference Include="HeuristicLab.Data-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 47 <SpecificVersion>False</SpecificVersion> 48 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Data-3.3.dll</HintPath> 49 </Reference> 50 <Reference Include="HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4"> 51 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.dll</HintPath> 52 </Reference> 53 <Reference Include="HeuristicLab.Operators-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 54 <SpecificVersion>False</SpecificVersion> 55 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Operators-3.3.dll</HintPath> 56 </Reference> 57 <Reference Include="HeuristicLab.Optimization-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 58 <SpecificVersion>False</SpecificVersion> 59 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Optimization-3.3.dll</HintPath> 60 </Reference> 61 <Reference Include="HeuristicLab.Parameters-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 62 <SpecificVersion>False</SpecificVersion> 63 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Parameters-3.3.dll</HintPath> 64 </Reference> 65 <Reference Include="HeuristicLab.Persistence-3.3"> 66 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Persistence-3.3.dll</HintPath> 67 </Reference> 68 <Reference Include="HeuristicLab.PluginInfrastructure-3.3"> 69 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.PluginInfrastructure-3.3.dll</HintPath> 70 </Reference> 71 <Reference Include="HeuristicLab.SequentialEngine-3.3"> 72 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.SequentialEngine-3.3.dll</HintPath> 73 </Reference> 36 74 <Reference Include="System" /> 37 75 <Reference Include="System.Core" /> 76 <Reference Include="System.Drawing" /> 38 77 <Reference Include="System.Xml.Linq" /> 39 78 <Reference Include="System.Data.DataSetExtensions" /> … … 43 82 </ItemGroup> 44 83 <ItemGroup> 84 <Compile Include="ISymbolicExpressionTreeProblem.cs" /> 45 85 <Compile Include="Feature.cs" /> 86 <Compile Include="Problems\GrammaticalOptimizationEvaluator.cs" /> 87 <Compile Include="Problems\IGrammaticalOptimizationEvaluator.cs" /> 88 <Compile Include="Problems\GenericSymbExprProblem.cs" /> 89 <Compile Include="Problems\GenericSymbExprGrammar.cs" /> 46 90 <Compile Include="Problems\EvenParityProblem.cs" /> 47 91 <Compile Include="Problems\ExpressionInterpreter.cs" /> -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/IProblem.cs
r11832 r11846 3 3 using System.Linq; 4 4 using System.Text; 5 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 5 6 6 7 namespace HeuristicLab.Problems.GrammaticalOptimization { 8 // represents a grammatical optimization problem 7 9 public interface IProblem { 8 10 double BestKnownQuality(int maxLen); -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/ExpressionInterpreter.cs
r11832 r11846 35 35 var d = new double[vars.Length]; 36 36 for (int i = 0; i < vars.Length; i++) d[i] = vars[i] ? 1.0 : 0.0; 37 return ! (Expr(d, erc).IsAlmost(0));37 return ! DoubleExtensions.IsAlmost(Expr(d, erc), 0); 38 38 } 39 39 … … 63 63 // helper for xor 64 64 private double Not(double x) { 65 return x.IsAlmost(0) ? 1.0 : 0.0;65 return DoubleExtensions.IsAlmost(x, 0) ? 1.0 : 0.0; 66 66 } 67 67 -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/SymbolicRegressionPoly10Problem.cs
r11832 r11846 3 3 using System.Collections.Generic; 4 4 using System.Collections.Specialized; 5 using System.Diagnostics; 5 6 using System.Linq; 6 7 using System.Net; … … 9 10 using System.Text; 10 11 using HeuristicLab.Common; 12 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 11 13 12 14 namespace HeuristicLab.Problems.GrammaticalOptimization { 13 public class SymbolicRegressionPoly10Problem : I Problem {15 public class SymbolicRegressionPoly10Problem : ISymbolicExpressionTreeProblem { 14 16 // private const string grammarString = @" 15 17 // G(E): … … 22 24 "; 23 25 26 // for tree-based GP in HL we need a different grammar for the same language 27 // E = expr, S = sum, P = product 28 private const string hlGrammarString = @" 29 G(E): 30 E -> S | P | a | b | c | d | e | f | g | h | i | j 31 S -> EE | EEE 32 P -> EE | EEE 33 "; 34 // mininmal tree for the optimal expr (40 nodes) 35 // E S 36 // E S 37 // E P 38 // E a E b 39 // E P 40 // E c E d 41 // E P 42 // E e E f 43 // E S 44 // E P 45 // E a E g E i 46 // E P 47 // E c E f E j 48 49 private SymbolicExpressionGrammar symbExprGrammar; 50 public ISymbolicExpressionGrammar SymbolicExpressionGrammar { 51 get { 52 return symbExprGrammar; 53 } 54 } 24 55 25 56 private readonly IGrammar grammar; … … 31 62 public SymbolicRegressionPoly10Problem() { 32 63 this.grammar = new Grammar(grammarString); 64 this.symbExprGrammar = new GenericSymbExprGrammar(new Grammar(hlGrammarString)); 33 65 34 66 this.N = 500; … … 71 103 return HeuristicLab.Common.Extensions.RSq(y, Enumerable.Range(0, N).Select(i => interpreter.Interpret(sentence, x[i])).ToArray()); 72 104 } 73 74 105 75 106 … … 130 161 } 131 162 163 public string ConvertTreeToSentence(ISymbolicExpressionTree tree) { 164 var sb = new StringBuilder(); 165 166 TreeToSentence(tree.Root.GetSubtree(0).GetSubtree(0), sb); 167 168 return sb.ToString(); 169 } 170 171 private void TreeToSentence(ISymbolicExpressionTreeNode treeNode, StringBuilder sb) { 172 if (treeNode.SubtreeCount == 0) { 173 // terminal 174 sb.Append(treeNode.Symbol.Name); 175 } else { 176 string op = string.Empty; 177 switch (treeNode.Symbol.Name) { 178 case "S": op = "+"; break; 179 case "P": op = "*"; break; 180 default: { 181 Debug.Assert(treeNode.SubtreeCount == 1); 182 break; 183 } 184 } 185 // nonterminal 186 if (op == "+") sb.Append("("); 187 TreeToSentence(treeNode.Subtrees.First(), sb); 188 foreach (var subTree in treeNode.Subtrees.Skip(1)) { 189 sb.Append(op); 190 TreeToSentence(subTree, sb); 191 } 192 if (op == "+") sb.Append(")"); 193 } 194 } 132 195 } 133 196 } -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/SentenceSetStatistics.cs
r11730 r11846 41 41 public override string ToString() { 42 42 return 43 string.Format("Sentences: {0,10} avg.-quality {1,7:F5} best {2,7:F5} {3,2} {4,10} {5 } first {6,7:F5} {7} last {8,7:F5} {9}",43 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 44 NumberOfSentences, AverageQuality, 45 BestSentenceQuality, BestSentenceQuality.IsAlmost(1.0)?1.0:0.0,46 BestSentenceIndex, BestSentence,47 FirstSentenceQuality, FirstSentence,48 LastSentenceQuality, LastSentence45 BestSentenceQuality, DoubleExtensions.IsAlmost(BestSentenceQuality, 1.0) ? 1.0 : 0.0, 46 BestSentenceIndex, TrimToSize(BestSentence, 30), 47 FirstSentenceQuality, TrimToSize(FirstSentence, 20), 48 LastSentenceQuality, TrimToSize(LastSentence, 20) 49 49 ); 50 } 51 52 private string TrimToSize(string s, int len) { 53 if (s.Length < len) return s; 54 else { 55 var sb = new StringBuilder(len); 56 sb.Append(s.Substring(0, len - 3)); 57 sb.Append("..."); 58 return sb.ToString(); 59 } 50 60 } 51 61 } -
branches/HeuristicLab.Problems.GrammaticalOptimization/Main/Main.csproj
r11732 r11846 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> 38 41 <Reference Include="System" /> 39 42 <Reference Include="System.Core" /> 40 <Reference Include="System.Xml.Linq" />41 <Reference Include="System.Data.DataSetExtensions" />42 <Reference Include="Microsoft.CSharp" />43 <Reference Include="System.Data" />44 <Reference Include="System.Xml" />45 43 </ItemGroup> 46 44 <ItemGroup> … … 52 50 <Project>{24408F7D-EE0F-4886-A08B-EC324D662E47}</Project> 53 51 <Name>HeuristicLab.Algorithms.Bandits</Name> 52 </ProjectReference> 53 <ProjectReference Include="..\HeuristicLab.Algorithms.GeneticProgramming\HeuristicLab.Algorithms.GeneticProgramming.csproj"> 54 <Project>{14BEC23F-63FD-4954-B8AE-E2F4962E9B57}</Project> 55 <Name>HeuristicLab.Algorithms.GeneticProgramming</Name> 54 56 </ProjectReference> 55 57 <ProjectReference Include="..\HeuristicLab.Algorithms.GrammaticalOptimization\HeuristicLab.Algorithms.GrammaticalOptimization.csproj"> -
branches/HeuristicLab.Problems.GrammaticalOptimization/Main/Program.cs
r11832 r11846 1 1 using System; 2 2 using System.Collections.Generic; 3 using System.Data;4 3 using System.Diagnostics; 5 4 using System.Globalization; 6 using System.Linq;7 5 using System.Text; 6 using System.Threading; 8 7 using System.Threading.Tasks; 9 8 using HeuristicLab.Algorithms.Bandits; … … 11 10 using HeuristicLab.Algorithms.Bandits.GrammarPolicies; 12 11 using HeuristicLab.Algorithms.Bandits.Models; 12 using HeuristicLab.Algorithms.GeneticProgramming; 13 13 using HeuristicLab.Algorithms.GrammaticalOptimization; 14 14 using HeuristicLab.Problems.GrammaticalOptimization; 15 using HeuristicLab.Problems.GrammaticalOptimization.SymbReg;16 15 using BoltzmannExplorationPolicy = HeuristicLab.Algorithms.Bandits.BanditPolicies.BoltzmannExplorationPolicy; 17 16 using EpsGreedyPolicy = HeuristicLab.Algorithms.Bandits.BanditPolicies.EpsGreedyPolicy; 17 using IProblem = HeuristicLab.Problems.GrammaticalOptimization.IProblem; 18 18 using RandomPolicy = HeuristicLab.Algorithms.Bandits.BanditPolicies.RandomPolicy; 19 19 using UCTPolicy = HeuristicLab.Algorithms.Bandits.BanditPolicies.UCTPolicy; … … 25 25 26 26 //RunDemo(); 27 RunGridTest(); 27 RunGpDemo(); 28 // RunGridTest(); 28 29 } 29 30 … … 296 297 (double)sw.ElapsedMilliseconds * 1000 / maxIterations); 297 298 } 299 300 public static void RunGpDemo() { 301 302 int iterations = 0; 303 const int maxIterations = 100000; 304 const int seed = 31415; 305 var globalStatistics = new SentenceSetStatistics(); 306 307 var gp = new StandardGP(new SymbolicRegressionPoly10Problem(), new Random(seed)); 308 gp.FoundNewBestSolution += (sentence, quality) => { 309 Console.WriteLine("{0}", globalStatistics); 310 }; 311 gp.SolutionEvaluated += (sentence, quality) => { 312 iterations++; 313 globalStatistics.AddSentence(sentence, quality); 314 315 if (iterations % 10000 == 0) { 316 Console.WriteLine("{0}", globalStatistics); 317 } 318 }; 319 320 var sw = new Stopwatch(); 321 322 sw.Start(); 323 gp.Run(maxIterations); 324 sw.Stop(); 325 326 Console.WriteLine(globalStatistics); 327 Console.WriteLine("{0:F2} sec {1,10:F1} sols/sec {2,10:F1} ns/sol", 328 sw.Elapsed.TotalSeconds, 329 maxIterations / (double)sw.Elapsed.TotalSeconds, 330 (double)sw.ElapsedMilliseconds * 1000 / maxIterations); 331 } 298 332 } 299 333 }
Note: See TracChangeset
for help on using the changeset viewer.