Changeset 15273 for branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Problem
- Timestamp:
- 07/19/17 12:55:58 (8 years ago)
- Location:
- branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Problem
- Files:
-
- 1 deleted
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Problem/BenchmarkSuite/BenchmarkSuitePushSolutionView.cs
r15189 r15273 53 53 var example = Evaluator.Data.Examples[exampleComboBox.SelectedIndex]; 54 54 55 interpreter.SetInput( 56 integers: example.InputInteger, 57 floats: example.InputFloat, 58 booleans: example.InputBoolean, 59 chars: example.InputChar, 60 strings: example.InputString, 61 integerVectors: example.InputIntegerVector, 62 floatVectors: example.InputFloatVector, 63 stringVectors: example.InputStringVector); 64 65 //interpreter.BooleanStack.Push(example.InputBoolean); 66 //interpreter.IntegerStack.Push(example.InputInteger); 67 //interpreter.FloatStack.Push(example.InputFloat); 68 //interpreter.CharStack.Push(example.InputChar); 69 //interpreter.StringStack.Push(example.InputString); 70 //interpreter.IntegerVectorStack.Push(example.InputIntegerVector.Select(x => x.ToList()).ToList()); 71 //interpreter.FloatVectorStack.Push(example.InputFloatVector.Select(x => x.ToList()).ToList()); 72 //interpreter.StringVectorStack.Push(example.InputStringVector.Select(x => x.ToList()).ToList()); 73 //interpreter.BooleanVectorStack.Push(example.InputBooleanVector.Select(x => x.ToList()).ToList()); 55 interpreter.InitExample(example); 74 56 } 75 57 … … 92 74 } 93 75 76 94 77 protected override void OnContentChanged() { 95 78 if (Content == null) return; … … 104 87 } 105 88 106 interpreter = pool.Create(Content.Random); 89 var random = Content.GetRandom(); 90 interpreter = pool.Create(random); 107 91 UpdateExamples(Evaluator.Data); 108 92 … … 181 165 grid.Columns.Add(relativeDiffColumn); 182 166 183 using (var pushInterpreter = pool.Create(Content.Random)) { 167 var random = Content.GetRandom(); 168 using (var pushInterpreter = pool.Create(random)) { 184 169 var rowIndex = 1; 185 170 for (var i = start; i < end; i++, rowIndex++) { -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Problem/BenchmarkSuite/PushBenchmarkSuiteEvaluator.cs
r15189 r15273 131 131 var example = Data.Examples[exampleIndex]; 132 132 133 //interpreter.BooleanStack.Push(example.InputBoolean); 134 //interpreter.IntegerStack.Push(example.InputInteger); 135 //interpreter.FloatStack.Push(example.InputFloat); 136 //interpreter.CharStack.Push(example.InputChar); 137 //interpreter.StringStack.Push(example.InputString); 138 //interpreter.StringVectorStack.Push(example.InputStringVector); 139 //interpreter.IntegerVectorStack.Push(example.InputIntegerVector); 140 //interpreter.FloatVectorStack.Push(example.InputFloatVector); 141 142 interpreter.SetInput( 143 integers: example.InputInteger, 144 floats: example.InputFloat, 145 booleans: example.InputBoolean, 146 chars: example.InputChar, 147 strings: example.InputString, 148 integerVectors: example.InputIntegerVector, 149 floatVectors: example.InputFloatVector, 150 stringVectors: example.InputStringVector); 151 133 interpreter.InitExample(example); 152 134 interpreter.Run(program); 153 135 -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Problem/BenchmarkSuite/PushBenchmarkSuiteProblem.cs
r15189 r15273 8 8 using HeuristicLab.BenchmarkSuite.Problems; 9 9 using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions; 10 using HeuristicLab.Problems.ProgramSynthesis.Push.Extensions;11 10 12 11 using Instances; … … 49 48 Description = data.Description; 50 49 BestKnownQuality = data.BestResult; 51 config.MaxPointsInProgram = data.MaxSize;52 config.EvalPushLimit = data.EvalLimit;53 config.ErcOptions = data.ErcOptions;54 config.FloatStringFormat = data.FloatStringFormat;50 Config.MaxPointsInProgram = data.MaxSize; 51 Config.EvalPushLimit = data.EvalLimit; 52 Config.ErcOptions = data.ErcOptions; 53 Config.FloatStringFormat = data.FloatStringFormat; 55 54 56 config.SetEnabledStacks((StackTypes)data.EnabledDataTypes);57 data.InitInExpressions(config);55 Config.SetEnabledStacks((StackTypes)data.EnabledDataTypes); 56 Config.InitInExpressions(data.TotalInputArgumentCount); 58 57 59 58 Encoding.Bounds[0, 0] = 0; 60 Encoding.Bounds[0, 1] = config.EnabledExpressions.Count;59 Encoding.Bounds[0, 1] = Config.EnabledExpressions.Count; 61 60 62 InitProgramLength = data.MaxSize / 2;61 Encoding.Length = data.MaxSize; 63 62 } 64 63 … … 67 66 double bestQuality, 68 67 IRandom random, 69 IReadOnlyPushConfiguration config, 70 IPushEvaluator evaluator) { 71 return new PushBenchmarkSuiteSolution(program, bestQuality, random, (IReadOnlyPushConfiguration)config.Clone(), (PushBenchmarkSuiteEvaluator)PushEvaluator.Clone()); 68 IReadOnlyPushConfiguration config) { 69 return new PushBenchmarkSuiteSolution(program, bestQuality, random, config, (PushBenchmarkSuiteEvaluator)PushEvaluator.Clone()); 72 70 } 73 71 } -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Problem/PushProblem.cs
r15189 r15273 7 7 using Configuration; 8 8 using Core; 9 using Data.Pool;10 9 using HeuristicLab.Data; 11 10 using HeuristicLab.Encodings.IntegerVectorEncoding; … … 13 12 using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions; 14 13 using HeuristicLab.Problems.ProgramSynthesis.Push.Individual; 15 using HeuristicLab.Problems.ProgramSynthesis.Push.Problem.BenchmarkSuite; 14 using HeuristicLab.Problems.ProgramSynthesis.Push.ObjectPools.Random; 15 using HeuristicLab.Problems.ProgramSynthesis.Push.SolutionCreator; 16 16 17 17 using Interpreter; … … 19 19 using Parameters; 20 20 using Persistence.Default.CompositeSerializers.Storable; 21 using Random;22 21 23 22 [StorableClass] 24 23 public abstract class PushProblem : SingleObjectiveBasicProblem<IntegerVectorEncoding> { 25 24 [Storable] 26 protected readonly PushConfigurationParameterCollection config; 27 protected PushInterpreterPool pool; 28 protected readonly ObjectPool<IRandom> randomPool = new ObjectPool<IRandom>(() => new MersenneTwister(), Environment.ProcessorCount * 4); 25 protected readonly PushConfigurationParameterCollection Config; 26 27 protected PushInterpreterPool Pool; 28 protected readonly SeededRandomPool RandomPool = new SeededRandomPool(); 29 29 30 30 [Storable] 31 31 protected readonly IPushEvaluator PushEvaluator; 32 32 33 private const string B estTrainingSolutionResultName= "Best Solution";34 private const string T estQualityResultName= "Test Quality";35 36 protected PushProblem( PushBenchmarkSuiteEvaluator evaluator) {37 config = new PushConfigurationParameterCollection();33 private const string BEST_TRAINING_SOLUTION_RESULT_NAME = "Best Solution"; 34 private const string TEST_QUALITY_RESULT_NAME = "Test Quality"; 35 36 protected PushProblem(IPushEvaluator evaluator) { 37 Config = new PushConfigurationParameterCollection(); 38 38 PushEvaluator = evaluator; 39 39 … … 52 52 protected PushProblem(PushProblem original, Cloner cloner) 53 53 : base(original, cloner) { 54 config = cloner.Clone(original.config);54 Config = cloner.Clone(original.Config); 55 55 PushEvaluator = cloner.Clone(original.PushEvaluator); 56 56 … … 68 68 69 69 private void InitData() { 70 pool = new PushInterpreterPool(Environment.ProcessorCount * 2, 4096, 1024, config);70 Pool = new PushInterpreterPool(Environment.ProcessorCount * 2, 4096, 1024, Config); 71 71 } 72 72 73 73 private void InitEvents() { 74 config.EnabledExpressionsChanged += EnabledExpressionsChanged; 74 Config.EnabledExpressionsChanged += EnabledExpressionsChanged; 75 Reset += PushProblemReset; 75 76 } 76 77 77 78 private void EnabledExpressionsChanged(object sender, EnabledExpressionsChangedEventArgs e) { 78 Encoding.Bounds[0, 1] = config.EnabledExpressions.Count; 79 Encoding.BoundsParameter.Value[0, 1] = config.EnabledExpressions.Count; 80 } 81 82 public ILookupParameter<IntValue> SeedParamater 83 { 84 get { return (ILookupParameter<IntValue>)Parameters["Seed"]; } 85 } 86 87 #region Parameters 88 private const string InitProgramLengthParameterName = "InitProgramLength"; 89 private const string InitProgramLengthParameterDescription = "This is the initial size of a push program."; 90 private const string PushConfigurationParameterName = "PushConfiguration"; 79 Encoding.Bounds[0, 1] = Config.EnabledExpressions.Count; 80 Encoding.BoundsParameter.Value[0, 1] = Config.EnabledExpressions.Count; 81 } 82 83 private const string PUSH_CONFIGURATION_PARAMETER_NAME = "PushConfiguration"; 91 84 92 85 public const string CasesScopeParameterName = "CaseQualities"; … … 94 87 95 88 private void InitParameters() { 96 foreach (var paramater in config.Parameters) {89 foreach (var paramater in Config.Parameters) { 97 90 if (!Parameters.ContainsKey(paramater.Name)) { 98 91 Parameters.Add(paramater); … … 100 93 } 101 94 102 if (!Parameters.ContainsKey(P ushConfigurationParameterName))103 Parameters.Add(new ValueParameter<IReadOnlyPushConfiguration>(P ushConfigurationParameterName, config) {95 if (!Parameters.ContainsKey(PUSH_CONFIGURATION_PARAMETER_NAME)) 96 Parameters.Add(new ValueParameter<IReadOnlyPushConfiguration>(PUSH_CONFIGURATION_PARAMETER_NAME, Config) { 104 97 Hidden = true 105 98 }); 106 99 107 if (!Parameters.ContainsKey(InitProgramLengthParameterName)) {108 Parameters.Add(new FixedValueParameter<IntValue>(109 InitProgramLengthParameterName,110 InitProgramLengthParameterDescription,111 new IntValue(50)));112 }113 114 Encoding.LengthParameter = InitProgramLengthParameter as IFixedValueParameter<IntValue>;115 116 100 if (!Parameters.ContainsKey(CasesScopeParameterName)) 117 101 Parameters.Add(new LookupParameter<BoolArray>(CasesScopeParameterName, "The training cases that have been successfully executed.")); … … 121 105 } 122 106 107 private void PushProblemReset(object sender, EventArgs e) { 108 // clear pools and free reserved memory 109 Pool.Clear(); 110 IndividualMapper.Clear(); 111 RandomPool.Clear(); 112 113 // reset seed 114 Config.Seed = 0; 115 } 116 123 117 protected override void OnReset() { 124 118 base.OnReset(); 125 119 126 120 // clear pools and free reserved memory 127 pool.Clear();121 Pool.Clear(); 128 122 IndividualMapper.Clear(); 123 RandomPool.Clear(); 124 Config.Seed = 0; 129 125 } 130 126 131 127 private void InitEncoding() { 132 128 Encoding.Bounds[0, 0] = 0; 133 Encoding.Bounds[0, 1] = config.EnabledExpressions.Count;134 Encoding.Length = config.MaxPointsInProgram;129 Encoding.Bounds[0, 1] = Config.EnabledExpressions.Count; 130 Encoding.Length = Config.MaxPointsInProgram; 135 131 } 136 132 137 133 private void InitOperators() { 138 Operators.Add(new PushExpressionFrequencyAnalyzer()); 139 } 140 141 /// <summary> 142 /// This is the inital size of an push program generated by a solution creator 143 /// </summary> 144 public IValueParameter<IntValue> InitProgramLengthParameter 145 { 146 get { return (IValueParameter<IntValue>)Parameters[InitProgramLengthParameterName]; } 147 } 148 149 public int InitProgramLength 150 { 151 get { return InitProgramLengthParameter.Value.Value; } 152 set 153 { 154 InitProgramLengthParameter.Value.Value = value; 155 } 156 } 157 #endregion 134 135 var solutionCreator = Operators.OfType<PushSolutionCreator>().FirstOrDefault(); 136 137 if (solutionCreator == null) { 138 solutionCreator = new PushSolutionCreator(); 139 Operators.Add(solutionCreator); 140 } 141 142 solutionCreator.ErcOptions = Config.ErcOptions; 143 144 if (!Operators.OfType<PushExpressionFrequencyAnalyzer>().Any()) { 145 Operators.Add(new PushExpressionFrequencyAnalyzer()); 146 } 147 148 SolutionCreator = solutionCreator; 149 } 158 150 159 151 public override bool Maximization { get { return false; } } … … 165 157 var bestIdx = Array.IndexOf(qualities, bestQuality); 166 158 var vector = individuals[bestIdx].IntegerVector(); 167 var seed = vector.GetSeed(); 168 var rand = randomPool.Allocate(); 169 rand.Reset(seed); 170 171 var program = (PushProgram)vector.ToPushProgram(config, rand).Clone(); 159 160 var rand = RandomPool.ResetAndAllocate(); 161 var program = (PushProgram)vector.ToPushProgram(Config, rand).Clone(); 162 RandomPool.Free(rand); 163 164 rand = RandomPool.ResetAndAllocate(); 172 165 var isIndividualBetter = AnalyzeBestTrainingSolution(program, bestQuality, results, rand); 166 RandomPool.Free(rand); 173 167 174 168 if (isIndividualBetter) { 175 rand .Reset(seed);169 rand = RandomPool.ResetAndAllocate(); 176 170 AnalyzeBestTestSolution(program, results, rand); 177 } 178 179 randomPool.Free(rand); 171 RandomPool.Free(rand); 172 } 180 173 } 181 174 182 175 private void AnalyzeBestTestSolution(PushProgram program, ResultCollection results, IRandom random) { 183 var testResult = PushEvaluator.EvaluateTraining( pool, program, random);184 185 if (!results.ContainsKey(T estQualityResultName)) {186 results.Add(new Result(T estQualityResultName, new DoubleValue(testResult.AvgQuality)));176 var testResult = PushEvaluator.EvaluateTraining(Pool, program, random); 177 178 if (!results.ContainsKey(TEST_QUALITY_RESULT_NAME)) { 179 results.Add(new Result(TEST_QUALITY_RESULT_NAME, new DoubleValue(testResult.AvgQuality))); 187 180 } else { 188 ((DoubleValue)results[T estQualityResultName].Value).Value = testResult.AvgQuality;181 ((DoubleValue)results[TEST_QUALITY_RESULT_NAME].Value).Value = testResult.AvgQuality; 189 182 } 190 183 } 191 184 192 185 private bool AnalyzeBestTrainingSolution(PushProgram program, double bestQuality, ResultCollection results, IRandom random) { 193 if (!results.ContainsKey(B estTrainingSolutionResultName)) {186 if (!results.ContainsKey(BEST_TRAINING_SOLUTION_RESULT_NAME)) { 194 187 var solution = CreatePushSolution( 195 188 program, 196 189 bestQuality, 197 random, 198 config, 199 PushEvaluator); 200 201 results.Add(new Result(BestTrainingSolutionResultName, solution)); 190 (IRandom)random.Clone(), 191 (IReadOnlyPushConfiguration)Config.Clone()); 192 193 results.Add(new Result(BEST_TRAINING_SOLUTION_RESULT_NAME, solution)); 202 194 return true; 203 195 } 204 196 205 var currentBestQuality = ((PushSolution)results[B estTrainingSolutionResultName].Value).Quality;197 var currentBestQuality = ((PushSolution)results[BEST_TRAINING_SOLUTION_RESULT_NAME].Value).Quality; 206 198 207 199 if ((!Maximization && currentBestQuality > bestQuality) || 208 200 (Maximization && currentBestQuality < bestQuality)) { 209 results[B estTrainingSolutionResultName].Value = CreatePushSolution(201 results[BEST_TRAINING_SOLUTION_RESULT_NAME].Value = CreatePushSolution( 210 202 program, 211 203 bestQuality, 212 204 random, 213 config, 214 PushEvaluator); 205 Config); 215 206 return true; 216 207 } 217 208 218 209 return false; 219 }220 221 private EvaluationResult EvaluateIntegerVector(IntegerVector vector) {222 var rand = randomPool.Allocate();223 var seed = vector.GetSeed();224 rand.Reset(seed);225 226 var program = vector.ToPushProgram(config, rand);227 var result = PushEvaluator.EvaluateTraining(pool, program, rand);228 randomPool.Free(rand);229 230 return result;231 210 } 232 211 … … 235 214 double bestQuality, 236 215 IRandom random, 237 IReadOnlyPushConfiguration config, 238 IPushEvaluator evaluator); 216 IReadOnlyPushConfiguration config); 239 217 240 218 public override double Evaluate(Individual individual, IRandom random) { 241 var vector = individual.IntegerVector(); 242 var result = EvaluateIntegerVector(vector); 219 // init seed of random pool 220 //Interlocked.CompareExchange(ref RandomPool.Seed, random.Next(), 0); 221 //Config.Seed = RandomPool.Seed; 222 223 //var rand = RandomPool.ResetAndAllocate(); 224 var program = individual.ToPushProgram(Config, random); 225 //RandomPool.Free(rand); 226 227 //rand = RandomPool.ResetAndAllocate(); 228 var result = PushEvaluator.EvaluateTraining(Pool, program, random); 229 //RandomPool.Free(rand); 243 230 244 231 individual[CaseQualitiesScopeParameterName] = new DoubleArray(result.ExampleQualities); -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Problem/PushSolution.cs
r15017 r15273 11 11 public readonly double Quality; 12 12 [Storable] 13 p ublicreadonly IRandom Random;13 protected readonly IRandom Random; 14 14 [Storable] 15 15 public readonly IReadOnlyPushConfiguration Config; … … 46 46 } 47 47 48 public IRandom GetRandom() { 49 return (IRandom)Random.Clone(); 50 } 51 48 52 public virtual PushSolution Simplify() { 49 53 return new PushSolution(Program, Quality, Random, Config, Evaluator, true);
Note: See TracChangeset
for help on using the changeset viewer.