Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17456 was 17175, checked in by abeham, 5 years ago

#2457: branched integer encoding, some changes

File size: 47.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Drawing;
25using System.IO;
26using System.IO.Compression;
27using System.Linq;
28using System.Threading;
29using System.Threading.Tasks;
30using HEAL.Attic;
31using HeuristicLab.Algorithms.DataAnalysis;
32using HeuristicLab.Analysis;
33using HeuristicLab.Analysis.SelfOrganizingMaps;
34using HeuristicLab.Collections;
35using HeuristicLab.Common;
36using HeuristicLab.Common.Resources;
37using HeuristicLab.Core;
38using HeuristicLab.Data;
39using HeuristicLab.MainForm;
40using HeuristicLab.Optimization;
41using HeuristicLab.Persistence.Default.Xml;
42using HeuristicLab.Problems.DataAnalysis;
43using HeuristicLab.Random;
44using Algorithm = HeuristicLab.Clients.OKB.Administration.Algorithm;
45using Problem = HeuristicLab.Clients.OKB.Administration.Problem;
46using RunCreationClient = HeuristicLab.Clients.OKB.RunCreation.RunCreationClient;
47using SingleObjectiveOKBProblem = HeuristicLab.Clients.OKB.RunCreation.SingleObjectiveOKBProblem;
48using SingleObjectiveOKBSolution = HeuristicLab.Clients.OKB.RunCreation.SingleObjectiveOKBSolution;
49
50namespace HeuristicLab.OptimizationExpertSystem.Common {
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.")]
52  [Creatable(CreatableAttribute.Categories.TestingAndAnalysis, Priority = 119)]
53  public sealed class KnowledgeCenter : IContent {
54    private bool SuppressEvents { get; set; }
55
56    public string Filename { get; set; }
57
58    public static Image StaticItemImage {
59      get { return VSImageLibrary.Library; }
60    }
61
62    private readonly IntValue maximumEvaluations;
63    public IntValue MaximumEvaluations {
64      get { return maximumEvaluations; }
65    }
66
67    private readonly DoubleValue minimumTarget;
68    public DoubleValue MinimumTarget {
69      get { return minimumTarget; }
70    }
71   
72    private readonly RunCollection instanceRuns;
73    public RunCollection InstanceRuns {
74      get { return instanceRuns; }
75    }
76
77    private readonly RunCollection seededRuns;
78    public RunCollection SeededRuns {
79      get { return seededRuns; }
80    }
81
82    private readonly RunCollection knowledgeBase;
83    public RunCollection KnowledgeBase {
84      get { return knowledgeBase; }
85    }
86
87    private readonly SingleObjectiveOKBProblem problem;
88    public SingleObjectiveOKBProblem Problem {
89      get { return problem; }
90    }
91
92    private readonly ItemList<IAlgorithm> algorithmInstances;
93    private readonly ReadOnlyItemList<IAlgorithm> readonlyAlgorithmInstances;
94    public ReadOnlyItemList<IAlgorithm> AlgorithmInstances {
95      get { return readonlyAlgorithmInstances; }
96    }
97
98    private readonly RunCollection problemInstances;
99    public RunCollection ProblemInstances {
100      get { return problemInstances; }
101    }
102
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      }
111    }
112
113    private readonly CheckedItemList<IScope> solutionSeedingPool;
114    public CheckedItemList<IScope> SolutionSeedingPool {
115      get { return solutionSeedingPool; }
116    }
117
118    private readonly EnumValue<SeedingStrategyTypes> seedingStrategy;
119    public EnumValue<SeedingStrategyTypes> SeedingStrategy {
120      get { return seedingStrategy; }
121    }
122   
123    private BidirectionalLookup<long, IRun> algorithmId2RunMapping;
124    private BidirectionalDictionary<long, IAlgorithm> algorithmId2AlgorithmInstanceMapping;
125    private BidirectionalDictionary<long, IRun> problemId2ProblemInstanceMapping;
126   
127    public bool Maximization {
128      get { return Problem != null && Problem.ProblemId >= 0 && ((IValueParameter<BoolValue>)Problem.MaximizationParameter).Value.Value; }
129    }
130
131    public KnowledgeCenter() {
132      maximumEvaluations = new IntValue(10000);
133      minimumTarget = new DoubleValue(0.05);
134      instanceRuns = new RunCollection();
135      seededRuns = new RunCollection();
136      knowledgeBase = new RunCollection();
137      algorithmInstances = new ItemList<IAlgorithm>();
138      readonlyAlgorithmInstances = algorithmInstances.AsReadOnly();
139      problemInstances = new RunCollection();
140      recommendationModel = FixedRankModel.GetEmpty();
141      problem = new SingleObjectiveOKBProblem();
142      algorithmId2RunMapping = new BidirectionalLookup<long, IRun>();
143      algorithmId2AlgorithmInstanceMapping = new BidirectionalDictionary<long, IAlgorithm>();
144      problemId2ProblemInstanceMapping = new BidirectionalDictionary<long, IRun>();
145      solutionSeedingPool = new CheckedItemList<IScope>();
146      seedingStrategy = new EnumValue<SeedingStrategyTypes>(SeedingStrategyTypes.NoSeeding);
147      RegisterEventHandlers();
148    }
149
150    private void ProblemOnProblemChanged(object sender, EventArgs eventArgs) {
151      // TODO: Potentially, knowledge base has to be re-downloaded
152    }
153
154    private void RegisterEventHandlers() {
155      maximumEvaluations.ValueChanged += MaximumEvaluationsOnValueChanged;
156      minimumTarget.ValueChanged += MinimumTargetOnValueChanged;
157      problem.ProblemChanged += ProblemOnProblemChanged;
158      problem.Solutions.ItemsAdded += ProblemSolutionsChanged;
159      problem.Solutions.ItemsReplaced += ProblemSolutionsChanged;
160      problem.Solutions.ItemsRemoved += ProblemSolutionsChanged;
161      problem.Solutions.CollectionReset += ProblemSolutionsChanged;
162      instanceRuns.CollectionReset += InformationChanged;
163      instanceRuns.ItemsAdded += InformationChanged;
164      instanceRuns.ItemsRemoved += InformationChanged;
165      instanceRuns.Reset += InformationChanged;
166      instanceRuns.UpdateOfRunsInProgressChanged += InformationChanged;
167      knowledgeBase.CollectionReset += InformationChanged;
168      knowledgeBase.ItemsAdded += InformationChanged;
169      knowledgeBase.ItemsRemoved += InformationChanged;
170    }
171
172    private void MaximumEvaluationsOnValueChanged(object sender, EventArgs eventArgs) {
173
174    }
175
176    private void MinimumTargetOnValueChanged(object sender, EventArgs e) {
177
178    }
179
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
187    private void InformationChanged(object sender, EventArgs e) {
188      var runCollection = sender as RunCollection;
189      if (runCollection != null && runCollection.UpdateOfRunsInProgress) return;
190    }
191   
192    public bool IsCurrentInstance(IRun run) {
193      if (!problemId2ProblemInstanceMapping.ContainsSecond(run)) return false;
194      return problemId2ProblemInstanceMapping.GetBySecond(run) == Problem.ProblemId;
195    }
196
197    public void UpdateInstanceProjection(string[] characteristics) {
198      if (characteristics.Length == 0) return;
199
200      var instances = GetProblemCharacteristics(characteristics);
201
202      var key2Idx = new BidirectionalDictionary<IRun, int>();
203      foreach (var kvp in instances.Select((k, i) => new { Index = i, Key = k.Key }))
204        key2Idx.Add(kvp.Key, kvp.Index);
205
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));
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
220      double[,] v = null;
221      var ds = new double[instances.Count, characteristics.Length];
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);
232      }
233      #endregion
234      #region SOM
235      var features = new DoubleMatrix(characteristics.Length, instances.Count);
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      }
241      var somCoords = SOM.Map(features, new MersenneTwister(42), somSize: 10, learningRadius: 20, iterations: 200, jittering: true);
242      #endregion
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      }
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);
251      #endregion
252
253      ProblemInstances.UpdateOfRunsInProgress = true;
254      try {
255        foreach (var instance in ProblemInstances) {
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");
273          }
274
275          if (instance.Results.TryGetValue("Projection.MDS.X", out item)) {
276            ((DoubleValue)item).Value = coords[key2Idx.GetByFirst(instance), 0];
277          } else instance.Results.Add("Projection.MDS.X", new DoubleValue(coords[key2Idx.GetByFirst(instance), 0]));
278          if (instance.Results.TryGetValue("Projection.MDS.Y", out item)) {
279            ((DoubleValue)item).Value = coords[key2Idx.GetByFirst(instance), 1];
280          } else instance.Results.Add("Projection.MDS.Y", new DoubleValue(coords[key2Idx.GetByFirst(instance), 1]));
281
282          if (instance.Results.TryGetValue("Projection.SOM.X", out item)) {
283            ((DoubleValue)item).Value = somCoords[key2Idx.GetByFirst(instance), 0];
284          } else instance.Results.Add("Projection.SOM.X", new DoubleValue(somCoords[key2Idx.GetByFirst(instance), 0]));
285          if (instance.Results.TryGetValue("Projection.SOM.Y", out item)) {
286            ((DoubleValue)item).Value = somCoords[key2Idx.GetByFirst(instance), 1];
287          } else instance.Results.Add("Projection.SOM.Y", new DoubleValue(somCoords[key2Idx.GetByFirst(instance), 1]));
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]));
295        }
296      } finally { ProblemInstances.UpdateOfRunsInProgress = false; }
297    }
298
299    private static readonly HashSet<string> InterestingValueNames = new HashSet<string>() {
300      "QualityPerEvaluations", "QualityPerClock", "Problem Name", "Problem Type", "Algorithm Name", "Algorithm Type", "Maximization", "BestKnownQuality"
301    };
302
303    public Task<ResultCollection> StartAlgorithmAsync(int index) {
304      return StartAlgorithmAsync(index, CancellationToken.None);
305    }
306
307    public Task<ResultCollection> StartAlgorithmAsync(int index, CancellationToken cancellation) {
308      var selectedInstance = algorithmInstances[index];
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
313      var seedingStrategyLocal = SeedingStrategy.Value;
314      if (seedingStrategyLocal != SeedingStrategyTypes.NoSeeding) {
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));
320        seedingCreator.SampleFromPreexistingParameter.Value.Value = seedingStrategyLocal == SeedingStrategyTypes.SeedBySampling;
321        // TODO: WHY!? WHY??!?
322        ((dynamic)problemClone.SolutionCreatorParameter).Value = (dynamic)seedingCreator;
323      }
324      algorithmClone.Problem = problemClone;
325      algorithmClone.Prepare(true);
326      IParameter stopParam;
327      var monitorStop = true;
328      if (algorithmClone.Parameters.TryGetValue("MaximumEvaluations", out stopParam)) {
329        var maxEvalParam = stopParam as IValueParameter<Data.IntValue>;
330        if (maxEvalParam != null) {
331          maxEvalParam.Value.Value = MaximumEvaluations.Value;
332          monitorStop = false;
333        }
334      }
335
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);
339
340      #region EventHandler closures
341      EventHandler exeStateChanged = (sender, e) => {
342        if (algorithmClone.ExecutionState == ExecutionState.Stopped) {
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            }
350          }
351          if (seedingStrategyLocal == SeedingStrategyTypes.NoSeeding) {
352            lock (InstanceRuns) {
353              InstanceRuns.Add(algorithmClone.Runs.Last());
354            }
355          } else {
356            lock (SeededRuns) {
357              SeededRuns.Add(algorithmClone.Runs.Last());
358            }
359          }
360          waitHandle.Set();
361        }
362      };
363
364      EventHandler<EventArgs<Exception>> exceptionOccurred = (sender, e) => {
365        waitHandle.Set();
366      };
367
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;
372        if (evalSols >= MaximumEvaluations.Value && algorithmClone.ExecutionState == ExecutionState.Started)
373          algorithmClone.Stop();
374      };
375      #endregion
376
377      algorithmClone.ExecutionStateChanged += exeStateChanged;
378      algorithmClone.ExceptionOccurred += exceptionOccurred;
379      if (monitorStop) algorithmClone.ExecutionTimeChanged += timeChanged;
380
381      return Task.Factory.StartNew(() => {
382        algorithmClone.Start();
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        }
395        waitHandle.Dispose();
396        return algorithmClone.Results;
397      }, TaskCreationOptions.LongRunning);
398    }
399
400    public ResultCollection StartAlgorithm(int index, CancellationToken cancellation) {
401      var task = StartAlgorithmAsync(index, cancellation);
402      task.Wait(cancellation);
403      return task.Result;
404    }
405
406    public Task UpdateKnowledgeBaseAsync(IProgress progress = null) {
407      if (progress == null) progress = new Progress();
408      progress.Start("Updating Knowledge Base from OKB");
409      OnDownloadStarted(progress);
410      return Task.Factory.StartNew(() => { DoUpdateKnowledgeBase(progress); }, TaskCreationOptions.LongRunning);
411    }
412
413    public void UpdateKnowledgeBase(IProgress progress = null) {
414      UpdateKnowledgeBaseAsync(progress).Wait();
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 {
421        progress.Message = "Connecting to OKB...";
422        progress.ProgressValue = 0;
423        // FIXME: How to tell if refresh is necessary?
424        var refreshTasks = new[] {
425          Task.Factory.StartNew(() => queryClient.Refresh()),
426          Task.Factory.StartNew(() => adminClient.Refresh())
427        };
428        Task.WaitAll(refreshTasks);
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
437        problemId2ProblemInstanceMapping.Clear();
438        progress.Message = "Downloading algorithm and problem instances...";
439        progress.ProgressValue = 0;
440
441        int[] p = { 0 };
442        ProblemInstances.UpdateOfRunsInProgress = true;
443        ProblemInstances.Clear();
444        algorithmId2AlgorithmInstanceMapping.Clear();
445        algorithmId2RunMapping.Clear();
446        algorithmInstances.Clear();
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);
460          }
461        });
462
463        var interestingValues = queryClient.ValueNames.Where(x => InterestingValueNames.Contains(x.Name)).ToList();
464
465        progress.Message = "Downloading runs...";
466        progress.ProgressValue = 0;
467        p[0] = 0;
468        var count = queryClient.GetNumberOfRuns(problemClassFilter);
469        if (count == 0) return;
470       
471        var runList = new List<IRun>();
472        var runIds = LoadRunsFromCache(queryClient.GetRunIds(problemClassFilter), runList, progress);
473        var batches = runIds.Select((v, i) => new { Idx = i, Val = v }).GroupBy(x => x.Idx / 500, x => x.Val);
474        Parallel.ForEach(batches.Select(x => x.ToList()), new ParallelOptions { MaxDegreeOfParallelism = Math.Min(Environment.ProcessorCount, 4) },
475          (batch) => {
476          var okbRuns = queryClient.GetRunsWithValues(batch, true, interestingValues);
477          var hlRuns = okbRuns.AsParallel().Select(x => new { AlgorithmId = x.Algorithm.Id, RunId = x.Id, Run = queryClient.ConvertToOptimizationRun(x) }).ToList();
478          lock (runList) {
479            var toCache = new List<Tuple<long, long, IRun>>();
480            foreach (var r in hlRuns) {
481              algorithmId2RunMapping.Add(r.AlgorithmId, r.Run);
482              runList.Add(r.Run);
483              toCache.Add(Tuple.Create(r.AlgorithmId, r.RunId, r.Run));
484            }
485            SaveToCache(toCache);
486            progress.Message = string.Format("Downloaded runs {0} to {1} of {2}...", p[0], p[0] + batch.Count, count);
487            p[0] += batch.Count;
488            progress.ProgressValue = p[0] / (double)count;
489          }
490        });
491        progress.Message = "Finishing...";
492
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        }
502       
503        try {
504          KnowledgeBase.UpdateOfRunsInProgress = true;
505          KnowledgeBase.Clear();
506          KnowledgeBase.AddRange(runList);
507        } finally { KnowledgeBase.UpdateOfRunsInProgress = false; }
508
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
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;
519
520          IItem bkParam;
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);
538            prob.Parameters["BestKnownQuality"] = bkParam;
539          }
540        }
541
542        // add algorithm instance ranks as features to the problem instances for a range of targets
543        foreach (var target in new[] {0, 0.01, 0.05, 0.1, 0.2, 0.5}) {
544          var cls = GetPerformanceClasses(target, 5);
545          foreach (var kvp in cls) {
546            var prob = kvp.Key;
547            foreach (var kvp2 in kvp.Value) {
548              var resultName = "Rank." + algorithmId2AlgorithmInstanceMapping.GetByFirst(kvp2.Key) + "@" + (target * 100) + "%";
549              prob.Results[resultName] = new IntValue(kvp2.Value);
550            }
551          }
552        }
553      } finally { progress.Finish(); ProblemInstances.UpdateOfRunsInProgress = false; }
554      UpdateInstanceProjection(ProblemInstances.ResultNames.Where(x => x.StartsWith("Characteristic.")).ToArray());
555    }
556
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);
571            progress.Message = string.Format("Downloaded algorithm {0} (okb-id: {1})...", algInst.Name, algInst.Id);
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);
602            progress.Message = string.Format("Downloaded problem {0} (okb-id: {1})....", pInst.Name, pInst.Id);
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 {
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              }
649            }
650          });
651      } catch { }
652      return hashSet.ToList();
653    }
654
655    private void SaveToCache(IEnumerable<Tuple<long, long, IRun>> runs) {
656      try {
657        var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "HeuristicLab.OKB", "cache", "runsperalg");
658        if (!Directory.Exists(path)) Directory.CreateDirectory(path);
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            }
671          }
672        }
673      } catch { }
674    }
675
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
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
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
786    public Dictionary<IAlgorithm, double> GetAlgorithmPerformance(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                          .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);
805    }
806
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)
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));
826    }
827
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
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
839    public IEnumerable<IRegressionProblem> GetRegressionProblemPerAlgorithmInstance(double target, string[] characteristics) {
840      if (Problem == null) yield break;
841      var features = GetProblemCharacteristics(characteristics);
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);
846      foreach (var relevantRuns in knowledgeBase.GroupBy(x => algorithmId2RunMapping.GetBySecond(x).Single())) {
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>());
850        foreach (var pc in characteristics)
851          ds.AddVariable(pc, new List<double>());
852        ds.AddVariable("ERT", new List<double>());
853        foreach (var pr in problemRuns) {
854          var prob = problemMap[pr.Key];
855          var f = features[prob];
856          var max = ((BoolValue)prob.Parameters["Maximization"]).Value;
857          var bkq = ((DoubleValue)prob.Parameters["BestKnownQuality"]).Value;
858          var ert = ExpectedRuntimeHelper.CalculateErt(pr.ToList(), "QualityPerEvaluations", GetTarget(bkq, target, max), max).ExpectedRuntime;
859          if (double.IsInfinity(ert)) ert = int.MaxValue;
860          ds.AddRow(new object[] { pr.Key }.Concat(f.Cast<object>()).Concat(new object[] { ert }));
861        }
862        var datAnalysisData = new RegressionProblemData(ds, characteristics, "ERT");
863        var result = new RegressionProblem() {
864          Name = algorithmId2AlgorithmInstanceMapping.GetByFirst(relevantRuns.Key).Name
865        };
866        result.ProblemDataParameter.Value = datAnalysisData;
867        yield return result;
868      }
869    }
870
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      }
898    }
899
900    public Dictionary<IRun, double> GetProblemDistances(string[] characteristics) {
901      var result = new Dictionary<IRun, double>();
902      var currentInstance = problemId2ProblemInstanceMapping.GetByFirst(Problem.ProblemId);
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);
909      }
910      return result;
911    }
912
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
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));
938        var ranks = ClusteringHelper<long>.Cluster(nClasses, values, x => double.IsInfinity(x.Value))
939          .GetByCluster().ToList();
940        foreach (var c in ranks) {
941          foreach (var a in c.Value)
942            result[problemMap[pr.Key]][a.Key] = c.Key;
943        }
944      }
945      return result;
946    }
947
948    public double GetTarget(double bestKnownQuality, double target, bool maximization) {
949      return bestKnownQuality * (maximization ? (1 - target) : (1 + target));
950    }
951
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    }
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    }
963
964    public event EventHandler RecommendationModelChanged;
965    private void OnRecommenderModelChanged() {
966      var handler = RecommendationModelChanged;
967      if (handler != null) handler(this, EventArgs.Empty);
968    }
969
970    public IEnumerable<KeyValuePair<IAlgorithm, double>> GetAlgorithmInstanceRanking() {
971      return RecommendationModel.GetRanking(ProblemInstances.Single(IsCurrentInstance));
972    }
973  }
974}
Note: See TracBrowser for help on using the repository browser.