Changeset 10974


Ignore:
Timestamp:
06/11/14 12:07:15 (5 years ago)
Author:
gkronber
Message:

#2109: minor code cleanup

Location:
branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/ArtificialAnt/GEArtificialAntEvaluator.cs

    r10968 r10974  
    3535
    3636namespace HeuristicLab.Problems.GrammaticalEvolution {
    37   [Item("GEArtificialAntEvaluator", "Evaluates an artificial ant solution, implemented in Grammatical Evolution.")]
     37  [Item("GEArtificialAntEvaluator", "Evaluates an artificial ant solution for grammatical evolution.")]
    3838  [StorableClass]
    3939  public class GEArtificialAntEvaluator : SingleSuccessorOperator,
  • branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/ArtificialAnt/GEArtificialAntProblem.cs

    r10968 r10974  
    128128    #endregion
    129129
    130     // BackwardsCompatibility3.3
    131     #region Backwards compatible code, remove with 3.4
    132     [Obsolete]
    133     [Storable(Name = "operators")]
    134     private IEnumerable<IOperator> oldOperators {
    135       get { return null; }
    136       set {
    137         if (value != null && value.Any())
    138           Operators.AddRange(value);
    139       }
    140     }
    141     #endregion
    142 
    143130    [StorableConstructor]
    144131    private GEArtificialAntProblem(bool deserializing) : base(deserializing) { }
  • branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/Mappers/GenotypeToPhenotypeMapper.cs

    r10968 r10974  
    3434namespace HeuristicLab.Problems.GrammaticalEvolution {
    3535  /// <summary>
    36   /// GenotypeToPhenotypeMapper
     36  /// Abstract base class for GenotypeToPhenotypeMappers
    3737  /// </summary>
    3838  public abstract class GenotypeToPhenotypeMapper : IntegerVectorOperator, IGenotypeToPhenotypeMapper {
     
    6666
    6767      // no terminal node exists for the given parent node
    68       if (possibleSymbolsList.Count() < 1) return null;
     68      if (!possibleSymbolsList.Any()) return null;
    6969
    7070      var newNode = possibleSymbolsList.SelectRandom(random).CreateTreeNode();
  • branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/Mappers/PIGEMapper.cs

    r10968 r10974  
    7171  public class PIGEMapper : GenotypeToPhenotypeMapper {
    7272
     73    private object nontVectorLocker = new object();
    7374    private IntegerVector nontVector;
    7475
     
    7879    }
    7980
    80     private IntegerVector GetNontVector(IRandom random, IntMatrix bounds, int length) {
     81    private static IntegerVector GetNontVector(IRandom random, IntMatrix bounds, int length) {
    8182      IntegerVector v = new IntegerVector(length);
    8283      v.Randomize(random, bounds);
     
    114115      tree.Root = rootNode;
    115116
    116       if (NontVector == null) {
    117         NontVector = GetNontVector(random, bounds, length);
     117      // Map can be called simultaniously on multiple threads
     118      lock (nontVectorLocker) {
     119        if (NontVector == null) {
     120          NontVector = GetNontVector(random, bounds, length);
     121        }
    118122      }
    119123
  • branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/Symbolic/GESymbolicDataAnalysisEvaluator.cs

    r10968 r10974  
    129129    [StorableHook(HookType.AfterDeserialization)]
    130130    private void AfterDeserialization() {
    131       if (Parameters.ContainsKey(ApplyLinearScalingParameterName) && !(Parameters[ApplyLinearScalingParameterName] is LookupParameter<BoolValue>))
    132         Parameters.Remove(ApplyLinearScalingParameterName);
    133       if (!Parameters.ContainsKey(ApplyLinearScalingParameterName))
    134         Parameters.Add(new LookupParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the individual should be linearly scaled before evaluating."));
    135       if (!Parameters.ContainsKey(ValidRowIndicatorParameterName))
    136         Parameters.Add(new ValueLookupParameter<StringValue>(ValidRowIndicatorParameterName, "An indicator variable in the data set that specifies which rows should be evaluated (those for which the indicator <> 0) (optional)."));
    137     }
    138 
    139     protected IEnumerable<int> GenerateRowsToEvaluate() {
    140       return GenerateRowsToEvaluate(RelativeNumberOfEvaluatedSamplesParameter.ActualValue.Value);
    141     }
    142 
    143     protected IEnumerable<int> GenerateRowsToEvaluate(double percentageOfRows) {
    144       IEnumerable<int> rows;
    145       int samplesStart = EvaluationPartitionParameter.ActualValue.Start;
    146       int samplesEnd = EvaluationPartitionParameter.ActualValue.End;
    147       int testPartitionStart = ProblemDataParameter.ActualValue.TestPartition.Start;
    148       int testPartitionEnd = ProblemDataParameter.ActualValue.TestPartition.End;
    149       if (samplesEnd < samplesStart) throw new ArgumentException("Start value is larger than end value.");
    150 
    151       if (percentageOfRows.IsAlmost(1.0))
    152         rows = Enumerable.Range(samplesStart, samplesEnd - samplesStart);
    153       else {
    154         int seed = RandomParameter.ActualValue.Next();
    155         int count = (int)((samplesEnd - samplesStart) * percentageOfRows);
    156         if (count == 0) count = 1;
    157         rows = RandomEnumerable.SampleRandomNumbers(seed, samplesStart, samplesEnd, count);
    158       }
    159 
    160       rows = rows.Where(i => i < testPartitionStart || testPartitionEnd <= i);
    161       if (ValidRowIndicatorParameter.ActualValue != null) {
    162         string indicatorVar = ValidRowIndicatorParameter.ActualValue.Value;
    163         var problemData = ProblemDataParameter.ActualValue;
    164         var indicatorRow = problemData.Dataset.GetReadOnlyDoubleValues(indicatorVar);
    165         rows = rows.Where(r => !indicatorRow[r].IsAlmost(0.0));
    166       }
    167       return rows;
    168     }
    169 
    170     [ThreadStatic]
    171     private static double[] cache;
    172     protected static void CalculateWithScaling(IEnumerable<double> targetValues, IEnumerable<double> estimatedValues,
    173       double lowerEstimationLimit, double upperEstimationLimit,
    174       IOnlineCalculator calculator, int maxRows) {
    175       if (cache == null || cache.Length < maxRows) {
    176         cache = new double[maxRows];
    177       }
    178 
    179       // calculate linear scaling
    180       int i = 0;
    181       var linearScalingCalculator = new OnlineLinearScalingParameterCalculator();
    182       var targetValuesEnumerator = targetValues.GetEnumerator();
    183       var estimatedValuesEnumerator = estimatedValues.GetEnumerator();
    184       while (targetValuesEnumerator.MoveNext() & estimatedValuesEnumerator.MoveNext()) {
    185         double target = targetValuesEnumerator.Current;
    186         double estimated = estimatedValuesEnumerator.Current;
    187         cache[i] = estimated;
    188         if (!double.IsNaN(estimated) && !double.IsInfinity(estimated))
    189           linearScalingCalculator.Add(estimated, target);
    190         i++;
    191       }
    192       if (linearScalingCalculator.ErrorState == OnlineCalculatorError.None && (targetValuesEnumerator.MoveNext() || estimatedValuesEnumerator.MoveNext()))
    193         throw new ArgumentException("Number of elements in target and estimated values enumeration do not match.");
    194 
    195       double alpha = linearScalingCalculator.Alpha;
    196       double beta = linearScalingCalculator.Beta;
    197       if (linearScalingCalculator.ErrorState != OnlineCalculatorError.None) {
    198         alpha = 0.0;
    199         beta = 1.0;
    200       }
    201 
    202       //calculate the quality by using the passed online calculator
    203       targetValuesEnumerator = targetValues.GetEnumerator();
    204       var scaledBoundedEstimatedValuesEnumerator = Enumerable.Range(0, i).Select(x => cache[x] * beta + alpha)
    205         .LimitToRange(lowerEstimationLimit, upperEstimationLimit).GetEnumerator();
    206 
    207       while (targetValuesEnumerator.MoveNext() & scaledBoundedEstimatedValuesEnumerator.MoveNext()) {
    208         calculator.Add(targetValuesEnumerator.Current, scaledBoundedEstimatedValuesEnumerator.Current);
    209       }
    210131    }
    211132  }
  • branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/Symbolic/GESymbolicDataAnalysisProblem.cs

    r10968 r10974  
    6161    private const string BoundsParameterName = "Bounds";
    6262    private const string GenotypeToPhenotypeMapperParameterName = "GenotypeToPhenotypeMapper";
    63 
    6463    private const string ProblemDataParameterDescription = "";
    6564    private const string SymbolicExpressionTreeGrammarParameterDescription = "The grammar that should be used for symbolic expression tree.";
    66     private const string SymoblicExpressionTreeInterpreterParameterDescription = "The interpreter that should be used to evaluate the symbolic expression tree.";
    67     private const string MaximumSymbolicExpressionTreeDepthParameterDescription = "Maximal depth of the symbolic expression. The minimum depth needed for the algorithm is 3 because two levels are reserved for the ProgramRoot and the Start symbol.";
     65    private const string SymbolicExpressionTreeInterpreterParameterDescription = "The interpreter that should be used to evaluate the symbolic expression tree.";
    6866    private const string MaximumSymbolicExpressionTreeLengthParameterDescription = "Maximal length of the symbolic expression.";
    69     private const string MaximumFunctionDefinitionsParameterDescription = "Maximal number of automatically defined functions";
    70     private const string MaximumFunctionArgumentsParameterDescription = "Maximal number of arguments of automatically defined functions.";
    7167    private const string RelativeNumberOfEvaluatedSamplesParameterDescription = "The relative number of samples of the dataset partition, which should be randomly chosen for evaluation.";
    7268    private const string FitnessCalculationPartitionParameterDescription = "The partition of the problem data training partition, that should be used to calculate the fitness of an individual.";
     
    8480      get { return (IValueParameter<T>)Parameters[ProblemDataParameterName]; }
    8581    }
    86     public IValueParameter<GESymbolicExpressionGrammar> SymbolicExpressionTreeGrammarParameter {
    87       get { return (IValueParameter<GESymbolicExpressionGrammar>)Parameters[SymbolicExpressionTreeGrammarParameterName]; }
     82    public IValueParameter<ISymbolicDataAnalysisGrammar> SymbolicExpressionTreeGrammarParameter {
     83      get { return (IValueParameter<ISymbolicDataAnalysisGrammar>)Parameters[SymbolicExpressionTreeGrammarParameterName]; }
    8884    }
    8985    public IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicExpressionTreeInterpreterParameter {
     
    125121    }
    126122
    127     public GESymbolicExpressionGrammar SymbolicExpressionTreeGrammar {
     123    public ISymbolicDataAnalysisGrammar SymbolicExpressionTreeGrammar {
    128124      get { return SymbolicExpressionTreeGrammarParameter.Value; }
    129125      set { SymbolicExpressionTreeGrammarParameter.Value = value; }
     
    167163      : base(evaluator, solutionCreator) {
    168164      Parameters.Add(new ValueParameter<T>(ProblemDataParameterName, ProblemDataParameterDescription, problemData));
    169       Parameters.Add(new ValueParameter<GESymbolicExpressionGrammar>(SymbolicExpressionTreeGrammarParameterName, SymbolicExpressionTreeGrammarParameterDescription));
    170       Parameters.Add(new ValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicExpressionTreeInterpreterParameterName, SymoblicExpressionTreeInterpreterParameterDescription));
     165      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisGrammar>(SymbolicExpressionTreeGrammarParameterName, SymbolicExpressionTreeGrammarParameterDescription));
     166      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicExpressionTreeInterpreterParameterName, SymbolicExpressionTreeInterpreterParameterDescription));
    171167      Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, MaximumSymbolicExpressionTreeLengthParameterDescription));
    172168      Parameters.Add(new FixedValueParameter<IntRange>(FitnessCalculationPartitionParameterName, FitnessCalculationPartitionParameterDescription));
     
    200196    }
    201197
    202     protected virtual void UpdateGrammar() {
     198    private void UpdateGrammar() {
    203199      DeregisterGrammarHandler();
    204200      // create a new grammar instance with the correct allowed input variables
     
    209205
    210206    private void InitializeOperators() {
    211       Operators.AddRange(ApplicationManager.Manager.GetInstances<IIntegerVectorOperator>().OfType<IOperator>());
     207      Operators.AddRange(ApplicationManager.Manager.GetInstances<IIntegerVectorOperator>());
    212208      Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
    213209      Operators.Add(new SymbolicDataAnalysisVariableFrequencyAnalyzer());
  • branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/Symbolic/GESymbolicRegressionSingleObjectiveEvaluator.cs

    r10968 r10974  
    4444      get { return (IValueParameter<ISymbolicRegressionSingleObjectiveEvaluator>)Parameters[EvaluatorParameterName]; }
    4545    }
    46     public ILookupParameter<IRandom> RandomParameter {
    47       get { return (ILookupParameter<IRandom>)Parameters[RandomParameterName]; }
    48     }
    4946    public ILookupParameter<IntMatrix> BoundsParameter {
    5047      get { return (ILookupParameter<IntMatrix>)Parameters[BoundsParameterName]; }
     
    5451    }
    5552
    56     private ISymbolicRegressionSingleObjectiveEvaluator Evaluator {
     53    public ISymbolicRegressionSingleObjectiveEvaluator Evaluator {
    5754      get { return EvaluatorParameter.Value; }
    5855    }
  • branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/Symbolic/GESymbolicRegressionSingleObjectiveProblem.cs

    r10968 r10974  
    3535namespace HeuristicLab.Problems.GrammaticalEvolution {
    3636  [Item("Grammatical Evolution Symbolic Regression Problem (single objective)",
    37         "Represents a single objective symbolic regression problem, implemented in Grammatical Evolution.")]
     37        "Represents grammatical evolution for single objective symbolic regression problems.")]
    3838  [StorableClass]
    3939  [Creatable("Problems")]
     
    7272
    7373      ApplyLinearScalingParameter.Value.Value = true;
    74       Maximization.Value = true;
     74      Maximization.Value = Evaluator.Maximization;
    7575      MaximumSymbolicExpressionTreeLength.Value = InitialMaximumTreeLength;
    7676
     
    8686
    8787    private void RegisterEventHandlers() {
    88       // nothing to do
     88      // when the ge evaluator itself changes
     89      EvaluatorParameter.ValueChanged += (sender, args) => {
     90        // register a new hander for the symbreg evaluator in the ge evaluator
     91        // hacky because we the evaluator does not have an event for changes of the maximization property
     92        EvaluatorParameter.Value.EvaluatorParameter.ValueChanged +=
     93          (_, __) => Maximization.Value = Evaluator.Maximization;
     94      };
     95      EvaluatorParameter.Value.EvaluatorParameter.ValueChanged +=
     96        (sender, args) => Maximization.Value = Evaluator.Maximization;
    8997    }
    9098
  • branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/Symbolic/IGESymbolicDataAnalysisProblem.cs

    r10968 r10974  
    3232  public interface IGESymbolicDataAnalysisProblem : IDataAnalysisProblem, IHeuristicOptimizationProblem {
    3333
    34     IValueParameter<GESymbolicExpressionGrammar> SymbolicExpressionTreeGrammarParameter { get; }
     34    IValueParameter<ISymbolicDataAnalysisGrammar> SymbolicExpressionTreeGrammarParameter { get; }
    3535    IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicExpressionTreeInterpreterParameter { get; }
    3636    IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter { get; }
     
    3939    IFixedValueParameter<IntRange> ValidationPartitionParameter { get; }
    4040
    41     GESymbolicExpressionGrammar SymbolicExpressionTreeGrammar { get; set; }
     41    ISymbolicDataAnalysisGrammar SymbolicExpressionTreeGrammar { get; set; }
    4242    ISymbolicDataAnalysisExpressionTreeInterpreter SymbolicExpressionTreeInterpreter { get; set; }
    4343    IntValue MaximumSymbolicExpressionTreeLength { get; }
     
    4848
    4949  public interface IGESymbolicDataAnalysisSingleObjectiveProblem : IGESymbolicDataAnalysisProblem, ISingleObjectiveHeuristicOptimizationProblem { }
    50   public interface IGESymbolicDataAnalysisMultiObjectiveProblem : IGESymbolicDataAnalysisProblem, IMultiObjectiveHeuristicOptimizationProblem { }
    5150}
  • branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/Symbolic/IGESymbolicRegressionSingleObjectiveEvaluator.cs

    r10968 r10974  
    2323#endregion
    2424
     25using HeuristicLab.Core;
    2526using HeuristicLab.Problems.DataAnalysis;
     27using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
    2628
    2729namespace HeuristicLab.Problems.GrammaticalEvolution {
    2830  public interface IGESymbolicRegressionSingleObjectiveEvaluator : IGESymbolicRegressionEvaluator,
    2931                                                                   IGESymbolicDataAnalysisSingleObjectiveEvaluator<IRegressionProblemData> {
     32    IValueParameter<ISymbolicRegressionSingleObjectiveEvaluator> EvaluatorParameter { get; }
    3033  }
    3134}
Note: See TracChangeset for help on using the changeset viewer.