Changeset 11846 for branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GrammaticalOptimization
- Timestamp:
- 02/01/15 16:32:26 (10 years ago)
- Location:
- branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GrammaticalOptimization
- Files:
-
- 2 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
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 }
Note: See TracChangeset
for help on using the changeset viewer.