Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3133_ProblemModifiers/HeuristicLab.Problems.Modifiers/Modifier/MultiModelEvaluationRemovalStrategy.cs @ 18066

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

#3133 added implementation of problem modifiers

File size: 5.3 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("DD354BFC-0384-4A26-AE3B-C1195111385A")]
35  [Item("MultiModelEvaluationRemovalStrategy", "A modifier attached to a problem")]
36  public class MultiModelEvaluationRemovalStrategy : ModelBasedEvaluationRemoverProblemModifier {
37    #region ParameterNames
38    public const string SurvivalProbabilityParameterName = "SurvivalProbability";
39    public const string ModelBuilderParameterName = "ModelBuilder";
40    public const string InModelComparisonParameterName = "InModelComparison";
41    #endregion
42
43    #region Parameters
44    public IFixedValueParameter<PercentValue> SurvivalProbabilityParameter => (IFixedValueParameter<PercentValue>)Parameters[SurvivalProbabilityParameterName];
45    public IValueParameter<ItemList<IAlgorithm>> ModelBuilderParameter => (IValueParameter<ItemList<IAlgorithm>>)Parameters[ModelBuilderParameterName];
46    public IFixedValueParameter<BoolValue> InModelComparisonParameter => (IFixedValueParameter<BoolValue>)Parameters[InModelComparisonParameterName];
47    #endregion
48
49    #region ParameterProperties
50    public double SurvivalProbability => SurvivalProbabilityParameter.Value.Value;
51    public IAlgorithm[] ModelBuilder => ModelBuilderParameter.Value.ToArray();
52    public bool InModelComparison => InModelComparisonParameter.Value.Value;
53    #endregion
54
55    #region Constructors & Cloning
56    [StorableConstructor]
57    protected MultiModelEvaluationRemovalStrategy(StorableConstructorFlag _) : base(_) { }
58    protected MultiModelEvaluationRemovalStrategy(MultiModelEvaluationRemovalStrategy original, Cloner cloner) : base(original, cloner) { }
59    public MultiModelEvaluationRemovalStrategy() {
60      Parameters.Add(new FixedValueParameter<PercentValue>(
61        SurvivalProbabilityParameterName,
62        "Probability of survival despite bad prediction values",
63        new PercentValue(0.05)));
64      Parameters.Add(new ValueParameter<ItemList<IAlgorithm>>(ModelBuilderParameterName, "The model builder", new ItemList<IAlgorithm>(new IAlgorithm[] { new LinearRegression(), new RandomForestRegression() })));
65      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)));
66    }
67    public override IDeepCloneable Clone(Cloner cloner) {
68      return new MultiModelEvaluationRemovalStrategy(this, cloner);
69    }
70    #endregion
71
72    #region ModelBasedEvaluationRemoverProblemModifier
73    protected override IRegressionSolution BuildRunningModel(RegressionProblemData pd, IRandom random, int objectiveNumber) {
74      if (pd.TrainingPartition.Size <= 0) return null;
75      try {
76        var m = ModelBuilder[objectiveNumber % ModelBuilder.Length];
77        m.Problem = new RegressionProblem() { ProblemData = pd };
78        if (m.Parameters.ContainsKey("Seed") && m.Parameters["Seed"] is IValueParameter<IntValue> seedParameter) {
79          seedParameter.Value.Value = random.Next();
80          if (m.Parameters.ContainsKey("SetSeedRandomly") &&
81              (m.Parameters["SetSeedRandomly"] is IValueParameter<BoolValue> setSeedRandomly)) {
82            setSeedRandomly.Value.Value = false;
83          }
84        }
85        m.Start();
86        var res = m.Results.Select(x => x.Value).OfType<IRegressionSolution>().Single();
87        m.Prepare();
88        m.Runs.Clear();
89        return res;
90      } catch (Exception) {
91        return null;
92      }
93    }
94    protected override bool RemoveEvaluation(Individual individual, bool[] maximization, IRandom random) {
95      if (random.NextDouble() < SurvivalProbability) return false;
96      if (solutions == null || solutions.Count != maximization.Length || solutions.Any(s => s == null)) return false;
97      var q = solutions.Select(x => x.Model.GetEstimatedValues(ToDataset(ExtractInputs(individual)), new[] { 0 }).Single()).ToArray();
98      return lastPopulation.Any(x => DominationCalculator<double[]>.Dominates(InModelComparison ? x.Item3 : x.Item2, q, maximization, false) == DominationResult.Dominates);
99    }
100    #endregion
101  }
102}
Note: See TracBrowser for help on using the repository browser.