Changeset 11895 for branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GeneticProgramming
- Timestamp:
- 02/05/15 07:03:15 (10 years ago)
- Location:
- branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GeneticProgramming
- Files:
-
- 1 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GeneticProgramming/GenericSymbExprEvaluator.cs
r11857 r11895 6 6 using HeuristicLab.Operators; 7 7 using HeuristicLab.Parameters; 8 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 8 9 using HeuristicLab.PluginInfrastructure; 9 10 10 11 namespace HeuristicLab.Problems.GrammaticalOptimization { 11 // this type is not storable because we use a func<ITree,double> for evaluation, which references back to the original grammatical optimization problem12 12 [NonDiscoverableType] 13 [StorableClass] 13 14 [Item("GenericSymbExprEvaluator", "Evaluator for grammatical optimization problems (using a symbolic expression tree encoding).")] 14 15 public class GenericSymbExprEvaluator : SingleSuccessorOperator, IGenericSymbExprEvaluator { … … 22 23 } 23 24 25 // cannot save these (eval won't work when loaded from file 24 26 private Func<string, double> evalFunc; 25 27 private Func<ISymbolicExpressionTree, string> toStringFunc; 26 28 29 [StorableConstructor] 30 private GenericSymbExprEvaluator(bool deserializing) : base(deserializing) { } 27 31 public GenericSymbExprEvaluator(GenericSymbExprEvaluator original, Cloner cloner) 28 32 : base(original, cloner) { -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GeneticProgramming/HeuristicLab.Algorithms.GeneticProgramming.csproj
r11857 r11895 36 36 <Reference Include="HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm-3.3"> 37 37 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm-3.3.dll</HintPath> 38 </Reference> 39 <Reference Include="HeuristicLab.Analysis-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 40 <SpecificVersion>False</SpecificVersion> 41 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Analysis-3.3.dll</HintPath> 38 42 </Reference> 39 43 <Reference Include="HeuristicLab.Collections-3.3"> … … 75 79 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Selection-3.3.dll</HintPath> 76 80 </Reference> 81 <Reference Include="HeuristicLab.SequentialEngine-3.3"> 82 <HintPath>..\..\..\..\..\Program Files\HeuristicLab 3.3\HeuristicLab.SequentialEngine-3.3.dll</HintPath> 83 </Reference> 77 84 <Reference Include="System" /> 78 85 <Reference Include="System.Core" /> … … 80 87 </ItemGroup> 81 88 <ItemGroup> 89 <Compile Include="IGPSolver.cs" /> 82 90 <Compile Include="GenericSymbExprEvaluator.cs" /> 83 91 <Compile Include="GenericSymbExprGrammar.cs" /> -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GeneticProgramming/OffspringSelectionGP.cs
r11851 r11895 1 1 using System; 2 using System.IO; 3 using System.IO.Compression; 2 4 using System.Linq; 3 5 using System.Threading; 4 6 using HeuristicLab.Algorithms.GrammaticalOptimization; 7 using HeuristicLab.Analysis; 8 using HeuristicLab.Common; 5 9 using HeuristicLab.Data; 6 10 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 7 using HeuristicLab. Optimization;11 using HeuristicLab.Persistence.Default.Xml; 8 12 using HeuristicLab.Problems.GrammaticalOptimization; 9 using HeuristicLab.Selection;10 using HeuristicLab.Algorithms.GeneticAlgorithm;11 13 12 14 namespace HeuristicLab.Algorithms.GeneticProgramming { 13 public class OffspringSelectionGP : SolverBase {15 public class OffspringSelectionGP : SolverBase, IGPSolver { 14 16 public int PopulationSize { get; set; } 15 17 public double MutationRate { get; set; } … … 19 21 private readonly ISymbolicExpressionTreeProblem problem; 20 22 private readonly Random random; 23 private readonly bool saveAlg; 21 24 22 public OffspringSelectionGP(ISymbolicExpressionTreeProblem problem, Random random ) {25 public OffspringSelectionGP(ISymbolicExpressionTreeProblem problem, Random random, bool saveAlg = false) { 23 26 this.problem = problem; 24 27 this.random = random; … … 28 31 MaxSolutionSize = 100; 29 32 MaxSolutionDepth = 17; 33 this.saveAlg = saveAlg; 30 34 } 31 35 … … 33 37 var hlProblem = new GenericSymbExprProblem(problem); 34 38 var onEvalLocker = new object(); 35 hlProblem.Evaluator.SolutionEvaluated += (sentence, quality) => {36 // raise solution evaluated event for each GP solution, don't scale quality to 0..137 // need to synchronize in case we are using a parallel engine38 lock (onEvalLocker) {39 OnSolutionEvaluated(sentence, quality);40 }41 };42 39 hlProblem.MaximumSymbolicExpressionTreeLength.Value = MaxSolutionSize; 43 40 hlProblem.MaximumSymbolicExpressionTreeDepth.Value = MaxSolutionDepth; … … 45 42 using (var wh = new AutoResetEvent(false)) { 46 43 var osga = new OffspringSelectionGeneticAlgorithm.OffspringSelectionGeneticAlgorithm(); 47 osga.Engine = new ParallelEngine.ParallelEngine(); 44 // osga.Engine = new ParallelEngine.ParallelEngine(); 45 osga.Engine = new SequentialEngine.SequentialEngine(); 48 46 osga.ExceptionOccurred += (sender, args) => { Console.WriteLine(args.Value.Message); wh.Set(); }; 49 47 osga.Stopped += (sender, args) => { wh.Set(); }; 48 49 int numEvals = 0; 50 hlProblem.Evaluator.SolutionEvaluated += (sentence, quality) => { 51 // raise solution evaluated event for each GP solution, don't scale quality to 0..1 52 // need to synchronize in case we are using a parallel engine 53 lock (onEvalLocker) { 54 OnSolutionEvaluated(sentence, quality); 55 56 // stop when maxEvals has been reached 57 if (numEvals++ >= maxEvaluations) { 58 osga.Stop(); 59 } 60 } 61 }; 62 50 63 51 64 osga.Problem = hlProblem; … … 59 72 osga.Crossover = osga.CrossoverParameter.ValidValues.Single(op => op.Name == "SubtreeSwappingCrossover"); 60 73 osga.Selector = osga.SelectorParameter.ValidValues.Single(op => op.Name == "GenderSpecificSelection"); 74 var multiAnalzer = (MultiAnalyzer)osga.Analyzer; 75 multiAnalzer.Operators.Add(new BestSymbolicExpressionTreeAnalyzer()); 61 76 62 77 osga.PopulationSize.Value = PopulationSize; 63 osga.MaximumGenerations.Value = 100000 ; // some very large value (we stop based on evaluations)64 osga.MaximumSelectionPressure.Value = 1000 ;78 osga.MaximumGenerations.Value = 1000000; // some very large value (we stop based on evaluations) 79 osga.MaximumSelectionPressure.Value = 1000000; 65 80 osga.MaximumEvaluatedSolutions.Value = maxEvaluations; 66 81 osga.MutationProbability.Value = MutationRate; 82 osga.ComparisonFactorLowerBound.Value = 1.0; 83 osga.ComparisonFactorUpperBound.Value = 1.0; 84 osga.SuccessRatio.Value = 1.0; 67 85 68 86 osga.SetSeedRandomly = new BoolValue(false); … … 73 91 74 92 wh.WaitOne(); 93 94 95 if (saveAlg) { 96 var path = @"C:\Users\P24581\Desktop"; 97 var fileName = string.Format("osgp-{0}{1:D2}{2:D2}{3:D2}{4:D2}.hl", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute); 98 var fullPath = Path.Combine(path, fileName); 99 HeuristicLab.Persistence.Core.ConfigurationService.Instance.LoadSettings(); 100 XmlGenerator.Serialize(osga, fullPath, CompressionLevel.Fastest); 101 } 75 102 } 76 103 } -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GeneticProgramming/StandardGP.cs
r11851 r11895 1 1 using System; 2 using System.IO; 3 using System.IO.Compression; 2 4 using System.Linq; 3 5 using System.Threading; 4 6 using HeuristicLab.Algorithms.GrammaticalOptimization; 7 using HeuristicLab.Common; 5 8 using HeuristicLab.Data; 6 9 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 7 using HeuristicLab. Optimization;10 using HeuristicLab.Persistence.Default.Xml; 8 11 using HeuristicLab.Problems.GrammaticalOptimization; 9 12 using HeuristicLab.Selection; 10 using HeuristicLab.Algorithms.GeneticAlgorithm;11 13 12 14 namespace HeuristicLab.Algorithms.GeneticProgramming { 13 public class StandardGP : SolverBase {15 public class StandardGP : SolverBase, IGPSolver { 14 16 public int PopulationSize { get; set; } 15 17 public double MutationRate { get; set; } … … 20 22 private readonly ISymbolicExpressionTreeProblem problem; 21 23 private readonly Random random; 24 private readonly bool saveAlg; 22 25 23 public StandardGP(ISymbolicExpressionTreeProblem problem, Random random ) {26 public StandardGP(ISymbolicExpressionTreeProblem problem, Random random, bool saveAlg = false) { 24 27 this.problem = problem; 25 28 this.random = random; … … 30 33 MaxSolutionSize = 100; 31 34 MaxSolutionDepth = 17; 35 this.saveAlg = saveAlg; 32 36 } 33 37 … … 35 39 var hlProblem = new GenericSymbExprProblem(problem); 36 40 var onEvalLocker = new object(); 37 hlProblem.Evaluator.SolutionEvaluated += (sentence, quality) => {38 // raise solution evaluated event for each GP solution, don't scale quality to 0..139 // need to synchronize in case we are using a parallel engine40 lock (onEvalLocker) {41 OnSolutionEvaluated(sentence, quality);42 }43 };44 41 hlProblem.MaximumSymbolicExpressionTreeLength.Value = MaxSolutionSize; 45 42 hlProblem.MaximumSymbolicExpressionTreeDepth.Value = MaxSolutionDepth; … … 51 48 ga.ExceptionOccurred += (sender, args) => { Console.WriteLine(args.Value.Message); wh.Set(); }; 52 49 ga.Stopped += (sender, args) => { wh.Set(); }; 50 51 int numEvals = 0; 52 hlProblem.Evaluator.SolutionEvaluated += (sentence, quality) => { 53 // raise solution evaluated event for each GP solution, don't scale quality to 0..1 54 // need to synchronize in case we are using a parallel engine 55 lock (onEvalLocker) { 56 OnSolutionEvaluated(sentence, quality); 57 58 // stop when maxEvals has been reached 59 if (numEvals++ >= maxEvaluations) { 60 ga.Stop(); 61 } 62 } 63 }; 64 53 65 54 66 ga.Problem = hlProblem; … … 66 78 67 79 ga.PopulationSize.Value = PopulationSize; 68 ga.MaximumGenerations.Value = maxEvaluations / PopulationSize + 1; // one extra generation in case maxEvaluations is not a multiple of PopulationSize80 ga.MaximumGenerations.Value = 1000000; // very large value (we stop in the evaluate handler) 69 81 ga.MutationProbability.Value = MutationRate; 70 82 … … 76 88 77 89 wh.WaitOne(); 90 91 92 if (saveAlg) { 93 var path = @"C:\Users\P24581\Desktop"; 94 var fileName = string.Format("osgp-{0}{1:D2}{2:D2}{3:D2}{4:D2}.hl", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute); 95 var fullPath = Path.Combine(path, fileName); 96 HeuristicLab.Persistence.Core.ConfigurationService.Instance.LoadSettings(); 97 XmlGenerator.Serialize(ga, fullPath, CompressionLevel.Fastest); 98 } 78 99 } 79 100 }
Note: See TracChangeset
for help on using the changeset viewer.