Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3076_IA_evaluators_analyzers/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveConstraintConstOptEvaluator.cs @ 17636

Last change on this file since 17636 was 17636, checked in by chaider, 4 years ago

#3076 Changed variable ranges to be readonly

File size: 3.9 KB
Line 
1using System.Collections.Generic;
2using HEAL.Attic;
3using HeuristicLab.Common;
4using HeuristicLab.Core;
5using HeuristicLab.Data;
6using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
7
8namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
9  [Item("Constraint ConstOpt Evaluator", "")]
10  [StorableType("4170FD8B-DDD9-43B7-8BF8-F9C7290D4D1C")]
11  public class SymbolicRegressionSingleObjectiveConstraintConstOptEvaluator : SymbolicRegressionSingleObjectiveEvaluator {
12    [StorableConstructor]
13    protected SymbolicRegressionSingleObjectiveConstraintConstOptEvaluator(StorableConstructorFlag _) : base(_) { }
14
15    protected SymbolicRegressionSingleObjectiveConstraintConstOptEvaluator(
16      SymbolicRegressionSingleObjectiveConstraintConstOptEvaluator original, Cloner cloner)
17      : base(original, cloner) { }
18
19    public override IDeepCloneable Clone(Cloner cloner) {
20      return new SymbolicRegressionSingleObjectiveConstraintConstOptEvaluator(this, cloner);
21    }
22
23    public SymbolicRegressionSingleObjectiveConstraintConstOptEvaluator() { }
24
25    public override bool Maximization => true;
26
27    public override IOperation InstrumentedApply() {
28      var solution = SymbolicExpressionTreeParameter.ActualValue;
29      var rows     = GenerateRowsToEvaluate();
30
31      var quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution,
32                              EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper,
33                              ProblemDataParameter.ActualValue, rows, ApplyLinearScalingParameter.ActualValue.Value);
34      QualityParameter.ActualValue = new DoubleValue(quality);
35      return base.InstrumentedApply();
36    }
37
38    public static double Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
39                                   ISymbolicExpressionTree tree, double lowerEstimationLimit,
40                                   double upperEstimationLimit, IRegressionProblemData problemData,
41                                   IEnumerable<int> rows, bool applyLinearScaling) {
42      var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, rows);
43      var targetValues    = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
44
45      var constraints    = problemData.IntervalConstraints.EnabledConstraints;
46      var variableRanges = problemData.VariableRanges.GetReadonlyDictionary();
47
48      if (!SymbolicRegressionConstraintAnalyzer.ConstraintsSatisfied(constraints, variableRanges, tree)) return 0;
49
50      var r2 = SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(interpreter, tree, problemData,
51                                                                                 problemData.TrainingIndices,
52                                                                                 applyLinearScaling, 10);
53      return r2;
54    }
55
56    public override double Evaluate(IExecutionContext context, ISymbolicExpressionTree tree,
57                                    IRegressionProblemData problemData, IEnumerable<int> rows) {
58      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
59      EstimationLimitsParameter.ExecutionContext                    = context;
60      ApplyLinearScalingParameter.ExecutionContext                  = context;
61
62      var r2 = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree,
63                         EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper,
64                         problemData, rows,
65                         ApplyLinearScalingParameter.ActualValue.Value);
66
67      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
68      EstimationLimitsParameter.ExecutionContext                    = null;
69      ApplyLinearScalingParameter.ExecutionContext                  = null;
70
71      return r2;
72    }
73  }
74}
Note: See TracBrowser for help on using the repository browser.