Changeset 14777 for branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Problem/PushProblem.cs
- Timestamp:
- 03/23/17 01:11:18 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Problem/PushProblem.cs
r14747 r14777 10 10 11 11 using HeuristicLab.BenchmarkSuite; 12 using HeuristicLab.BenchmarkSuite.Problems; 12 13 using HeuristicLab.Data; 14 using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions; 15 using HeuristicLab.Problems.ProgramSynthesis.Push.Stack; 13 16 14 17 using Instances; … … 17 20 using Parameters; 18 21 using Persistence.Default.CompositeSerializers.Storable; 19 using Stack;20 22 21 23 [StorableClass] 22 24 [Creatable(CreatableAttribute.Categories.GeneticProgrammingProblems, Priority = 180)] 23 25 [Item("Push Problem", "")] 24 public class PushProblem : SingleObjectiveBasicProblem<IntegerVectorEncoding>, IProblemInstanceConsumer< IBenchmarkSuiteDataDescriptor> {26 public class PushProblem : SingleObjectiveBasicProblem<IntegerVectorEncoding>, IProblemInstanceConsumer<Data> { 25 27 [Storable] 26 28 private readonly PushConfiguration config; 27 29 private PushInterpreterPool pool; 30 private IBenchmarkSuiteDataDescriptor dataDescriptor; 28 31 29 32 public PushProblem() { 30 33 config = new PushConfiguration(); 31 pool = new PushInterpreterPool(10000, 1024, 65536, config);34 pool = new PushInterpreterPool(10000, 4096, 65536, config); 32 35 33 36 InitEvents(); … … 98 101 private const string MaxPointsInRandomInstructionParameterDescription = "MaxPointsInRandomInstruction"; 99 102 private const string DataBoundsParameterName = "DataBounds"; 103 private const string MaxProgramDepthParameterName = "MaxProgramDepth"; 104 private const string MaxProgramDepthParameterDescription = ""; 100 105 101 106 private void InitParameters() { 102 var bounds = new IntMatrix(1, 3, new[] { "Training Start", "Training End | Test Start", "Test End" }); 103 bounds.ItemChanged += (s, e) => { 104 if (this.DataDescriptor == null) 105 return; 106 107 var max = this.DataDescriptor.OriginalTestCount + this.DataDescriptor.OriginalTrainingCount - 1; 108 bounds[0, 0] = Math.Min(Math.Max(bounds[0, 0], 0), max); 109 bounds[0, 2] = Math.Max(Math.Min(bounds[0, 2], max), 0); 110 bounds[0, 1] = Math.Min(Math.Max(bounds[0, 0], bounds[0, 1]), bounds[0, 2]); 111 }; 112 113 Parameters.Add(new FixedValueParameter<IntMatrix>( 114 DataBoundsParameterName, 115 bounds)); 107 108 Parameters.Add(new FixedValueParameter<DataBounds>(DataBoundsParameterName)); 116 109 117 110 Parameters.Add(new FixedValueParameter<IntValue>( … … 174 167 new IntValue(config.MaxPointsInRandomExpression)) { Hidden = true }); 175 168 176 Parameters.Add(new ValueParameter<IBenchmarkSuiteDataDescriptor>(DataParameterName, DataParameterDescription)); 169 Parameters.Add( 170 new FixedValueParameter<IntValue>( 171 MaxProgramDepthParameterName, 172 MaxProgramDepthParameterDescription, 173 new IntValue(config.MaxDepth))); 174 175 Parameters.Add(new ValueParameter<Data>( 176 DataParameterName, 177 DataParameterDescription)); 177 178 178 179 Encoding.Bounds[0, 0] = 0; … … 181 182 } 182 183 183 public IValueParameter< IntMatrix> DataBoundsParameter184 { 185 get { return (IValueParameter< IntMatrix>)Parameters[DataBoundsParameterName]; }186 } 187 188 public IntMatrixDataBounds184 public IValueParameter<DataBounds> DataBoundsParameter 185 { 186 get { return (IValueParameter<DataBounds>)Parameters[DataBoundsParameterName]; } 187 } 188 189 public DataBounds DataBounds 189 190 { 190 191 get { return DataBoundsParameter.Value; } … … 204 205 } 205 206 206 public IValueParameter< IBenchmarkSuiteDataDescriptor> DataParameter207 { 208 get { return (IValueParameter< IBenchmarkSuiteDataDescriptor>)Parameters[DataParameterName]; }209 } 210 211 public IBenchmarkSuiteDataDescriptor DataDescriptor207 public IValueParameter<Data> DataParameter 208 { 209 get { return (IValueParameter<Data>)Parameters[DataParameterName]; } 210 } 211 212 public Data Data 212 213 { 213 214 get { return DataParameter.Value; } … … 217 218 public IValueParameter<IntValue> EvalPushLimitParameter 218 219 { 219 get { return (IValueParameter<IntValue>) this.Parameters[EvalPushLimitParameterName]; }220 get { return (IValueParameter<IntValue>)Parameters[EvalPushLimitParameterName]; } 220 221 } 221 222 … … 227 228 this.EvalPushLimitParameter.Value.Value = value; 228 229 config.EvalPushLimit = value; 230 } 231 } 232 233 public IValueParameter<IntValue> MaxDepthParameter 234 { 235 get { return (IValueParameter<IntValue>)this.Parameters[MaxProgramDepthParameterName]; } 236 } 237 238 public int MaxDepth 239 { 240 get { return config.MaxDepth; } 241 set 242 { 243 this.MaxDepthParameter.Value.Value = value; 244 config.MaxDepth = value; 229 245 } 230 246 } … … 394 410 var bestIdx = Array.IndexOf(qualities, bestQuality); 395 411 var bestIndividual = individuals[bestIdx]; 396 var solution = new PushSolution(bestIndividual.IntegerVector(), bestQuality, Data Descriptor, random, config);412 var solution = new PushSolution(bestIndividual.IntegerVector(), bestQuality, Data, random, config, DataBounds.TrainingRange.Start, DataBounds.TrainingRange.End); 397 413 398 414 if (!results.ContainsKey(bestSolutionResultName)) { … … 404 420 405 421 public override double Evaluate(Individual individual, IRandom random) { 406 if (DataBounds[0, 1] <= 0) return default(double); 407 408 var program = individual.PushProgram(config.EnabledExpressions as IReadOnlyList<string>); 409 var result = 0d; 410 411 using (var interpreter = pool.GetInstance(random)) { 412 for (var i = DataBounds[0, 0]; i < DataBounds[0, 1]; i++) { 413 var example = DataDescriptor.Examples[i]; 414 415 interpreter.BooleanStack.Push(example.InputBoolean); 416 interpreter.IntegerStack.Push(example.InputInt); 417 interpreter.FloatStack.Push(example.InputFloat); 418 419 interpreter.Run(program); 420 421 result += GetDiff(example.OutputInt, interpreter.IntegerStack, DataDescriptor.WorstResult, LongDiffer) 422 + GetDiff(example.OutputFloat, interpreter.FloatStack, DataDescriptor.WorstResult, DoubleDiffer) 423 + GetDiff(example.OutputBoolean, interpreter.BooleanStack, DataDescriptor.WorstResult, BooleanDiffer); 424 425 interpreter.Clear(); 426 } 427 } 428 429 return result / DataBounds[0, 1]; 430 } 431 432 private static double DoubleDiffer(double a, double b) { 433 var result = a - b; 434 435 return result == double.MinValue ? double.MaxValue : Math.Abs(result); 436 } 437 438 private static double LongDiffer(long a, long b) { 439 var result = a - b; 440 441 return result == long.MinValue ? long.MaxValue : Math.Abs(result); 442 } 443 444 private static double BooleanDiffer(bool a, bool b) { 445 return a && b ? 0 : a || b ? 1 : 2; 446 } 447 448 private static double GetDiff<T>(IReadOnlyList<T> estimated, IStack<T> resultStack, double worstResult, Func<T, T, double> differ) 449 where T : IComparable { 450 451 var count = Math.Min(estimated.Count, resultStack.Count); 452 var result = resultStack.Peek(count); 453 var comparableLength = Math.Min(estimated.Count, result.Length); 454 var diff = 0d; 455 456 for (var i = 0; i < comparableLength; i++) { 457 diff += Math.Min(differ(estimated[i], result[0]), worstResult); 458 } 459 460 if (estimated.Count > result.Length) { 461 diff += worstResult * (estimated.Count - comparableLength); 462 } 463 464 return diff; 465 } 466 467 public void Load(IBenchmarkSuiteDataDescriptor descriptor) { 468 this.DataDescriptor = descriptor; 469 BestKnownQuality = descriptor.BestResult; 470 471 DataBounds[0, 0] = 0; 472 DataBounds[0, 2] = this.DataDescriptor.OriginalTrainingCount + this.DataDescriptor.OriginalTestCount; 473 DataBounds[0, 1] = this.DataDescriptor.OriginalTrainingCount; 422 return PushEvaluator.Evaluate(individual, pool, random, Data, DataBounds.TrainingRange.Start, DataBounds.TrainingRange.End); 423 } 424 425 public void Load(Data data) { 426 Data = data; 427 BestKnownQuality = data.BestResult; 428 MaxProgramLength = data.MaxSize; 429 EvalPushLimit = data.EvalLimit; 430 431 config.EnabledExpressions = (IList<string>)ExpressionTable.GetEnabledExpressionsByStackTypes((StackTypes)data.EnabledDataTypes); 432 433 // update enabled stack types 434 foreach (var stackType in ExpressionTable.StackTypeToNamesTable.Keys) { 435 var enabledStackExpressions = config.EnabledExpressions.Intersect(ExpressionTable.StackTypeToNamesTable[stackType]); 436 config.SetStack(stackType, enabledStackExpressions.Any()); 437 } 438 439 Encoding.Bounds[0, 0] = 0; 440 Encoding.Bounds[0, 1] = config.EnabledExpressions.Count - 1; 441 Encoding.Length = config.MaxPointsInProgram; 442 443 // TODO 444 // data.MaxGenerations 445 // data.ProgEvalBudget 446 447 DataBounds.TrainingRange.Start = 0; 448 DataBounds.TrainingRange.End = Data.OriginalTrainingCount - 1; 449 DataBounds.TestRange.Start = Data.OriginalTrainingCount; 450 DataBounds.TestRange.End = Data.OriginalTrainingCount + Data.OriginalTestCount; 474 451 } 475 452 }
Note: See TracChangeset
for help on using the changeset viewer.