Free cookie consent management tool by TermsFeed Policy Generator

Changeset 2977


Ignore:
Timestamp:
03/09/10 09:40:52 (15 years ago)
Author:
gkronber
Message:

Moved linear scaling functionality out of tree evaluator into a separate operator. #823 (Implement tree evaluator with linear scaling to improve convergence in symbolic regression.)

Location:
trunk/sources
Files:
1 added
2 deleted
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/DefaultStructureIdentificationOperators.cs

    r2722 r2977  
    3939      SequentialProcessor seq = new SequentialProcessor();
    4040      seq.AddSubOperator(new DefaultFunctionLibraryInjector());
    41       seq.AddSubOperator(new ScalingTreeEvaluatorInjector());
     41      seq.AddSubOperator(new HL3TreeEvaluatorInjector());
    4242      op.OperatorGraph.AddOperator(seq);
    4343      op.OperatorGraph.InitialOperator = seq;
     
    4646
    4747    public static IOperator CreateInitialPopulationEvaluator() {
    48       MeanSquaredErrorEvaluator eval = new MeanSquaredErrorEvaluator();
    49       eval.Name = "Evaluator";
    50       eval.GetVariableInfo("MSE").ActualName = "Quality";
     48      CombinedOperator combinedOp = new CombinedOperator();
     49      combinedOp.Name = "Evaluator";
     50      SequentialProcessor seq = new SequentialProcessor();
     51
     52      SimpleEvaluator eval = new SimpleEvaluator();
     53      eval.GetVariableInfo("Values").ActualName = "TrainingValues";
    5154      eval.GetVariableInfo("SamplesStart").ActualName = "ActualTrainingSamplesStart";
    5255      eval.GetVariableInfo("SamplesEnd").ActualName = "ActualTrainingSamplesEnd";
    53       return eval;
     56
     57      LinearScaler scaler = new LinearScaler();
     58      scaler.GetVariableInfo("Values").ActualName = "TrainingValues";
     59
     60      SimpleMSEEvaluator mseEval = new SimpleMSEEvaluator();
     61      mseEval.GetVariableInfo("Values").ActualName = "TrainingValues";
     62      mseEval.GetVariableInfo("MSE").ActualName = "Quality";
     63
     64      seq.AddSubOperator(eval);
     65      seq.AddSubOperator(scaler);
     66      seq.AddSubOperator(mseEval);
     67      combinedOp.OperatorGraph.AddOperator(seq);
     68      combinedOp.OperatorGraph.InitialOperator = seq;
     69      return combinedOp;
    5470    }
    5571
     
    7086      UniformSequentialSubScopesProcessor subScopesProc = new UniformSequentialSubScopesProcessor();
    7187      SequentialProcessor individualProc = new SequentialProcessor();
    72       MeanSquaredErrorEvaluator validationEvaluator = new MeanSquaredErrorEvaluator();
     88      SimpleEvaluator validationEvaluator = new SimpleEvaluator();
    7389      validationEvaluator.Name = "ValidationEvaluator";
    74       validationEvaluator.GetVariableInfo("MSE").ActualName = "ValidationQuality";
     90      validationEvaluator.GetVariableInfo("Values").ActualName = "ValidationValues";
    7591      validationEvaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
    7692      validationEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
    77 
     93      LinearScaler validationScaler = new LinearScaler();
     94      validationScaler.GetVariableInfo("Values").ActualName = "ValidationValues";
     95      SimpleMSEEvaluator mseEval = new SimpleMSEEvaluator();
     96      mseEval.GetVariableInfo("Values").ActualName = "ValidationValues";
     97      mseEval.GetVariableInfo("MSE").ActualName = "ValidationQuality";
    7898      individualProc.AddSubOperator(validationEvaluator);
     99      individualProc.AddSubOperator(validationScaler);
     100      individualProc.AddSubOperator(mseEval);
    79101
    80102      Counter bestValidationSolutionAgeCounter = new Counter();
     
    127149      seqSubScopeProc.AddSubOperator(solutionProc);
    128150
    129       ScalingTreeEvaluatorInjector evaluatorInjector = new ScalingTreeEvaluatorInjector();
    130       evaluatorInjector.AddVariable(new HeuristicLab.Core.Variable("PunishmentFactor", new DoubleData(1000.0)));
    131       evaluatorInjector.GetVariableInfo("TreeEvaluator").ActualName = "ModelAnalysisTreeEvaluator";
    132 
    133151      #region simple evaluators
    134152      SimpleEvaluator trainingEvaluator = new SimpleEvaluator();
     
    137155      trainingEvaluator.GetVariableInfo("SamplesEnd").ActualName = "TrainingSamplesEnd";
    138156      trainingEvaluator.GetVariableInfo("Values").ActualName = "TrainingValues";
    139       trainingEvaluator.GetVariableInfo("TreeEvaluator").ActualName = "ModelAnalysisTreeEvaluator";
    140157      SimpleEvaluator validationEvaluator = new SimpleEvaluator();
    141158      validationEvaluator.Name = "ValidationEvaluator";
     
    143160      validationEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
    144161      validationEvaluator.GetVariableInfo("Values").ActualName = "ValidationValues";
    145       validationEvaluator.GetVariableInfo("TreeEvaluator").ActualName = "ModelAnalysisTreeEvaluator";
    146162      SimpleEvaluator testEvaluator = new SimpleEvaluator();
    147163      testEvaluator.Name = "TestEvaluator";
     
    149165      testEvaluator.GetVariableInfo("SamplesEnd").ActualName = "TestSamplesEnd";
    150166      testEvaluator.GetVariableInfo("Values").ActualName = "TestValues";
    151       testEvaluator.GetVariableInfo("TreeEvaluator").ActualName = "ModelAnalysisTreeEvaluator";
    152       solutionProc.AddSubOperator(evaluatorInjector);
     167
     168      LinearScaler trainingScaler = new LinearScaler();
     169      trainingScaler.GetVariableInfo("Values").ActualName = "TrainingValues";
     170
     171      LinearScaler validationScaler = new LinearScaler();
     172      validationScaler.GetVariableInfo("Values").ActualName = "ValidationValues";
     173
     174      LinearScaler testScaler = new LinearScaler();
     175      testEvaluator.GetVariableInfo("Values").ActualName = "TestValues";
     176
    153177      solutionProc.AddSubOperator(trainingEvaluator);
    154178      solutionProc.AddSubOperator(validationEvaluator);
    155179      solutionProc.AddSubOperator(testEvaluator);
     180      solutionProc.AddSubOperator(trainingScaler);
     181      solutionProc.AddSubOperator(validationScaler);
     182      solutionProc.AddSubOperator(testScaler);
    156183      #endregion
    157184
     
    171198
    172199      NodeBasedVariableImpactCalculator nodeImpactCalculator = new NodeBasedVariableImpactCalculator();
    173       nodeImpactCalculator.GetVariableInfo("TreeEvaluator").ActualName = "ModelAnalysisTreeEvaluator";
    174200      nodeImpactCalculator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
    175201      nodeImpactCalculator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
  • trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/HeuristicLab.GP.StructureIdentification-3.3.csproj

    r2843 r2977  
    8888    <Compile Include="LinearScalingPredictorBuilder.cs" />
    8989    <Compile Include="Evaluators\VariableFrequencyBasedImpactCalculator.cs" />
    90     <Compile Include="ScalingTreeEvaluator.cs" />
    91     <Compile Include="ScalingTreeEvaluatorInjector.cs" />
    9290    <Compile Include="Evaluators\TreeComplexityEvaluator.cs" />
    9391    <Compile Include="FunctionLibraryInjectors\ArithmeticFunctionLibraryInjector.cs" />
  • trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/LinearScalingPredictorBuilder.cs

    r2722 r2977  
    3535      : base() {
    3636      AddVariableInfo(new VariableInfo("FunctionTree", "The function tree", typeof(IGeneticProgrammingModel), VariableKind.In));
    37       AddVariableInfo(new VariableInfo("PunishmentFactor", "The punishment factor limits the estimated values to a certain range", typeof(DoubleData), VariableKind.In));
    38       AddVariableInfo(new VariableInfo("Dataset", "The dataset", typeof(Dataset), VariableKind.In));
    39       AddVariableInfo(new VariableInfo("TrainingSamplesStart", "Start index of training set", typeof(DoubleData), VariableKind.In));
    40       AddVariableInfo(new VariableInfo("TrainingSamplesEnd", "End index of training set", typeof(DoubleData), VariableKind.In));
    41       AddVariableInfo(new VariableInfo("TargetVariable", "Name of the target variable", typeof(StringData), VariableKind.In));
     37      AddVariableInfo(new VariableInfo("Beta", "Beta parameter for linear scaling as calculated by LinearScaler", typeof(DoubleData), VariableKind.In));
     38      AddVariableInfo(new VariableInfo("Alpha", "Alpha parameter for linear scaling as calculated by LinearScaler", typeof(DoubleData), VariableKind.In));
     39      AddVariableInfo(new VariableInfo("UpperEstimationLimit", "Upper limit for estimated value (optional)", typeof(DoubleData), VariableKind.In));
     40      AddVariableInfo(new VariableInfo("LowerEstimationLimit", "Lower limit for estimated value (optional)", typeof(DoubleData), VariableKind.In));
    4241      AddVariableInfo(new VariableInfo("Predictor", "The predictor combines the function tree and the evaluator and can be used to generate estimated values", typeof(IPredictor), VariableKind.New));
    4342    }
     
    4948    public override IOperation Apply(IScope scope) {
    5049      IGeneticProgrammingModel model = GetVariableValue<IGeneticProgrammingModel>("FunctionTree", scope, true);
    51       double punishmentFactor = GetVariableValue<DoubleData>("PunishmentFactor", scope, true).Data;
    52       Dataset dataset = GetVariableValue<Dataset>("Dataset", scope, true);
    53       int start = GetVariableValue<IntData>("TrainingSamplesStart", scope, true).Data;
    54       int end = GetVariableValue<IntData>("TrainingSamplesEnd", scope, true).Data;
    55       string targetVariable = GetVariableValue<StringData>("TargetVariable", scope, true).Data;
    56       IPredictor predictor = CreatePredictor(model, punishmentFactor, dataset, targetVariable, start, end);
     50      //double punishmentFactor = GetVariableValue<DoubleData>("PunishmentFactor", scope, true).Data;
     51      //Dataset dataset = GetVariableValue<Dataset>("Dataset", scope, true);
     52      //int start = GetVariableValue<IntData>("TrainingSamplesStart", scope, true).Data;
     53      //int end = GetVariableValue<IntData>("TrainingSamplesEnd", scope, true).Data;
     54      //string targetVariable = GetVariableValue<StringData>("TargetVariable", scope, true).Data;
     55      double alpha = GetVariableValue<DoubleData>("Alpha", scope, true).Data;
     56      double beta = GetVariableValue<DoubleData>("Beta", scope, true).Data;
     57      DoubleData lowerLimit = GetVariableValue<DoubleData>("LowerEstimationLimit", scope, true, false);
     58      DoubleData upperLimit = GetVariableValue<DoubleData>("UpperEstimationLimit", scope, true, false);
     59      IPredictor predictor;
     60      if (lowerLimit == null || upperLimit == null)
     61        predictor = CreatePredictor(model, beta, alpha, double.NegativeInfinity, double.PositiveInfinity);
     62      else
     63        predictor = CreatePredictor(model, beta, alpha, lowerLimit.Data, upperLimit.Data);
    5764      scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("Predictor"), predictor));
    5865      return null;
    5966    }
    6067
    61     public static IPredictor CreatePredictor(IGeneticProgrammingModel model, double punishmentFactor,
    62   Dataset dataset, string targetVariable, int start, int end) {
    63       return CreatePredictor(model, punishmentFactor, dataset, dataset.GetVariableIndex(targetVariable), start, end);
    64     }
    65 
    66 
    67     public static IPredictor CreatePredictor(IGeneticProgrammingModel model, double punishmentFactor,
    68       Dataset dataset, int targetVariable, int start, int end) {
     68    public static IPredictor CreatePredictor(IGeneticProgrammingModel model, double beta, double alpha, double lowerLimit, double upperLimit) {
    6969
    7070      var evaluator = new HL3TreeEvaluator();
    71       // evaluate for all rows
    72       evaluator.PrepareForEvaluation(dataset, model.FunctionTree);
    73       var result = from row in Enumerable.Range(start, end - start)
    74                    let y = evaluator.Evaluate(row)
    75                    let y_ = dataset.GetValue(row, targetVariable)
    76                    select new { Row = row, Estimation = y, Target = y_ };
    77 
    78       // calculate alpha and beta on the subset of rows with valid values
    79       var filteredResult = result.Where(x => IsValidValue(x.Target) && IsValidValue(x.Estimation));
    80       var target = filteredResult.Select(x => x.Target);
    81       var estimation = filteredResult.Select(x => x.Estimation);
    82       double a, b;
    83       if (filteredResult.Count() > 2) {
    84         double tMean = target.Sum() / target.Count();
    85         double xMean = estimation.Sum() / estimation.Count();
    86         double sumXT = 0;
    87         double sumXX = 0;
    88         foreach (var r in result) {
    89           double x = r.Estimation;
    90           double t = r.Target;
    91           sumXT += (x - xMean) * (t - tMean);
    92           sumXX += (x - xMean) * (x - xMean);
    93         }
    94         b = sumXT / sumXX;
    95         a = tMean - b * xMean;
    96       } else {
    97         b = 1.0;
    98         a = 0.0;
    99       }
    100       double mean = dataset.GetMean(targetVariable, start, end);
    101       double range = dataset.GetRange(targetVariable, start, end);
    102       double minEstimatedValue = mean - punishmentFactor * range;
    103       double maxEstimatedValue = mean + punishmentFactor * range;
    104       evaluator.LowerEvaluationLimit = minEstimatedValue;
    105       evaluator.UpperEvaluationLimit = maxEstimatedValue;
    106       var resultModel = new GeneticProgrammingModel(MakeSum(MakeProduct(model.FunctionTree, b), a));
    107       return new Predictor(evaluator, resultModel, minEstimatedValue, maxEstimatedValue);
     71      evaluator.LowerEvaluationLimit = lowerLimit;
     72      evaluator.UpperEvaluationLimit = upperLimit;
     73      var resultModel = new GeneticProgrammingModel(MakeSum(MakeProduct(model.FunctionTree, beta), alpha));
     74      return new Predictor(evaluator, resultModel, lowerLimit, upperLimit);
    10875    }
    109 
    110     private static bool IsValidValue(double d) {
    111       return !double.IsInfinity(d) && !double.IsNaN(d);
    112     }
    113 
    11476
    11577    private static IFunctionTree MakeSum(IFunctionTree tree, double x) {
     
    13496      return constX;
    13597    }
    136 
    137     private static void CalculateScalingParameters(IEnumerable<double> xs, IEnumerable<double> ys, out double k, out double d) {
    138       if (xs.Count() != ys.Count()) throw new ArgumentException();
    139       double xMean = xs.Sum() / xs.Count();
    140       double yMean = ys.Sum() / ys.Count();
    141 
    142       var yEnumerator = ys.GetEnumerator();
    143       var xEnumerator = xs.GetEnumerator();
    144 
    145       double sumXY = 0.0;
    146       double sumXX = 0.0;
    147       while (xEnumerator.MoveNext() && yEnumerator.MoveNext()) {
    148         sumXY += (xEnumerator.Current - xMean) * (yEnumerator.Current - yMean);
    149         sumXX += (xEnumerator.Current - xMean) * (xEnumerator.Current - xMean);
    150       }
    151 
    152       k = sumXY / sumXX;
    153       d = yMean - k * xMean;
    154     }
    15598  }
    15699}
  • trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/OffspringSelectionGPRegression.cs

    r2566 r2977  
    198198      VariableInjector injector = base.CreateGlobalInjector();
    199199      injector.AddVariable(new HeuristicLab.Core.Variable("PunishmentFactor", new DoubleData()));
     200      // NB: LowerEstimationLimit and UpperEstimationLimit should replace the direct use of PunishmentFactor in the algorithm (gkronber 9 March, 2010)
     201      injector.AddVariable(new HeuristicLab.Core.Variable("LowerEstimationLimit", new DoubleData(double.NegativeInfinity)));
     202      injector.AddVariable(new HeuristicLab.Core.Variable("UpperEstimationLimit", new DoubleData(double.PositiveInfinity)));
    200203      injector.AddVariable(new HeuristicLab.Core.Variable("BestValidationSolutionAge", new IntData()));
    201204      injector.AddVariable(new HeuristicLab.Core.Variable("MaxBestValidationSolutionAge", new IntData()));
  • trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/StandardGPRegression.cs

    r2566 r2977  
    4141    public virtual string TargetVariable {
    4242      get { return ProblemInjector.GetVariableValue<StringData>("TargetVariable", null, false).Data; }
    43       set { ProblemInjector.GetVariableValue<StringData>("TargetVariable", null, false).Data = value; }
     43      set {
     44        ProblemInjector.GetVariableValue<StringData>("TargetVariable", null, false).Data = value;
     45      }
    4446    }
    4547
    4648    public virtual Dataset Dataset {
    4749      get { return ProblemInjector.GetVariableValue<Dataset>("Dataset", null, false); }
    48       set { ProblemInjector.GetVariable("Dataset").Value = value; }
     50      set {
     51        ProblemInjector.GetVariable("Dataset").Value = value;
     52      }
    4953    }
    5054
     
    101105    public virtual double PunishmentFactor {
    102106      get { return GetVariableInjector().GetVariable("PunishmentFactor").GetValue<DoubleData>().Data; }
    103       set { GetVariableInjector().GetVariable("PunishmentFactor").GetValue<DoubleData>().Data = value; }
     107      set {
     108        GetVariableInjector().GetVariable("PunishmentFactor").GetValue<DoubleData>().Data = value;
     109      }
     110    }
     111
     112    public virtual double UpperEstimationLimit {
     113      get { return GetVariableInjector().GetVariable("UpperEstimationLimit").GetValue<DoubleData>().Data; }
     114      set { GetVariableInjector().GetVariable("UpperEstimationLimit").GetValue<DoubleData>().Data = value; }
     115    }
     116    public virtual double LowerEstimationLimit {
     117      get { return GetVariableInjector().GetVariable("LowerEstimationLimit").GetValue<DoubleData>().Data; }
     118      set { GetVariableInjector().GetVariable("LowerEstimationLimit").GetValue<DoubleData>().Data = value; }
    104119    }
    105120
     
    193208      VariableInjector injector = base.CreateGlobalInjector();
    194209      injector.AddVariable(new HeuristicLab.Core.Variable("PunishmentFactor", new DoubleData()));
     210      // NB: LowerEstimationLimit and UpperEstimationLimit should replace the direct use of PunishmentFactor in the algorithm (gkronber 9 March, 2010)
     211      injector.AddVariable(new HeuristicLab.Core.Variable("LowerEstimationLimit", new DoubleData(double.NegativeInfinity)));
     212      injector.AddVariable(new HeuristicLab.Core.Variable("UpperEstimationLimit", new DoubleData(double.PositiveInfinity)));
    195213      injector.AddVariable(new HeuristicLab.Core.Variable("BestValidationSolutionAge", new IntData()));
    196214      injector.AddVariable(new HeuristicLab.Core.Variable("MaxBestValidationSolutionAge", new IntData()));
     
    235253
    236254      IOperator combinedTerminationCriterion = AlgorithmBase.CombineTerminationCriterions(base.CreateTerminationCondition(), bestSolutionAge);
    237      
     255
    238256      terminationCritertion.OperatorGraph.AddOperator(combinedTerminationCriterion);
    239257      terminationCritertion.OperatorGraph.InitialOperator = combinedTerminationCriterion;
  • trunk/sources/HeuristicLab.Modeling/3.2/HeuristicLab.Modeling-3.2.csproj

    r2808 r2977  
    8484    <Compile Include="AnalyzerModel.cs" />
    8585    <Compile Include="BestSolutionStorer.cs" />
     86    <Compile Include="LinearScaler.cs" />
    8687    <Compile Include="SimpleStableCorrelationCoefficientEvaluator.cs" />
    8788    <Compile Include="SimplePearsonCorrelationCoefficientEvaluator.cs" />
  • trunk/sources/HeuristicLab.Modeling/3.2/SimpleEvaluatorBase.cs

    r2357 r2977  
    99namespace HeuristicLab.Modeling {
    1010  public abstract class SimpleEvaluatorBase : OperatorBase {
    11     protected const int ORIGINAL_INDEX = 0;
    12     protected const int ESTIMATION_INDEX = 1;
     11    public const int ORIGINAL_INDEX = 0;
     12    public const int ESTIMATION_INDEX = 1;
    1313
    1414    public virtual string OutputVariableName {
Note: See TracChangeset for help on using the changeset viewer.