#region License Information /* HeuristicLab * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Linq; using HEAL.Attic; using HeuristicLab.Algorithms.DataAnalysis; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HeuristicLab.Problems.DataAnalysis; namespace HeuristicLab.Problems.Modifiers { [StorableType("DD354BFC-0384-4A26-AE3B-C1195111385A")] [Item("MultiModelEvaluationRemovalStrategy", "A modifier attached to a problem")] public class MultiModelEvaluationRemovalStrategy : ModelBasedEvaluationRemoverProblemModifier { #region ParameterNames public const string SurvivalProbabilityParameterName = "SurvivalProbability"; public const string ModelBuilderParameterName = "ModelBuilder"; public const string InModelComparisonParameterName = "InModelComparison"; #endregion #region Parameters public IFixedValueParameter SurvivalProbabilityParameter => (IFixedValueParameter)Parameters[SurvivalProbabilityParameterName]; public IValueParameter> ModelBuilderParameter => (IValueParameter>)Parameters[ModelBuilderParameterName]; public IFixedValueParameter InModelComparisonParameter => (IFixedValueParameter)Parameters[InModelComparisonParameterName]; #endregion #region ParameterProperties public double SurvivalProbability => SurvivalProbabilityParameter.Value.Value; public IAlgorithm[] ModelBuilder => ModelBuilderParameter.Value.ToArray(); public bool InModelComparison => InModelComparisonParameter.Value.Value; #endregion #region Constructors & Cloning [StorableConstructor] protected MultiModelEvaluationRemovalStrategy(StorableConstructorFlag _) : base(_) { } protected MultiModelEvaluationRemovalStrategy(MultiModelEvaluationRemovalStrategy original, Cloner cloner) : base(original, cloner) { } public MultiModelEvaluationRemovalStrategy() { Parameters.Add(new FixedValueParameter( SurvivalProbabilityParameterName, "Probability of survival despite bad prediction values", new PercentValue(0.05))); Parameters.Add(new ValueParameter>(ModelBuilderParameterName, "The model builder", new ItemList(new IAlgorithm[] { new LinearRegression(), new RandomForestRegression() }))); Parameters.Add(new FixedValueParameter(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))); } public override IDeepCloneable Clone(Cloner cloner) { return new MultiModelEvaluationRemovalStrategy(this, cloner); } #endregion #region ModelBasedEvaluationRemoverProblemModifier protected override IRegressionSolution BuildRunningModel(RegressionProblemData pd, IRandom random, int objectiveNumber) { if (pd.TrainingPartition.Size <= 0) return null; try { var m = ModelBuilder[objectiveNumber % ModelBuilder.Length]; m.Problem = new RegressionProblem() { ProblemData = pd }; if (m.Parameters.ContainsKey("Seed") && m.Parameters["Seed"] is IValueParameter seedParameter) { seedParameter.Value.Value = random.Next(); if (m.Parameters.ContainsKey("SetSeedRandomly") && (m.Parameters["SetSeedRandomly"] is IValueParameter setSeedRandomly)) { setSeedRandomly.Value.Value = false; } } m.Start(); var res = m.Results.Select(x => x.Value).OfType().Single(); m.Prepare(); m.Runs.Clear(); return res; } catch (Exception) { return null; } } protected override bool RemoveEvaluation(Individual individual, bool[] maximization, IRandom random) { if (random.NextDouble() < SurvivalProbability) return false; if (solutions == null || solutions.Count != maximization.Length || solutions.Any(s => s == null)) return false; var q = solutions.Select(x => x.Model.GetEstimatedValues(ToDataset(ExtractInputs(individual)), new[] { 0 }).Single()).ToArray(); return lastPopulation.Any(x => DominationCalculator.Dominates(InModelComparison ? x.Item3 : x.Item2, q, maximization, false) == DominationResult.Dominates); } #endregion } }