[12842] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[13667] | 3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[12842] | 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 |
|
---|
[15736] | 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Drawing;
|
---|
| 25 | using System.IO;
|
---|
| 26 | using System.Linq;
|
---|
| 27 | using System.Threading;
|
---|
| 28 | using System.Threading.Tasks;
|
---|
| 29 | using HeuristicLab.Algorithms.DataAnalysis;
|
---|
[12860] | 30 | using HeuristicLab.Analysis;
|
---|
[13750] | 31 | using HeuristicLab.Analysis.SelfOrganizingMaps;
|
---|
[13551] | 32 | using HeuristicLab.Collections;
|
---|
[13485] | 33 | using HeuristicLab.Common;
|
---|
| 34 | using HeuristicLab.Common.Resources;
|
---|
[12842] | 35 | using HeuristicLab.Core;
|
---|
[13485] | 36 | using HeuristicLab.Data;
|
---|
| 37 | using HeuristicLab.MainForm;
|
---|
[12842] | 38 | using HeuristicLab.Optimization;
|
---|
[13485] | 39 | using HeuristicLab.Persistence.Default.Xml;
|
---|
[13774] | 40 | using HeuristicLab.Problems.DataAnalysis;
|
---|
[13750] | 41 | using HeuristicLab.Random;
|
---|
[13809] | 42 | using Algorithm = HeuristicLab.Clients.OKB.Administration.Algorithm;
|
---|
| 43 | using Problem = HeuristicLab.Clients.OKB.Administration.Problem;
|
---|
[13551] | 44 | using RunCreationClient = HeuristicLab.Clients.OKB.RunCreation.RunCreationClient;
|
---|
| 45 | using SingleObjectiveOKBProblem = HeuristicLab.Clients.OKB.RunCreation.SingleObjectiveOKBProblem;
|
---|
[13713] | 46 | using SingleObjectiveOKBSolution = HeuristicLab.Clients.OKB.RunCreation.SingleObjectiveOKBSolution;
|
---|
[12842] | 47 |
|
---|
[13663] | 48 | namespace HeuristicLab.OptimizationExpertSystem.Common {
|
---|
[13722] | 49 | [Item("Knowledge Center", "Currently in experimental phase, an expert system that makes algorithm suggestions based on fitness landscape analysis features and an optimization knowledge base.")]
|
---|
[12860] | 50 | [Creatable(CreatableAttribute.Categories.TestingAndAnalysis, Priority = 119)]
|
---|
[13722] | 51 | public sealed class KnowledgeCenter : IContent {
|
---|
[13752] | 52 | private bool SuppressEvents { get; set; }
|
---|
[12842] | 53 |
|
---|
| 54 | public string Filename { get; set; }
|
---|
| 55 |
|
---|
[13774] | 56 | public static Image StaticItemImage {
|
---|
[12842] | 57 | get { return VSImageLibrary.Library; }
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[13751] | 60 | private readonly IntValue maximumEvaluations;
|
---|
[13722] | 61 | public IntValue MaximumEvaluations {
|
---|
[12847] | 62 | get { return maximumEvaluations; }
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[13757] | 65 | private readonly DoubleValue minimumTarget;
|
---|
| 66 | public DoubleValue MinimumTarget {
|
---|
| 67 | get { return minimumTarget; }
|
---|
| 68 | }
|
---|
| 69 |
|
---|
[13751] | 70 | private readonly RunCollection instanceRuns;
|
---|
[13722] | 71 | public RunCollection InstanceRuns {
|
---|
| 72 | get { return instanceRuns; }
|
---|
[12842] | 73 | }
|
---|
| 74 |
|
---|
[13751] | 75 | private readonly RunCollection seededRuns;
|
---|
[13722] | 76 | public RunCollection SeededRuns {
|
---|
| 77 | get { return seededRuns; }
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[13751] | 80 | private readonly RunCollection knowledgeBase;
|
---|
[13551] | 81 | public RunCollection KnowledgeBase {
|
---|
| 82 | get { return knowledgeBase; }
|
---|
[12842] | 83 | }
|
---|
| 84 |
|
---|
[13751] | 85 | private readonly SingleObjectiveOKBProblem problem;
|
---|
[13551] | 86 | public SingleObjectiveOKBProblem Problem {
|
---|
[12842] | 87 | get { return problem; }
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[13774] | 90 | private readonly ItemList<IAlgorithm> algorithmInstances;
|
---|
| 91 | private readonly ReadOnlyItemList<IAlgorithm> readonlyAlgorithmInstances;
|
---|
| 92 | public ReadOnlyItemList<IAlgorithm> AlgorithmInstances {
|
---|
| 93 | get { return readonlyAlgorithmInstances; }
|
---|
[12847] | 94 | }
|
---|
| 95 |
|
---|
[13751] | 96 | private readonly RunCollection problemInstances;
|
---|
[12957] | 97 | public RunCollection ProblemInstances {
|
---|
| 98 | get { return problemInstances; }
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[13787] | 101 | private IRecommendationModel recommendationModel;
|
---|
| 102 | public IRecommendationModel RecommendationModel {
|
---|
| 103 | get { return recommendationModel; }
|
---|
| 104 | set {
|
---|
| 105 | if (recommendationModel == value) return;
|
---|
| 106 | recommendationModel = value;
|
---|
| 107 | OnRecommenderModelChanged();
|
---|
| 108 | }
|
---|
[13757] | 109 | }
|
---|
[13751] | 110 |
|
---|
| 111 | private readonly CheckedItemList<IScope> solutionSeedingPool;
|
---|
[13713] | 112 | public CheckedItemList<IScope> SolutionSeedingPool {
|
---|
| 113 | get { return solutionSeedingPool; }
|
---|
[13663] | 114 | }
|
---|
[13713] | 115 |
|
---|
[13751] | 116 | private readonly EnumValue<SeedingStrategyTypes> seedingStrategy;
|
---|
[13713] | 117 | public EnumValue<SeedingStrategyTypes> SeedingStrategy {
|
---|
| 118 | get { return seedingStrategy; }
|
---|
| 119 | }
|
---|
[13663] | 120 |
|
---|
[13551] | 121 | private BidirectionalLookup<long, IRun> algorithmId2RunMapping;
|
---|
| 122 | private BidirectionalDictionary<long, IAlgorithm> algorithmId2AlgorithmInstanceMapping;
|
---|
[13751] | 123 | private BidirectionalDictionary<long, IRun> problemId2ProblemInstanceMapping;
|
---|
| 124 |
|
---|
[13774] | 125 | public bool Maximization {
|
---|
[13722] | 126 | get { return Problem != null && Problem.ProblemId >= 0 && ((IValueParameter<BoolValue>)Problem.MaximizationParameter).Value.Value; }
|
---|
[12842] | 127 | }
|
---|
| 128 |
|
---|
[13722] | 129 | public KnowledgeCenter() {
|
---|
[13748] | 130 | maximumEvaluations = new IntValue(10000);
|
---|
[13759] | 131 | minimumTarget = new DoubleValue(0.05);
|
---|
[13722] | 132 | instanceRuns = new RunCollection();
|
---|
[13748] | 133 | seededRuns = new RunCollection();
|
---|
[13551] | 134 | knowledgeBase = new RunCollection();
|
---|
[13774] | 135 | algorithmInstances = new ItemList<IAlgorithm>();
|
---|
| 136 | readonlyAlgorithmInstances = algorithmInstances.AsReadOnly();
|
---|
[12957] | 137 | problemInstances = new RunCollection();
|
---|
[13787] | 138 | recommendationModel = FixedRankModel.GetEmpty();
|
---|
[13551] | 139 | problem = new SingleObjectiveOKBProblem();
|
---|
| 140 | algorithmId2RunMapping = new BidirectionalLookup<long, IRun>();
|
---|
| 141 | algorithmId2AlgorithmInstanceMapping = new BidirectionalDictionary<long, IAlgorithm>();
|
---|
[13751] | 142 | problemId2ProblemInstanceMapping = new BidirectionalDictionary<long, IRun>();
|
---|
[13713] | 143 | solutionSeedingPool = new CheckedItemList<IScope>();
|
---|
| 144 | seedingStrategy = new EnumValue<SeedingStrategyTypes>(SeedingStrategyTypes.NoSeeding);
|
---|
[12860] | 145 | RegisterEventHandlers();
|
---|
[12842] | 146 | }
|
---|
| 147 |
|
---|
[13551] | 148 | private void ProblemOnProblemChanged(object sender, EventArgs eventArgs) {
|
---|
[13748] | 149 | // TODO: Potentially, knowledge base has to be re-downloaded
|
---|
[13551] | 150 | }
|
---|
| 151 |
|
---|
[12860] | 152 | private void RegisterEventHandlers() {
|
---|
[13722] | 153 | maximumEvaluations.ValueChanged += MaximumEvaluationsOnValueChanged;
|
---|
[13757] | 154 | minimumTarget.ValueChanged += MinimumTargetOnValueChanged;
|
---|
[13551] | 155 | problem.ProblemChanged += ProblemOnProblemChanged;
|
---|
[13713] | 156 | problem.Solutions.ItemsAdded += ProblemSolutionsChanged;
|
---|
| 157 | problem.Solutions.ItemsReplaced += ProblemSolutionsChanged;
|
---|
| 158 | problem.Solutions.ItemsRemoved += ProblemSolutionsChanged;
|
---|
| 159 | problem.Solutions.CollectionReset += ProblemSolutionsChanged;
|
---|
[13722] | 160 | instanceRuns.CollectionReset += InformationChanged;
|
---|
| 161 | instanceRuns.ItemsAdded += InformationChanged;
|
---|
| 162 | instanceRuns.ItemsRemoved += InformationChanged;
|
---|
| 163 | instanceRuns.Reset += InformationChanged;
|
---|
| 164 | instanceRuns.UpdateOfRunsInProgressChanged += InformationChanged;
|
---|
[13551] | 165 | knowledgeBase.CollectionReset += InformationChanged;
|
---|
| 166 | knowledgeBase.ItemsAdded += InformationChanged;
|
---|
| 167 | knowledgeBase.ItemsRemoved += InformationChanged;
|
---|
[12842] | 168 | }
|
---|
| 169 |
|
---|
[13722] | 170 | private void MaximumEvaluationsOnValueChanged(object sender, EventArgs eventArgs) {
|
---|
[13787] | 171 |
|
---|
[13722] | 172 | }
|
---|
| 173 |
|
---|
[13757] | 174 | private void MinimumTargetOnValueChanged(object sender, EventArgs e) {
|
---|
[13787] | 175 |
|
---|
[13757] | 176 | }
|
---|
| 177 |
|
---|
[13713] | 178 | private void ProblemSolutionsChanged(object sender, EventArgs e) {
|
---|
| 179 | foreach (var sol in Problem.Solutions.Select(x => x.Solution).OfType<IScope>()) {
|
---|
| 180 | if (!SolutionSeedingPool.Contains(sol))
|
---|
| 181 | SolutionSeedingPool.Add(sol, false);
|
---|
| 182 | }
|
---|
| 183 | }
|
---|
| 184 |
|
---|
[12860] | 185 | private void InformationChanged(object sender, EventArgs e) {
|
---|
| 186 | var runCollection = sender as RunCollection;
|
---|
| 187 | if (runCollection != null && runCollection.UpdateOfRunsInProgress) return;
|
---|
[12842] | 188 | }
|
---|
[13774] | 189 |
|
---|
[13751] | 190 | public bool IsCurrentInstance(IRun run) {
|
---|
| 191 | if (!problemId2ProblemInstanceMapping.ContainsSecond(run)) return false;
|
---|
| 192 | return problemId2ProblemInstanceMapping.GetBySecond(run) == Problem.ProblemId;
|
---|
| 193 | }
|
---|
[13561] | 194 |
|
---|
[13787] | 195 | public void UpdateInstanceProjection(string[] characteristics) {
|
---|
| 196 | if (characteristics.Length == 0) return;
|
---|
[13751] | 197 |
|
---|
[13787] | 198 | var instances = GetProblemCharacteristics(characteristics);
|
---|
[13718] | 199 |
|
---|
[13751] | 200 | var key2Idx = new BidirectionalDictionary<IRun, int>();
|
---|
[13561] | 201 | foreach (var kvp in instances.Select((k, i) => new { Index = i, Key = k.Key }))
|
---|
| 202 | key2Idx.Add(kvp.Key, kvp.Index);
|
---|
| 203 |
|
---|
[14776] | 204 | Func<double[], double[], double> euclid = (a, b) => Math.Sqrt(a.Zip(b, (x, y) => (x - y)).Sum(x => x * x));
|
---|
| 205 | Func<DoubleArray, DoubleArray, double> euclidDArray = (a, b) => Math.Sqrt(a.Zip(b, (x, y) => (x - y)).Sum(x => x * x));
|
---|
[13561] | 206 | #region MDS
|
---|
| 207 | var num = instances.Count;
|
---|
| 208 | var matrix = new DoubleMatrix(num, num);
|
---|
| 209 | for (var i = 0; i < num - 1; i++) {
|
---|
| 210 | for (var j = i + 1; j < num; j++) {
|
---|
| 211 | matrix[i, j] = matrix[j, i] = euclid(instances[key2Idx.GetBySecond(i)], instances[key2Idx.GetBySecond(j)]);
|
---|
| 212 | }
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | var coords = MultidimensionalScaling.KruskalShepard(matrix);
|
---|
| 216 | #endregion
|
---|
| 217 | #region PCA
|
---|
[13791] | 218 | double[,] v = null;
|
---|
[13787] | 219 | var ds = new double[instances.Count, characteristics.Length];
|
---|
[13791] | 220 | if (characteristics.Length > 1) {
|
---|
| 221 | foreach (var instance in instances) {
|
---|
| 222 | var arr = instance.Value;
|
---|
| 223 | for (var feature = 0; feature < arr.Length; feature++)
|
---|
| 224 | ds[key2Idx.GetByFirst(instance.Key), feature] = arr[feature];
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | int info;
|
---|
| 228 | double[] s2;
|
---|
| 229 | alglib.pcabuildbasis(ds, ds.GetLength(0), ds.GetLength(1), out info, out s2, out v);
|
---|
[13561] | 230 | }
|
---|
| 231 | #endregion
|
---|
[13750] | 232 | #region SOM
|
---|
[13787] | 233 | var features = new DoubleMatrix(characteristics.Length, instances.Count);
|
---|
[13750] | 234 | foreach (var instance in instances) {
|
---|
| 235 | var arr = instance.Value;
|
---|
| 236 | for (var feature = 0; feature < arr.Length; feature++)
|
---|
| 237 | features[feature, key2Idx.GetByFirst(instance.Key)] = arr[feature];
|
---|
| 238 | }
|
---|
[13791] | 239 | var somCoords = SOM.Map(features, new MersenneTwister(42), somSize: 10, learningRadius: 20, iterations: 200, jittering: true);
|
---|
[13750] | 240 | #endregion
|
---|
[14667] | 241 | #region TSNE
|
---|
| 242 | var tsneFeatures = new DoubleArray[instances.Count];
|
---|
| 243 | foreach (var instance in instances) {
|
---|
| 244 | tsneFeatures[key2Idx.GetByFirst(instance.Key)] = new DoubleArray(instance.Value);
|
---|
| 245 | }
|
---|
[15255] | 246 | var tsneCoords = TSNEStatic<DoubleArray>.Run(tsneFeatures, new EuclideanDistance(), new FastRandom(42),
|
---|
| 247 | newDimensions: 2, perplexity: Math.Min((instances.Count - 1) / 4, 50), theta: 0,
|
---|
| 248 | stopLyingIter: 0, momSwitchIter: 0, momentum: 0, finalMomentum: 0, eta: 10);
|
---|
[14667] | 249 | #endregion
|
---|
[12957] | 250 |
|
---|
| 251 | ProblemInstances.UpdateOfRunsInProgress = true;
|
---|
| 252 | try {
|
---|
| 253 | foreach (var instance in ProblemInstances) {
|
---|
[13791] | 254 | IItem item;
|
---|
| 255 | if (v != null) {
|
---|
| 256 | double x = 0, y = 0;
|
---|
| 257 | for (var feature = 0; feature < ds.GetLength(1); feature++) {
|
---|
| 258 | x += ds[key2Idx.GetByFirst(instance), feature] * v[feature, 0];
|
---|
| 259 | y += ds[key2Idx.GetByFirst(instance), feature] * v[feature, 1];
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | if (instance.Results.TryGetValue("Projection.PCA.X", out item)) {
|
---|
| 263 | ((DoubleValue)item).Value = x;
|
---|
| 264 | } else instance.Results.Add("Projection.PCA.X", new DoubleValue(x));
|
---|
| 265 | if (instance.Results.TryGetValue("Projection.PCA.Y", out item)) {
|
---|
| 266 | ((DoubleValue)item).Value = y;
|
---|
| 267 | } else instance.Results.Add("Projection.PCA.Y", new DoubleValue(y));
|
---|
| 268 | } else {
|
---|
| 269 | instance.Results.Remove("Projection.PCA.X");
|
---|
| 270 | instance.Results.Remove("Projection.PCA.Y");
|
---|
[12957] | 271 | }
|
---|
[13561] | 272 |
|
---|
| 273 | if (instance.Results.TryGetValue("Projection.MDS.X", out item)) {
|
---|
[13751] | 274 | ((DoubleValue)item).Value = coords[key2Idx.GetByFirst(instance), 0];
|
---|
| 275 | } else instance.Results.Add("Projection.MDS.X", new DoubleValue(coords[key2Idx.GetByFirst(instance), 0]));
|
---|
[13561] | 276 | if (instance.Results.TryGetValue("Projection.MDS.Y", out item)) {
|
---|
[13751] | 277 | ((DoubleValue)item).Value = coords[key2Idx.GetByFirst(instance), 1];
|
---|
| 278 | } else instance.Results.Add("Projection.MDS.Y", new DoubleValue(coords[key2Idx.GetByFirst(instance), 1]));
|
---|
[13750] | 279 |
|
---|
| 280 | if (instance.Results.TryGetValue("Projection.SOM.X", out item)) {
|
---|
[13751] | 281 | ((DoubleValue)item).Value = somCoords[key2Idx.GetByFirst(instance), 0];
|
---|
| 282 | } else instance.Results.Add("Projection.SOM.X", new DoubleValue(somCoords[key2Idx.GetByFirst(instance), 0]));
|
---|
[13750] | 283 | if (instance.Results.TryGetValue("Projection.SOM.Y", out item)) {
|
---|
[13751] | 284 | ((DoubleValue)item).Value = somCoords[key2Idx.GetByFirst(instance), 1];
|
---|
| 285 | } else instance.Results.Add("Projection.SOM.Y", new DoubleValue(somCoords[key2Idx.GetByFirst(instance), 1]));
|
---|
[14667] | 286 |
|
---|
| 287 | if (instance.Results.TryGetValue("Projection.TSNE.X", out item)) {
|
---|
| 288 | ((DoubleValue)item).Value = tsneCoords[key2Idx.GetByFirst(instance), 0];
|
---|
| 289 | } else instance.Results.Add("Projection.TSNE.X", new DoubleValue(tsneCoords[key2Idx.GetByFirst(instance), 0]));
|
---|
| 290 | if (instance.Results.TryGetValue("Projection.TSNE.Y", out item)) {
|
---|
| 291 | ((DoubleValue)item).Value = tsneCoords[key2Idx.GetByFirst(instance), 1];
|
---|
| 292 | } else instance.Results.Add("Projection.TSNE.Y", new DoubleValue(tsneCoords[key2Idx.GetByFirst(instance), 1]));
|
---|
[12957] | 293 | }
|
---|
| 294 | } finally { ProblemInstances.UpdateOfRunsInProgress = false; }
|
---|
| 295 | }
|
---|
| 296 |
|
---|
[13485] | 297 | private static readonly HashSet<string> InterestingValueNames = new HashSet<string>() {
|
---|
[15736] | 298 | "QualityPerEvaluations", "QualityPerClock", "Problem Name", "Problem Type", "Algorithm Name", "Algorithm Type", "Maximization", "BestKnownQuality"
|
---|
[13485] | 299 | };
|
---|
| 300 |
|
---|
[13722] | 301 | public Task<ResultCollection> StartAlgorithmAsync(int index) {
|
---|
| 302 | return StartAlgorithmAsync(index, CancellationToken.None);
|
---|
| 303 | }
|
---|
| 304 |
|
---|
| 305 | public Task<ResultCollection> StartAlgorithmAsync(int index, CancellationToken cancellation) {
|
---|
[13774] | 306 | var selectedInstance = algorithmInstances[index];
|
---|
[13713] | 307 | var algorithmClone = (IAlgorithm)selectedInstance.Clone();
|
---|
| 308 | var problemClone = Problem.CloneProblem() as ISingleObjectiveHeuristicOptimizationProblem;
|
---|
| 309 | if (problemClone == null) throw new InvalidOperationException("Problem is not of type " + typeof(ISingleObjectiveHeuristicOptimizationProblem).FullName);
|
---|
| 310 | // TODO: It is assumed the problem instance by default is configured using no preexisting solution creator
|
---|
[13722] | 311 | var seedingStrategyLocal = SeedingStrategy.Value;
|
---|
| 312 | if (seedingStrategyLocal != SeedingStrategyTypes.NoSeeding) {
|
---|
[13713] | 313 | if (!SolutionSeedingPool.CheckedItems.Any()) throw new InvalidOperationException("There are no solutions selected for seeding.");
|
---|
| 314 | // TODO: It would be necessary to specify the solution creator somewhere (property and GUI)
|
---|
| 315 | var seedingCreator = problemClone.Operators.OfType<IPreexistingSolutionCreator>().FirstOrDefault();
|
---|
| 316 | if (seedingCreator == null) throw new InvalidOperationException("The problem does not contain a solution creator that allows seeding.");
|
---|
| 317 | seedingCreator.PreexistingSolutionsParameter.Value.Replace(SolutionSeedingPool.CheckedItems.Select(x => x.Value));
|
---|
[13722] | 318 | seedingCreator.SampleFromPreexistingParameter.Value.Value = seedingStrategyLocal == SeedingStrategyTypes.SeedBySampling;
|
---|
[13713] | 319 | // TODO: WHY!? WHY??!?
|
---|
| 320 | ((dynamic)problemClone.SolutionCreatorParameter).Value = (dynamic)seedingCreator;
|
---|
| 321 | }
|
---|
| 322 | algorithmClone.Problem = problemClone;
|
---|
| 323 | algorithmClone.Prepare(true);
|
---|
[13649] | 324 | IParameter stopParam;
|
---|
| 325 | var monitorStop = true;
|
---|
[13713] | 326 | if (algorithmClone.Parameters.TryGetValue("MaximumEvaluations", out stopParam)) {
|
---|
[13649] | 327 | var maxEvalParam = stopParam as IValueParameter<Data.IntValue>;
|
---|
| 328 | if (maxEvalParam != null) {
|
---|
[13722] | 329 | maxEvalParam.Value.Value = MaximumEvaluations.Value;
|
---|
[13649] | 330 | monitorStop = false;
|
---|
| 331 | }
|
---|
| 332 | }
|
---|
| 333 |
|
---|
[13713] | 334 | // TODO: The following can be simplified when we have async implementation patterns for our algorithms:
|
---|
| 335 | // TODO: The closures can be removed and replaced with private member methods
|
---|
| 336 | var waitHandle = new AutoResetEvent(false);
|
---|
[13649] | 337 |
|
---|
[13713] | 338 | #region EventHandler closures
|
---|
| 339 | EventHandler exeStateChanged = (sender, e) => {
|
---|
[13722] | 340 | if (algorithmClone.ExecutionState == ExecutionState.Stopped) {
|
---|
[13748] | 341 | lock (Problem.Solutions) {
|
---|
| 342 | foreach (var solution in algorithmClone.Results.Where(x => x.Name.ToLower().Contains("solution")).Select(x => x.Value).OfType<IScope>()) {
|
---|
| 343 | Problem.Solutions.Add(new SingleObjectiveOKBSolution(Problem.ProblemId) {
|
---|
| 344 | Quality = solution.Variables.ContainsKey(Problem.Problem.Evaluator.QualityParameter.ActualName) ? ((DoubleValue)solution.Variables[Problem.Problem.Evaluator.QualityParameter.ActualName].Value).Value : double.NaN,
|
---|
| 345 | Solution = (IItem)solution.Clone()
|
---|
| 346 | });
|
---|
| 347 | }
|
---|
[13713] | 348 | }
|
---|
[13722] | 349 | if (seedingStrategyLocal == SeedingStrategyTypes.NoSeeding) {
|
---|
[13748] | 350 | lock (InstanceRuns) {
|
---|
| 351 | InstanceRuns.Add(algorithmClone.Runs.Last());
|
---|
| 352 | }
|
---|
| 353 | } else {
|
---|
| 354 | lock (SeededRuns) {
|
---|
| 355 | SeededRuns.Add(algorithmClone.Runs.Last());
|
---|
| 356 | }
|
---|
| 357 | }
|
---|
[13713] | 358 | waitHandle.Set();
|
---|
[13649] | 359 | }
|
---|
[13713] | 360 | };
|
---|
[13649] | 361 |
|
---|
[13713] | 362 | EventHandler<EventArgs<Exception>> exceptionOccurred = (sender, e) => {
|
---|
| 363 | waitHandle.Set();
|
---|
| 364 | };
|
---|
[13649] | 365 |
|
---|
[13713] | 366 | EventHandler timeChanged = (sender, e) => {
|
---|
| 367 | IResult evalSolResult;
|
---|
| 368 | if (!algorithmClone.Results.TryGetValue("EvaluatedSolutions", out evalSolResult) || !(evalSolResult.Value is Data.IntValue)) return;
|
---|
| 369 | var evalSols = ((Data.IntValue)evalSolResult.Value).Value;
|
---|
[13722] | 370 | if (evalSols >= MaximumEvaluations.Value && algorithmClone.ExecutionState == ExecutionState.Started)
|
---|
[13713] | 371 | algorithmClone.Stop();
|
---|
| 372 | };
|
---|
| 373 | #endregion
|
---|
[13649] | 374 |
|
---|
[13713] | 375 | algorithmClone.ExecutionStateChanged += exeStateChanged;
|
---|
| 376 | algorithmClone.ExceptionOccurred += exceptionOccurred;
|
---|
| 377 | if (monitorStop) algorithmClone.ExecutionTimeChanged += timeChanged;
|
---|
[13649] | 378 |
|
---|
[13713] | 379 | return Task.Factory.StartNew(() => {
|
---|
| 380 | algorithmClone.Start();
|
---|
[13722] | 381 | OnAlgorithmInstanceStarted(algorithmClone);
|
---|
| 382 | var cancelRequested = false;
|
---|
| 383 | while (!waitHandle.WaitOne(200)) {
|
---|
| 384 | if (cancellation.IsCancellationRequested) {
|
---|
| 385 | cancelRequested = true;
|
---|
| 386 | break;
|
---|
| 387 | }
|
---|
| 388 | }
|
---|
| 389 | if (cancelRequested) {
|
---|
| 390 | try { algorithmClone.Stop(); } catch { } // ignore race condition if it is stopped in the meantime
|
---|
| 391 | waitHandle.WaitOne();
|
---|
| 392 | }
|
---|
[13713] | 393 | waitHandle.Dispose();
|
---|
[13722] | 394 | return algorithmClone.Results;
|
---|
| 395 | }, TaskCreationOptions.LongRunning);
|
---|
[13649] | 396 | }
|
---|
| 397 |
|
---|
[13722] | 398 | public ResultCollection StartAlgorithm(int index, CancellationToken cancellation) {
|
---|
| 399 | var task = StartAlgorithmAsync(index, cancellation);
|
---|
| 400 | task.Wait(cancellation);
|
---|
| 401 | return task.Result;
|
---|
[13649] | 402 | }
|
---|
| 403 |
|
---|
[13718] | 404 | public Task UpdateKnowledgeBaseAsync(IProgress progress = null) {
|
---|
[16728] | 405 | progress?.Start("Updating Knowledge Base from OKB");
|
---|
[13718] | 406 | OnDownloadStarted(progress);
|
---|
| 407 | return Task.Factory.StartNew(() => { DoUpdateKnowledgeBase(progress); }, TaskCreationOptions.LongRunning);
|
---|
[13485] | 408 | }
|
---|
| 409 |
|
---|
[13718] | 410 | public void UpdateKnowledgeBase(IProgress progress = null) {
|
---|
| 411 | UpdateKnowledgeBaseAsync(progress).Wait();
|
---|
[13485] | 412 | }
|
---|
| 413 |
|
---|
| 414 | private void DoUpdateKnowledgeBase(IProgress progress) {
|
---|
| 415 | var queryClient = Clients.OKB.Query.QueryClient.Instance;
|
---|
| 416 | var adminClient = Clients.OKB.Administration.AdministrationClient.Instance;
|
---|
| 417 | try {
|
---|
[16728] | 418 | if (progress != null) {
|
---|
| 419 | progress.Message = "Connecting to OKB...";
|
---|
| 420 | progress.ProgressValue = 0;
|
---|
| 421 | }
|
---|
[13551] | 422 | // FIXME: How to tell if refresh is necessary?
|
---|
[13759] | 423 | var refreshTasks = new[] {
|
---|
| 424 | Task.Factory.StartNew(() => queryClient.Refresh()),
|
---|
| 425 | Task.Factory.StartNew(() => adminClient.Refresh())
|
---|
| 426 | };
|
---|
| 427 | Task.WaitAll(refreshTasks);
|
---|
[13551] | 428 |
|
---|
| 429 | var probInstance = adminClient.Problems.SingleOrDefault(x => x.Id == Problem.ProblemId);
|
---|
| 430 | if (probInstance == null) throw new InvalidOperationException("The chosen problem instance cannot be found in the OKB.");
|
---|
| 431 | var probClassId = probInstance.ProblemClassId;
|
---|
| 432 |
|
---|
| 433 | var problemClassFilter = (Clients.OKB.Query.StringComparisonAvailableValuesFilter)queryClient.Filters.Single(x => x.Label == "Problem Class Name");
|
---|
| 434 | problemClassFilter.Value = adminClient.ProblemClasses.Single(x => x.Id == probClassId).Name;
|
---|
| 435 |
|
---|
[13757] | 436 | problemId2ProblemInstanceMapping.Clear();
|
---|
[16728] | 437 | if (progress != null) {
|
---|
| 438 | progress.Message = "Downloading algorithm and problem instances...";
|
---|
| 439 | progress.ProgressValue = 0;
|
---|
| 440 | }
|
---|
[13752] | 441 | int[] p = { 0 };
|
---|
[13551] | 442 | ProblemInstances.UpdateOfRunsInProgress = true;
|
---|
| 443 | ProblemInstances.Clear();
|
---|
| 444 | algorithmId2AlgorithmInstanceMapping.Clear();
|
---|
[13804] | 445 | algorithmId2RunMapping.Clear();
|
---|
| 446 | algorithmInstances.Clear();
|
---|
[13809] | 447 |
|
---|
| 448 | var characteristics = new HashSet<string>();
|
---|
| 449 | var totalProblems = adminClient.Problems.Count(x => x.ProblemClassId == probClassId);
|
---|
| 450 | var totalAlgorithms = adminClient.Algorithms.Count;
|
---|
| 451 | var problems = adminClient.Problems.Where(x => x.ProblemClassId == probClassId);
|
---|
| 452 | var algorithms = adminClient.Algorithms;
|
---|
| 453 | var combined = problems.Cast<object>().Concat(algorithms.Cast<object>()).Shuffle(new MersenneTwister());
|
---|
| 454 | Parallel.ForEach(combined, new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }, (inst) => {
|
---|
| 455 | var pInst = inst as Clients.OKB.Administration.Problem;
|
---|
| 456 | if (pInst != null) DownloadProblemInstance(progress, pInst, p, totalProblems + totalAlgorithms, characteristics);
|
---|
| 457 | else {
|
---|
| 458 | var aInst = inst as Clients.OKB.Administration.Algorithm;
|
---|
| 459 | DownloadAlgorithmInstance(progress, aInst, p, totalProblems + totalAlgorithms);
|
---|
[13551] | 460 | }
|
---|
[13752] | 461 | });
|
---|
[13551] | 462 |
|
---|
[13485] | 463 | var interestingValues = queryClient.ValueNames.Where(x => InterestingValueNames.Contains(x.Name)).ToList();
|
---|
| 464 |
|
---|
[16728] | 465 | if (progress != null) {
|
---|
| 466 | progress.Message = "Downloading runs...";
|
---|
| 467 | progress.ProgressValue = 0;
|
---|
| 468 | }
|
---|
[13752] | 469 | p[0] = 0;
|
---|
[13551] | 470 | var count = queryClient.GetNumberOfRuns(problemClassFilter);
|
---|
[13485] | 471 | if (count == 0) return;
|
---|
[13649] | 472 |
|
---|
[13752] | 473 | var runList = new List<IRun>();
|
---|
[13809] | 474 | var runIds = LoadRunsFromCache(queryClient.GetRunIds(problemClassFilter), runList, progress);
|
---|
[13752] | 475 | var batches = runIds.Select((v, i) => new { Idx = i, Val = v }).GroupBy(x => x.Idx / 500, x => x.Val);
|
---|
[13809] | 476 | Parallel.ForEach(batches.Select(x => x.ToList()), new ParallelOptions { MaxDegreeOfParallelism = Math.Min(Environment.ProcessorCount, 4) },
|
---|
| 477 | (batch) => {
|
---|
[13752] | 478 | var okbRuns = queryClient.GetRunsWithValues(batch, true, interestingValues);
|
---|
[13809] | 479 | var hlRuns = okbRuns.AsParallel().Select(x => new { AlgorithmId = x.Algorithm.Id, RunId = x.Id, Run = queryClient.ConvertToOptimizationRun(x) }).ToList();
|
---|
[13752] | 480 | lock (runList) {
|
---|
[13809] | 481 | var toCache = new List<Tuple<long, long, IRun>>();
|
---|
[13752] | 482 | foreach (var r in hlRuns) {
|
---|
| 483 | algorithmId2RunMapping.Add(r.AlgorithmId, r.Run);
|
---|
| 484 | runList.Add(r.Run);
|
---|
[13809] | 485 | toCache.Add(Tuple.Create(r.AlgorithmId, r.RunId, r.Run));
|
---|
[13485] | 486 | }
|
---|
[13809] | 487 | SaveToCache(toCache);
|
---|
[13752] | 488 | p[0] += batch.Count;
|
---|
[16728] | 489 | if (progress != null) {
|
---|
| 490 | progress.Message = string.Format("Downloaded runs {0} to {1} of {2}...", p[0], p[0] + batch.Count, count);
|
---|
| 491 | progress.ProgressValue = p[0] / (double)count;
|
---|
| 492 | }
|
---|
[13752] | 493 | }
|
---|
| 494 | });
|
---|
[16728] | 495 | if (progress != null) {
|
---|
| 496 | progress.Message = "Finishing...";
|
---|
| 497 | }
|
---|
[13804] | 498 | // remove algorithm instances that do not appear in any downloaded run
|
---|
| 499 | for (var algIdx = 0; algIdx < algorithmInstances.Count; algIdx++) {
|
---|
| 500 | var id = algorithmId2AlgorithmInstanceMapping.GetBySecond(algorithmInstances[algIdx]);
|
---|
| 501 | if (!algorithmId2RunMapping.ContainsFirst(id)) {
|
---|
| 502 | algorithmId2AlgorithmInstanceMapping.RemoveByFirst(id);
|
---|
| 503 | algorithmInstances.RemoveAt(algIdx);
|
---|
| 504 | algIdx--;
|
---|
| 505 | }
|
---|
| 506 | }
|
---|
[13787] | 507 |
|
---|
| 508 | try {
|
---|
| 509 | KnowledgeBase.UpdateOfRunsInProgress = true;
|
---|
| 510 | KnowledgeBase.Clear();
|
---|
| 511 | KnowledgeBase.AddRange(runList);
|
---|
| 512 | } finally { KnowledgeBase.UpdateOfRunsInProgress = false; }
|
---|
| 513 |
|
---|
[13551] | 514 | var algInstRunDict = runList.Where(x => x.Parameters.ContainsKey("Problem Name") && x.Parameters["Problem Name"] is StringValue)
|
---|
| 515 | .GroupBy(x => ((StringValue)x.Parameters["Problem Name"]).Value)
|
---|
| 516 | .ToDictionary(x => x.Key, x => x.GroupBy(y => ((StringValue)y.Parameters["Algorithm Name"]).Value)
|
---|
| 517 | .ToDictionary(y => y.Key, y => y.ToList()));
|
---|
| 518 |
|
---|
[13787] | 519 | // set best-known quality to best-found in case it is not known
|
---|
| 520 | foreach (var kvp in algInstRunDict) {
|
---|
| 521 | var prob = ProblemInstances.SingleOrDefault(x => ((StringValue)x.Parameters["Problem Name"]).Value == kvp.Key);
|
---|
| 522 | if (prob == null) continue;
|
---|
| 523 | var maximization = ((BoolValue)prob.Parameters["Maximization"]).Value;
|
---|
[13551] | 524 |
|
---|
[13649] | 525 | IItem bkParam;
|
---|
[15736] | 526 | if (!prob.Parameters.TryGetValue("BestKnownQuality", out bkParam) || !(bkParam is DoubleValue) || double.IsNaN(((DoubleValue)bkParam).Value)) {
|
---|
[13787] | 527 | var list = kvp.Value.SelectMany(x => x.Value)
|
---|
[15736] | 528 | .Where(x => x.Results.ContainsKey("QualityPerClock"))
|
---|
| 529 | .Select(x => (IndexedDataTable<double>)x.Results["QualityPerClock"])
|
---|
| 530 | .Where(x => x.Rows.Count > 0 && x.Rows.First().Values.Count > 0)
|
---|
| 531 | .Select(x => x.Rows.First().Values.Last().Item2);
|
---|
[13787] | 532 | if (!list.Any()) continue;
|
---|
[13649] | 533 | bkParam = new DoubleValue(maximization ? list.Max() : list.Min());
|
---|
[13787] | 534 | prob.Parameters["BestKnownQuality"] = bkParam;
|
---|
| 535 | }
|
---|
| 536 | }
|
---|
[13551] | 537 |
|
---|
[13787] | 538 | // add algorithm instance ranks as features to the problem instances for a range of targets
|
---|
[13797] | 539 | foreach (var target in new[] {0, 0.01, 0.05, 0.1, 0.2, 0.5}) {
|
---|
[13787] | 540 | var cls = GetPerformanceClasses(target, 5);
|
---|
| 541 | foreach (var kvp in cls) {
|
---|
| 542 | var prob = kvp.Key;
|
---|
| 543 | foreach (var kvp2 in kvp.Value) {
|
---|
[13797] | 544 | var resultName = "Rank." + algorithmId2AlgorithmInstanceMapping.GetByFirst(kvp2.Key) + "@" + (target * 100) + "%";
|
---|
[13787] | 545 | prob.Results[resultName] = new IntValue(kvp2.Value);
|
---|
[13485] | 546 | }
|
---|
| 547 | }
|
---|
| 548 | }
|
---|
[16728] | 549 | } finally { progress?.Finish(); ProblemInstances.UpdateOfRunsInProgress = false; }
|
---|
[13787] | 550 | UpdateInstanceProjection(ProblemInstances.ResultNames.Where(x => x.StartsWith("Characteristic.")).ToArray());
|
---|
[13485] | 551 | }
|
---|
| 552 |
|
---|
[13809] | 553 | private void DownloadAlgorithmInstance(IProgress progress, Algorithm algInst, int[] p, int total) {
|
---|
| 554 | IAlgorithm alg = null;
|
---|
| 555 | var data = Clients.OKB.Administration.AdministrationClient.GetAlgorithmData(algInst.Id);
|
---|
| 556 | if (data != null) {
|
---|
| 557 | using (var stream = new MemoryStream(data)) {
|
---|
| 558 | try {
|
---|
| 559 | alg = (IAlgorithm)XmlParser.Deserialize<IContent>(stream);
|
---|
| 560 | } catch { }
|
---|
| 561 | stream.Close();
|
---|
| 562 | }
|
---|
| 563 | if (alg != null) {
|
---|
[16728] | 564 | lock (this) {
|
---|
[13809] | 565 | algorithmInstances.Add(alg);
|
---|
| 566 | algorithmId2AlgorithmInstanceMapping.Add(algInst.Id, alg);
|
---|
| 567 | p[0]++;
|
---|
[16728] | 568 | if (progress != null) {
|
---|
| 569 | progress.Message = string.Format("Downloaded algorithm {0} (okb-id: {1})...", algInst.Name, algInst.Id);
|
---|
| 570 | progress.ProgressValue = p[0] / (double)total;
|
---|
| 571 | }
|
---|
[13809] | 572 | }
|
---|
| 573 | }
|
---|
| 574 | }
|
---|
| 575 | }
|
---|
| 576 |
|
---|
| 577 | private void DownloadProblemInstance(IProgress progress, Problem pInst, int[] p, int totalProblems, HashSet<string> characteristics) {
|
---|
| 578 | var charas = new List<string>();
|
---|
| 579 | IRun probRun = null;
|
---|
| 580 | var data = Clients.OKB.Administration.AdministrationClient.GetProblemData(pInst.Id);
|
---|
| 581 | if (data != null) {
|
---|
| 582 | using (var stream = new MemoryStream(data)) {
|
---|
| 583 | try {
|
---|
| 584 | var prob = (IProblem)XmlParser.Deserialize<IContent>(stream);
|
---|
| 585 | probRun = new Run() {Name = prob.Name};
|
---|
| 586 | prob.CollectParameterValues(probRun.Parameters);
|
---|
| 587 | probRun.Parameters["Problem Name"] = new StringValue(prob.Name);
|
---|
| 588 | probRun.Parameters["Problem Type"] = new StringValue(prob.GetType().Name);
|
---|
| 589 | foreach (var v in RunCreationClient.Instance.GetCharacteristicValues(pInst.Id)) {
|
---|
| 590 | probRun.Results.Add("Characteristic." + v.Name, RunCreationClient.Instance.ConvertToItem(v));
|
---|
| 591 | charas.Add("Characteristic." + v.Name);
|
---|
| 592 | }
|
---|
| 593 | } catch { }
|
---|
| 594 | stream.Close();
|
---|
| 595 | }
|
---|
| 596 | if (probRun != null) {
|
---|
[16728] | 597 | lock (this) {
|
---|
[13809] | 598 | problemId2ProblemInstanceMapping.Add(pInst.Id, probRun);
|
---|
| 599 | ProblemInstances.Add(probRun);
|
---|
| 600 | p[0]++;
|
---|
[16728] | 601 |
|
---|
| 602 | if (progress != null) {
|
---|
| 603 | progress.Message = string.Format("Downloaded problem {0} (okb-id: {1})....", pInst.Name, pInst.Id);
|
---|
| 604 | progress.ProgressValue = p[0] / (double)totalProblems;
|
---|
| 605 | }
|
---|
[13809] | 606 | foreach (var c in charas) characteristics.Add(c);
|
---|
| 607 | }
|
---|
| 608 | }
|
---|
| 609 | }
|
---|
| 610 | }
|
---|
| 611 |
|
---|
| 612 | private List<long> LoadRunsFromCache(IEnumerable<long> runIds, List<IRun> runList, IProgress progress) {
|
---|
| 613 | var hashSet = new HashSet<long>(runIds);
|
---|
| 614 | var total = hashSet.Count;
|
---|
| 615 | try {
|
---|
| 616 | var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "HeuristicLab.OKB", "cache", "runs");
|
---|
| 617 | Parallel.ForEach(Directory.EnumerateDirectories(path).Select((d, i) => new { Index = i, Directory = d }).GroupBy(x => x.Index / 100), new ParallelOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount },
|
---|
| 618 | (folderGroup) => {
|
---|
| 619 | var localRunList = new List<Tuple<long, long, IRun>>();
|
---|
| 620 | foreach (var runPath in folderGroup.Select(x => x.Directory)) {
|
---|
| 621 | long runId;
|
---|
| 622 | var runFolder = new DirectoryInfo(runPath).Name;
|
---|
| 623 | if (!long.TryParse(runFolder, out runId) || !hashSet.Contains(runId)) continue;
|
---|
| 624 | var runFilePath = Directory.EnumerateFiles(runPath).Single();
|
---|
| 625 | var runFileName = Path.GetFileNameWithoutExtension(runFilePath);
|
---|
| 626 | long algId;
|
---|
| 627 | if (!long.TryParse(runFileName, out algId)) continue;
|
---|
| 628 | IRun run = null;
|
---|
| 629 | try {
|
---|
| 630 | using (var file = File.OpenRead(runFilePath))
|
---|
| 631 | run = XmlParser.Deserialize<IRun>(file);
|
---|
| 632 | } catch {
|
---|
| 633 | File.Delete(runFilePath);
|
---|
| 634 | Directory.Delete(runPath);
|
---|
| 635 | }
|
---|
| 636 | if (run != null) localRunList.Add(Tuple.Create(algId, runId, run));
|
---|
| 637 | }
|
---|
| 638 | lock (runList) {
|
---|
| 639 | foreach (var r in localRunList) {
|
---|
| 640 | hashSet.Remove(r.Item2);
|
---|
| 641 | algorithmId2RunMapping.Add(r.Item1, r.Item3);
|
---|
| 642 | runList.Add(r.Item3);
|
---|
| 643 | }
|
---|
[16728] | 644 | if (progress != null) {
|
---|
| 645 | progress.Message = string.Format("Retrieved {0} of {1} from cache", runList.Count, total);
|
---|
| 646 | progress.ProgressValue = (double)runList.Count / total;
|
---|
| 647 | }
|
---|
[13809] | 648 | }
|
---|
| 649 | });
|
---|
| 650 | } catch { }
|
---|
| 651 | return hashSet.ToList();
|
---|
| 652 | }
|
---|
| 653 |
|
---|
| 654 | private void SaveToCache(IEnumerable<Tuple<long, long, IRun>> runs) {
|
---|
| 655 | try {
|
---|
| 656 | var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "HeuristicLab.OKB", "cache", "runs");
|
---|
| 657 | if (!Directory.Exists(path)) Directory.CreateDirectory(path);
|
---|
| 658 | foreach (var r in runs) {
|
---|
| 659 | var runPath = Path.Combine(path, r.Item2.ToString());
|
---|
| 660 | if (!Directory.Exists(runPath)) Directory.CreateDirectory(runPath);
|
---|
| 661 | using (var file = File.Open(Path.Combine(runPath, r.Item1.ToString()), FileMode.Create, FileAccess.ReadWrite)) {
|
---|
| 662 | XmlGenerator.Serialize(r.Item3, file);
|
---|
| 663 | }
|
---|
| 664 | }
|
---|
| 665 | } catch { }
|
---|
| 666 | }
|
---|
| 667 |
|
---|
[13791] | 668 | public static double[][] GetFeatures(IRun[] problemInstances, string[] characteristics, double[] medianValues = null) {
|
---|
| 669 | var instances = new double[problemInstances.Length][];
|
---|
| 670 | for (var p = 0; p < problemInstances.Length; p++) {
|
---|
| 671 | instances[p] = new double[characteristics.Length];
|
---|
| 672 | for (var f = 0; f < characteristics.Length; f++) {
|
---|
| 673 | IItem item;
|
---|
| 674 | if (problemInstances[p].Results.TryGetValue(characteristics[f], out item)) {
|
---|
| 675 | double val = 0;
|
---|
| 676 | var dItem = item as DoubleValue;
|
---|
| 677 | if (dItem != null) {
|
---|
| 678 | val = dItem.Value;
|
---|
| 679 | } else {
|
---|
| 680 | var iItem = item as IntValue;
|
---|
| 681 | if (iItem != null) val = iItem.Value;
|
---|
| 682 | else val = double.NaN;
|
---|
| 683 | }
|
---|
| 684 | if (double.IsNaN(val) && medianValues != null)
|
---|
| 685 | instances[p][f] = medianValues[f];
|
---|
| 686 | else instances[p][f] = val;
|
---|
| 687 | } else instances[p][f] = medianValues != null ? medianValues[f] : double.NaN;
|
---|
| 688 | }
|
---|
| 689 | }
|
---|
| 690 | return instances;
|
---|
| 691 | }
|
---|
| 692 |
|
---|
[13878] | 693 | public static double[][] GetFeaturesStandardized(IRun[] problemInstances, string[] characteristics, out double[] means, out double[] sdevs, double[] medianValues = null) {
|
---|
| 694 | var instances = new double[problemInstances.Length][];
|
---|
| 695 | var columns = new List<double>[characteristics.Length];
|
---|
| 696 | for (var p = 0; p < problemInstances.Length; p++) {
|
---|
| 697 | instances[p] = new double[characteristics.Length];
|
---|
| 698 | for (var f = 0; f < characteristics.Length; f++) {
|
---|
| 699 | if (columns[f] == null) {
|
---|
| 700 | columns[f] = new List<double>(problemInstances.Length);
|
---|
| 701 | }
|
---|
| 702 | IItem item;
|
---|
| 703 | if (problemInstances[p].Results.TryGetValue(characteristics[f], out item)) {
|
---|
| 704 | double val = 0;
|
---|
| 705 | var dItem = item as DoubleValue;
|
---|
| 706 | if (dItem != null) {
|
---|
| 707 | val = dItem.Value;
|
---|
| 708 | } else {
|
---|
| 709 | var iItem = item as IntValue;
|
---|
| 710 | if (iItem != null) val = iItem.Value;
|
---|
| 711 | else val = double.NaN;
|
---|
| 712 | }
|
---|
| 713 | if (double.IsNaN(val) && medianValues != null)
|
---|
| 714 | instances[p][f] = medianValues[f];
|
---|
| 715 | else instances[p][f] = val;
|
---|
| 716 | columns[f].Add(instances[p][f]);
|
---|
| 717 | } else instances[p][f] = medianValues != null ? medianValues[f] : double.NaN;
|
---|
| 718 | }
|
---|
| 719 | }
|
---|
| 720 |
|
---|
| 721 | means = new double[characteristics.Length];
|
---|
| 722 | sdevs = new double[characteristics.Length];
|
---|
| 723 | for (var f = 0; f < characteristics.Length; f++) {
|
---|
| 724 | var mean = columns[f].Average();
|
---|
| 725 | var dev = columns[f].StandardDeviation();
|
---|
| 726 | means[f] = mean;
|
---|
| 727 | sdevs[f] = dev;
|
---|
| 728 | for (var p = 0; p < problemInstances.Length; p++) {
|
---|
| 729 | if (dev.IsAlmost(0)) instances[p][f] = 0;
|
---|
| 730 | else instances[p][f] = (instances[p][f] - mean) / dev;
|
---|
| 731 | }
|
---|
| 732 | }
|
---|
| 733 |
|
---|
| 734 | return instances;
|
---|
| 735 | }
|
---|
| 736 |
|
---|
[13791] | 737 | public static double[] GetMedianValues(IRun[] problemInstances, string[] characteristics) {
|
---|
| 738 | var values = new List<double>[characteristics.Length];
|
---|
| 739 | foreach (var problemInstance in problemInstances) {
|
---|
| 740 | for (var f = 0; f < characteristics.Length; f++) {
|
---|
| 741 | if (values[f] == null) values[f] = new List<double>(problemInstances.Length);
|
---|
| 742 | IItem item;
|
---|
| 743 | if (problemInstance.Results.TryGetValue(characteristics[f], out item)) {
|
---|
| 744 | var dItem = item as DoubleValue;
|
---|
| 745 | if (dItem != null) values[f].Add(dItem.Value);
|
---|
| 746 | else {
|
---|
| 747 | var iItem = item as IntValue;
|
---|
| 748 | if (iItem != null) values[f].Add(iItem.Value);
|
---|
| 749 | }
|
---|
| 750 | }
|
---|
| 751 | }
|
---|
| 752 | }
|
---|
| 753 | return values.Select(x => x.Count == 0 ? 0.0 : x.Median()).ToArray();
|
---|
| 754 | }
|
---|
| 755 |
|
---|
| 756 | public Dictionary<IRun, double[]> GetProblemCharacteristics(string[] characteristics) {
|
---|
| 757 | var map = ProblemInstances.Select((v, i) => new { Index = i, ProblemInstance = v }).ToDictionary(x => x.Index, x => x.ProblemInstance);
|
---|
| 758 | var instances = GetFeatures(ProblemInstances.ToArray(), characteristics);
|
---|
| 759 | var median = GetMedianValues(ProblemInstances.ToArray(), characteristics);
|
---|
| 760 |
|
---|
| 761 | var allValues = instances.Select(x => x.Select((f, i) => new { Idx = i, Val = double.IsNaN(f) ? median[i] : f }).ToList())
|
---|
| 762 | .SelectMany(x => x)
|
---|
| 763 | .GroupBy(x => x.Idx, x => x.Val)
|
---|
| 764 | .OrderBy(x => x.Key).ToList();
|
---|
| 765 | var avg = allValues.Select(x => x.Average()).ToList();
|
---|
| 766 | var stdev = allValues.Select(x => x.StandardDeviation()).ToList();
|
---|
| 767 |
|
---|
| 768 | // normalize characteristic values by transforming them to their z-score
|
---|
| 769 | foreach (var features in instances) {
|
---|
| 770 | for (var i = 0; i < features.Length; i++) {
|
---|
| 771 | if (double.IsNaN(features[i])) features[i] = median[i];
|
---|
| 772 | if (stdev[i] > 0) features[i] = (features[i] - avg[i]) / stdev[i];
|
---|
| 773 | }
|
---|
| 774 | }
|
---|
| 775 | return instances.Select((v, i) => new { ProblemInstance = map[i], Features = v }).ToDictionary(x => x.ProblemInstance, x => x.Features);
|
---|
| 776 | }
|
---|
| 777 |
|
---|
[13774] | 778 | public Dictionary<IAlgorithm, double> GetAlgorithmPerformance(IRun problemInstance) {
|
---|
| 779 | if (!problemInstance.Parameters.ContainsKey("BestKnownQuality")) return new Dictionary<IAlgorithm, double>();
|
---|
[13797] | 780 | var target = GetTarget(((DoubleValue)problemInstance.Parameters["BestKnownQuality"]).Value, MinimumTarget.Value, Maximization);
|
---|
[13774] | 781 | return knowledgeBase.Where(x => ((StringValue)x.Parameters["Problem Name"]).Value == ((StringValue)problemInstance.Parameters["Problem Name"]).Value)
|
---|
| 782 | .GroupBy(x => algorithmId2AlgorithmInstanceMapping.GetByFirst(algorithmId2RunMapping.GetBySecond(x).Single()))
|
---|
[15736] | 783 | .ToDictionary(x => x.Key, x => ExpectedRuntimeHelper.CalculateErt(x.ToList(), "QualityPerClock", target, Maximization).ExpectedRuntime);
|
---|
[13774] | 784 | }
|
---|
| 785 |
|
---|
[13878] | 786 | public Dictionary<IAlgorithm, double> GetAlgorithmPerformanceLog10(IRun problemInstance) {
|
---|
| 787 | if (!problemInstance.Parameters.ContainsKey("BestKnownQuality")) return new Dictionary<IAlgorithm, double>();
|
---|
| 788 | var target = GetTarget(((DoubleValue)problemInstance.Parameters["BestKnownQuality"]).Value, MinimumTarget.Value, Maximization);
|
---|
| 789 | return knowledgeBase.Where(x => ((StringValue)x.Parameters["Problem Name"]).Value == ((StringValue)problemInstance.Parameters["Problem Name"]).Value)
|
---|
| 790 | .GroupBy(x => algorithmId2AlgorithmInstanceMapping.GetByFirst(algorithmId2RunMapping.GetBySecond(x).Single()))
|
---|
[15736] | 791 | .ToDictionary(x => x.Key, x => Math.Log10(ExpectedRuntimeHelper.CalculateErt(x.ToList(), "QualityPerClock", target, Maximization).ExpectedRuntime));
|
---|
[13878] | 792 | }
|
---|
| 793 |
|
---|
[13791] | 794 | public Dictionary<IAlgorithm, List<IRun>> GetAlgorithmRuns(IRun problemInstance) {
|
---|
| 795 | return knowledgeBase.Where(x => ((StringValue)x.Parameters["Problem Name"]).Value == ((StringValue)problemInstance.Parameters["Problem Name"]).Value)
|
---|
| 796 | .GroupBy(x => algorithmId2AlgorithmInstanceMapping.GetByFirst(algorithmId2RunMapping.GetBySecond(x).Single()))
|
---|
| 797 | .ToDictionary(x => x.Key, x => x.ToList());
|
---|
| 798 | }
|
---|
| 799 |
|
---|
[13774] | 800 | public Dictionary<IAlgorithm, List<IRun>> GetKnowledgeBaseByAlgorithm() {
|
---|
| 801 | return KnowledgeBase.GroupBy(x => algorithmId2AlgorithmInstanceMapping.GetByFirst(algorithmId2RunMapping.GetBySecond(x).Single()))
|
---|
| 802 | .ToDictionary(x => x.Key, x => x.ToList());
|
---|
| 803 | }
|
---|
| 804 |
|
---|
[13787] | 805 | public IEnumerable<IRegressionProblem> GetRegressionProblemPerAlgorithmInstance(double target, string[] characteristics) {
|
---|
[13774] | 806 | if (Problem == null) yield break;
|
---|
[13787] | 807 | var features = GetProblemCharacteristics(characteristics);
|
---|
[13774] | 808 | // TODO: knowledgebase only stores problem name as a string
|
---|
| 809 | // this doesn't work if there are two equally named problem instances
|
---|
| 810 | var problemMap = ProblemInstances.Select(x => new { Key = ((StringValue)x.Parameters["Problem Name"]).Value, Value = x })
|
---|
| 811 | .ToDictionary(x => x.Key, x => x.Value);
|
---|
[13649] | 812 | foreach (var relevantRuns in knowledgeBase.GroupBy(x => algorithmId2RunMapping.GetBySecond(x).Single())) {
|
---|
[13774] | 813 | var problemRuns = relevantRuns.GroupBy(x => ((StringValue)x.Parameters["Problem Name"]).Value).ToList();
|
---|
| 814 | var ds = new ModifiableDataset();
|
---|
| 815 | ds.AddVariable("Problem Name", new List<string>());
|
---|
[13787] | 816 | foreach (var pc in characteristics)
|
---|
| 817 | ds.AddVariable(pc, new List<double>());
|
---|
[13774] | 818 | ds.AddVariable("ERT", new List<double>());
|
---|
| 819 | foreach (var pr in problemRuns) {
|
---|
| 820 | var prob = problemMap[pr.Key];
|
---|
[13787] | 821 | var f = features[prob];
|
---|
| 822 | var max = ((BoolValue)prob.Parameters["Maximization"]).Value;
|
---|
[13774] | 823 | var bkq = ((DoubleValue)prob.Parameters["BestKnownQuality"]).Value;
|
---|
[15736] | 824 | var ert = ExpectedRuntimeHelper.CalculateErt(pr.ToList(), "QualityPerClock", GetTarget(bkq, target, max), max).ExpectedRuntime;
|
---|
[13803] | 825 | if (double.IsInfinity(ert)) ert = int.MaxValue;
|
---|
[13787] | 826 | ds.AddRow(new object[] { pr.Key }.Concat(f.Cast<object>()).Concat(new object[] { ert }));
|
---|
[12860] | 827 | }
|
---|
[13787] | 828 | var datAnalysisData = new RegressionProblemData(ds, characteristics, "ERT");
|
---|
[13774] | 829 | var result = new RegressionProblem() {
|
---|
| 830 | Name = algorithmId2AlgorithmInstanceMapping.GetByFirst(relevantRuns.Key).Name
|
---|
| 831 | };
|
---|
| 832 | result.ProblemDataParameter.Value = datAnalysisData;
|
---|
| 833 | yield return result;
|
---|
[12860] | 834 | }
|
---|
[13774] | 835 | }
|
---|
[12842] | 836 |
|
---|
[13787] | 837 | public IEnumerable<IClassificationProblem> GetClassificationProblemPerAlgorithmInstance(double target, string[] characteristics) {
|
---|
| 838 | if (Problem == null) yield break;
|
---|
| 839 |
|
---|
| 840 | var classes = GetPerformanceClasses(target, 5);
|
---|
| 841 | var features = GetProblemCharacteristics(characteristics);
|
---|
| 842 |
|
---|
| 843 | foreach (var alg in AlgorithmInstances) {
|
---|
| 844 | var ds = new ModifiableDataset();
|
---|
| 845 | ds.AddVariable("Problem Name", new List<string>());
|
---|
| 846 | foreach (var pc in characteristics)
|
---|
| 847 | ds.AddVariable(pc, new List<double>());
|
---|
| 848 | ds.AddVariable("Class", new List<double>());
|
---|
| 849 |
|
---|
| 850 | foreach (var c in classes) {
|
---|
| 851 | int cls;
|
---|
| 852 | if (c.Value.TryGetValue(algorithmId2AlgorithmInstanceMapping.GetBySecond(alg), out cls)) {
|
---|
| 853 | ds.AddRow(new object[] { ((StringValue)c.Key.Parameters["Problem Name"]).Value }
|
---|
| 854 | .Concat(features[c.Key].Cast<object>()).Concat(new object[] { cls }));
|
---|
| 855 | }
|
---|
| 856 | }
|
---|
| 857 | var datAnalysisData = new ClassificationProblemData(ds, characteristics, "Class");
|
---|
| 858 | var result = new ClassificationProblem() {
|
---|
| 859 | Name = alg.Name
|
---|
| 860 | };
|
---|
| 861 | result.ProblemDataParameter.Value = datAnalysisData;
|
---|
| 862 | yield return result;
|
---|
| 863 | }
|
---|
[12842] | 864 | }
|
---|
| 865 |
|
---|
[13787] | 866 | public Dictionary<IRun, double> GetProblemDistances(string[] characteristics) {
|
---|
| 867 | var result = new Dictionary<IRun, double>();
|
---|
[13759] | 868 | var currentInstance = problemId2ProblemInstanceMapping.GetByFirst(Problem.ProblemId);
|
---|
[13787] | 869 | var features = GetProblemCharacteristics(characteristics);
|
---|
| 870 | var cF = features[currentInstance];
|
---|
| 871 | foreach (var b in ProblemInstances) {
|
---|
| 872 | if (b == currentInstance) continue;
|
---|
| 873 | var sum = features[b].Select((t, f) => (cF[f] - t) * (cF[f] - t)).Sum();
|
---|
| 874 | result[b] = Math.Sqrt(sum);
|
---|
[13757] | 875 | }
|
---|
[13759] | 876 | return result;
|
---|
[13757] | 877 | }
|
---|
| 878 |
|
---|
[13787] | 879 | public Dictionary<IRun, Dictionary<long, int>> GetPerformanceClasses(double target, int nClasses) {
|
---|
| 880 | var result = new Dictionary<IRun, Dictionary<long, int>>();
|
---|
| 881 | var problemMap = ProblemInstances.Select(x => new { Key = ((StringValue)x.Parameters["Problem Name"]).Value, Value = x })
|
---|
| 882 | .ToDictionary(x => x.Key, x => x.Value);
|
---|
| 883 | foreach (var pr in KnowledgeBase.GroupBy(x => ((StringValue)x.Parameters["Problem Name"]).Value).ToList()) {
|
---|
| 884 | var bkq = ((DoubleValue)problemMap[pr.Key].Parameters["BestKnownQuality"]).Value;
|
---|
| 885 | var max = ((BoolValue)problemMap[pr.Key].Parameters["Maximization"]).Value;
|
---|
| 886 |
|
---|
| 887 | result[problemMap[pr.Key]] = new Dictionary<long, int>();
|
---|
| 888 |
|
---|
[13794] | 889 | var values = pr.GroupBy(x => algorithmId2RunMapping.GetBySecond(x).Single())
|
---|
[15736] | 890 | .ToDictionary(x => x.Key, x => Math.Log10(ExpectedRuntimeHelper.CalculateErt(x.ToList(), "QualityPerClock", GetTarget(bkq, target, max), max).ExpectedRuntime));
|
---|
[13803] | 891 | var ranks = ClusteringHelper<long>.Cluster(nClasses, values, x => double.IsInfinity(x.Value))
|
---|
[13794] | 892 | .GetByCluster().ToList();
|
---|
| 893 | foreach (var c in ranks) {
|
---|
| 894 | foreach (var a in c.Value)
|
---|
[13797] | 895 | result[problemMap[pr.Key]][a.Key] = c.Key;
|
---|
[13787] | 896 | }
|
---|
[13759] | 897 | }
|
---|
[13787] | 898 | return result;
|
---|
[13757] | 899 | }
|
---|
| 900 |
|
---|
[13797] | 901 | public double GetTarget(double bestKnownQuality, double target, bool maximization) {
|
---|
| 902 | return bestKnownQuality * (maximization ? (1 - target) : (1 + target));
|
---|
[13787] | 903 | }
|
---|
| 904 |
|
---|
[13718] | 905 | public event EventHandler<EventArgs<IProgress>> DownloadStarted;
|
---|
| 906 | private void OnDownloadStarted(IProgress progress) {
|
---|
| 907 | var handler = DownloadStarted;
|
---|
| 908 | if (handler != null) handler(this, new EventArgs<IProgress>(progress));
|
---|
| 909 | }
|
---|
[13722] | 910 |
|
---|
| 911 | public event EventHandler<EventArgs<IAlgorithm>> AlgorithmInstanceStarted;
|
---|
| 912 | private void OnAlgorithmInstanceStarted(IAlgorithm instance) {
|
---|
| 913 | var handler = AlgorithmInstanceStarted;
|
---|
| 914 | if (handler != null) handler(this, new EventArgs<IAlgorithm>(instance));
|
---|
| 915 | }
|
---|
[13757] | 916 |
|
---|
[13787] | 917 | public event EventHandler RecommendationModelChanged;
|
---|
| 918 | private void OnRecommenderModelChanged() {
|
---|
| 919 | var handler = RecommendationModelChanged;
|
---|
| 920 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 921 | }
|
---|
| 922 |
|
---|
[13794] | 923 | public IEnumerable<KeyValuePair<IAlgorithm, double>> GetAlgorithmInstanceRanking() {
|
---|
[13791] | 924 | return RecommendationModel.GetRanking(ProblemInstances.Single(IsCurrentInstance));
|
---|
[13787] | 925 | }
|
---|
[12842] | 926 | }
|
---|
| 927 | }
|
---|