Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/26/17 19:34:13 (7 years ago)
Author:
pkimmesw
Message:

#2665 Fixed analyzer, fixed Plush encoding + operators, adpated print evaluation according to McPhee

Location:
branches/PushGP/HeuristicLab.PushGP
Files:
12 added
8 deleted
34 edited

Legend:

Unmodified
Added
Removed
  • branches/PushGP/HeuristicLab.PushGP

    • Property svn:ignore set to
      *.user
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Analyzer/IndividualZeroErrorAnalyzer.cs

    r15275 r15289  
    1919    private const string RESULTS_PARAMETER_NAME = "Results";
    2020    private const string INDIVIDUAL_ZERO_ERROR_PARAMETER_NAME = "ZeroErrorIndividualsPerCase";
     21    private const string INDIVIDUAL_ZERO_ERROR_HISTORY_PARAMETER_NAME = INDIVIDUAL_ZERO_ERROR_PARAMETER_NAME + "History";
    2122
    2223    private const string RESULT_PARAMETER_NAME = "Zero Error Individuals Per Case";
    2324    private const string RESULT_PARAMETER_DESCRIPTION = "Relative frequency of instructions aggregated over the whole population.";
    2425    private const string Y_AXIS_TITLE = "Count of zero error individuals";
    25     private const string X_AXIS_TITLE = "Case Index";
     26    private const string X_AXIS_TITLE = "Case Nr";
    2627    private const string ROW_NAME = "Cases";
     28    private const string HISTORY_TABLE_NAME = "Individual Zero Error history";
    2729
    2830    public IndividualZeroErrorAnalyzer() {
     
    3133        "The data table to store the count of individuals with zero error."));
    3234
     35      Parameters.Add(new ValueLookupParameter<DataTableHistory>(
     36        INDIVIDUAL_ZERO_ERROR_HISTORY_PARAMETER_NAME,
     37        "The data table to store the history."));
     38
    3339      Parameters.Add(new LookupParameter<ResultCollection>(
    3440        RESULTS_PARAMETER_NAME,
     
    3844       IntegerVectorPushProblem.CaseQualitiesScopeParameterName,
    3945       "The quality of every single training case for each individual."));
     46
     47      Parameters.Add(new ValueParameter<BoolValue>(
     48        "StoreHistory",
     49        "True if the history of the analysis should be stored.",
     50        new BoolValue(false)));
     51
     52      Parameters.Add(new ValueParameter<IntValue>(
     53        "UpdateInterval",
     54        "The interval in which the analysis should be applied.",
     55        new IntValue(1)));
     56
     57      Parameters.Add(new ValueParameter<IntValue>(
     58        "UpdateCounter",
     59        "The value which counts how many times the operator was called since the last update.",
     60        new IntValue(0)));
     61
     62      IndividualZeroErrorParameter.Hidden = true;
     63      IndividualZeroErrorHistoryParameter.Hidden = true;
     64      ResultsParameter.Hidden = true;
     65      UpdateCounterParameter.Hidden = true;
    4066    }
    4167
     
    5177    public bool EnabledByDefault { get { return true; } }
    5278
     79    public ValueParameter<BoolValue> StoreHistoryParameter
     80    {
     81      get { return (ValueParameter<BoolValue>)Parameters["StoreHistory"]; }
     82    }
     83    public ValueParameter<IntValue> UpdateIntervalParameter
     84    {
     85      get { return (ValueParameter<IntValue>)Parameters["UpdateInterval"]; }
     86    }
     87    public ValueParameter<IntValue> UpdateCounterParameter
     88    {
     89      get { return (ValueParameter<IntValue>)Parameters["UpdateCounter"]; }
     90    }
     91
    5392    public ILookupParameter<DataTable> IndividualZeroErrorParameter
    5493    {
     
    5695    }
    5796
     97    public ValueLookupParameter<DataTableHistory> IndividualZeroErrorHistoryParameter
     98    {
     99      get { return (ValueLookupParameter<DataTableHistory>)Parameters[INDIVIDUAL_ZERO_ERROR_HISTORY_PARAMETER_NAME]; }
     100    }
     101
    58102    public ILookupParameter<ResultCollection> ResultsParameter
    59103    {
     
    67111
    68112    public override IOperation Apply() {
     113      UpdateCounterParameter.Value.Value++;
     114      // the analyzer runs periodically, every 'updateInterval' times
     115      if (UpdateCounterParameter.Value.Value != UpdateIntervalParameter.Value.Value)
     116        return base.Apply();
     117
     118      UpdateCounterParameter.Value.Value = 0; // reset counter
     119
    69120      var caseQualitiesPerIndividual = CaseQualitiesParameter.ActualValue;
    70121      var caseCount = caseQualitiesPerIndividual[0].Length;
     
    80131            YAxisTitle = Y_AXIS_TITLE,
    81132            XAxisTitle = X_AXIS_TITLE,
    82             XAxisMinimumFixedValue = 0,
     133            YAxisMinimumFixedValue = 0,
     134            YAxisMaximumFixedValue = 1,
     135            YAxisMinimumAuto = false,
     136            SecondYAxisMaximumAuto = false,
     137            XAxisMinimumFixedValue = 1,
    83138            XAxisMaximumFixedValue = caseCount,
    84139            XAxisMaximumAuto = false,
     
    100155              StartIndexZero = true,
    101156              IsVisibleInLegend = false,
    102               ChartType = DataRowVisualProperties.DataRowChartType.Bars,
     157              ChartType = DataRowVisualProperties.DataRowChartType.Columns,
    103158            }
    104159        };
     
    107162      }
    108163
    109       var caseCountChanged = row.Values.Count != caseCount;
    110 
    111       if (caseCountChanged) {
    112         individualZeroErrorCounts.VisualProperties.XAxisMaximumFixedValue = caseCount;
    113         row.Values.Clear();
    114       }
     164      row.Values.Clear();
    115165
    116166      for (var i = 0; i < caseCount; i++) {
     
    124174        }
    125175
    126         if (caseCountChanged)
    127           row.Values.Add(count);
    128         else
    129           row.Values[i] = count;
     176        var relativeCount = count / (double)caseQualitiesPerIndividual.Length;
     177
     178        row.Values.Add(relativeCount);
     179      }
     180
     181      var storeHistory = StoreHistoryParameter.Value.Value;
     182
     183      if (storeHistory) {
     184        var history = IndividualZeroErrorHistoryParameter.ActualValue;
     185
     186        if (history == null) {
     187          history = new DataTableHistory();
     188          IndividualZeroErrorHistoryParameter.ActualValue = history;
     189        }
     190
     191        history.Add((DataTable)individualZeroErrorCounts.Clone());
     192
     193        if (!results.ContainsKey(HISTORY_TABLE_NAME)) {
     194          results.Add(new Result(HISTORY_TABLE_NAME, history));
     195        } else {
     196          results[HISTORY_TABLE_NAME].Value = history;
     197        }
    130198      }
    131199
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Analyzer/IndividualZeroErrorDistributionAnalyzer.cs

    r15275 r15289  
    2020    private const string RESULTS_PARAMETER_NAME = "Results";
    2121    private const string INDIVIDUAL_ZERO_ERROR_PARAMETER_NAME = "ZeroErrorIndividualsPerCaseDistribution";
     22    private const string INDIVIDUAL_ZERO_ERROR_HISTORY_PARAMETER_NAME = "ZeroErrorIndividualsPerCaseDistributionHistory";
    2223
     24    private const string HISTORY_TABLE_NAME = "Individual Zero Error Distribution History";
    2325    private const string RESULT_PARAMETER_NAME = "Zero Error Individual Per Case Distribution";
    2426    private const string RESULT_PARAMETER_DESCRIPTION = "Relative frequency of instructions aggregated over the whole population.";
    2527    private const string Y_AXIS_TITLE = "Number of zero error cases";
    26     private const string X_AXIS_TITLE = "Number of individuals";
     28    private const string X_AXIS_TITLE = "Individuals Nr";
    2729    private const string ROW_NAME = "Cases";
    2830
     
    3133        INDIVIDUAL_ZERO_ERROR_PARAMETER_NAME,
    3234        "The data table to store the count of individuals with zero error."));
     35
     36      Parameters.Add(new ValueLookupParameter<DataTableHistory>(
     37        INDIVIDUAL_ZERO_ERROR_HISTORY_PARAMETER_NAME,
     38        "The data table to store the history."));
    3339
    3440      Parameters.Add(new LookupParameter<ResultCollection>(
     
    3945       IntegerVectorPushProblem.CaseQualitiesScopeParameterName,
    4046       "The quality of every single training case for each individual."));
     47
     48      Parameters.Add(new ValueParameter<BoolValue>(
     49        "StoreHistory",
     50        "True if the history of the analysis should be stored.",
     51        new BoolValue(false)));
     52
     53      Parameters.Add(new ValueParameter<IntValue>(
     54        "UpdateInterval",
     55        "The interval in which the analysis should be applied.",
     56        new IntValue(1)));
     57
     58      Parameters.Add(new ValueParameter<IntValue>(
     59        "UpdateCounter",
     60        "The value which counts how many times the operator was called since the last update.",
     61        new IntValue(0)));
     62
     63      IndividualZeroErrorDistributionParameter.Hidden = true;
     64      IndividualZeroErrorDistributionHistoryParameter.Hidden = true;
     65      ResultsParameter.Hidden = true;
     66      UpdateCounterParameter.Hidden = true;
    4167    }
    4268
    43     [StorableConstructorAttribute]
     69    [StorableConstructor]
    4470    public IndividualZeroErrorDistributionAnalyzer(bool deserializing) : base(deserializing) { }
    4571
     
    5278    public bool EnabledByDefault { get { return true; } }
    5379
     80    public ValueParameter<BoolValue> StoreHistoryParameter
     81    {
     82      get { return (ValueParameter<BoolValue>)Parameters["StoreHistory"]; }
     83    }
     84    public ValueParameter<IntValue> UpdateIntervalParameter
     85    {
     86      get { return (ValueParameter<IntValue>)Parameters["UpdateInterval"]; }
     87    }
     88    public ValueParameter<IntValue> UpdateCounterParameter
     89    {
     90      get { return (ValueParameter<IntValue>)Parameters["UpdateCounter"]; }
     91    }
     92
    5493    public ILookupParameter<DataTable> IndividualZeroErrorDistributionParameter
    5594    {
    5695      get { return (ILookupParameter<DataTable>)Parameters[INDIVIDUAL_ZERO_ERROR_PARAMETER_NAME]; }
     96    }
     97
     98    public ValueLookupParameter<DataTableHistory> IndividualZeroErrorDistributionHistoryParameter
     99    {
     100      get { return (ValueLookupParameter<DataTableHistory>)Parameters[INDIVIDUAL_ZERO_ERROR_HISTORY_PARAMETER_NAME]; }
    57101    }
    58102
     
    68112
    69113    public override IOperation Apply() {
     114      UpdateCounterParameter.Value.Value++;
     115      // the analyzer runs periodically, every 'updateInterval' times
     116      if (UpdateCounterParameter.Value.Value != UpdateIntervalParameter.Value.Value)
     117        return base.Apply();
     118
     119      UpdateCounterParameter.Value.Value = 0; // reset counter
     120
    70121      var caseQualitiesPerIndividual = CaseQualitiesParameter.ActualValue;
    71122      var individualCount = caseQualitiesPerIndividual.Length;
     
    82133            YAxisTitle = Y_AXIS_TITLE,
    83134            XAxisTitle = X_AXIS_TITLE,
    84             XAxisMinimumFixedValue = 0,
    85             XAxisMaximumAuto = true,
     135            YAxisMaximumFixedValue = 1,
     136            YAxisMinimumFixedValue = 0,
     137            YAxisMaximumAuto = false,
     138            YAxisMinimumAuto = false,
     139            XAxisMinimumFixedValue = 1,
     140            XAxisMaximumFixedValue = individualCount,
     141            XAxisMaximumAuto = false,
    86142            XAxisMinimumAuto = false,
    87143          }
     
    101157              StartIndexZero = true,
    102158              IsVisibleInLegend = false,
    103               ChartType = DataRowVisualProperties.DataRowChartType.Histogram,
     159              ChartType = DataRowVisualProperties.DataRowChartType.Columns,
    104160            }
    105161        };
     
    108164      }
    109165
    110       var caseCountChanged = row.Values.Count != caseCount;
     166      row.Values.Clear();
    111167
    112       if (caseCountChanged) {
    113         row.Values.Clear();
     168      for (var i = 0; i < caseQualitiesPerIndividual.Length; i++) {
     169        var count = caseQualitiesPerIndividual[i].Count(q => q == 0.0);
     170        var ratio = count / (double)caseCount;
     171
     172        row.Values.Add(ratio);
    114173      }
    115174
    116       // key represents the number of cases with zero error
    117       // value represents the number of individuals with key cases with zero error
    118       var distribution = caseQualitiesPerIndividual
    119               .Select(individual => individual.Count(x => x == 0.0))
    120               .GroupBy(x => x)
    121               .ToDictionary(x => x.Key, x => x.Count());
     175      var storeHistory = StoreHistoryParameter.Value.Value;
    122176
    123       for (var i = 0; i < caseCount; i++) {
    124         int count;
    125         distribution.TryGetValue(i, out count);
    126         var x = count / (double)individualCount;
     177      if (storeHistory) {
     178        var history = IndividualZeroErrorDistributionHistoryParameter.ActualValue;
    127179
    128         if (caseCountChanged)
    129           row.Values.Add(x);
    130         else
    131           row.Values[i] = x;
     180        if (history == null) {
     181          history = new DataTableHistory();
     182          IndividualZeroErrorDistributionHistoryParameter.ActualValue = history;
     183        }
     184
     185        history.Add((DataTable)individualZeroErrorDistribution.Clone());
     186
     187        if (!results.ContainsKey(HISTORY_TABLE_NAME)) {
     188          results.Add(new Result(HISTORY_TABLE_NAME, history));
     189        } else {
     190          results[HISTORY_TABLE_NAME].Value = history;
     191        }
    132192      }
    133193
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Analyzer/PushExpressionFrequencyAnalyzer.cs

    r15273 r15289  
    11namespace HeuristicLab.Problems.ProgramSynthesis.Push.Analyzer {
    22  using System;
     3  using System.Collections.Generic;
    34  using System.Linq;
    45
     
    1415  using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
    1516  using HeuristicLab.Problems.ProgramSynthesis.Push.Configuration;
     17  using HeuristicLab.Problems.ProgramSynthesis.Push.Encoding;
    1618  using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
    1719  using HeuristicLab.Problems.ProgramSynthesis.Push.Individual;
    18   using HeuristicLab.Problems.ProgramSynthesis.Push.ObjectPools.Random;
    1920
    2021  /// <summary>
     
    2425  [StorableClass]
    2526  public class PushExpressionFrequencyAnalyzer : SingleSuccessorOperator, IPushExpressionAnalyzer {
    26     private readonly SeededRandomPool randomPool = new SeededRandomPool();
    2727
    2828    private const string PUSH_CONFIGURATION_PARAMETER_NAME = "PushConfiguration";
     
    4040      Parameters.Add(new LookupParameter<IReadOnlyPushConfiguration>(PUSH_CONFIGURATION_PARAMETER_NAME, "The current specified push configuration."));
    4141      Parameters.Add(new ScopeTreeLookupParameter<IntegerVector>(INTEGER_VECTOR_PARAMETER_NAME, "The integer vectors to analyze."));
     42      Parameters.Add(new ScopeTreeLookupParameter<PlushVector>("Plush", "The plush vectors to analyze."));
    4243      Parameters.Add(new LookupParameter<DataTable>(EXPRESSION_FREQUENCIES_PARAMETER_NAME, "The data table to store the instruction frequencies."));
    4344      Parameters.Add(new LookupParameter<ResultCollection>(RESULTS_PARAMETER_NAME, "The result collection where the symbol frequencies should be stored."));
     
    6061    {
    6162      get { return (ILookupParameter<IReadOnlyPushConfiguration>)Parameters[PUSH_CONFIGURATION_PARAMETER_NAME]; }
     63    }
     64
     65    public IScopeTreeLookupParameter<PlushVector> PlushVectorParameter
     66    {
     67      get { return (IScopeTreeLookupParameter<PlushVector>)Parameters["Plush"]; }
    6268    }
    6369
     
    95101    public override IOperation Apply() {
    96102      var config = PushConfigurationParameter.ActualValue;
    97       var integerVectors = IntegerVectorParameter.ActualValue;
    98103
    99       randomPool.Seed = config.Seed;
     104      IReadOnlyList<PushProgram> pushPrograms = null;
    100105
    101       var pushPrograms = integerVectors.Select(iv => {
    102         var random = randomPool.ResetAndAllocate();
    103         var program = iv.ToPushProgram(config, random);
    104         randomPool.Free(random);
    105 
    106         return program;
    107       });
     106      if (IntegerVectorParameter.ActualValue.Length > 0) {
     107        pushPrograms = IntegerVectorParameter.ActualValue.Select(iv => iv.ToPushProgram(config)).ToList();
     108      } else if (PlushVectorParameter.ActualValue.Length > 0) {
     109        pushPrograms = PlushVectorParameter.ActualValue.Select(pv => pv.PushProgram).ToList();
     110      } else {
     111        // nothing to do
     112        return base.Apply();
     113      }
    108114
    109115      var results = ResultsParameter.ActualValue;
     
    136142          group => group.Count());
    137143
    138       var totalNumberofExpressions = Math.Max(1.0, expressionFrequencies.Values.Sum());
     144      var totalNumberOfExpressions = Math.Max(1.0, expressionFrequencies.Values.Sum());
    139145
    140146      // all rows must have the same number of values so we can just take the first
     
    152158        }
    153159
    154         frequencies.Rows[pair.Key].Values.Add(Math.Round(pair.Value / totalNumberofExpressions, 3));
     160        frequencies.Rows[pair.Key].Values.Add(Math.Round(pair.Value / totalNumberOfExpressions, 3));
    155161      }
    156162
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Attributes/PushStackAttribute.cs

    r14834 r15289  
    77  public class PushStackAttribute : Attribute {
    88    public readonly StackTypes StackType;
     9
    910    public PushStackAttribute(StackTypes stackType) {
    1011      StackType = stackType;
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Configuration/IEnabledExpressionsConfiguration.cs

    r15273 r15289  
    2020    int InExpressionCount { get; }
    2121
     22
    2223    IReadOnlyList<string> EnabledExpressions { get; }
    2324    IReadOnlyDictionary<StackTypes, int> ExpressionsPerStackCount { get; }
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Configuration/IReadonlyPushConfiguration.cs

    r15273 r15289  
    11namespace HeuristicLab.Problems.ProgramSynthesis.Push.Configuration {
    2   using System.Collections.Generic;
    32  using Base.Erc;
    43
     
    65
    76  public interface IReadOnlyPushConfiguration : IReadOnlyExpressionsConfiguration {
    8     int Seed { get; }
    97    int EvalPushLimit { get; }
    108    int MaxDepth { get; }
     
    1816    double ParenthesesCloseBiasLevel { get; }
    1917    IReadOnlyErcOptions ErcOptions { get; }
    20     IReadOnlyList<string> EnabledExpressions { get; }
    2118    string FloatStringFormat { get; }
    2219
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Configuration/PushConfiguration.cs

    r15032 r15289  
    5252
    5353
    54     IReadOnlyList<string> IReadOnlyPushConfiguration.EnabledExpressions { get { return enabledExpressions; } }
     54    IReadOnlyList<string> IReadOnlyExpressionsConfiguration.EnabledExpressions { get { return enabledExpressions; } }
    5555
    5656    [Storable]
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Configuration/PushConfigurationParameterCollection.cs

    r15273 r15289  
    154154    }
    155155
    156     IReadOnlyList<string> IReadOnlyPushConfiguration.EnabledExpressions
     156    IReadOnlyList<string> IReadOnlyExpressionsConfiguration.EnabledExpressions
    157157    {
    158158      get
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Constants/PushEnvironment.cs

    r15189 r15289  
    1717    public const string CharSymbolStr = "'";
    1818    public const string StringSymbolStr = "\"";
    19     public const string NewLine = "\n";
     19    public const char NewLine = '\n';
     20    public const string NewLineStr = "\n";
    2021  }
    2122}
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Crossover/AlternationCrossover.cs

    r15275 r15289  
    1010  using HeuristicLab.Parameters;
    1111  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     12  using HeuristicLab.Problems.ProgramSynthesis.Base.Extensions;
    1213  using HeuristicLab.Problems.ProgramSynthesis.Push.Encoding;
    1314  using HeuristicLab.Random;
     
    2324    public AlternationCrossover() {
    2425      Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator which should be used for stochastic crossover operators."));
     26
    2527      Parameters.Add(new ScopeTreeLookupParameter<PlushVector>("Parents", "The parent vectors which should be crossed."));
    2628      ParentsParameter.ActualName = "PlushVector";
     29
    2730      Parameters.Add(new LookupParameter<PlushVector>("Child", "The child vector resulting from the crossover."));
    2831      ChildParameter.ActualName = "PlushVector";
     32
    2933      Parameters.Add(new FixedValueParameter<PercentValue>("AlternationRate", "Specifies the probability of switching to another parent.", new PercentValue(0.5)));
    3034      Parameters.Add(new FixedValueParameter<DoubleValue>("AlignmentDeviation", "When alternating between parents, the index at which to continue copying may be offset backward or forward some amount based on a random sample from a normal distribution with mean 0 and standard deviation set by the alignment deviation parameter", new DoubleValue(1.0)));
     
    9094
    9195    public sealed override IOperation InstrumentedApply() {
    92       ChildParameter.ActualValue = Cross(RandomParameter.ActualValue, ParentsParameter.ActualValue);
     96      ChildParameter.ActualValue = Cross(
     97        RandomParameter.ActualValue,
     98        ParentsParameter.ActualValue,
     99        AlternationRate,
     100        MaxLength,
     101        AlignmentDeviation);
    93102      return base.InstrumentedApply();
    94103    }
    95104
    96     private PlushVector Cross(IRandom random, ItemArray<PlushVector> parents) {
    97       var normalDistributedRandom = new NormalDistributedRandom(random, Mean, AlignmentDeviation);
     105    private static PlushVector Cross(
     106      IRandom random,
     107      ItemArray<PlushVector> parents,
     108      double alternationRate,
     109      int maxChildLength,
     110      double alignmentDeviation) {
     111      var normalDistributedRandom = new NormalDistributedRandom(random, Mean, alignmentDeviation);
    98112      var maxLength = parents.Max(p => p.Entries.Count);
    99113      var parentIndex = random.Next(0, 2);
    100114      var parent = parents[parentIndex];
    101115      var child = new PlushVector(maxLength);
    102       var maxChildLength = MaxLength;
    103116
    104117      for (var i = 0; i < maxLength && child.Entries.Count <= maxChildLength; i++) {
     
    109122
    110123        // switch parent?
    111         if (random.NextDouble() < AlternationRate) {
     124        if (random.NextDouble() < alternationRate) {
    112125          parentIndex = parentIndex == 0 ? 1 : 0;
    113126          parent = parents[parentIndex];
    114           i += normalDistributedRandom.Next();
     127          i += normalDistributedRandom.NextRounded();
    115128          i = Math.Max(i, 0);
    116129        }
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Encoding/PlushEncoding.cs

    r15275 r15289  
    3434      inInstructionProbabilityParameter = new FixedValueParameter<PercentValue>(Name + ".InInstructionProbability");
    3535
     36      Parameters.Add(minLengthParameter);
     37      Parameters.Add(maxLengthParameter);
     38      Parameters.Add(minCloseParameter);
     39      Parameters.Add(maxCloseParameter);
     40      Parameters.Add(closeBiasLevelParameter);
    3641      Parameters.Add(instructionsParameter);
    3742      Parameters.Add(ercOptionsParameter);
     
    371376        creator.MinCloseParameter.ActualName = MinCloseParameter.Name;
    372377        creator.MaxCloseParameter.ActualName = MinCloseParameter.Name;
     378        creator.CloseBiasLevelParameter.ActualName = CloseBiasLevelParameter.Name;
    373379        creator.ErcOptionsParameter.ActualName = ErcOptionsParameter.Name;
    374380        creator.InstructionsParameter.ActualName = InstructionsParameter.Name;
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Encoding/PlushVector.cs

    r15275 r15289  
    2626
    2727    public PlushVector(PlushVector origin, Cloner cloner) : base(origin, cloner) {
     28      entries = new List<PlushEntry>(origin.entries);
     29      pushProgram = origin.pushProgram;
    2830    }
    2931
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/PrintExpressions.cs

    r15189 r15289  
    7272
    7373    public override void Eval(IInternalPushInterpreter interpreter) {
    74       Eval(interpreter, interpreter.BooleanStack);
     74      var value = interpreter.BooleanStack.Pop();
     75      interpreter.PrintStack.Push(value);
    7576    }
    7677  }
     
    9293
    9394    public override void Eval(IInternalPushInterpreter interpreter) {
    94       Eval(interpreter, interpreter.CharStack);
     95      var value = interpreter.CharStack.Pop();
     96      interpreter.PrintStack.Push(value);
    9597    }
    9698  }
     
    113115
    114116    public override void Eval(IInternalPushInterpreter interpreter) {
    115       Eval(interpreter, interpreter.ExecStack);
     117      var value = interpreter.ExecStack.Pop();
     118      interpreter.PrintStack.Push(value);
    116119    }
    117120  }
     
    136139      var str = value.ToString(interpreter.Configuration.FloatStringFormat, CultureInfo.InvariantCulture);
    137140
    138       if (interpreter.PrintStack.IsEmpty)
    139         interpreter.PrintStack.Push(str);
    140       else
    141         interpreter.PrintStack.Top += str;
     141      interpreter.PrintStack.Push(str);
    142142    }
    143143  }
     
    180180
    181181    public override void Eval(IInternalPushInterpreter interpreter) {
    182       Eval(interpreter, interpreter.StringStack);
     182      var value = interpreter.StringStack.Pop();
     183      interpreter.PrintStack.Push(value);
    183184    }
    184185  }
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Generators/CodeGenerator/CodeGeneratorUtils.cs

    r15275 r15289  
    2828        ? customExpressions.Values.ElementAt(index - enabledExpressionCount)
    2929        : MapToExpression(index, random, ercOptions, config);
     30    }
     31
     32    internal static Expression MapToExpression(
     33      int index,
     34      IReadOnlyExpressionsConfiguration config) {
     35
     36      switch (index) {
     37        case PushSolutionEncoding.Noop:
     38          return Noop;
     39      }
     40
     41      var name = config.EnabledExpressions[index];
     42      return ExpressionTable.GetExpression(name);
    3043    }
    3144
     
    7689        instructions.InExpressionCount > 0) {
    7790
    78         var nr = random.Next(0, instructions.InExpressionCount) + 1;
    79         expression = ExpressionTable.InExpressionTable[nr];
     91        var index = random.Next(0, instructions.InExpressionCount);
     92        expression = ExpressionTable.InExpressionTable[index];
    8093      } else {
    8194        expression = GetRandomExpression(random, instructions);
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Individual/IndividualExtensions.cs

    r15273 r15289  
    11namespace HeuristicLab.Problems.ProgramSynthesis.Push.Individual {
    22  using System.Collections.Generic;
    3 
    4   using HeuristicLab.Core;
    53  using HeuristicLab.Encodings.IntegerVectorEncoding;
    64  using HeuristicLab.Optimization;
    75  using HeuristicLab.Problems.ProgramSynthesis.Push.Configuration;
    86  using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
    9   using HeuristicLab.Problems.ProgramSynthesis.Push.Generators.CodeGenerator;
    107  using HeuristicLab.Problems.ProgramSynthesis.Push.SolutionCreator;
    118
    129  public static class IndividualExtensions {
    1310
    14     public static PushProgram ToPushProgram(this Individual individual, IReadOnlyPushConfiguration config, IRandom random) {
    15       return individual.IntegerVector().ToPushProgram(config, random);
     11    public static PushProgram ToPushProgram(this Individual individual, IReadOnlyPushConfiguration config) {
     12      return individual.IntegerVector().ToPushProgram(config);
    1613    }
    1714
    18     public static PushProgram ToPushProgram(this IntegerVector vector, IReadOnlyPushConfiguration config, IRandom random) {
     15    public static PushProgram ToPushProgram(this IntegerVector vector, IReadOnlyPushConfiguration config) {
    1916      //var currentIndex = 0;
    2017      //var close = 0;
     
    2421        var index = vector[i];
    2522
    26         if (index == PushSolutionEncoding.End)
    27           break;
     23        if (index == PushSolutionEncoding.End) break;
     24        if (index == PushSolutionEncoding.Noop) continue;
    2825
    29         // skip noops
    30         if (index == PushSolutionEncoding.Noop)
    31           continue;
     26        var name = config.EnabledExpressions[index];
     27        var expression = ExpressionTable.GetExpression(name);
    3228
    33         var expression = CodeGeneratorUtils.MapToExpression(
    34           index,
    35           random,
    36           config.ErcOptions,
    37           config);
     29        //var expression = CodeGeneratorUtils.MapToExpression(
     30        //  index,
     31        //  random,
     32        //  config.ErcOptions,
     33        //  config);
    3834
    3935        expressions.Add(expression);
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter/Extensions.cs

    r15273 r15289  
    55  using System;
    66  using System.Collections;
     7  using System.Globalization;
    78  using System.Reflection;
    89
     
    2627    }
    2728
    28     public static Type GetStackEntryType(this StackTypes stackType) {
    29       if (StackProperties.ContainsKey(stackType) &&
    30           StackProperties[stackType].PropertyType.IsGenericType) {
    31         var genericTypeDef = StackProperties[stackType].PropertyType.GetGenericTypeDefinition();
    32         if (genericTypeDef == typeof(IPushStack<>))
    33           return genericTypeDef.GetGenericArguments()[0];
     29    public static string StringifyResult(this IPushInterpreter interpreter, ExampleArgumentType type, int offset) {
     30      var emptyString = string.Empty;
     31
     32      switch (type) {
     33        case ExampleArgumentType.Integer:
     34          return GetEntryAsString(offset, interpreter.IntegerStack);
     35
     36        case ExampleArgumentType.IntegerVector:
     37          return GetVectorEntryAsString(offset, interpreter.IntegerVectorStack);
     38
     39        case ExampleArgumentType.Float:
     40          return interpreter.FloatStack.Count > offset
     41            ? interpreter.FloatStack[offset].ToString(CultureInfo.CurrentUICulture)
     42            : emptyString;
     43
     44        case ExampleArgumentType.FloatVector:
     45          return GetVectorEntryAsString(offset, interpreter.FloatVectorStack);
     46
     47        case ExampleArgumentType.Boolean:
     48          return GetEntryAsString(offset, interpreter.BooleanStack);
     49
     50        case ExampleArgumentType.Char:
     51          return GetEntryAsString(offset, interpreter.CharStack);
     52
     53        case ExampleArgumentType.Print:
     54          return interpreter.PrintStack.ToString();
     55
     56        case ExampleArgumentType.String:
     57          return GetEntryAsString(offset, interpreter.StringStack);
     58
     59        case ExampleArgumentType.StringVector:
     60          return GetVectorEntryAsString(offset, interpreter.StringVectorStack);
     61
     62        default: return string.Empty;
    3463      }
     64    }
    3565
    36       return StackProperties.ContainsKey(stackType)
    37         ? StackProperties[stackType]
    38             .PropertyType
    39             .GetInterfaces()
    40             .First(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IPushStack<>))
    41             .GetGenericArguments()
    42             .First()
    43         : null;
     66    private static string GetEntryAsString<T>(int offset, IPushStack<T> stack) {
     67      return stack.Count > offset
     68       ? stack[offset].ToString()
     69       : string.Empty;
     70    }
     71
     72    private static string GetVectorEntryAsString<T>(int offset, IPushStack<IReadOnlyList<T>> vectorStack) {
     73      return vectorStack.Count > offset
     74       ? "[" + string.Join(",", vectorStack[offset]) + "]"
     75       : string.Empty;
    4476    }
    4577
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Manipulator/UniformMutation.cs

    r15275 r15289  
    11namespace HeuristicLab.Problems.ProgramSynthesis.Push.Manipulator {
     2
    23  using HeuristicLab.Common;
    34  using HeuristicLab.Core;
     
    1112  using HeuristicLab.Problems.ProgramSynthesis.Push.Encoding;
    1213  using HeuristicLab.Problems.ProgramSynthesis.Push.Generators.CodeGenerator;
    13   using HeuristicLab.Random;
    1414
    1515  /// <summary>
     
    2323      Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator which should be used for stochastic manipulation operators."));
    2424      Parameters.Add(new LookupParameter<PlushVector>("PlushVector", "The vector which should be manipulated."));
    25       Parameters.Add(new ValueLookupParameter<IntValue>("MinLength", "The min length of the vector."));
    26       Parameters.Add(new ValueLookupParameter<IntValue>("MaxLength", "The max length of the vector."));
    2725      Parameters.Add(new ValueLookupParameter<IReadOnlyExpressionsConfiguration>("Instructions", "The enabled instructions"));
    2826      Parameters.Add(new ValueLookupParameter<IReadOnlyErcOptions>("ErcOptions", "ERC options"));
    2927      Parameters.Add(new ValueLookupParameter<PercentValue>("InInstructionProbability", "The probability of IN Instructions"));
    3028
    31       Parameters.Add(new FixedValueParameter<PercentValue>("InstructionMutationProbability", "The probability a value gets mutated", new PercentValue(0.02)));
    32       Parameters.Add(new FixedValueParameter<DoubleValue>("CloseMutationDeviation", "When mutating individual, the amount of which the close marker is incremented or decrement is based on a random sample from a normal distribution with mean 0 and standard deviation set by the close mutation deviation parameter", new DoubleValue(1.0)));
     29      Parameters.Add(new FixedValueParameter<PercentValue>("InstructionMutationProbability", "The probability an instruction gets mutated", new PercentValue(0.02)));
     30      Parameters.Add(new FixedValueParameter<PercentValue>("CloseMutationProbability", "The probability close gets mutated", new PercentValue(0.02)));
     31      Parameters.Add(new FixedValueParameter<PercentValue>("CloseIncrementRate", "When mutating individual, the increment rate specifies if close should be increased or decreased by 1.", new PercentValue(0.5)));
    3332    }
    3433
     
    5049    }
    5150
    52     public IValueParameter<PercentValue> CloseMutationDeviationParameter
     51    public IValueParameter<PercentValue> CloseMutationProbabilityParameter
    5352    {
    54       get { return (IValueParameter<PercentValue>)Parameters["CloseMutationDeviation"]; }
     53      get { return (IValueParameter<PercentValue>)Parameters["CloseMutationProbability"]; }
    5554    }
    5655
    57     public double CloseMutationDeviation
     56    public double CloseMutationProbability
    5857    {
    59       get { return CloseMutationDeviationParameter.Value.Value; }
    60       set { CloseMutationDeviationParameter.Value.Value = value; }
     58      get { return CloseMutationProbabilityParameter.Value.Value; }
     59      set { CloseMutationProbabilityParameter.Value.Value = value; }
     60    }
     61
     62    public IValueParameter<PercentValue> CloseIncrementRateParameter
     63    {
     64      get { return (IValueParameter<PercentValue>)Parameters["CloseIncrementRate"]; }
     65    }
     66
     67    public double CloseIncrementRate
     68    {
     69      get { return CloseIncrementRateParameter.Value.Value; }
     70      set { CloseIncrementRateParameter.Value.Value = value; }
    6171    }
    6272
     
    7282    {
    7383      get { return (ILookupParameter<PlushVector>)Parameters["PlushVector"]; }
    74     }
    75     public IValueLookupParameter<IntValue> MinLengthParameter
    76     {
    77       get { return (IValueLookupParameter<IntValue>)Parameters["MinLength"]; }
    78     }
    79     public IValueLookupParameter<IntValue> MaxLengthParameter
    80     {
    81       get { return (IValueLookupParameter<IntValue>)Parameters["MaxLength"]; }
    8284    }
    8385    public IValueLookupParameter<IReadOnlyExpressionsConfiguration> InstructionsParameter
     
    99101
    100102    public sealed override IOperation InstrumentedApply() {
    101       Manipulate(RandomParameter.ActualValue, PlushVectorParameter.ActualValue);
     103      Manipulate(
     104        RandomParameter.ActualValue,
     105        PlushVectorParameter.ActualValue,
     106        CloseIncrementRate,
     107        ErcOptionsParameter.ActualValue,
     108        InstructionsParameter.ActualValue,
     109        InInstructionProbabilityParameter.ActualValue.Value,
     110        CloseMutationProbability,
     111        InstructionMutationProbability);
    102112      return base.InstrumentedApply();
    103113    }
    104114
    105     private void Manipulate(IRandom random, PlushVector plushVector) {
    106       var normalDistributedRandom = new NormalDistributedRandom(random, 0, CloseMutationDeviation);
    107       var ercOptions = ErcOptionsParameter.ActualValue;
    108       var instructions = InstructionsParameter.ActualValue;
    109       var inInstructionProbability = InInstructionProbabilityParameter.ActualValue.Value;
    110       var instructionMutationProbability = InstructionMutationProbability;
     115    private static void Manipulate(
     116      IRandom random,
     117      PlushVector plushVector,
     118      double closeIncrementRate,
     119      IReadOnlyErcOptions ercOptions,
     120      IReadOnlyExpressionsConfiguration instructions,
     121      double inInstructionProbability,
     122      double closeMutationProbability,
     123      double instructionMutationProbability
     124      ) {
    111125
    112126      for (var i = 0; i < plushVector.Entries.Count; i++) {
     
    123137        }
    124138
    125         entry.Close += normalDistributedRandom.Next();
     139        x = random.NextDouble();
     140
     141        if (x < closeMutationProbability) {
     142          x = random.NextDouble();
     143          entry.Close += x < closeIncrementRate ? 1 : -1;
     144
     145          if (entry.Close < 0)
     146            entry.Close = 0;
     147        }
    126148      }
    127149
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/ObjectPools/Random/SeededRandomPool.cs

    r15273 r15289  
    1212    public SeededRandomPool() : this(new Random().Next()) { }
    1313
    14     public SeededRandomPool(int seed) : this(seed, () => new MersenneTwister()) { }
     14    public SeededRandomPool(int seed) : this(seed, () => new FastRandom()) { }
    1515
    1616    public SeededRandomPool(int seed, Func<IRandom> randomCreator) {
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Problem/BenchmarkSuite/IntegerVectorPushBenchmarkSuiteProblem.cs

    r15275 r15289  
    77  using HeuristicLab.BenchmarkSuite;
    88  using HeuristicLab.BenchmarkSuite.Problems;
     9  using HeuristicLab.Problems.ProgramSynthesis.Push.Evaluator;
    910  using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
     11  using HeuristicLab.Problems.ProgramSynthesis.Push.Solution;
     12  using HeuristicLab.Problems.ProgramSynthesis.Push.Solution.BenchmarkSuite;
    1013
    1114  using Instances;
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Problem/BenchmarkSuite/PlushPushBenchmarkSuiteProblem.cs

    r15275 r15289  
    77  using HeuristicLab.Problems.Instances;
    88  using HeuristicLab.Problems.ProgramSynthesis.Push.Configuration;
     9  using HeuristicLab.Problems.ProgramSynthesis.Push.Evaluator;
    910  using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
     11  using HeuristicLab.Problems.ProgramSynthesis.Push.Solution;
     12  using HeuristicLab.Problems.ProgramSynthesis.Push.Solution.BenchmarkSuite;
    1013  using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
    1114
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Problem/IntegerVectorPushProblem.cs

    r15275 r15289  
    44  using Common;
    55  using Configuration;
    6   using Core;
    7   using HeuristicLab.Data;
    86  using HeuristicLab.Encodings.IntegerVectorEncoding;
    97  using HeuristicLab.Problems.ProgramSynthesis.Push.Analyzer;
     8  using HeuristicLab.Problems.ProgramSynthesis.Push.Evaluator;
    109  using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
    1110  using HeuristicLab.Problems.ProgramSynthesis.Push.Individual;
    1211  using HeuristicLab.Problems.ProgramSynthesis.Push.SolutionCreator;
     12
    1313  using Optimization;
    1414  using Persistence.Default.CompositeSerializers.Storable;
     
    5454    //  IndividualMapper.Clear();
    5555    //  RandomPool.Clear();
    56     //  Config.Seed = 0;
    5756    //}
    5857
     
    8180    }
    8281
    83     protected override PushProgram MapIndividual(Individual individual, IRandom random) {
    84       var program = individual.ToPushProgram(Config, random);
     82    protected override PushProgram MapIndividual(Individual individual) {
     83      var program = individual.ToPushProgram(Config);
    8584
    8685      return program;
    8786    }
    8887
    89     public override double Evaluate(Individual individual, IRandom random) {
    90       // init seed of random pool
    91       //Interlocked.CompareExchange(ref RandomPool.Seed, random.Next(), 0);
    92       //Config.Seed = RandomPool.Seed;
     88    //public override double Evaluate(Individual individual, IRandom random) {
     89    //  var seed = random.Next();
    9390
    94       //var rand = RandomPool.ResetAndAllocate();
    95       var program = MapIndividual(individual, random);
    96       //RandomPool.Free(rand);
     91    //  var program = individual.ToPushProgram(Config);
    9792
    98       //rand = RandomPool.ResetAndAllocate();
    99       var result = PushEvaluator.EvaluateTraining(Pool, program, random);
    100       //RandomPool.Free(rand);
     93    //  var rand = new FastRandom(seed);
     94    //  var result = PushEvaluator.EvaluateTraining(Pool, program, rand);
    10195
    102       individual[CaseQualitiesScopeParameterName] = new DoubleArray(result.ExampleQualities);
     96    //  individual[CaseQualitiesScopeParameterName] = new DoubleArray(result.ExampleQualities);
     97    //  individual[SeedScopeParameterName] = new IntValue(seed);
    10398
    104       return result.AvgQuality;
    105     }
     99    //  return result.AvgQuality;
     100    //}
    106101  }
    107102}
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Problem/PlushPushProblem.cs

    r15275 r15289  
    99  using HeuristicLab.Problems.ProgramSynthesis.Push.Analyzer;
    1010  using HeuristicLab.Problems.ProgramSynthesis.Push.Encoding;
     11  using HeuristicLab.Problems.ProgramSynthesis.Push.Evaluator;
    1112  using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
    1213
     
    4344    }
    4445
    45     protected override PushProgram MapIndividual(Individual individual, IRandom random) {
     46    protected override PushProgram MapIndividual(Individual individual) {
    4647      var plushVector = individual.PlushVector();
    4748      var program = plushVector.PushProgram;
     
    4950      return program;
    5051    }
    51 
    52     public override double Evaluate(Individual individual, IRandom random) {
    53       var program = MapIndividual(individual, random);
    54       var result = PushEvaluator.EvaluateTraining(Pool, program, random);
    55 
    56       individual[CaseQualitiesScopeParameterName] = new DoubleArray(result.ExampleQualities);
    57 
    58       return result.AvgQuality;
    59     }
    6052  }
    6153}
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Problem/PushProblemBase.cs

    r15275 r15289  
    1010  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    1111  using HeuristicLab.Problems.ProgramSynthesis.Push.Configuration;
     12  using HeuristicLab.Problems.ProgramSynthesis.Push.Evaluator;
    1213  using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
    1314  using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter;
    1415  using HeuristicLab.Problems.ProgramSynthesis.Push.ObjectPools.Random;
     16  using HeuristicLab.Problems.ProgramSynthesis.Push.Solution;
     17  using HeuristicLab.Random;
    1518
    1619  [StorableClass]
     
    3134    public const string CasesScopeParameterName = "CaseQualities";
    3235    public const string CaseQualitiesScopeParameterName = "CaseQualities";
     36    public const string SeedScopeParameterName = "Seed";
    3337
    3438    protected PushProblemBase(IPushEvaluator evaluator) {
     
    8892      var bestQuality = Maximization ? qualities.Max() : qualities.Min();
    8993      var bestIdx = Array.IndexOf(qualities, bestQuality);
    90       var program = MapIndividual(individuals[bestIdx], random);
     94      var bestIndividual = individuals[bestIdx];
     95      var seed = (IntValue)bestIndividual[SeedScopeParameterName];
     96      var program = MapIndividual(bestIndividual);
    9197
    92       var isIndividualBetter = AnalyzeBestTrainingSolution(program, bestQuality, results, random);
     98      var rand = new FastRandom(seed.Value);
     99      var isIndividualBetter = AnalyzeBestTrainingSolution(program, bestQuality, results, rand);
    93100
    94101      if (isIndividualBetter) {
    95         AnalyzeBestTestSolution(program, results, random);
     102        rand.Reset(seed.Value);
     103        AnalyzeBestTestSolution(program, results, rand);
    96104      }
    97105    }
    98106
    99107    protected void AnalyzeBestTestSolution(PushProgram program, ResultCollection results, IRandom random) {
    100       var testResult = PushEvaluator.EvaluateTraining(Pool, program, random);
     108      var testResult = PushEvaluator.EvaluateTest(Pool, program, random);
    101109
    102110      if (!results.ContainsKey(TEST_QUALITY_RESULT_NAME)) {
     
    108116
    109117    protected bool AnalyzeBestTrainingSolution(PushProgram program, double bestQuality, ResultCollection results, IRandom random) {
    110       if (!results.ContainsKey(BEST_TRAINING_SOLUTION_RESULT_NAME)) {
    111         var solution = CreatePushSolution(
     118      var solution = CreatePushSolution(
    112119            program,
    113120            bestQuality,
    114             (IRandom)random.Clone(),
     121            random, // is already cloned
    115122            (IReadOnlyPushConfiguration)Config.Clone());
    116123
     124      if (!results.ContainsKey(BEST_TRAINING_SOLUTION_RESULT_NAME)) {
    117125        results.Add(new Result(BEST_TRAINING_SOLUTION_RESULT_NAME, solution));
    118126        return true;
     
    123131      if ((!Maximization && currentBestQuality > bestQuality) ||
    124132           (Maximization && currentBestQuality < bestQuality)) {
    125         results[BEST_TRAINING_SOLUTION_RESULT_NAME].Value = CreatePushSolution(
    126             program,
    127             bestQuality,
    128             random,
    129             Config);
     133        results[BEST_TRAINING_SOLUTION_RESULT_NAME].Value = solution;
    130134        return true;
    131135      }
     
    135139
    136140    public override double Evaluate(Individual individual, IRandom random) {
    137       var program = MapIndividual(individual, random);
    138       var result = PushEvaluator.EvaluateTraining(Pool, program, random);
     141      var program = MapIndividual(individual);
     142      var seed = random.Next();
     143      var rand = new FastRandom(seed);
     144      var result = PushEvaluator.EvaluateTraining(Pool, program, rand);
    139145
    140146      individual[CaseQualitiesScopeParameterName] = new DoubleArray(result.ExampleQualities);
     147      individual[SeedScopeParameterName] = new IntValue(seed);
    141148
    142149      return result.AvgQuality;
    143150    }
    144151
    145     protected abstract PushProgram MapIndividual(Individual individual, IRandom random);
     152    protected abstract PushProgram MapIndividual(Individual individual);
    146153
    147154    protected abstract PushSolution CreatePushSolution(
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Selector/LexicaseSelector.cs

    r15275 r15289  
    6060
    6161    protected override IScope[] Select(List<IScope> population) {
    62       var count = NumberOfSelectedSubScopesParameter.ActualValue.Value;
    63       var copy = CopySelectedParameter.Value.Value;
    64       var maximization = MaximizationParameter.ActualValue.Value;
    65       var random = RandomParameter.ActualValue;
     62      var selected = Apply(
     63        population,
     64        NumberOfSelectedSubScopesParameter.ActualValue.Value,
     65        CopySelectedParameter.Value.Value,
     66        MaximizationParameter.ActualValue.Value,
     67        RandomParameter.ActualValue,
     68        CaseQualitiesParameter.ActualValue);
     69
     70      return selected;
     71    }
     72
     73    public static IScope[] Apply(
     74      List<IScope> population,
     75      int count,
     76      bool copy,
     77      bool maximization,
     78      IRandom random,
     79      ItemArray<DoubleArray> caseQualities) {
    6680      var selected = new IScope[count];
    67       var caseQualities = CaseQualitiesParameter.ActualValue;
    6881      var repeats = (int)Math.Ceiling(count / (double)population.Count);
    6982      var caseCount = caseQualities[0].Length;
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/SolutionCreator/IPlushCreator.cs

    r15275 r15289  
    1212    IValueLookupParameter<IntValue> MinCloseParameter { get; }
    1313    IValueLookupParameter<IntValue> MaxCloseParameter { get; }
     14    IValueLookupParameter<DoubleValue> CloseBiasLevelParameter { get; }
    1415    IValueLookupParameter<PercentValue> InInstructionProbabilityParameter { get; }
    1516    IValueLookupParameter<IReadOnlyErcOptions> ErcOptionsParameter { get; }
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/SolutionCreator/PlushCreator.cs

    r15275 r15289  
    7777      get { return (IValueLookupParameter<IntValue>)Parameters["MaxClose"]; }
    7878    }
    79     public IValueLookupParameter<DoubleValue> CloseBiasLevel
     79    public IValueLookupParameter<DoubleValue> CloseBiasLevelParameter
    8080    {
    8181      get { return (IValueLookupParameter<DoubleValue>)Parameters["CloseBiasLevel"]; }
     
    9999      var minClose = MinCloseParameter.ActualValue.Value;
    100100      var maxClose = MaxCloseParameter.ActualValue.Value;
    101       var biasLevel = CloseBiasLevel.ActualValue.Value;
     101      var biasLevel = CloseBiasLevelParameter.ActualValue.Value;
    102102
    103103      if (minLength > maxLength)
     
    111111
    112112      for (var i = 0; i < length; i++) {
    113         var expression = CodeGeneratorUtils.GetRandomExpression(random, ercOptions, instructions, inInstructionProbability);
     113        var expression = CodeGeneratorUtils.GetRandomExpression(
     114          random,
     115          ercOptions,
     116          instructions,
     117          inInstructionProbability);
     118
    114119        var close = random.NextBiased(minClose, maxClose, biasLevel);
    115120
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/SolutionCreator/PushSolutionCreator.cs

    r15273 r15289  
    11namespace HeuristicLab.Problems.ProgramSynthesis.Push.SolutionCreator {
    2   using System;
    32
    43  using HeuristicLab.Common;
     
    98  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    109  using HeuristicLab.Problems.ProgramSynthesis.Base.Erc;
     10  using HeuristicLab.Problems.ProgramSynthesis.Push.Configuration;
    1111
    1212  /// <summary>
     
    3434      if (!Parameters.ContainsKey(IN_INSTRUCTION_PROBABILITY))
    3535        Parameters.Add(new FixedValueParameter<PercentValue>(IN_INSTRUCTION_PROBABILITY, new PercentValue(0.05)));
     36
     37      Parameters.Add(new ValueLookupParameter<IReadOnlyExpressionsConfiguration>("Instructions", "The enabled instructions"));
    3638    }
    3739
     
    6870    }
    6971
     72    public IValueLookupParameter<IReadOnlyExpressionsConfiguration> InstructionsParameter
     73    {
     74      get { return (IValueLookupParameter<IReadOnlyExpressionsConfiguration>)Parameters["Instructions"]; }
     75    }
     76
    7077    //public IValueParameter<IntValue> MaxLengthParameter
    7178    //{
     
    94101      var result = new IntegerVector(len);
    95102
    96       var variableLength = random.Next(MinLength, len);
    97       var contentLength = Math.Min(variableLength, len);
     103      var contentLength = random.Next(MinLength, len);
    98104      var lowerBound = bounds[0, 0];
    99105      var upperBound = bounds[0, 1];
    100       var inInstructionRelativeProbability = ErcOptions.ErcProbability + InInstructionProbability;
     106      //var inInstructionRelativeProbability = ErcOptions.ErcProbability + InInstructionProbability;
     107      //var instructions = InstructionsParameter.ActualValue;
    101108
    102109      for (var i = 0; i < contentLength; i++) {
    103         var x = random.NextDouble();
     110        //var x = random.NextDouble();
    104111
    105         if (ErcOptions.ErcProbability > 0 &&
    106             x <= ErcOptions.ErcProbability)
    107           result[i] = PushSolutionEncoding.Erc;
    108         else if (InInstructionProbability > 0 &&
    109                  x <= inInstructionRelativeProbability)
    110           result[i] = PushSolutionEncoding.In;
    111         else
    112           result[i] = random.Next(lowerBound, upperBound);
     112        //if (ErcOptions.ErcProbability > 0 && x <= ErcOptions.ErcProbability) {
     113        //  result[i] = PushSolutionEncoding.Erc;
     114        //} else if (InInstructionProbability > 0 && x <= inInstructionRelativeProbability && instructions.InExpressionCount > 0) {
     115        //  var index = random.Next(0, instructions.InExpressionCount);
     116        //  var expression = ExpressionTable.InExpressionTable[index];
     117
     118        //  instructions.EnabledExpressions
     119        //  //result[i] = PushSolutionEncoding.In;
     120        //} else {
     121        result[i] = random.Next(lowerBound, upperBound);
     122        //}
    113123      }
    114124
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Stack/IPrintStack.cs

    r15189 r15289  
    11namespace HeuristicLab.Problems.ProgramSynthesis.Push.Stack {
    2   public interface IPrintStack : IPushStack<string> {
     2  public interface IPrintStack : IPushStack {
    33    void NewLine();
    44    void Push<T>(T item);
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Stack/IPushStack.cs

    r15189 r15289  
    1616    void Push(IReadOnlyList<T> items);
    1717    void Push(IEnumerable<T> items);
     18    void Swap(int count);
     19    void Yank(int index);
     20    void RemoveTop();
     21    void Remove(int count);
     22    void RemoveAt(int index);
     23    void RemoveAt(int index, int count);
    1824    new void Clear();
    1925    IReadOnlyList<T> Peek(int count);
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Stack/IPushStackBase.cs

    r15032 r15289  
    99
    1010    void Clear();
    11     void Swap(int count);
    12     void Yank(int index);
    13     void RemoveTop();
    14     void Remove(int count);
    15     void RemoveAt(int index);
    16     void RemoveAt(int index, int count);
     11
    1712    IEnumerable<string> AsStrings();
    1813    IEnumerable<object> AsObjects();
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Stack/InterpreterStackStringifier.cs

    r15189 r15289  
    4444    }
    4545
    46     private static IEnumerable<string> StringifyLiteralStack<T>(IPushStack<T> stack, string prefix = "", string postfix = "", bool reverse = true) {
     46    private static IEnumerable<string> StringifyLiteralStack(IPushStack stack, string prefix = "", string postfix = "", bool reverse = true) {
    4747      var result = stack.AsStrings();
    4848
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Stack/PrintStack.cs

    r15189 r15289  
    66  using System.Collections.Generic;
    77
     8  using HeuristicLab.Problems.ProgramSynthesis.Push.Constants;
    89  using HeuristicLab.Problems.ProgramSynthesis.Push.Extensions;
    910
     
    1213  /// </summary>
    1314  public class PrintStack : IPrintStack {
    14     private string currentTop;
    15     private readonly IPushStack<string> stack = new PushStack<string>();
     15    private int lineCount = 0;
    1616    private readonly StringBuilder stringBuilder = new StringBuilder();
    1717
    18     private string CurrentTop
    19     {
    20       get
    21       {
    22         return currentTop ?? (currentTop = stringBuilder.ToString());
    23       }
    24     }
    25 
    26     public IEnumerator<string> GetEnumerator() {
    27       return stack.GetEnumerator();
    28     }
    29 
    3018    IEnumerator IEnumerable.GetEnumerator() {
    31       return GetEnumerator();
     19      var lines = AsStrings();
     20      return lines.GetEnumerator();
    3221    }
    3322
     
    3625    }
    3726
    38     void IPushStack<string>.Clear() {
    39       stack.Clear();
     27    void IPushStack.Clear() {
    4028      stringBuilder.Clear();
    41       currentTop = null;
    42     }
    43 
    44     public IReadOnlyList<string> Peek(int count) {
    45       if (count == 1)
    46         return new[] { CurrentTop };
    47 
    48       var peek = stack.Peek(count - 1);
    49       var result = new List<string>(peek) { CurrentTop };
    50 
    51       return result;
    52     }
    53 
    54     public void Insert(int index, string item) {
    55       if (index == 0)
    56         Add(item);
    57 
    58       stack.Insert(index - 1, item);
    59     }
    60 
    61     public string Pop() {
    62       var result = CurrentTop;
    63 
    64       var top = stack.Pop();
    65       SetBuilderToTop(top);
    66 
    67       return result;
    68     }
    69 
    70     public IReadOnlyList<string> Pop(int count) {
    71       if (count == 1)
    72         return new[] { Pop() };
    73 
    74       var popped = stack.Pop(count - 1);
    75       var result = new List<string>(popped) { CurrentTop };
    76 
    77       var newTop = stack.Pop();
    78       SetBuilderToTop(newTop);
    79 
    80       return result;
    81     }
    82 
    83     private void ClearTop() {
    84       stringBuilder.Clear();
    85       currentTop = null;
    86     }
    87 
    88     private void SetBuilderToTop(string top) {
    89       stringBuilder.Clear();
    90       stringBuilder.Append(top);
    91       currentTop = top;
    92     }
    93 
    94     public bool TryPop(out string item) {
    95       if (string.IsNullOrEmpty(CurrentTop)) {
    96         item = null;
    97         return false;
    98       }
    99 
    100       item = Pop();
    101       return true;
    102     }
    103 
    104     public string ElementAt(int index) {
    105       if (index == 0)
    106         return CurrentTop;
    107 
    108       return stack.ElementAt(index - 1);
    109     }
    110 
    111     public string Top
    112     {
    113       get
    114       {
    115         return CurrentTop;
    116       }
    117       set
    118       {
    119         SetBuilderToTop(value);
    120       }
    121     }
    122 
    123     public string TopOrDefault { get { return CurrentTop; } }
    124 
    125     public string Bottom { get { return Count > 1 ? stack.Bottom : CurrentTop; } }
    126 
    127     public string BottomOrDefault { get { return Bottom; } }
    128 
    129     public string this[int key]
    130     {
    131       get
    132       {
    133         return key == 0 ? CurrentTop : stack[key - 1];
    134       }
    135       set
    136       {
    137         if (key == 0)
    138           SetBuilderToTop(value);
    139         else
    140           stack[key - 1] = value;
    141       }
    142     }
    143 
    144     public int Count
    145     {
    146       get
    147       {
    148         return stack.Count + (string.IsNullOrEmpty(CurrentTop) ? 0 : 1);
    149       }
    15029    }
    15130
    15231    public void NewLine() {
    153       if (IsEmpty) return;
    154 
    155       stack.Push(CurrentTop);
    156       ClearTop();
     32      stringBuilder.Append(PushEnvironment.NewLine);
     33      lineCount++;
    15734    }
    15835
    15936    public void Push<T>(T item) {
     37      if (IsEmpty) lineCount = 1;
    16038      stringBuilder.Append(item);
    161       currentTop = null;
    16239    }
    16340
    16441    public void Push(float item) {
     42      if (IsEmpty) lineCount = 1;
    16543      stringBuilder.Concat(item);
    166       currentTop = null;
    16744    }
    16845
    16946    public void Push(double item) {
     47      if (IsEmpty) lineCount = 1;
    17048      stringBuilder.Concat(item);
    171       currentTop = null;
    17249    }
    17350
    17451    public void Push(long item) {
     52      if (IsEmpty) lineCount = 1;
    17553      stringBuilder.Concat(item);
    176       currentTop = null;
    17754    }
    17855
    17956    public void Push(int item) {
     57      if (IsEmpty) lineCount = 1;
    18058      stringBuilder.Concat(item);
    181       currentTop = null;
    18259    }
    18360
    18461    public void Push(char item) {
     62      if (IsEmpty) lineCount = 1;
    18563      stringBuilder.Append(item);
    186       currentTop = null;
    18764    }
    18865
    18966    public void Push(bool item) {
     67      if (IsEmpty) lineCount = 1;
    19068      stringBuilder.Append(item);
    191       currentTop = null;
    19269    }
    19370
    19471
    19572    public void Push(string item) {
     73      if (IsEmpty) lineCount = 1;
    19674      stringBuilder.Append(item);
    197       currentTop = null;
    198     }
    199 
    200     public void Push(string item1, string item2) {
    201       stringBuilder.Append(item1);
    202       stringBuilder.Append(item2);
    203       currentTop = null;
    204     }
    205 
    206     public void Push(string item1, string item2, string item3) {
    207       stringBuilder.Append(item1);
    208       stringBuilder.Append(item2);
    209       stringBuilder.Append(item3);
    210       currentTop = null;
    211     }
    212 
    213     public void Push(string item1, string item2, string item3, string item4) {
    214       stringBuilder.Append(item1);
    215       stringBuilder.Append(item2);
    216       stringBuilder.Append(item3);
    217       stringBuilder.Append(item4);
    218       currentTop = null;
    21975    }
    22076
     
    22278      for (var i = startIndex; i < items.Count; i++)
    22379        stringBuilder.Append(items[i]);
    224 
    225       currentTop = null;
    22680    }
    22781
     
    23387      foreach (var item in items)
    23488        stringBuilder.Append(item);
    235 
    236       currentTop = null;
    237     }
    238 
    239     void IPushStack.Clear() {
    240       stringBuilder.Clear();
    241       currentTop = null;
    242       stack.Clear();
    243     }
    244 
    245     public void Swap(int count) {
    246       stack.Push(CurrentTop);
    247       stack.Swap(count);
    248 
    249       var newTop = stack.Pop();
    250       SetBuilderToTop(newTop);
    251     }
    252 
    253     public void Yank(int index) {
    254       if (index == Count - 1) return;
    255 
    256       stack.Push(CurrentTop);
    257 
    258       var item = stack[index];
    259       stack.RemoveAt(index);
    260       SetBuilderToTop(item);
    261     }
    262 
    263     public void RemoveTop() {
    264       var newTop = stack.Pop();
    265       SetBuilderToTop(newTop);
    266     }
    267 
    268     public void Remove(int count) {
    269       if (count == 1)
    270         RemoveTop();
    271 
    272       stack.Remove(count - 1);
    273       var newTop = stack.Pop();
    274       SetBuilderToTop(newTop);
    275     }
    276 
    277     public void RemoveAt(int index) {
    278       if (index == 0)
    279         ClearTop();
    280       else
    281         stack.RemoveAt(index - 1);
    282     }
    283 
    284     public void RemoveAt(int index, int count) {
    285       if (index == 0)
    286         count--;
    287 
    288       stack.RemoveAt(index - 1, count);
    289 
    290       if (index == 0) {
    291         var newTop = stack.Pop();
    292         SetBuilderToTop(newTop);
    293       }
    29489    }
    29590
    29691    public IEnumerable<string> AsStrings() {
    297       foreach (var item in stack)
    298         yield return item;
    299 
    300       if (!IsEmpty)
    301         yield return CurrentTop;
     92      var lines = stringBuilder.ToString().Split(PushEnvironment.NewLine);
     93      return lines;
    30294    }
    30395
     
    30698    }
    30799
    308     void ICollection<string>.Clear() {
    309       stack.Clear();
    310       stringBuilder.Clear();
    311     }
    312 
    313     public bool Contains(string item) {
    314 
    315       return CurrentTop.Equals(item) || stack.Contains(item);
    316     }
    317 
    318     public void CopyTo(string[] array, int arrayIndex) {
    319       stack.CopyTo(array, arrayIndex);
    320       array[arrayIndex + stack.Count] = CurrentTop;
    321     }
    322 
    323     public bool Remove(string item) {
    324       if (item.Equals(CurrentTop)) {
    325         RemoveTop();
    326         return true;
    327       }
    328 
    329       return stack.Remove(item);
    330     }
    331 
    332     int IPushStack<string>.Count
     100    int IPushStack.Count
    333101    {
    334102      get
    335103      {
    336         return Count;
     104        return lineCount;
    337105      }
    338106    }
    339107
    340     public bool IsEmpty { get { return stack.IsEmpty && string.IsNullOrEmpty(CurrentTop); } }
     108    public bool IsEmpty { get { return stringBuilder.Length == 0; } }
    341109
    342110    public bool IsEnabled { get; set; }
    343111
    344     int ICollection<string>.Count
    345     {
    346       get
    347       {
    348         return Count;
    349       }
     112    public bool IsReadOnly { get { return false; } }
     113
     114    public override string ToString() {
     115      return stringBuilder.ToString();
    350116    }
    351 
    352     public bool IsReadOnly { get { return false; } }
    353117  }
    354118}
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Views/PushDebuggerView.cs

    r15273 r15289  
    99  using Core.Views;
    1010  using Expressions;
     11
     12  using HeuristicLab.Problems.ProgramSynthesis.Push.Solution;
     13
    1114  using Interpreter;
    1215  using MainForm;
    13   using Problem;
    1416  using Stack;
    1517
     
    227229
    228230      // align numbers right
    229       var stackEntryType = type.GetStackEntryType();
    230       if (stackEntryType == typeof(double) ||
    231           stackEntryType == typeof(long)) {
     231      if (type == StackTypes.Integer ||
     232          type == StackTypes.Float) {
    232233        list.DrawMode = DrawMode.OwnerDrawFixed;
    233234        list.DrawItem += (sender, e) => {
Note: See TracChangeset for help on using the changeset viewer.