Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/10/17 00:27:31 (7 years ago)
Author:
pkimmesw
Message:

#2665 LexicaseSelector, Performance improvements, UI Fixes, Debugger only shows used stacks, fixed Debugger stepping, Added vector expressions, ERCOptions,

File:
1 edited

Legend:

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

    r14777 r14834  
    44  using System.Collections.Generic;
    55  using System.Linq;
     6  using BenchmarkSuite.Problems;
    67  using Common;
    78  using Configuration;
    89  using Core;
     10  using Data.Pool;
    911  using Encodings.IntegerVectorEncoding;
    10 
    11   using HeuristicLab.BenchmarkSuite;
    12   using HeuristicLab.BenchmarkSuite.Problems;
     12  using Expressions;
    1313  using HeuristicLab.Data;
    14   using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
    15   using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
     14  using HeuristicLab.Problems.ProgramSynthesis.Push.Erc;
    1615
    1716  using Instances;
     
    2019  using Parameters;
    2120  using Persistence.Default.CompositeSerializers.Storable;
     21  using Random;
     22  using Stack;
    2223
    2324  [StorableClass]
     
    2829    private readonly PushConfiguration config;
    2930    private PushInterpreterPool pool;
    30     private IBenchmarkSuiteDataDescriptor dataDescriptor;
     31    private readonly ObjectPool<IRandom> randomPool = new ObjectPool<IRandom>(() => new MersenneTwister());
     32
     33    public const string CaseQualitiesScopeParameterName = "CaseQualities";
    3134
    3235    public PushProblem() {
    3336      config = new PushConfiguration();
    34       pool = new PushInterpreterPool(10000, 4096, 65536, config);
    35 
     37
     38      InitData();
    3639      InitEvents();
    3740      InitParameters();
    38       Instructions = config;
    3941    }
    4042
     
    4749      : base(original, cloner) {
    4850      config = cloner.Clone(original.config);
    49       pool = new PushInterpreterPool(65536, 1024, 65536, config);
    50 
    51       Instructions = config;
    52 
    53       this.InitEvents();
     51
     52      InitData();
     53      InitEvents();
    5454    }
    5555
     
    5757    // ReSharper disable once UnusedMember.Local
    5858    private void AfterDeserialization() {
    59       pool = new PushInterpreterPool(config);
    60       Instructions = config;
    61 
     59      InitData();
    6260      InitEvents();
     61    }
     62
     63    private void InitData() {
     64      pool = new PushInterpreterPool(Environment.ProcessorCount * 2, 4096, 1024, config);
     65
     66      //var solutionCreator = new PointsBasedPushProgramCreator(config.ErcOptions);
     67      //SolutionCreator = solutionCreator;
     68      //Encoding.SolutionCreator = solutionCreator;
    6369    }
    6470
     
    6874
    6975    private void EnabledExpressionsChanged(object sender, EnabledExpressionsChangedEventArgs e) {
    70       this.Encoding.Bounds[0, 1] = config.EnabledExpressions.Count - 1;
    71       this.Encoding.BoundsParameter.Value[0, 1] = config.EnabledExpressions.Count - 1;
     76      this.Encoding.Bounds[0, 1] = config.EnabledExpressions.Count;
     77      this.Encoding.BoundsParameter.Value[0, 1] = config.EnabledExpressions.Count;
    7278    }
    7379
    7480    #region Parameters
    7581
     82    private const string DataBoundsParameterName = "DataBounds";
     83    private const string DataParameterName = "Data";
     84    private const string DataParameterDescription = "Program Synthesis";
     85    private const string InstructionsParameterName = "Instructions";
     86    private const string InstructionsParameterDescription = "Enables/Disables Instructions";
    7687    private const string EvalPushLimitParameterName = "EvalPushLimit";
    7788    private const string EvalPushLimitParameterDescription = "This is the maximum allowed number of \"executions\" in a single top-level call to the interpreter. The execution of a single Push instruction counts as one execution, as does the processing of a single literal, as does the descent into one layer of parentheses (that is, the processing of the \"(\" counts as one execution).";
    78     private const string MaxProgramLengthParameterName = "MaxProgramLength";
     89    private const string MaxPointsInProgramParameterName = "MaxProgramLength";
    7990    private const string MaxProgramLengthParameterDescription = "This is the maximum size of an item on the CODE stack, expressed as a number of points. A point is an instruction, a literal, or a pair of parentheses.";
    8091    private const string TopLevelPushCodeParameterName = "TopLevelPushCode";
     
    8293    private const string TopLevelPopCodeParameterName = "TopLevelPopCode";
    8394    private const string TopLevelPopCodeParameterDescription = "When TRUE, the CODE stack will be popped at the end of top level calls to the interpreter. The default is FALSE.";
    84     private const string InstructionsParameterName = "Instructions";
    85     private const string InstructionsParameterDescription = "Enables/Disables Instructions";
    86     private const string DataParameterName = "Data";
    87     private const string DataParameterDescription = "Program Synthesis";
    8895    private const string MinRandomIntegerParameterName = "MinRandomInteger";
    8996    private const string MinRandomIntegerParameterDescription = "The minimum INTEGER that will be produced as an ephemeral random INTEGER constant or from a call to INTEGER.RAND.";
     
    96103    private const string NewErcNameProbabilityParameterName = "NewErcNameProbability";
    97104    private const string NewErcNameProbabilityParameterDescription = "The probability that the selection of the ephemeral random NAME constant for inclusion in randomly generated code will produce a new name.";
    98     private const string ErcProbabilityParameterName = "ErcProbability";
    99     private const string ErcProbabilityParameterDescription = "The probability that the selection of a epheral random literal constant for inclusion in randomly generated code will produce a new literal.";
    100105    private const string MaxPointsInRandomInstructionParameterName = "MaxPointsInRandomInstruction";
    101106    private const string MaxPointsInRandomInstructionParameterDescription = "MaxPointsInRandomInstruction";
    102     private const string DataBoundsParameterName = "DataBounds";
    103     private const string MaxProgramDepthParameterName = "MaxProgramDepth";
    104     private const string MaxProgramDepthParameterDescription = "";
     107    private const string ErcOptionsParameterName = "ERC options";
     108    private const string MaxStringLengthParameterName = "Max. string length";
     109    private const string MaxDepthParameterName = "Max. program recursion";
    105110
    106111    private void InitParameters() {
    107 
    108112      Parameters.Add(new FixedValueParameter<DataBounds>(DataBoundsParameterName));
     113
     114      Parameters.Add(new ValueParameter<IEnabledExpressionsConfiguration>(
     115        InstructionsParameterName,
     116        InstructionsParameterDescription,
     117        config));
     118
     119      Parameters.Add(new ValueParameter<Data>(
     120        DataParameterName,
     121        DataParameterDescription));
     122
     123      Parameters.Add(new FixedValueParameter<ErcOptions>(ErcOptionsParameterName, config.ErcOptions));
    109124
    110125      Parameters.Add(new FixedValueParameter<IntValue>(
     
    114129
    115130      Parameters.Add(new FixedValueParameter<IntValue>(
    116         MaxProgramLengthParameterName,
     131        MaxPointsInProgramParameterName,
    117132        MaxProgramLengthParameterDescription,
    118133        new IntValue(config.MaxPointsInProgram)));
     134      Encoding.LengthParameter = Parameters[MaxPointsInProgramParameterName] as IFixedValueParameter<IntValue>;
    119135
    120136      Parameters.Add(new FixedValueParameter<BoolValue>(
     
    128144        new BoolValue(config.TopLevelPopCode)) { Hidden = true });
    129145
    130       Parameters.Add(new ValueParameter<IEnabledExpressionsConfiguration>(
    131         InstructionsParameterName,
    132         InstructionsParameterDescription));
    133 
    134       Parameters.Add(new FixedValueParameter<IntValue>(
    135         MinRandomIntegerParameterName,
    136         MinRandomIntegerParameterDescription,
    137         new IntValue(config.MinRandomInteger)) { Hidden = true });
    138 
    139       Parameters.Add(new FixedValueParameter<IntValue>(
    140         MaxRandomIntegerParameterName,
    141         MaxRandomIntegerParameterDescription,
    142         new IntValue(config.MaxRandomInteger)) { Hidden = true });
    143 
    144       Parameters.Add(new FixedValueParameter<DoubleValue>(
    145         MinRandomFloatParameterName,
    146         MinRandomFloatParameterDescription,
    147         new DoubleValue(config.MinRandomFloat)) { Hidden = true });
    148 
    149       Parameters.Add(new FixedValueParameter<DoubleValue>(
    150         MaxRandomFloatParameterName,
    151         MaxRandomFloatParameterDescription,
    152         new DoubleValue(config.MaxRandomFloat)) { Hidden = true });
    153 
    154       Parameters.Add(new FixedValueParameter<PercentValue>(
    155         NewErcNameProbabilityParameterName,
    156         NewErcNameProbabilityParameterDescription,
    157         new PercentValue(config.NewErcNameProbability)) { Hidden = true });
    158 
    159       Parameters.Add(new FixedValueParameter<PercentValue>(
    160         ErcProbabilityParameterName,
    161         ErcProbabilityParameterDescription,
    162         new PercentValue(config.ErcProbability)) { Hidden = true });
    163 
    164146      Parameters.Add(new FixedValueParameter<IntValue>(
    165147        MaxPointsInRandomInstructionParameterName,
     
    167149        new IntValue(config.MaxPointsInRandomExpression)) { Hidden = true });
    168150
    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));
     151      Parameters.Add(new FixedValueParameter<IntValue>(
     152        MaxStringLengthParameterName,
     153        new IntValue(config.MaxStringLength)) { Hidden = true });
     154
     155      Parameters.Add(new FixedValueParameter<IntValue>(
     156        MaxDepthParameterName,
     157        new IntValue(config.MaxDepth)) { Hidden = true });
     158
     159      Parameters.Add(new LookupParameter<BoolArray>("Cases", "The training cases that have been successfully executed."));
     160      Parameters.Add(new LookupParameter<DoubleArray>(CaseQualitiesScopeParameterName, "The quality of every single training case for each individual"));
    178161
    179162      Encoding.Bounds[0, 0] = 0;
    180       Encoding.Bounds[0, 1] = config.EnabledExpressions.Count - 1;
     163      Encoding.Bounds[0, 1] = config.EnabledExpressions.Count;
    181164      Encoding.Length = config.MaxPointsInProgram;
    182165    }
     
    216199    }
    217200
     201    public IValueParameter<ErcOptions> ErcOptionsParameter
     202    {
     203      get { return (IValueParameter<ErcOptions>)Parameters[ErcOptionsParameterName]; }
     204    }
     205
     206    public ErcOptions ErcOptions
     207    {
     208      get { return config.ErcOptions; }
     209      set
     210      {
     211        ErcOptionsParameter.Value = value;
     212        config.ErcOptions = value;
     213      }
     214    }
     215
     216    /// <summary>
     217    ///     This is the maximum allowed number of "executions" in a single top-level call to the interpreter.
     218    ///     The execution of a single Push instruction counts as one execution, as does the processing of a single literal,
     219    ///     as does the descent into one layer of parentheses (that is, the processing of the "(" counts as one execution).
     220    ///     When this limit is exceeded the interpreter aborts immediately, leaving its stacks in the states they were in prior
     221    ///     to the abort (so they may still be examined by a calling program). Whether or not this counts as an "abnormal"
     222    ///     termination
     223    ///     is up to the calling program.
     224    /// </summary>
    218225    public IValueParameter<IntValue> EvalPushLimitParameter
    219226    {
     
    226233      set
    227234      {
    228         this.EvalPushLimitParameter.Value.Value = value;
     235        EvalPushLimitParameter.Value.Value = value;
    229236        config.EvalPushLimit = value;
    230237      }
    231238    }
    232239
     240    /// <summary>
     241    /// This is the maximum of depth a push program can have. Expressions, which lead to exceed this limit are interpreted as NOOP.
     242    /// </summary>
    233243    public IValueParameter<IntValue> MaxDepthParameter
    234244    {
    235       get { return (IValueParameter<IntValue>)this.Parameters[MaxProgramDepthParameterName]; }
     245      get { return (IValueParameter<IntValue>)Parameters[MaxDepthParameterName]; }
    236246    }
    237247
     
    241251      set
    242252      {
    243         this.MaxDepthParameter.Value.Value = value;
     253        MaxDepthParameter.Value.Value = value;
    244254        config.MaxDepth = value;
    245255      }
    246256    }
    247257
    248     public IValueParameter<IntValue> MaxProgramLengthParameter
    249     {
    250       get { return (IValueParameter<IntValue>)this.Parameters[MaxProgramLengthParameterName]; }
    251     }
    252 
    253     public int MaxProgramLength
     258    /// <summary>
     259    ///     This is the maximum size of an item on the CODE stack, expressed as a number of points.
     260    ///     A point is an instruction, a literal, or a pair of parentheses. Any instruction that would cause this limit to be
     261    ///     exceeded
     262    ///     should instead act as a NOOP, leaving all stacks in the states that they were in before the execution of the
     263    ///     instruction.
     264    /// </summary>
     265    public IValueParameter<IntValue> MaxPointsInProgramParameter
     266    {
     267      get { return (IValueParameter<IntValue>)this.Parameters[MaxPointsInProgramParameterName]; }
     268    }
     269
     270    public int MaxPointsInProgram
    254271    {
    255272      get { return config.MaxPointsInProgram; }
    256273      set
    257274      {
    258         this.MaxProgramLengthParameter.Value.Value = value;
    259         this.Encoding.LengthParameter.Value.Value = value;
     275        MaxPointsInProgramParameter.Value.Value = value;
    260276        config.MaxPointsInProgram = value;
    261277      }
    262278    }
    263279
    264     public IValueParameter<BoolValue> TopLevelPushParameter
     280    /// <summary>
     281    ///     The maximum number of points in an expression produced by the CODE.RAND instruction.
     282    /// </summary>
     283    public IValueParameter<IntValue> MaxPointsInRandomExpressionParameter
     284    {
     285      get { return (IValueParameter<IntValue>)Parameters[MaxPointsInRandomInstructionParameterName]; }
     286    }
     287
     288    public int MaxPointsInRandomExpression
     289    {
     290      get { return config.MaxPointsInRandomExpression; }
     291      set
     292      {
     293        MaxPointsInRandomExpressionParameter.Value.Value = value;
     294        config.MaxPointsInRandomExpression = value;
     295      }
     296    }
     297
     298    /// <summary>
     299    ///     When TRUE (which is the default), code passed to the top level of the interpreter
     300    ///     will be pushed onto the CODE stack prior to execution.
     301    /// </summary>
     302    public IValueParameter<BoolValue> TopLevelPushCodeParameter
    265303    {
    266304      get { return (IValueParameter<BoolValue>)this.Parameters[TopLevelPushCodeParameterName]; }
     
    272310      set
    273311      {
    274         this.TopLevelPushParameter.Value.Value = value;
     312        TopLevelPushCodeParameter.Value.Value = value;
    275313        config.TopLevelPushCode = value;
    276314      }
    277315    }
    278316
    279     public IValueParameter<BoolValue> TopLevelPopParameter
     317    /// <summary>
     318    ///     When TRUE, the CODE stack will be popped at the end of top level calls to the interpreter. The default is FALSE.
     319    /// </summary>
     320    public IValueParameter<BoolValue> TopLevelPopCodeParameter
    280321    {
    281322      get { return (IValueParameter<BoolValue>)this.Parameters[TopLevelPopCodeParameterName]; }
     
    287328      set
    288329      {
    289         this.TopLevelPushParameter.Value.Value = value;
     330        TopLevelPopCodeParameter.Value.Value = value;
    290331        config.TopLevelPopCode = value;
    291332      }
    292333    }
    293334
    294     public IValueParameter<IntValue> MinRandomIntegerParameter
    295     {
    296       get { return (IValueParameter<IntValue>)this.Parameters[MinRandomIntegerParameterName]; }
    297     }
    298 
    299     public int MinRandomInteger
    300     {
    301       get { return config.MinRandomInteger; }
    302       set
    303       {
    304         this.MinRandomIntegerParameter.Value.Value = value;
    305         config.MinRandomInteger = value;
    306       }
    307     }
    308 
    309     public IValueParameter<IntValue> MaxRandomIntegerParameter
    310     {
    311       get { return (IValueParameter<IntValue>)this.Parameters[MaxRandomIntegerParameterName]; }
    312     }
    313 
    314     public int MaxRandomInteger
    315     {
    316       get { return config.MaxRandomInteger; }
    317       set
    318       {
    319         this.MaxRandomIntegerParameter.Value.Value = value;
    320         config.MaxRandomInteger = value;
    321       }
    322     }
    323 
    324     public IValueParameter<DoubleValue> MinRandomFloatParameter
    325     {
    326       get { return (IValueParameter<DoubleValue>)this.Parameters[MinRandomFloatParameterName]; }
    327     }
    328 
    329     public double MinRandomFloat
    330     {
    331       get { return config.MinRandomFloat; }
    332       set
    333       {
    334         this.MinRandomFloatParameter.Value.Value = value;
    335         config.MinRandomFloat = value;
    336       }
    337     }
    338 
    339     public IValueParameter<DoubleValue> MaxRandomFloatParameter
    340     {
    341       get { return (IValueParameter<DoubleValue>)this.Parameters[MaxRandomFloatParameterName]; }
    342     }
    343 
    344     public double MaxRandomFloat
    345     {
    346       get { return config.MaxRandomFloat; }
    347       set
    348       {
    349         this.MaxRandomFloatParameter.Value.Value = value;
    350         config.MaxRandomFloat = value;
    351       }
    352     }
    353 
    354     public IValueParameter<PercentValue> NewErcNameProbabilityParameter
    355     {
    356       get { return (IValueParameter<PercentValue>)this.Parameters[NewErcNameProbabilityParameterName]; }
    357     }
    358 
    359     public double NewErcNameProbability
    360     {
    361       get { return config.NewErcNameProbability; }
    362       set
    363       {
    364         this.NewErcNameProbabilityParameter.Value.Value = value;
    365         config.NewErcNameProbability = value;
    366       }
    367     }
    368 
    369     public IValueParameter<PercentValue> ErcProbabilityParameter
    370     {
    371       get { return (IValueParameter<PercentValue>)this.Parameters[ErcProbabilityParameterName]; }
    372     }
    373 
    374     public double ErcProbability
    375     {
    376       get { return config.ErcProbability; }
    377       set
    378       {
    379         this.ErcProbabilityParameter.Value.Value = value;
    380         config.ErcProbability = value;
    381       }
    382     }
    383 
    384     public IValueParameter<IntValue> MaxPointsInRandomInstructionParameter
    385     {
    386       get { return (IValueParameter<IntValue>)Parameters[MaxPointsInRandomInstructionParameterName]; }
    387     }
    388 
    389     public int MaxPointsInRandomInstruction
    390     {
    391       get { return config.MaxPointsInRandomExpression; }
    392       set
    393       {
    394         this.MaxPointsInRandomInstructionParameter.Value.Value = value;
    395         config.MaxPointsInRandomExpression = value;
    396       }
    397     }
    398 
     335    public IValueParameter<IntValue> MaxStringLengthParameter
     336    {
     337      get { return (IValueParameter<IntValue>)Parameters[MaxStringLengthParameterName]; }
     338    }
     339
     340    public int MaxStringLength
     341    {
     342      get { return config.MaxStringLength; }
     343      set
     344      {
     345        MaxStringLengthParameter.Value.Value = value;
     346        config.MaxStringLength = value;
     347      }
     348    }
    399349    #endregion
    400350
     
    414364      if (!results.ContainsKey(bestSolutionResultName)) {
    415365        results.Add(new Result(bestSolutionResultName, solution));
    416       } else if (((PushSolution)results[bestSolutionResultName].Value).Quality < qualities[bestIdx]) {
    417         results[bestSolutionResultName].Value = solution;
     366      } else {
     367        var currentBestQuality = ((PushSolution)results[bestSolutionResultName].Value).Quality;
     368
     369        if (Maximization && currentBestQuality < bestQuality ||
     370           !Maximization && currentBestQuality > bestQuality) {
     371          results[bestSolutionResultName].Value = solution;
     372        }
    418373      }
    419374    }
    420375
    421376    public override double Evaluate(Individual individual, IRandom random) {
    422       return PushEvaluator.Evaluate(individual, pool, random, Data, DataBounds.TrainingRange.Start, DataBounds.TrainingRange.End);
     377      var program = individual.ToPushProgram(config, randomPool);
     378      var result = PushEvaluator.Evaluate(
     379        program,
     380        pool,
     381        random,
     382        Data,
     383        DataBounds.TrainingRange.Start,
     384        DataBounds.TrainingRange.End);
     385
     386      individual.SetScopeValue(CaseQualitiesScopeParameterName, new DoubleArray(result.ExampleQualities));
     387
     388      return result.TotalQuality;
    423389    }
    424390
     
    426392      Data = data;
    427393      BestKnownQuality = data.BestResult;
    428       MaxProgramLength = data.MaxSize;
     394      MaxPointsInProgram = data.MaxSize;
    429395      EvalPushLimit = data.EvalLimit;
    430396
    431       config.EnabledExpressions = (IList<string>)ExpressionTable.GetEnabledExpressionsByStackTypes((StackTypes)data.EnabledDataTypes);
     397      config.EnabledExpressions = (IList<string>)ExpressionTable.GetExpressionsByStackTypes((StackTypes)data.EnabledDataTypes);
    432398
    433399      // update enabled stack types
    434400      foreach (var stackType in ExpressionTable.StackTypeToNamesTable.Keys) {
    435         var enabledStackExpressions = config.EnabledExpressions.Intersect(ExpressionTable.StackTypeToNamesTable[stackType]);
    436         config.SetStack(stackType, enabledStackExpressions.Any());
     401        var enable = config.EnabledExpressions.Intersect(ExpressionTable.StackTypeToNamesTable[stackType]).Any();
     402        config.SetStack(stackType, enable);
    437403      }
    438404
    439405      Encoding.Bounds[0, 0] = 0;
    440       Encoding.Bounds[0, 1] = config.EnabledExpressions.Count - 1;
     406      Encoding.Bounds[0, 1] = config.EnabledExpressions.Count;
    441407      Encoding.Length = config.MaxPointsInProgram;
    442408
    443       // TODO
    444       // data.MaxGenerations
    445       // data.ProgEvalBudget
    446 
    447409      DataBounds.TrainingRange.Start = 0;
    448       DataBounds.TrainingRange.End = Data.OriginalTrainingCount - 1;
     410      DataBounds.TrainingRange.End = Data.OriginalTrainingCount;
    449411      DataBounds.TestRange.Start = Data.OriginalTrainingCount;
    450412      DataBounds.TestRange.End = Data.OriginalTrainingCount + Data.OriginalTestCount;
Note: See TracChangeset for help on using the changeset viewer.