Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/19/17 12:55:58 (8 years ago)
Author:
pkimmesw
Message:

#2665 Started Plush Encoding, Added Zero Error Individual Count Analyzer

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  
    5353      var example = Evaluator.Data.Examples[exampleComboBox.SelectedIndex];
    5454
    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);
    7456    }
    7557
     
    9274    }
    9375
     76
    9477    protected override void OnContentChanged() {
    9578      if (Content == null) return;
     
    10487      }
    10588
    106       interpreter = pool.Create(Content.Random);
     89      var random = Content.GetRandom();
     90      interpreter = pool.Create(random);
    10791      UpdateExamples(Evaluator.Data);
    10892
     
    181165      grid.Columns.Add(relativeDiffColumn);
    182166
    183       using (var pushInterpreter = pool.Create(Content.Random)) {
     167      var random = Content.GetRandom();
     168      using (var pushInterpreter = pool.Create(random)) {
    184169        var rowIndex = 1;
    185170        for (var i = start; i < end; i++, rowIndex++) {
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Problem/BenchmarkSuite/PushBenchmarkSuiteEvaluator.cs

    r15189 r15273  
    131131      var example = Data.Examples[exampleIndex];
    132132
    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);
    152134      interpreter.Run(program);
    153135
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Problem/BenchmarkSuite/PushBenchmarkSuiteProblem.cs

    r15189 r15273  
    88  using HeuristicLab.BenchmarkSuite.Problems;
    99  using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
    10   using HeuristicLab.Problems.ProgramSynthesis.Push.Extensions;
    1110
    1211  using Instances;
     
    4948      Description = data.Description;
    5049      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;
    5554
    56       config.SetEnabledStacks((StackTypes)data.EnabledDataTypes);
    57       data.InitInExpressions(config);
     55      Config.SetEnabledStacks((StackTypes)data.EnabledDataTypes);
     56      Config.InitInExpressions(data.TotalInputArgumentCount);
    5857
    5958      Encoding.Bounds[0, 0] = 0;
    60       Encoding.Bounds[0, 1] = config.EnabledExpressions.Count;
     59      Encoding.Bounds[0, 1] = Config.EnabledExpressions.Count;
    6160
    62       InitProgramLength = data.MaxSize / 2;
     61      Encoding.Length = data.MaxSize;
    6362    }
    6463
     
    6766      double bestQuality,
    6867      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());
    7270    }
    7371  }
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Problem/PushProblem.cs

    r15189 r15273  
    77  using Configuration;
    88  using Core;
    9   using Data.Pool;
    109  using HeuristicLab.Data;
    1110  using HeuristicLab.Encodings.IntegerVectorEncoding;
     
    1312  using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
    1413  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;
    1616
    1717  using Interpreter;
     
    1919  using Parameters;
    2020  using Persistence.Default.CompositeSerializers.Storable;
    21   using Random;
    2221
    2322  [StorableClass]
    2423  public abstract class PushProblem : SingleObjectiveBasicProblem<IntegerVectorEncoding> {
    2524    [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();
    2929
    3030    [Storable]
    3131    protected readonly IPushEvaluator PushEvaluator;
    3232
    33     private const string BestTrainingSolutionResultName = "Best Solution";
    34     private const string TestQualityResultName = "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();
    3838      PushEvaluator = evaluator;
    3939
     
    5252    protected PushProblem(PushProblem original, Cloner cloner)
    5353      : base(original, cloner) {
    54       config = cloner.Clone(original.config);
     54      Config = cloner.Clone(original.Config);
    5555      PushEvaluator = cloner.Clone(original.PushEvaluator);
    5656
     
    6868
    6969    private void InitData() {
    70       pool = new PushInterpreterPool(Environment.ProcessorCount * 2, 4096, 1024, config);
     70      Pool = new PushInterpreterPool(Environment.ProcessorCount * 2, 4096, 1024, Config);
    7171    }
    7272
    7373    private void InitEvents() {
    74       config.EnabledExpressionsChanged += EnabledExpressionsChanged;
     74      Config.EnabledExpressionsChanged += EnabledExpressionsChanged;
     75      Reset += PushProblemReset;
    7576    }
    7677
    7778    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";
    9184
    9285    public const string CasesScopeParameterName = "CaseQualities";
     
    9487
    9588    private void InitParameters() {
    96       foreach (var paramater in config.Parameters) {
     89      foreach (var paramater in Config.Parameters) {
    9790        if (!Parameters.ContainsKey(paramater.Name)) {
    9891          Parameters.Add(paramater);
     
    10093      }
    10194
    102       if (!Parameters.ContainsKey(PushConfigurationParameterName))
    103         Parameters.Add(new ValueParameter<IReadOnlyPushConfiguration>(PushConfigurationParameterName, config) {
     95      if (!Parameters.ContainsKey(PUSH_CONFIGURATION_PARAMETER_NAME))
     96        Parameters.Add(new ValueParameter<IReadOnlyPushConfiguration>(PUSH_CONFIGURATION_PARAMETER_NAME, Config) {
    10497          Hidden = true
    10598        });
    10699
    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 
    116100      if (!Parameters.ContainsKey(CasesScopeParameterName))
    117101        Parameters.Add(new LookupParameter<BoolArray>(CasesScopeParameterName, "The training cases that have been successfully executed."));
     
    121105    }
    122106
     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
    123117    protected override void OnReset() {
    124118      base.OnReset();
    125119
    126120      // clear pools and free reserved memory
    127       pool.Clear();
     121      Pool.Clear();
    128122      IndividualMapper.Clear();
     123      RandomPool.Clear();
     124      Config.Seed = 0;
    129125    }
    130126
    131127    private void InitEncoding() {
    132128      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;
    135131    }
    136132
    137133    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    }
    158150
    159151    public override bool Maximization { get { return false; } }
     
    165157      var bestIdx = Array.IndexOf(qualities, bestQuality);
    166158      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();
    172165      var isIndividualBetter = AnalyzeBestTrainingSolution(program, bestQuality, results, rand);
     166      RandomPool.Free(rand);
    173167
    174168      if (isIndividualBetter) {
    175         rand.Reset(seed);
     169        rand = RandomPool.ResetAndAllocate();
    176170        AnalyzeBestTestSolution(program, results, rand);
    177       }
    178 
    179       randomPool.Free(rand);
     171        RandomPool.Free(rand);
     172      }
    180173    }
    181174
    182175    private void AnalyzeBestTestSolution(PushProgram program, ResultCollection results, IRandom random) {
    183       var testResult = PushEvaluator.EvaluateTraining(pool, program, random);
    184 
    185       if (!results.ContainsKey(TestQualityResultName)) {
    186         results.Add(new Result(TestQualityResultName, 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)));
    187180      } else {
    188         ((DoubleValue)results[TestQualityResultName].Value).Value = testResult.AvgQuality;
     181        ((DoubleValue)results[TEST_QUALITY_RESULT_NAME].Value).Value = testResult.AvgQuality;
    189182      }
    190183    }
    191184
    192185    private bool AnalyzeBestTrainingSolution(PushProgram program, double bestQuality, ResultCollection results, IRandom random) {
    193       if (!results.ContainsKey(BestTrainingSolutionResultName)) {
     186      if (!results.ContainsKey(BEST_TRAINING_SOLUTION_RESULT_NAME)) {
    194187        var solution = CreatePushSolution(
    195188            program,
    196189            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));
    202194        return true;
    203195      }
    204196
    205       var currentBestQuality = ((PushSolution)results[BestTrainingSolutionResultName].Value).Quality;
     197      var currentBestQuality = ((PushSolution)results[BEST_TRAINING_SOLUTION_RESULT_NAME].Value).Quality;
    206198
    207199      if ((!Maximization && currentBestQuality > bestQuality) ||
    208200           (Maximization && currentBestQuality < bestQuality)) {
    209         results[BestTrainingSolutionResultName].Value = CreatePushSolution(
     201        results[BEST_TRAINING_SOLUTION_RESULT_NAME].Value = CreatePushSolution(
    210202            program,
    211203            bestQuality,
    212204            random,
    213             config,
    214             PushEvaluator);
     205            Config);
    215206        return true;
    216207      }
    217208
    218209      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;
    231210    }
    232211
     
    235214      double bestQuality,
    236215      IRandom random,
    237       IReadOnlyPushConfiguration config,
    238       IPushEvaluator evaluator);
     216      IReadOnlyPushConfiguration config);
    239217
    240218    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);
    243230
    244231      individual[CaseQualitiesScopeParameterName] = new DoubleArray(result.ExampleQualities);
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Problem/PushSolution.cs

    r15017 r15273  
    1111    public readonly double Quality;
    1212    [Storable]
    13     public readonly IRandom Random;
     13    protected readonly IRandom Random;
    1414    [Storable]
    1515    public readonly IReadOnlyPushConfiguration Config;
     
    4646    }
    4747
     48    public IRandom GetRandom() {
     49      return (IRandom)Random.Clone();
     50    }
     51
    4852    public virtual PushSolution Simplify() {
    4953      return new PushSolution(Program, Quality, Random, Config, Evaluator, true);
Note: See TracChangeset for help on using the changeset viewer.