Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3133_ProblemModifiers/HeuristicLab.Problems.Modifiers/Modifier/InModelSingularEvaluationRemovalStrategy.cs

Last change on this file was 18029, checked in by bwerth, 3 years ago

#3133 added implementation of problem modifiers

File size: 5.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Linq;
24using HEAL.Attic;
25using HeuristicLab.Algorithms.DataAnalysis;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Problems.DataAnalysis;
32
33namespace HeuristicLab.Problems.Modifiers {
34  [StorableType("888EE1F9-F142-40F4-B5DC-D541184F8A9D")]
35  [Item("InModelSingularEvaluationRemovalStrategy", "A modifier attached to a problem")]
36  public class InModelSingularEvaluationRemovalStrategy : ModelBasedEvaluationRemoverProblemModifier {
37
38    public const string SurvivalProbabilityParameterName = "SurvivalProbability";
39    public const string ModelBuilderParameterName = "ModelBuilder";
40    public const string InModelComparisonParameterName = "InModelComparison";
41    public IFixedValueParameter<PercentValue> SurvivalProbabilityParameter => (IFixedValueParameter<PercentValue>)Parameters[SurvivalProbabilityParameterName];
42    public IValueParameter<IAlgorithm> ModelBuilderParameter => (IValueParameter<IAlgorithm>)Parameters[ModelBuilderParameterName];
43    public IFixedValueParameter<BoolValue> InModelComparisonParameter => (IFixedValueParameter<BoolValue>)Parameters[InModelComparisonParameterName];
44    public double SurvivalProbability => SurvivalProbabilityParameter.Value.Value;
45    public IAlgorithm ModelBuilder => ModelBuilderParameter.Value;
46    public bool InModelComparison => InModelComparisonParameter.Value.Value;
47
48    [StorableConstructor]
49    protected InModelSingularEvaluationRemovalStrategy(StorableConstructorFlag _) : base(_) { }
50    protected InModelSingularEvaluationRemovalStrategy(InModelSingularEvaluationRemovalStrategy original, Cloner cloner) : base(original, cloner) { }
51
52    public InModelSingularEvaluationRemovalStrategy() : base() {
53      Parameters.Add(new FixedValueParameter<PercentValue>(
54        SurvivalProbabilityParameterName,
55        "Probability of survival despite bad prediction values",
56        new PercentValue(0.05)));
57      Parameters.Add(new ValueParameter<IAlgorithm>(ModelBuilderParameterName, "The model builder", new LinearRegression()));
58      Parameters.Add(new FixedValueParameter<BoolValue>(InModelComparisonParameterName, "Whether the actual qualities (false) or the predicted qualities (values) will be used to determine removal. This is relevant if the model has a strong offset to the actual values", new BoolValue(false)));
59    }
60
61    public override IDeepCloneable Clone(Cloner cloner) {
62      return new InModelSingularEvaluationRemovalStrategy(this, cloner);
63    }
64
65    protected override IRegressionSolution BuildRunningModel(RegressionProblemData pd, IRandom random, int objectiveNumber) {
66      if (pd.TrainingPartition.Size <= 0) return null;
67      try {
68        ModelBuilder.Problem = new RegressionProblem() { ProblemData = pd };
69        if (ModelBuilder.Parameters.ContainsKey("Seed") && (ModelBuilder.Parameters["Seed"] is IValueParameter<IntValue> seedparam)) {
70          seedparam.Value.Value = random.Next();
71          if (ModelBuilder.Parameters.ContainsKey("SetSeedRandomly") && (ModelBuilder.Parameters["SetSeedRandomly"] is IValueParameter<BoolValue> setseedparam)) {
72            setseedparam.Value.Value = false;
73          }
74        }
75        ModelBuilder.Start();
76        var res = ModelBuilder.Results.Select(x => x.Value).OfType<IRegressionSolution>().Single();
77        ModelBuilder.Prepare();
78        ModelBuilder.Runs.Clear();
79        return res;
80      } catch (Exception) {
81        return null;
82      }
83    }
84
85
86    protected override bool RemoveEvaluation(Individual individual, bool[] maximization, IRandom random) {
87      if (random.NextDouble() < SurvivalProbability) return false;
88      if (solutions == null || solutions.Count != maximization.Length || solutions.Any(s => s == null)) return false;
89      var q = solutions.Select(x => x.Model.GetEstimatedValues(ToDataset(ExtractInputs(individual)), new[] { 0 }).Single()).ToArray();
90      return lastPopulation.Any(x => DominationCalculator<double[]>.Dominates(InModelComparison ? x.Item3 : x.Item2, q, maximization, false) == DominationResult.Dominates);
91    }
92
93  }
94}
Note: See TracBrowser for help on using the repository browser.