Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/18/17 01:15:25 (8 years ago)
Author:
pkimmesw
Message:

#2665 BenchmarkSuite, all examples, partially tested, VectorExpressions added

Location:
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Problem
Files:
4 edited

Legend:

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

    r14834 r14875  
    4444    private static PushProgram ToPushProgram(this IntegerVector vector, IReadOnlyPushConfiguration config, IRandom random) {
    4545      var expressions = new Expression[vector.Length];
     46      var enabledExpressions = config.EnabledExpressions;
     47      var ercOptions = config.ErcOptions;
    4648
    47       for (var i = 0; i < vector.Length; i++) {
    48         expressions[i] = CodeGeneratorUtils.CreateExpressionOrErc(vector[i], random, config.EnabledExpressions, config.ErcOptions);
    49       }
     49      for (var i = 0; i < vector.Length; i++)
     50        expressions[i] = CodeGeneratorUtils.CreateExpressionOrErc(vector[i], random, enabledExpressions, ercOptions);
    5051
    5152      return new PushProgram(expressions);
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Problem/PushEvaluator.cs

    r14834 r14875  
    33
    44namespace HeuristicLab.Problems.ProgramSynthesis.Push.Problem {
     5
    56  using HeuristicLab.BenchmarkSuite;
    67  using HeuristicLab.BenchmarkSuite.Problems;
     8  using HeuristicLab.Common;
    79  using HeuristicLab.Core;
    810  using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
     
    1315    public static double Evaluate(IPushInterpreter interpreter, PushProgram program, Example example, double worstResult) {
    1416      interpreter.BooleanStack.Push(example.InputBoolean);
    15       interpreter.IntegerStack.Push(example.InputInt);
     17      interpreter.IntegerStack.Push(example.InputInteger);
    1618      interpreter.FloatStack.Push(example.InputFloat);
    1719
    1820      interpreter.Run(program);
    1921
    20       var result = GetDiff(example.OutputInt, interpreter.IntegerStack, worstResult, LongDiffer)
    21                  + GetDiff(example.OutputFloat, interpreter.FloatStack, worstResult, DoubleDiffer)
    22                  + GetDiff(example.OutputBoolean, interpreter.BooleanStack, worstResult, BooleanDiffer);
     22      var result = GetDiff(example.OutputInteger, interpreter.IntegerStack, worstResult, IntegerDiffer)
     23                 + GetDiff(example.OutputFloat, interpreter.FloatStack, worstResult, FloatDiffer)
     24                 + GetDiff(example.OutputBoolean, interpreter.BooleanStack, worstResult, BooleanDiffer)
     25                 + GetDiff(example.OutputString, interpreter.StringStack, worstResult, StringDiffer)
     26                 + GetDiff(example.OutputChar, interpreter.CharStack, worstResult, CharDiffer)
     27                 + GetVectorDiff(example.OutputIntegerVector, interpreter.IntegerVectorStack, worstResult, (a, b) => VectorDiffer(a, b, IntegerDiffer))
     28                 + GetVectorDiff(example.OutputFloatVector, interpreter.FloatVectorStack, worstResult, (a, b) => VectorDiffer(a, b, FloatDiffer))
     29                 + GetVectorDiff(example.OutputStringVector, interpreter.StringVectorStack, worstResult, (a, b) => VectorDiffer(a, b, StringDiffer));
    2330
    2431      return result;
    2532    }
    2633
    27     public static EvaluationResult Evaluate(PushProgram program, PushInterpreterPool pool, IRandom random, Data data, int startIndex, int endIndex) {
     34    public static EvaluationResult Evaluate(PushProgram program, PushInterpreterPool pool, IRandom random, ProblemData data, int startIndex, int endIndex) {
    2835      var length = endIndex - startIndex;
    2936      if (length <= 0) return null;
     
    4552    }
    4653
    47     //public static EvaluationResult Evaluate(Individual individual, PushInterpreterPool pool, IRandom random, Data data, int startIndex, int endIndex) {
    48     //  var program = individual.ToPushProgram(pool.PushGpConfiguration);
    49     //  return Evaluate(program, pool, random, data, startIndex, endIndex);
    50     //}
    51 
    52     private static double DoubleDiffer(double a, double b) {
     54    private static double FloatDiffer(double a, double b) {
    5355      var result = a - b;
    5456
    55       if (result == double.MinValue || double.IsPositiveInfinity(result) || double.IsNaN(result))
     57      if (result.IsAlmost(double.MinValue) || double.IsPositiveInfinity(result) || double.IsNaN(result))
    5658        return double.MaxValue;
    5759
    58       if (result == double.MaxValue || double.IsNegativeInfinity(result))
     60      if (result.IsAlmost(double.MaxValue) || double.IsNegativeInfinity(result))
    5961        return double.MinValue;
    6062
     
    6264    }
    6365
    64     private static double LongDiffer(long a, long b) {
     66    private static double IntegerDiffer(long a, long b) {
    6567      var result = a - b;
    6668
     
    7072    private static double BooleanDiffer(bool a, bool b) {
    7173      return a && b ? 0 : a || b ? 1 : 2;
     74    }
     75
     76    private static double StringDiffer(string a, string b) {
     77      return LevenshteinDistance(a, b);
     78    }
     79
     80    private static double CharDiffer(char a, char b) {
     81      return IntegerDiffer(a, b);
     82    }
     83
     84    private static double VectorDiffer<T>(IReadOnlyList<T> a, IReadOnlyList<T> b, Func<T, T, double> differ) {
     85      var length = Math.Min(a.Count, b.Count);
     86      var result = 0d;
     87
     88      for (var i = 0; i < length; i++) {
     89        result += differ(a[i], b[i]);
     90      }
     91
     92      if (a.Count > b.Count) {
     93        var remainingLength = a.Count - length;
     94        for (var i = 0; i < remainingLength; i++)
     95          result += differ(a[i], default(T));
     96      }
     97
     98      return result;
     99    }
     100
     101    private static double GetVectorDiff<T>(IReadOnlyList<IReadOnlyList<T>> estimated, IPushStack<List<T>> resultStack, double worstResult, Func<IReadOnlyList<T>, IReadOnlyList<T>, double> differ)
     102      where T : IComparable {
     103      if (estimated.Count == 0) return 0d;
     104
     105      var diff = 0d;
     106      var comparableLength = 0;
     107
     108      if (!resultStack.IsEmpty) {
     109        var count = Math.Min(estimated.Count, resultStack.Count);
     110        var result = resultStack.Peek(count);
     111        comparableLength = Math.Min(estimated.Count, result.Length);
     112
     113        for (var i = 0; i < comparableLength; i++) {
     114          diff += Math.Min(differ(estimated[i], result[i]), worstResult);
     115        }
     116      }
     117
     118      var emptyTArray = new T[0];
     119      for (var i = comparableLength; i < estimated.Count - comparableLength; i++) {
     120        diff += differ(estimated[i], emptyTArray);
     121      }
     122
     123      return diff;
    72124    }
    73125
     
    95147      return diff;
    96148    }
     149
     150    /// <summary>
     151    /// https://www.dotnetperls.com/levenshtein
     152    /// </summary>
     153    /// <param name="s"></param>
     154    /// <param name="t"></param>
     155    /// <returns></returns>
     156    private static int LevenshteinDistance(string s, string t) {
     157      if (s == null && t == null) return 0;
     158      if (s == null) return t.Length;
     159      if (t == null) return s.Length;
     160
     161      int n = s.Length;
     162      int m = t.Length;
     163      int[,] d = new int[n + 1, m + 1];
     164
     165      // Step 1
     166      if (n == 0) {
     167        return m;
     168      }
     169
     170      if (m == 0) {
     171        return n;
     172      }
     173
     174      // Step 2
     175      for (int i = 0; i <= n; d[i, 0] = i++) {
     176      }
     177
     178      for (int j = 0; j <= m; d[0, j] = j++) {
     179      }
     180
     181      // Step 3
     182      for (int i = 1; i <= n; i++) {
     183        //Step 4
     184        for (int j = 1; j <= m; j++) {
     185          // Step 5
     186          int cost = (t[j - 1] == s[i - 1]) ? 0 : 1;
     187
     188          // Step 6
     189          d[i, j] = Math.Min(
     190              Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1),
     191              d[i - 1, j - 1] + cost);
     192        }
     193      }
     194      // Step 7
     195      return d[n, m];
     196    }
    97197  }
    98198}
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Problem/PushProblem.cs

    r14834 r14875  
    1212  using Expressions;
    1313  using HeuristicLab.Data;
    14   using HeuristicLab.Problems.ProgramSynthesis.Push.Erc;
     14  using HeuristicLab.Problems.ProgramSynthesis.Base.Erc;
    1515
    1616  using Instances;
     
    2525  [Creatable(CreatableAttribute.Categories.GeneticProgrammingProblems, Priority = 180)]
    2626  [Item("Push Problem", "")]
    27   public class PushProblem : SingleObjectiveBasicProblem<IntegerVectorEncoding>, IProblemInstanceConsumer<Data> {
     27  public class PushProblem : SingleObjectiveBasicProblem<IntegerVectorEncoding>, IProblemInstanceConsumer<ProblemData> {
    2828    [Storable]
    2929    private readonly PushConfiguration config;
     
    3232
    3333    public const string CaseQualitiesScopeParameterName = "CaseQualities";
     34    private const string BestTrainingSolutionResultName = "Best Solution";
     35    private const string TestQualityResultName = "Test Quality";
    3436
    3537    public PushProblem() {
     
    6365    private void InitData() {
    6466      pool = new PushInterpreterPool(Environment.ProcessorCount * 2, 4096, 1024, config);
    65 
    66       //var solutionCreator = new PointsBasedPushProgramCreator(config.ErcOptions);
    67       //SolutionCreator = solutionCreator;
    68       //Encoding.SolutionCreator = solutionCreator;
    6967    }
    7068
     
    9391    private const string TopLevelPopCodeParameterName = "TopLevelPopCode";
    9492    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.";
    95     private const string MinRandomIntegerParameterName = "MinRandomInteger";
    96     private const string MinRandomIntegerParameterDescription = "The minimum INTEGER that will be produced as an ephemeral random INTEGER constant or from a call to INTEGER.RAND.";
    97     private const string MaxRandomIntegerParameterName = "MaxRandomInteger";
    98     private const string MaxRandomIntegerParameterDescription = "The maximum INTEGER that will be produced as an ephemeral random INTEGER constant or from a call to INTEGER.RAND.";
    99     private const string MinRandomFloatParameterName = "MinRandomFloat";
    100     private const string MinRandomFloatParameterDescription = "The minimum FLOAT that will be produced as an ephemeral random FLOAT constant or from a call to FLOAT.RAND.";
    101     private const string MaxRandomFloatParameterName = "MaxRandomFloat";
    102     private const string MaxRandomFloatParameterDescription = "The maximum FLOAT that will be produced as an ephemeral random FLOAT constant or from a call to FLOAT.RAND.";
    103     private const string NewErcNameProbabilityParameterName = "NewErcNameProbability";
    104     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.";
    10593    private const string MaxPointsInRandomInstructionParameterName = "MaxPointsInRandomInstruction";
    10694    private const string MaxPointsInRandomInstructionParameterDescription = "MaxPointsInRandomInstruction";
     
    117105        config));
    118106
    119       Parameters.Add(new ValueParameter<Data>(
     107      Parameters.Add(new ValueParameter<ProblemData>(
    120108        DataParameterName,
    121109        DataParameterDescription));
    122110
    123       Parameters.Add(new FixedValueParameter<ErcOptions>(ErcOptionsParameterName, config.ErcOptions));
     111      Parameters.Add(new ValueParameter<ErcOptions>(ErcOptionsParameterName, config.ErcOptions));
    124112
    125113      Parameters.Add(new FixedValueParameter<IntValue>(
     
    188176    }
    189177
    190     public IValueParameter<Data> DataParameter
    191     {
    192       get { return (IValueParameter<Data>)Parameters[DataParameterName]; }
    193     }
    194 
    195     public Data Data
     178    public IValueParameter<ProblemData> DataParameter
     179    {
     180      get { return (IValueParameter<ProblemData>)Parameters[DataParameterName]; }
     181    }
     182
     183    public ProblemData Data
    196184    {
    197185      get { return DataParameter.Value; }
     
    356344
    357345    public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
    358       const string bestSolutionResultName = "Best Solution";
    359346      var bestQuality = Maximization ? qualities.Max() : qualities.Min();
    360347      var bestIdx = Array.IndexOf(qualities, bestQuality);
    361       var bestIndividual = individuals[bestIdx];
    362       var solution = new PushSolution(bestIndividual.IntegerVector(), bestQuality, Data, random, config, DataBounds.TrainingRange.Start, DataBounds.TrainingRange.End);
    363 
    364       if (!results.ContainsKey(bestSolutionResultName)) {
    365         results.Add(new Result(bestSolutionResultName, solution));
     348      var bestIndividual = individuals[bestIdx].IntegerVector();
     349
     350      var isIndividualBetter = AnalyzeBestTrainingSolution(bestIndividual, bestQuality, results, random);
     351
     352      if (isIndividualBetter) {
     353        AnalyzeBestTestSolution(bestIndividual, results, random);
     354      }
     355    }
     356
     357    private void AnalyzeBestTestSolution(IntegerVector bestIndividual, ResultCollection results, IRandom random) {
     358      var program = bestIndividual.ToPushProgram(config, randomPool);
     359      var trainingResult = PushEvaluator.Evaluate(program, pool, random, Data, DataBounds.TestRange.Start, DataBounds.TestRange.End);
     360
     361      if (!results.ContainsKey(TestQualityResultName)) {
     362        results.Add(new Result(TestQualityResultName, new DoubleValue(trainingResult.TotalQuality)));
    366363      } 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         }
    373       }
     364        ((DoubleValue)results[TestQualityResultName].Value).Value = trainingResult.TotalQuality;
     365      }
     366    }
     367
     368    private bool AnalyzeBestTrainingSolution(IntegerVector bestIndividual, double bestQuality, ResultCollection results, IRandom random) {
     369      var solution = new PushSolution(bestIndividual, bestQuality, Data, random, config, DataBounds.TrainingRange.Start, DataBounds.TrainingRange.End);
     370
     371      if (!results.ContainsKey(BestTrainingSolutionResultName)) {
     372        results.Add(new Result(BestTrainingSolutionResultName, solution));
     373        return true;
     374      }
     375
     376      var currentBestQuality = ((PushSolution)results[BestTrainingSolutionResultName].Value).Quality;
     377
     378      if (Maximization && currentBestQuality < bestQuality ||
     379         !Maximization && currentBestQuality > bestQuality) {
     380        results[BestTrainingSolutionResultName].Value = solution;
     381        return true;
     382      }
     383
     384      return false;
    374385    }
    375386
     
    389400    }
    390401
    391     public void Load(Data data) {
     402    public void Load(ProblemData data) {
    392403      Data = data;
    393404      BestKnownQuality = data.BestResult;
    394405      MaxPointsInProgram = data.MaxSize;
    395406      EvalPushLimit = data.EvalLimit;
     407      ErcOptions = data.ErcOptions;
    396408
    397409      config.EnabledExpressions = (IList<string>)ExpressionTable.GetExpressionsByStackTypes((StackTypes)data.EnabledDataTypes);
     
    408420
    409421      DataBounds.TrainingRange.Start = 0;
    410       DataBounds.TrainingRange.End = Data.OriginalTrainingCount;
    411       DataBounds.TestRange.Start = Data.OriginalTrainingCount;
    412       DataBounds.TestRange.End = Data.OriginalTrainingCount + Data.OriginalTestCount;
     422      DataBounds.TrainingRange.End = Data.TrainingCount;
     423      DataBounds.TestRange.Start = Data.TrainingCount;
     424      DataBounds.TestRange.End = Data.TrainingCount + Data.TestCount;
    413425    }
    414426  }
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Problem/PushSolution.cs

    r14834 r14875  
    1414    public readonly IntegerVector IntegerVector;
    1515    [Storable]
    16     public readonly Data Data;
     16    public readonly ProblemData Data;
    1717    [Storable]
    1818    public readonly IRandom Random;
     
    2323    [Storable]
    2424    public readonly int DataEnd;
    25 
    2625    [Storable]
    2726    public readonly bool Simplify;
    2827
    29     public PushSolution(IntegerVector integerVector, double quality, Data data, IRandom random, IReadOnlyPushConfiguration config, int dataStart, int dataEnd, bool simplify = false)
     28    public PushSolution(IntegerVector integerVector, double quality, ProblemData data, IRandom random, IReadOnlyPushConfiguration config, int dataStart, int dataEnd, bool simplify = false)
    3029      : base("Solution", "A push solution.") {
    3130      IntegerVector = integerVector;
Note: See TracChangeset for help on using the changeset viewer.