Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/23/17 01:11:18 (8 years ago)
Author:
pkimmesw
Message:

#2665 simplifier, push solution results view, performance improvements, small bug fixes, ui fixes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Problem/PushProblem.cs

    r14747 r14777  
    1010
    1111  using HeuristicLab.BenchmarkSuite;
     12  using HeuristicLab.BenchmarkSuite.Problems;
    1213  using HeuristicLab.Data;
     14  using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
     15  using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
    1316
    1417  using Instances;
     
    1720  using Parameters;
    1821  using Persistence.Default.CompositeSerializers.Storable;
    19   using Stack;
    2022
    2123  [StorableClass]
    2224  [Creatable(CreatableAttribute.Categories.GeneticProgrammingProblems, Priority = 180)]
    2325  [Item("Push Problem", "")]
    24   public class PushProblem : SingleObjectiveBasicProblem<IntegerVectorEncoding>, IProblemInstanceConsumer<IBenchmarkSuiteDataDescriptor> {
     26  public class PushProblem : SingleObjectiveBasicProblem<IntegerVectorEncoding>, IProblemInstanceConsumer<Data> {
    2527    [Storable]
    2628    private readonly PushConfiguration config;
    2729    private PushInterpreterPool pool;
     30    private IBenchmarkSuiteDataDescriptor dataDescriptor;
    2831
    2932    public PushProblem() {
    3033      config = new PushConfiguration();
    31       pool = new PushInterpreterPool(10000, 1024, 65536, config);
     34      pool = new PushInterpreterPool(10000, 4096, 65536, config);
    3235
    3336      InitEvents();
     
    98101    private const string MaxPointsInRandomInstructionParameterDescription = "MaxPointsInRandomInstruction";
    99102    private const string DataBoundsParameterName = "DataBounds";
     103    private const string MaxProgramDepthParameterName = "MaxProgramDepth";
     104    private const string MaxProgramDepthParameterDescription = "";
    100105
    101106    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));
    116109
    117110      Parameters.Add(new FixedValueParameter<IntValue>(
     
    174167        new IntValue(config.MaxPointsInRandomExpression)) { Hidden = true });
    175168
    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));
    177178
    178179      Encoding.Bounds[0, 0] = 0;
     
    181182    }
    182183
    183     public IValueParameter<IntMatrix> DataBoundsParameter
    184     {
    185       get { return (IValueParameter<IntMatrix>)Parameters[DataBoundsParameterName]; }
    186     }
    187 
    188     public IntMatrix DataBounds
     184    public IValueParameter<DataBounds> DataBoundsParameter
     185    {
     186      get { return (IValueParameter<DataBounds>)Parameters[DataBoundsParameterName]; }
     187    }
     188
     189    public DataBounds DataBounds
    189190    {
    190191      get { return DataBoundsParameter.Value; }
     
    204205    }
    205206
    206     public IValueParameter<IBenchmarkSuiteDataDescriptor> DataParameter
    207     {
    208       get { return (IValueParameter<IBenchmarkSuiteDataDescriptor>)Parameters[DataParameterName]; }
    209     }
    210 
    211     public IBenchmarkSuiteDataDescriptor DataDescriptor
     207    public IValueParameter<Data> DataParameter
     208    {
     209      get { return (IValueParameter<Data>)Parameters[DataParameterName]; }
     210    }
     211
     212    public Data Data
    212213    {
    213214      get { return DataParameter.Value; }
     
    217218    public IValueParameter<IntValue> EvalPushLimitParameter
    218219    {
    219       get { return (IValueParameter<IntValue>)this.Parameters[EvalPushLimitParameterName]; }
     220      get { return (IValueParameter<IntValue>)Parameters[EvalPushLimitParameterName]; }
    220221    }
    221222
     
    227228        this.EvalPushLimitParameter.Value.Value = value;
    228229        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;
    229245      }
    230246    }
     
    394410      var bestIdx = Array.IndexOf(qualities, bestQuality);
    395411      var bestIndividual = individuals[bestIdx];
    396       var solution = new PushSolution(bestIndividual.IntegerVector(), bestQuality, DataDescriptor, random, config);
     412      var solution = new PushSolution(bestIndividual.IntegerVector(), bestQuality, Data, random, config, DataBounds.TrainingRange.Start, DataBounds.TrainingRange.End);
    397413
    398414      if (!results.ContainsKey(bestSolutionResultName)) {
     
    404420
    405421    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;
    474451    }
    475452  }
Note: See TracChangeset for help on using the changeset viewer.