Changeset 17821


Ignore:
Timestamp:
01/19/21 10:22:50 (11 months ago)
Author:
chaider
Message:

#3076 Refactoring Evaluators and Analyzers

Location:
branches/3076_IA_evaluators_analyzers/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4
Files:
1 added
5 deleted
4 edited

Legend:

Unmodified
Added
Removed
  • branches/3076_IA_evaluators_analyzers/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression-3.4.csproj

    r17795 r17821  
    176176    <Compile Include="SingleObjective\Evaluators\SymbolicRegressionMeanRelativeErrorEvaluator.cs" />
    177177    <Compile Include="SingleObjective\Evaluators\SymbolicRegressionSingleObjectiveConstraintEvaluator.cs" />
    178     <Compile Include="SingleObjective\Evaluators\SymbolicRegressionSingleObjectiveConstraintScalingNmseEvaluator.cs" />
    179     <Compile Include="SingleObjective\Evaluators\SymbolicRegressionSingleObjectiveConstraintConstOptNmseEvaluator.cs" />
    180178    <Compile Include="SingleObjective\SymbolicRegressionSolutionsAnalyzer.cs" />
     179    <Compile Include="SymbolicRegressionSplittingAnalyzer.cs" />
    181180    <Compile Include="SymbolicRegressionConstraintAnalyzer.cs" />
    182     <Compile Include="SymbolicRegressionMetaModelAnalyzer.cs" />
    183     <Compile Include="SymbolicRegressionMultiObjectiveMetaModelAnalyzer.cs" />
    184181    <Compile Include="SymbolicRegressionPhenotypicDiversityAnalyzer.cs" />
    185182    <Compile Include="SymbolicRegressionPruningAnalyzer.cs" />
     
    209206    <Compile Include="SingleObjective\Evaluators\SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.cs" />
    210207    <Compile Include="SymbolicRegressionPruningOperator.cs" />
    211     <Compile Include="SymbolicRegressionSingleObjectiveMetaModelAnalyzer.cs" />
    212208    <Compile Include="SymbolicRegressionSolution.cs" />
    213209    <Compile Include="SymbolicRegressionSolutionImpactValuesCalculator.cs" />
  • branches/3076_IA_evaluators_analyzers/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/SymbolicRegressionMultiObjectiveMultiHardConstraintEvaluator.cs

    r17733 r17821  
    1111  [StorableType("9397A63D-0C6B-4733-BD1A-59AAE9A9F006")]
    1212  public class SymbolicRegressionMultiObjectiveMultiHardConstraintEvaluator : SymbolicRegressionMultiObjectiveEvaluator {
     13    private const string BoundsEstimatorParameterName = "Bounds estimator";
     14    public IValueParameter<IBoundsEstimator> BoundsEstimatorParameter =>
     15      (IValueParameter<IBoundsEstimator>)Parameters[BoundsEstimatorParameterName];
     16
     17    public IBoundsEstimator BoundsEstimator {
     18      get => BoundsEstimatorParameter.Value;
     19      set => BoundsEstimatorParameter.Value = value;
     20    }
     21
    1322    #region Constructors
    1423
     
    4756
    4857      var qualities = Calculate(interpreter, solution, estimationLimits.Lower, estimationLimits.Upper, problemData,
    49                                 rows, applyLinearScaling, DecimalPlaces);
     58                                rows, applyLinearScaling, DecimalPlaces, BoundsEstimator);
    5059      QualitiesParameter.ActualValue = new DoubleArray(qualities);
    5160      return base.InstrumentedApply();
     
    6271                              EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper,
    6372                              problemData, rows,
    64                               ApplyLinearScalingParameter.ActualValue.Value, DecimalPlaces);
     73                              ApplyLinearScalingParameter.ActualValue.Value, DecimalPlaces, BoundsEstimator);
    6574
    6675      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
     
    7584                                     double upperEstimationLimit,
    7685                                     IRegressionProblemData problemData, IEnumerable<int> rows, bool applyLinearScaling,
    77                                      int decimalPlaces) {
     86                                     int decimalPlaces, IBoundsEstimator estimator) {
    7887      OnlineCalculatorError errorState;
    7988      var estimatedValues =
     
    91100
    92101      var constraints    = problemData.IntervalConstraints.Constraints.Where(c => c.Enabled);
    93       var variableRanges = problemData.VariableRanges.GetReadonlyDictionary();
     102      var intervalCollection = problemData.VariableRanges;
    94103
    95104      var objectives = new List<double> {nmse}; //Add r² to resultlist
    96105      var constraintObjectives = constraints.Select(constraint =>
    97                                                       ConstraintExceeded(constraint, new IntervalInterpreter(),
    98                                                                          variableRanges, solution));
     106        IntervalUtil.IntervalConstraintViolation(constraint, estimator, intervalCollection, solution) > 0 ? 0.0 : 1.0);
     107
    99108      objectives.AddRange(constraintObjectives); //Add hardconstraints for each constraint
    100109
     
    102111    }
    103112
    104     private static double ConstraintExceeded(IntervalConstraint constraint, IntervalInterpreter intervalInterpreter,
    105                                              IReadOnlyDictionary<string, Interval> variableRanges,
    106                                              ISymbolicExpressionTree solution) {
    107       return SymbolicRegressionConstraintAnalyzer.ConstraintSatisfied(constraint, new IntervalInterpreter(),
    108                                                                       variableRanges, solution, out double error) ? 0 : 1;
    109     }
    110113
    111114    /*
  • branches/3076_IA_evaluators_analyzers/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionSingleObjectiveProblem.cs

    r17611 r17821  
    9696        changed = true;
    9797      }
     98
     99      if (!Operators.OfType<SymbolicRegressionSplittingAnalyzer>().Any()) {
     100        Operators.Add(new SymbolicRegressionSplittingAnalyzer());
     101        changed = true;
     102      }
    98103      if (changed) {
    99104        ParameterizeOperators();
     
    119124      Operators.Add(new SymbolicExpressionTreePhenotypicSimilarityCalculator());
    120125      Operators.Add(new SymbolicRegressionConstraintAnalyzer());
     126      Operators.Add(new SymbolicRegressionSplittingAnalyzer());
    121127      Operators.Add(new SymbolicRegressionPhenotypicDiversityAnalyzer(Operators.OfType<SymbolicExpressionTreePhenotypicSimilarityCalculator>()) { DiversityResultName = "Phenotypic Diversity" });
    122128      ParameterizeOperators();
  • branches/3076_IA_evaluators_analyzers/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionConstraintAnalyzer.cs

    r17792 r17821  
    1717  [StorableType("4318C6BD-E0A1-45FE-AC30-96E7F73B51FB")]
    1818  public class SymbolicRegressionConstraintAnalyzer : SymbolicDataAnalysisAnalyzer, ISymbolicExpressionTreeAnalyzer {
    19     private const string ConstraintViolationsResultName = "Constraint Violations";
    2019    private const string ProblemDataParameterName = "ProblemData";
    2120    private const string ConstraintViolationParameterName = "ConstraintViolations";
    22     private const string PenalityMultiplierParameterName = "ConstraintsPenalityMultiplier";
    23     private const string PenalityMultiplierResultParameterName = "PenalityMultiplierResultParameter";
    24     private const string StepSizeParameterName = "Step Size";
    25     private const string MaximumStepsParameterName = "Maximum Steps";
    26     private const string StartPenalityParameterName = "Start Penality";
    2721    private const string ConstraintUnsatisfiedSolutionsParameterName = "Constraint Unsatisfied Solutions";
    2822
     
    3428    public IResultParameter<DataTable> ConstraintViolationParameter =>
    3529      (IResultParameter<DataTable>) Parameters[ConstraintViolationParameterName];
    36 
    37     public ILookupParameter<DoubleValue> PenalityMultiplierParameter =>
    38       (ILookupParameter<DoubleValue>) Parameters[PenalityMultiplierParameterName];
    39 
    40     public IValueParameter<IntValue> StepSizeParameter =>
    41       (IValueParameter<IntValue>) Parameters[StepSizeParameterName];
    42 
    43     public IValueParameter<IntValue> MaximumStepsParameter =>
    44       (IValueParameter<IntValue>) Parameters[MaximumStepsParameterName];
    45 
    46     public IResultParameter<DataTable> PenalityMultiplierResultParameter =>
    47       (IResultParameter<DataTable>) Parameters[PenalityMultiplierResultParameterName];
    48 
    49     public IValueParameter<DoubleValue> StartPenalityParameter =>
    50       (IValueParameter<DoubleValue>) Parameters[StartPenalityParameterName];
    5130
    5231    public IResultParameter<DataTable> ConstraintUnsatisfiedSolutionsParameter =>
     
    7554      Parameters.Add(new ResultParameter<DataTable>(ConstraintViolationParameterName,
    7655        "Shows the number of constraint violations!"));
    77       Parameters.Add(new LookupParameter<DoubleValue>(PenalityMultiplierParameterName,
    78         "Lookup parameter for the penality multiplier.", "PenalityMultiplier"));
    79       Parameters.Add(new ResultParameter<DataTable>(PenalityMultiplierResultParameterName,
    80         "Shows the behavior of the penality multiplier."));
    81       Parameters.Add(new ValueParameter<IntValue>(StepSizeParameterName,
    82         "Defines the step size for the increasing penality multiplier.", new IntValue(1)));
    83       Parameters.Add(new ValueParameter<IntValue>(MaximumStepsParameterName,
    84         "Defines maximum steps for the increasing penality multiplier.", new IntValue(1000)));
    85       Parameters.Add(new ValueParameter<DoubleValue>(StartPenalityParameterName,
    86         "The start value for the penality multiplier.", new DoubleValue(0.5)));
    8756      Parameters.Add(new ResultParameter<DataTable>(ConstraintUnsatisfiedSolutionsParameterName,
    8857        "Shows the number of solutions with unsatisfied constraints."));
     
    9362          XAxisTitle = "Generations",
    9463          YAxisTitle = "Constraint Violations"
    95         }
    96       };
    97 
    98       PenalityMultiplierResultParameter.DefaultValue = new DataTable(PenalityMultiplierResultParameterName) {
    99         VisualProperties = {
    100           XAxisTitle = "Generations",
    101           YAxisTitle = "Penality Multiplier"
    10264        }
    10365      };
     
    12486    [StorableHook(HookType.AfterDeserialization)]
    12587    private void AfterDeserialization() {
    126       if (!Parameters.ContainsKey(PenalityMultiplierParameterName))
    127         Parameters.Add(new LookupParameter<DoubleValue>(PenalityMultiplierParameterName,
    128           "Lookup parameter for the penality multiplier.", "PenalityMultiplier"));
     88      if (Parameters.ContainsKey(ConstraintUnsatisfiedSolutionsParameterName)) return;
     89      Parameters.Add(new ResultParameter<DataTable>(ConstraintUnsatisfiedSolutionsParameterName,
     90        "Shows the number of solutions with unsatisfied constraints."));
    12991
    130       if (!Parameters.ContainsKey(PenalityMultiplierResultParameterName)) {
    131         Parameters.Add(new ResultParameter<DataTable>(PenalityMultiplierResultParameterName,
    132           "Shows the behavior of the penality multiplier."));
    133 
    134         PenalityMultiplierResultParameter.DefaultValue = new DataTable(PenalityMultiplierResultParameterName) {
    135           VisualProperties = {
    136             XAxisTitle = "Generations",
    137             YAxisTitle = "Penality Multiplier"
    138           }
    139         };
    140       }
    141 
    142       if (!Parameters.ContainsKey(StepSizeParameterName))
    143         Parameters.Add(new ValueParameter<IntValue>(StepSizeParameterName,
    144           "Defines the step size for the increasing penality multiplier.", new IntValue(1)));
    145 
    146       if (!Parameters.ContainsKey(MaximumStepsParameterName))
    147         Parameters.Add(new ValueParameter<IntValue>(MaximumStepsParameterName,
    148           "Defines maximum steps for the increasing penality multiplier.", new IntValue(1000)));
    149 
    150 
    151       if (!Parameters.ContainsKey(StartPenalityParameterName))
    152         Parameters.Add(new ValueParameter<DoubleValue>(StartPenalityParameterName,
    153           "The start value for the penality multiplier.", new DoubleValue(0.5)));
    154 
    155 
    156       if(!Parameters.ContainsKey(ConstraintUnsatisfiedSolutionsParameterName)) {
    157         Parameters.Add(new ResultParameter<DataTable>(ConstraintUnsatisfiedSolutionsParameterName,
    158           "Shows the number of solutions with unsatisfied constraints."));
    159 
    160         ConstraintUnsatisfiedSolutionsParameter.DefaultValue = new DataTable(ConstraintUnsatisfiedSolutionsParameterName) {
    161             VisualProperties = {
    162             XAxisTitle = "Generations",
    163             YAxisTitle = "Constraint Unsatisfied Solutions"
    164           }
    165         };
    166       }
     92      ConstraintUnsatisfiedSolutionsParameter.DefaultValue = new DataTable(ConstraintUnsatisfiedSolutionsParameterName) {
     93        VisualProperties = {
     94          XAxisTitle = "Generations",
     95          YAxisTitle = "Constraint Unsatisfied Solutions"
     96        }
     97      };
    16798    }
    16899
    169100    public override IOperation Apply() {
    170101      Iterations++;
    171       var rowName = "GeneralizedExponentialDiscreteDoubleValueModifier";
    172       var stepSize = StepSizeParameter?.Value?.Value ?? 1;
    173       var startValue = StartPenalityParameter?.Value?.Value ?? 0.5;
    174       var maximumSteps = MaximumStepsParameter?.Value?.Value ?? 1000;
    175       var penalityMultiplier = new DoubleValue(LinearDiscreteDoubleValueModifier.Apply(0, startValue, 1.0,
    176         (int) (Iterations / stepSize) * stepSize, 0, maximumSteps));
    177       PenalityMultiplierParameter.ActualValue = penalityMultiplier;
    178       //GeneralizedExponentialDiscreteDoubleValueModifier
    179       //QuadraticDiscreteDoubleValueModifier
    180       //LinearDiscreteDoubleValueModifier
    181102
    182103      var problemData = RegressionProblemDataParameter.ActualValue;
     
    194115          newDataTable.Rows.Add(new DataRow(constraint.Expression));
    195116
    196       var interpreter = new IntervalInterpreter();
    197117      foreach (var constraint in constraints) {
    198         var violations = trees.Select(tree =>
    199                                  ConstraintSatisfied(constraint, interpreter, variableRanges, tree, out double error))
    200                               .Count(satisfied => !satisfied);
     118        var violations = trees.Count(tree => IntervalUtil.IntervalConstraintViolation(constraint, BoundsEstimator, intervalCollection, tree) > 0.0);
     119
    201120        newDataTable.Rows[constraint.Expression].Values.Add(violations);
    202121      }
    203 
    204       var penalityDataTable = PenalityMultiplierResultParameter.ActualValue;
    205       if (penalityDataTable.Rows.Count == 0)
    206         penalityDataTable.Rows.Add(new DataRow(rowName));
    207       penalityDataTable.Rows[rowName].Values.Add(penalityMultiplier.Value);
    208122
    209123      var constraintUnsatisfiedSolutionsDataTable = ConstraintUnsatisfiedSolutionsParameter.ActualValue;
     
    213127      constraintUnsatisfiedSolutionsDataTable.Rows[ConstraintUnsatisfiedSolutionsParameterName]
    214128        .Values
    215         .Add(
    216           solutions
    217           .Where(s => IntervalUtil.IntervalConstraintsViolation(constraints, BoundsEstimator, intervalCollection, s).Any(x => x != 0.0))
    218           .Count());
     129        .Add(solutions.Count(s => IntervalUtil.IntervalConstraintsViolation(constraints, BoundsEstimator, intervalCollection, s).Any(x => x != 0.0)));
    219130
    220131      return base.Apply();
    221132    }
    222133
    223     public static bool ConstraintSatisfied(IntervalConstraint constraint,
    224                                            IntervalInterpreter intervalInterpreter,
    225                                            IReadOnlyDictionary<string, Interval> variableRanges,
    226                                            ISymbolicExpressionTree solution,
    227                                            out double error) {
    228       if (constraint.Variable != null && !variableRanges.ContainsKey(constraint.Variable))
    229         throw new
    230           ArgumentException($"The given variable {constraint.Variable} in the constraint does not exists in the model.",
    231             nameof(IntervalConstraintsParser));
    232 
    233       Interval resultInterval;
    234 
    235       //Create new variable ranges for defined regions
    236       var regionRanges = new IntervalCollection();
    237       foreach (var kvp in variableRanges) {
    238         if (kvp.Key != constraint.Target &&
    239             constraint.Regions.GetReadonlyDictionary().TryGetValue(kvp.Key, out Interval val)) {
    240           regionRanges.AddInterval(kvp.Key, val);
    241         } else {
    242           regionRanges.AddInterval(kvp.Key, kvp.Value);
    243         }
    244       }
    245 
    246       // calculate result interval
    247       if (!constraint.IsDerivative) {
    248         resultInterval =
    249           intervalInterpreter.GetSymbolicExpressionTreeInterval(solution,
    250             regionRanges.GetReadonlyDictionary());
    251       } else {
    252         var tree = solution;
    253         for (var i = 0; i < constraint.NumberOfDerivations; ++i) {
    254           if (!IntervalInterpreter.IsCompatible(tree) || !DerivativeCalculator.IsCompatible(tree))
    255             throw new ArgumentException("Cube, Root, Power Symbols are not supported.");
    256           tree = DerivativeCalculator.Derive(tree, constraint.Variable);
    257         }
    258 
    259         resultInterval =
    260           intervalInterpreter.GetSymbolicExpressionTreeInterval(tree,
    261             regionRanges.GetReadonlyDictionary());
    262       }
    263 
    264       error = 0.0;
    265       bool satisfied = true;
    266       if (!constraint.Interval.Contains(resultInterval.LowerBound)) {
    267         satisfied = false;
    268         error += Math.Abs(resultInterval.LowerBound - constraint.Interval.LowerBound);
    269       }
    270 
    271       if (!constraint.Interval.Contains(resultInterval.UpperBound)) {
    272         satisfied = false;
    273         error += Math.Abs(resultInterval.UpperBound - constraint.Interval.UpperBound);
    274       }
    275 
    276       error *= constraint.Weight;
    277 
    278       //var satisfied = constraint.Interval.Contains(resultInterval);
    279       return satisfied;
    280     }
    281 
    282     public static bool ConstraintsSatisfied(IEnumerable<IntervalConstraint> constraints,
    283                                             IReadOnlyDictionary<string, Interval> variableRanges,
    284                                             ISymbolicExpressionTree solution,
    285                                             out double error) {
    286       var intervalInterpreter = new IntervalInterpreter();
    287       error = 0.0;
    288       var satisfied = true;
    289       foreach (var constraint in constraints) {
    290         if (constraint.Variable != null && !variableRanges.ContainsKey(constraint.Variable))
    291           throw new
    292             ArgumentException(
    293               $"The given variable {constraint.Variable} in the constraint does not exists in the model.",
    294               nameof(IntervalConstraintsParser));
    295 
    296         satisfied = ConstraintSatisfied(constraint, intervalInterpreter, variableRanges, solution, out double e) &&
    297                     satisfied;
    298         error += e;
    299         //if (!satisfied) return false;
    300       }
    301 
    302       return satisfied;
    303     }
     134   
    304135  }
    305136}
Note: See TracChangeset for help on using the changeset viewer.