#region License Information /* HeuristicLab * Copyright (C) 2002-2016 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 HeuristicLab.Analysis; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Optimization; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using System; using System.Collections.Generic; using System.Linq; namespace HeuristicLab.OptimizationExpertSystem.Common { [Item("Overall Best Recommender", "")] [StorableClass] public class OverallBestRecommender : ParameterizedNamedItem, IAlgorithmInstanceRecommender { private IFixedValueParameter NeighborhoodFactorParameter { get { return (IFixedValueParameter)Parameters["NeighborhoodFactor"]; } } [StorableConstructor] private OverallBestRecommender(bool deserializing) : base(deserializing) { } private OverallBestRecommender(OverallBestRecommender original, Cloner cloner) : base(original, cloner) { } public OverallBestRecommender() { } public override IDeepCloneable Clone(Cloner cloner) { return new OverallBestRecommender(this, cloner); } public IRecommendationModel TrainModel(IRun[] problemInstances, KnowledgeCenter kc, string[] characteristics) { var instances = new List>(); foreach (var relevantRuns in kc.GetKnowledgeBaseByAlgorithm()) { var algorithm = relevantRuns.Key; var pis = relevantRuns.Value.Select(x => ((StringValue)x.Parameters["Problem Name"]).Value).Distinct() .Select(x => Tuple.Create(x, problemInstances.SingleOrDefault(y => ((StringValue)y.Parameters["Problem Name"]).Value == x))) .Where(x => x.Item2 != null) .Select(x => Tuple.Create(x.Item1, ((DoubleValue)x.Item2.Parameters["BestKnownQuality"]).Value)) .ToDictionary(x => x.Item1, x => x.Item2); var avgERT = 0.0; var count = 0; foreach (var problemRuns in relevantRuns.Value.GroupBy(x => ((StringValue)x.Parameters["Problem Name"]).Value)) { double bkq; if (!pis.TryGetValue(problemRuns.Key, out bkq)) continue; var ert = ExpectedRuntimeHelper.CalculateErt(problemRuns.ToList(), "QualityPerEvaluations", kc.GetTarget(bkq, kc.MinimumTarget.Value, kc.Maximization), kc.Maximization).ExpectedRuntime; if (double.IsInfinity(ert)) ert = int.MaxValue; avgERT += ert; count++; } avgERT /= count; instances.Add(new KeyValuePair(algorithm, avgERT)); } return new FixedRankModel(instances.OrderBy(x => x.Value)); } } }