Free cookie consent management tool by TermsFeed Policy Generator

Changeset 17792


Ignore:
Timestamp:
12/15/20 14:52:22 (4 years ago)
Author:
dpiringe
Message:

#3076

  • updated SymbolicRegressionSingleObjectiveMetaModelAnalyzer to find a meta model with and without constant optimization
  • added a new result parameter of type DataTable in SymbolicRegressionConstraintAnalyzer to show the absolute number of unsatisfied individuals
  • added soft constraints handling for SymbolicRegressionSingleObjectiveConstraintEvaluator
Location:
branches/3076_IA_evaluators_analyzers/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/3076_IA_evaluators_analyzers/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveConstraintEvaluator.cs

    r17769 r17792  
    2525    private const string UseSoftConstraintsParameterName = "Use Soft Constraints Evaluation";
    2626
    27     private const string PenaltyMultiplierParameterName = "Constraints Penalty Multiplier";
     27    //private const string PenaltyMultiplierParameterName = "Constraints Penalty Multiplier";
    2828
    2929    private const string BoundsEstimatorParameterName = "Bounds estimator";
    3030
     31    private const string MaximumPenaltyFactorParamterName = "Maximum Penalty Factor";
     32
    3133
    3234    public IFixedValueParameter<BoolValue> UseConstantOptimizationParameter =>
    33       (IFixedValueParameter<BoolValue>) Parameters[UseConstantOptimizationParameterName];
     35      (IFixedValueParameter<BoolValue>)Parameters[UseConstantOptimizationParameterName];
    3436
    3537    public IFixedValueParameter<IntValue> ConstantOptimizationIterationsParameter =>
    36       (IFixedValueParameter<IntValue>) Parameters[ConstantOptimizationIterationsParameterName];
     38      (IFixedValueParameter<IntValue>)Parameters[ConstantOptimizationIterationsParameterName];
    3739
    3840    public IFixedValueParameter<BoolValue> UseSoftConstraintsParameter =>
    39       (IFixedValueParameter<BoolValue>) Parameters[UseSoftConstraintsParameterName];
    40 
    41     public IFixedValueParameter<DoubleValue> PenaltyMultiplierParameter =>
    42       (IFixedValueParameter<DoubleValue>) Parameters[PenaltyMultiplierParameterName];
     41      (IFixedValueParameter<BoolValue>)Parameters[UseSoftConstraintsParameterName];
     42
     43    //public IFixedValueParameter<DoubleValue> PenaltyMultiplierParameter =>
     44      //(IFixedValueParameter<DoubleValue>)Parameters[PenaltyMultiplierParameterName];
    4345
    4446    public IValueParameter<IBoundsEstimator> BoundsEstimatorParameter =>
    45       (IValueParameter<IBoundsEstimator>) Parameters[BoundsEstimatorParameterName];
     47      (IValueParameter<IBoundsEstimator>)Parameters[BoundsEstimatorParameterName];
     48    public IFixedValueParameter<DoubleValue> MaximumPenaltyFactorParamter =>
     49      (IFixedValueParameter<DoubleValue>)Parameters[MaximumPenaltyFactorParamterName];
    4650
    4751    public bool UseConstantOptimization {
     
    6165
    6266    public double PenaltyMultiplier {
    63       get => PenaltyMultiplierParameter.Value.Value;
    64       set => PenaltyMultiplierParameter.Value.Value = value;
     67      get => 1.0;//PenaltyMultiplierParameter.Value.Value;
     68      //set => PenaltyMultiplierParameter.Value.Value = value;
    6569    }
    6670
     
    6872      get => BoundsEstimatorParameter.Value;
    6973      set => BoundsEstimatorParameter.Value = value;
     74    }
     75
     76    public double MaximumPenaltyFactor {
     77      get => MaximumPenaltyFactorParamter.Value.Value;
     78      set => MaximumPenaltyFactorParamter.Value.Value = value;
    7079    }
    7180
     
    91100      Parameters.Add(new FixedValueParameter<BoolValue>(UseSoftConstraintsParameterName,
    92101        "Define whether the constraints are penalized by soft or hard constraints.", new BoolValue(false)));
    93       Parameters.Add(new FixedValueParameter<DoubleValue>(PenaltyMultiplierParameterName,
    94         "Specify how hard constraints violations should be punished", new DoubleValue(1.0)));
    95102      Parameters.Add(new ValueParameter<IBoundsEstimator>(BoundsEstimatorParameterName,
    96103        "Select the Boundsestimator.", new IABoundsEstimator()));
     104      Parameters.Add(new FixedValueParameter<DoubleValue>(MaximumPenaltyFactorParamterName,
     105        "Specify how hard constraints violations should be punished", new DoubleValue(1.5)));
    97106    }
    98107
     
    114123      }
    115124
    116       if (!Parameters.ContainsKey(PenaltyMultiplierParameterName)) {
    117         Parameters.Add(new FixedValueParameter<DoubleValue>(PenaltyMultiplierParameterName,
    118           "Specify how hard constraints violations should be punished", new DoubleValue(1.0)));
    119       }
     125      //if (!Parameters.ContainsKey(PenaltyMultiplierParameterName)) {
     126      //  Parameters.Add(new FixedValueParameter<DoubleValue>(PenaltyMultiplierParameterName,
     127      //    "Specify how hard constraints violations should be punished", new DoubleValue(1.0)));
     128      //}
    120129
    121130      if (!Parameters.ContainsKey(BoundsEstimatorParameterName))
    122131        Parameters.Add(new ValueParameter<IBoundsEstimator>(BoundsEstimatorParameterName,
    123132          "Select the Boundsestimator.", new IABoundsEstimator()));
     133
     134      if (!Parameters.ContainsKey(MaximumPenaltyFactorParamterName)) {
     135        Parameters.Add(new FixedValueParameter<DoubleValue>(MaximumPenaltyFactorParamterName,
     136          "Specify how hard constraints violations should be punished", new DoubleValue(1.5)));
     137      }
    124138    }
    125139
     
    166180            if (node.Symbol.Name == "Offset") {
    167181              node.RemoveSubtree(1);
    168               var alphaNode = new ConstantTreeNode(new Constant()) {Value = alpha};
     182              var alphaNode = new ConstantTreeNode(new Constant()) { Value = alpha };
    169183              node.AddSubtree(alphaNode);
    170184            } else if (node.Symbol.Name == "Scaling") {
    171185              node.RemoveSubtree(1);
    172               var betaNode = new ConstantTreeNode(new Constant()) {Value = beta};
     186              var betaNode = new ConstantTreeNode(new Constant()) { Value = beta };
    173187              node.AddSubtree(betaNode);
    174188            }
     
    177191
    178192      var quality = Calculate(interpreter, solution, estimationLimits.Lower, estimationLimits.Upper, problemData, rows,
    179         PenaltyMultiplier, BoundsEstimator);
     193        PenaltyMultiplier, BoundsEstimator, UseSoftConstraints, MaximumPenaltyFactor);
    180194      QualityParameter.ActualValue = new DoubleValue(quality);
    181195
     
    188202      double upperEstimationLimit,
    189203      IRegressionProblemData problemData, IEnumerable<int> rows,
    190       double penaltyMultiplier, IBoundsEstimator estimator) {
     204      double penaltyMultiplier, IBoundsEstimator estimator,
     205      bool useSoftConstraints, double maximumPenaltyFactor) {
     206
    191207      var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows);
    192208      var targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
     
    202218      }
    203219
    204       //if (!SymbolicRegressionConstraintAnalyzer.ConstraintsSatisfied(constraints, variableRanges, solution,
    205       //  out var error)) {
    206       //  if (useSoftConstraints) {
    207       //    if (double.IsNaN(error) || double.IsInfinity(error)) {
    208       //      nmse += penaltyMultiplier * 1.0;
    209       //    } else {
    210       //      nmse += penaltyMultiplier * error;
    211       //    }
    212 
    213       //    nmse = Math.Min(1.0, nmse);
    214       //  } else {
    215       //    nmse = 1.0;
    216       //  }
    217       //}
    218 
    219 
    220       if (IntervalUtil.IntervalConstraintsViolation(constraints, estimator, intervalCollection, solution)
    221                       .Any(x => x != 0.0)) {
     220      var constraintResults = IntervalUtil.IntervalConstraintsViolation(constraints, estimator, intervalCollection, solution);
     221
     222      if (constraintResults.Any(x => double.IsNaN(x) || double.IsInfinity(x))) {
     223        return 1.0;
     224      }
     225
     226
     227      if (useSoftConstraints) {
     228        if (maximumPenaltyFactor < 0.0)
     229          throw new ArgumentException("The parameter 'Maximum Penalty Factor' has to be greater or equal 0.0!");
     230
     231        var zip = constraints.Zip(constraintResults, (c, e) => new { Constraint = c, Error = e });
     232        double sum = 0.0;
     233
     234        foreach (var x in zip) {
     235          if (x.Constraint.Weight <= 0)
     236            throw new ArgumentException("Constraint weights <= 0 are not allowed!");
     237
     238          double e = x.Error / x.Constraint.Weight;
     239
     240          e = double.IsNaN(e) ? 0.0 : e;
     241          e = e > 1.0 ? 1.0 : e;
     242
     243          sum += Math.Sqrt(e) * maximumPenaltyFactor;
     244        }
     245
     246        double avgError = sum / zip.Count();
     247        nmse = nmse * avgError + nmse;
     248        nmse = Math.Min(1.0, nmse);
     249      } else if (constraintResults.Any(x => x != 0.0)) {
    222250        nmse = 1.0;
    223251      }
     
    235263      var nmse = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree,
    236264        EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper,
    237         problemData, rows, PenaltyMultiplier, BoundsEstimator);
     265        problemData, rows, PenaltyMultiplier, BoundsEstimator, UseSoftConstraints, MaximumPenaltyFactor);
    238266
    239267      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
  • branches/3076_IA_evaluators_analyzers/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionConstraintAnalyzer.cs

    r17769 r17792  
    2525    private const string MaximumStepsParameterName = "Maximum Steps";
    2626    private const string StartPenalityParameterName = "Start Penality";
     27    private const string ConstraintUnsatisfiedSolutionsParameterName = "Constraint Unsatisfied Solutions";
    2728
    2829    #region parameter properties
     
    4950      (IValueParameter<DoubleValue>) Parameters[StartPenalityParameterName];
    5051
     52    public IResultParameter<DataTable> ConstraintUnsatisfiedSolutionsParameter =>
     53      (IResultParameter<DataTable>)Parameters[ConstraintUnsatisfiedSolutionsParameterName];
     54
    5155    #endregion
    5256
    5357    public override bool EnabledByDefault => false;
    5458    public static int Iterations { get; set; } = 0;
     59
     60    public IBoundsEstimator BoundsEstimator { get; set; } = new IABoundsEstimator();
    5561
    5662    [StorableConstructor]
     
    7985      Parameters.Add(new ValueParameter<DoubleValue>(StartPenalityParameterName,
    8086        "The start value for the penality multiplier.", new DoubleValue(0.5)));
     87      Parameters.Add(new ResultParameter<DataTable>(ConstraintUnsatisfiedSolutionsParameterName,
     88        "Shows the number of solutions with unsatisfied constraints."));
    8189
    8290
     
    92100          XAxisTitle = "Generations",
    93101          YAxisTitle = "Penality Multiplier"
     102        }
     103      };
     104
     105      ConstraintUnsatisfiedSolutionsParameter.DefaultValue = new DataTable(ConstraintUnsatisfiedSolutionsParameterName) {
     106        VisualProperties = {
     107          XAxisTitle = "Generations",
     108          YAxisTitle = "Constraint Unsatisfied Solutions"
    94109        }
    95110      };
     
    137152        Parameters.Add(new ValueParameter<DoubleValue>(StartPenalityParameterName,
    138153          "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      }
    139167    }
    140168
     
    158186      var constraints = problemData.IntervalConstraints.EnabledConstraints;
    159187      var variableRanges = problemData.VariableRanges.GetReadonlyDictionary();
     188      var intervalCollection = problemData.VariableRanges;
    160189      var newDataTable = ConstraintViolationParameter.ActualValue;
     190      var solutions = this.SymbolicExpressionTree.ToArray();
    161191
    162192      if (newDataTable.Rows.Count == 0)
     
    176206        penalityDataTable.Rows.Add(new DataRow(rowName));
    177207      penalityDataTable.Rows[rowName].Values.Add(penalityMultiplier.Value);
     208
     209      var constraintUnsatisfiedSolutionsDataTable = ConstraintUnsatisfiedSolutionsParameter.ActualValue;
     210      if (constraintUnsatisfiedSolutionsDataTable.Rows.Count == 0)
     211        constraintUnsatisfiedSolutionsDataTable.Rows.Add(new DataRow(ConstraintUnsatisfiedSolutionsParameterName));
     212
     213      constraintUnsatisfiedSolutionsDataTable.Rows[ConstraintUnsatisfiedSolutionsParameterName]
     214        .Values
     215        .Add(
     216          solutions
     217          .Where(s => IntervalUtil.IntervalConstraintsViolation(constraints, BoundsEstimator, intervalCollection, s).Any(x => x != 0.0))
     218          .Count());
    178219
    179220      return base.Apply();
  • branches/3076_IA_evaluators_analyzers/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionSingleObjectiveMetaModelAnalyzer.cs

    r17776 r17792  
    1010using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    1111using HeuristicLab.Optimization;
     12using HeuristicLab.Parameters;
    1213
    1314namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
     
    2122
    2223    #region parameter properties
    23     public IResultParameter<ISymbolicRegressionSolution> BestMetaModelParameter =>
    24       (IResultParameter<ISymbolicRegressionSolution>)Parameters[BestMetaModelParameterName];
     24    public IResultParameter<ItemList<ISymbolicRegressionSolution>> BestMetaModelParameter =>
     25      (IResultParameter<ItemList<ISymbolicRegressionSolution>>)Parameters[BestMetaModelParameterName];
    2526    #endregion
    2627
     
    3233
    3334    public SymbolicRegressionSingleObjectiveMetaModelAnalyzer() {
    34       Parameters.Add(new ResultParameter<ISymbolicRegressionSolution>(BestMetaModelParameterName,
    35         "The best meta model found."));
     35      Parameters.Add(new ResultParameter<ItemList<ISymbolicRegressionSolution>>(BestMetaModelParameterName,
     36        "A list with the meta model for all problems."));
    3637    }
    3738
    3839    [StorableHook(HookType.AfterDeserialization)]
    3940    private void AfterDeserialization() {
     41      Parameters.Remove(BestMetaModelParameterName);
     42
    4043      if (!Parameters.ContainsKey(BestMetaModelParameterName))
    41         Parameters.Add(new ResultParameter<ISymbolicRegressionSolution>(BestMetaModelParameterName,
    42           "The best meta model found."));
     44        Parameters.Add(new ResultParameter<ItemList<ISymbolicRegressionSolution>>(BestMetaModelParameterName,
     45          "A list with all meta models for the problems."));
    4346    }
    4447
     
    5255      // init
    5356      var solutions = this.SymbolicExpressionTree.ToArray();
     57      var bestQualityWithConstantOpt = baseProblem.Maximization.Value ? double.MinValue : double.MaxValue;
     58      var bestQualityWithoutConstantOpt = baseProblem.Maximization.Value ? double.MinValue : double.MaxValue;
    5459      var evaluator = baseProblem.Evaluator;
    55       var bestQuality = baseProblem.Maximization.Value ? double.MinValue : double.MaxValue;
    56       SymbolicRegressionSolution bestMetaModel = null;
    57 
     60      var interpreter = baseProblem.SymbolicExpressionTreeInterpreter;
     61      ISymbolicExpressionTree bestSolutionWithConstantOpt = null;
     62      ISymbolicExpressionTree bestSolutionWithoutConstantOpt = null;
     63      var metaModels = new ItemList<ISymbolicRegressionSolution>();
    5864      // iterate solutions
    5965      foreach (var solution in solutions) {
    60         double qualityAvg = CalculateAverageQuality(solution, evaluator, problems);
     66        // calculate with constant optimization
     67        var tmpSolution = (ISymbolicExpressionTree) solution.Clone();
     68        double qualityAvg = CalculateAverageQuality(tmpSolution, evaluator, problems, interpreter, true);
     69        // check if this solution is the best
     70        bool isBest = baseProblem.Maximization.Value ? (bestQualityWithConstantOpt < qualityAvg) : (bestQualityWithConstantOpt > qualityAvg);
     71        if (isBest) {
     72          bestQualityWithConstantOpt = qualityAvg;
     73          bestSolutionWithConstantOpt = tmpSolution;
     74        }
    6175
     76        // calculate it again without constant optimization to have a comparison
     77        tmpSolution = (ISymbolicExpressionTree) solution.Clone();
     78        qualityAvg = CalculateAverageQuality(tmpSolution, evaluator, problems, interpreter, false);
    6279        // check if this solution is the best
    63         bool isBest = baseProblem.Maximization.Value ? (bestQuality < qualityAvg) : (bestQuality > qualityAvg);
     80        isBest = baseProblem.Maximization.Value ? (bestQualityWithoutConstantOpt < qualityAvg) : (bestQualityWithoutConstantOpt > qualityAvg);
    6481        if (isBest) {
    65           bestQuality = qualityAvg;
    66           bestMetaModel = BuildSolution(solution, targetVariable, baseProblem);
     82          bestQualityWithoutConstantOpt = qualityAvg;
     83          bestSolutionWithoutConstantOpt = tmpSolution;
    6784        }
    6885      }
    69       BestMetaModelParameter.ActualValue = bestMetaModel;
     86
     87      foreach(var problem in problems) {
     88        metaModels.Add(BuildSolution(bestSolutionWithConstantOpt, targetVariable, problem, "withConstantOpt"));
     89        metaModels.Add(BuildSolution(bestSolutionWithoutConstantOpt, targetVariable, problem, "withoutConstantOpt"));
     90      }
     91       
     92      BestMetaModelParameter.ActualValue = metaModels;
    7093    }
    7194
     
    7396      ISymbolicExpressionTree solution,
    7497      string targetVariable,
    75       SymbolicRegressionSingleObjectiveProblem baseProblem) {
     98      SymbolicRegressionSingleObjectiveProblem problem,
     99      string suffix) {
    76100      var model = new SymbolicRegressionModel(
    77101            targetVariable,
    78102            (ISymbolicExpressionTree)solution.Clone(),
    79103            new SymbolicDataAnalysisExpressionTreeInterpreter());
    80       return new SymbolicRegressionSolution(model, baseProblem.ProblemData);
     104      return new SymbolicRegressionSolution(model, problem.ProblemData) { Name = $"{problem.Name}_solution_{suffix}" };
    81105    }
    82106
    83107    private double CalculateAverageQuality(
    84108      ISymbolicExpressionTree solution,
    85       ISymbolicDataAnalysisSingleObjectiveEvaluator<IRegressionProblemData> evaluator,
    86       IEnumerable<SymbolicRegressionSingleObjectiveProblem> problems) {
     109      ISymbolicDataAnalysisSingleObjectiveEvaluator<IRegressionProblemData> evaluator,
     110      IEnumerable<SymbolicRegressionSingleObjectiveProblem> problems,
     111      ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
     112      bool useConstantOptimization) {
    87113      double qualitySum = 0.0;
    88114      // iterate problems
    89115      foreach (var problem in problems) {
    90         IDataset dataset = problem.ProblemData.Dataset;
    91         IEnumerable<int> rows = Enumerable.Range(0, dataset.Rows);
    92         // evalute problem with the evaluator of the base problem
    93         qualitySum += evaluator.Evaluate(ExecutionContext, solution, problem.ProblemData, rows);
     116        var problemData = problem.ProblemData;
     117
     118        if (useConstantOptimization) {
     119          SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(
     120            interpreter,
     121            solution,
     122            problemData,
     123            problemData.TrainingIndices,
     124            false, 10, true);
     125        }
     126
     127        qualitySum += evaluator.Evaluate(
     128          ExecutionContext,
     129          solution,
     130          problemData,
     131          problemData.TrainingIndices);
    94132      }
     133       
    95134      // calculate the average quality
    96135      return qualitySum / problems.Count();
Note: See TracChangeset for help on using the changeset viewer.