Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2457_ExpertSystem/HeuristicLab.OptimizationExpertSystem.Common/3.3/KnowledgeCenter.cs @ 17457

Last change on this file since 17457 was 17175, checked in by abeham, 6 years ago

#2457: branched integer encoding, some changes

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