Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/01/15 16:32:26 (9 years ago)
Author:
gkronber
Message:

#2283 implemented bridge to HL (solve grammatical optimization problem instances with StandardGP and OffspringSelectionGP)

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  
    4545    <Compile Include="AlternativesSampler.cs" />
    4646    <Compile Include="AlternativesContextSampler.cs" />
     47    <Compile Include="SolverBase.cs" />
     48    <Compile Include="ISolver.cs" />
    4749    <Compile Include="SequentialSearch.cs" />
    4850    <Compile Include="ExhaustiveRandomFirstSearch.cs" />
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GrammaticalOptimization/RandomSearch.cs

    r11793 r11846  
    33
    44namespace 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 {
    96    private readonly int maxLen;
    107    private readonly Random random;
     
    1714    }
    1815
    19     public void Run(int maxIterations) {
    20       double bestQuality = double.MinValue;
     16    public override void Run(int maxIterations) {
    2117      for (int i = 0; i < maxIterations; i++) {
    2218        var sentence = CreateSentence(problem.Grammar).ToString();
    2319        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);
    3021      }
    3122    }
     
    3526      return grammar.CompleteSentenceRandomly(random, sentence, maxLen);
    3627    }
    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     }
    4628  }
    4729}
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GrammaticalOptimization/SequentialSearch.cs

    r11806 r11846  
    2323  //    ... until phrase is terminal
    2424  // 3) Collect reward and update policy (feedback: state of visited rewards from step 2)
    25   public class SequentialSearch {
     25  public class SequentialSearch : SolverBase {
    2626    // only for storing states so that it is not necessary to allocate new state strings whenever we select a follow state using the policy
    2727    private class TreeNode {
     
    3737    }
    3838
    39 
    40     public event Action<string, double> FoundNewBestSolution;
    41     public event Action<string, double> SolutionEvaluated;
    4239
    4340    private readonly int maxLen;
     
    5249    private int maxSearchDepth;
    5350
    54     private double bestQuality;
    5551    private string bestPhrase;
    5652    private readonly List<string> stateChain;
     
    6662    }
    6763
    68     public void Run(int maxIterations) {
    69       bestQuality = double.MinValue;
     64    public override void Run(int maxIterations) {
    7065      Reset();
    7166
     
    7974          if (double.IsNaN(quality)) quality = 0.0;
    8075          Debug.Assert(quality >= 0 && quality <= 1.0);
     76
     77          if (quality > bestQuality) {
     78            bestPhrase = sentence;
     79          }
     80
     81          OnSolutionEvaluated(sentence, quality);
    8182          DistributeReward(quality);
    8283
    83           RaiseSolutionEvaluated(sentence, quality);
    84 
    85           if (quality > bestQuality) {
    86             bestQuality = quality;
    87             bestPhrase = sentence;
    88             RaiseFoundNewBestSolution(sentence, quality);
    89           }
    9084        }
    9185      }
     
    244238    }
    245239    #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 
    255241  }
    256242}
Note: See TracChangeset for help on using the changeset viewer.