Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem/3.3/Views/PerformanceModelingView.cs @ 13791

Last change on this file since 13791 was 13791, checked in by abeham, 8 years ago

#2457: working on recommendation algorithms

File size: 7.1 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 HeuristicLab.Common.Resources;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.MainForm;
26using HeuristicLab.Optimization;
27using HeuristicLab.OptimizationExpertSystem.Common;
28using HeuristicLab.PluginInfrastructure;
29using System;
30using System.Collections.Generic;
31using System.Linq;
32
33namespace HeuristicLab.OptimizationExpertSystem {
34  [View("Performance Modeling View")]
35  [Content(typeof(KnowledgeCenter), IsDefaultView = false)]
36  public partial class PerformanceModelingView : KnowledgeCenterViewBase {
37    private readonly CheckedItemList<StringValue> characteristics;
38
39    public PerformanceModelingView() {
40      InitializeComponent();
41      characteristics = new CheckedItemList<StringValue>();
42      characteristics.CheckedItemsChanged += CharacteristicsOnCheckedItemsChanged;
43      characteristicsViewHost.Content = characteristics;
44      recommendStartButton.Text = string.Empty;
45      recommendStartButton.Image = VSImageLibrary.Play;
46      refreshCharacteristicsButton.Text = string.Empty;
47      refreshCharacteristicsButton.Image = VSImageLibrary.Refresh;
48      UpdateRecommenderCombobox();
49    }
50
51    protected override void OnContentChanged() {
52      base.OnContentChanged();
53      if (Content == null) {
54        minTargetView.Content = null;
55      } else {
56        minTargetView.Content = Content.MinimumTarget;
57      }
58      UpdateCharacteristics();
59    }
60
61    protected override void SetEnabledStateOfControls() {
62      base.SetEnabledStateOfControls();
63      // TODO: up/download state
64      recommendStartButton.Enabled = Content != null && recommenderComboBox.SelectedIndex >= 0 && characteristics.CheckedItems.Any();
65      characteristicsViewHost.Enabled = Content != null;
66      xValidateButton.Enabled = Content != null && recommenderComboBox.SelectedIndex >= 0 && characteristics.CheckedItems.Any();
67    }
68
69    #region Update Controls
70    private void UpdateRecommenderCombobox() {
71      if (InvokeRequired) { Invoke((Action)UpdateRecommenderCombobox); return; }
72      var prevSelection = (IAlgorithmInstanceRecommender)recommenderComboBox.SelectedItem;
73      var prevNewIndex = -1;
74      recommenderComboBox.Items.Clear();
75
76      var i = 0;
77      foreach (var rcmd in ApplicationManager.Manager.GetInstances<IAlgorithmInstanceRecommender>()) {
78        recommenderComboBox.Items.Add(rcmd);
79        if (prevSelection == null || rcmd.GetType() == prevSelection.GetType())
80          prevNewIndex = prevSelection == null ? 0 : i;
81        i++;
82      }
83     
84      recommenderComboBox.SelectedIndex = prevNewIndex;
85    }
86
87    private void UpdateCharacteristics() {
88      if (InvokeRequired) { Invoke((Action)UpdateCharacteristics); return; }
89
90      var @checked = new HashSet<string>(characteristics.CheckedItems.Select(x => x.Value.Value));
91      if (@checked.Count == 0 && Content.ProblemInstances.ResultNames.Any(x => x.StartsWith("Characteristic.")))
92        @checked = new HashSet<string>(Content.ProblemInstances.ResultNames.Where(x => x.StartsWith("Characteristic.")));
93
94      characteristics.Clear();
95      if (Content == null || Content.ProblemInstances.Count == 0) return;
96
97      foreach (var c in Content.ProblemInstances.ResultNames) {
98        characteristics.Add(new StringValue(c), @checked.Contains(c));
99      }
100    }
101    #endregion
102
103    #region Content Event Handlers
104    protected override void OnProblemInstancesChanged() {
105      base.OnProblemInstancesChanged();
106      UpdateCharacteristics();
107      SetEnabledStateOfControls();
108    }
109    #endregion
110
111    #region Control Event Handlers
112    private void RecommenderComboBoxOnSelectedIndexChanged(object sender, EventArgs e) {
113      if (InvokeRequired) { Invoke((Action<object, EventArgs>)RecommenderComboBoxOnSelectedIndexChanged, sender, e); return; }
114      var rcmd = (IAlgorithmInstanceRecommender)recommenderComboBox.SelectedItem;
115      parameterCollectionView.Content = rcmd != null ? rcmd.Parameters : null;
116      SetEnabledStateOfControls();
117    }
118
119    private void RecommendStartButtonOnClick(object sender, EventArgs e) {
120      var rcmd = (IAlgorithmInstanceRecommender)recommenderComboBox.SelectedItem;
121      var trainingSet = Content.ProblemInstances.Where(x => !Content.IsCurrentInstance(x)).ToArray();
122      Content.RecommendationModel = rcmd.TrainModel(trainingSet, Content, characteristics.CheckedItems.Select(x => x.Value.Value).ToArray());
123    }
124
125    private void RefreshCharacteristicsButtonOnClick(object sender, EventArgs e) {
126      UpdateCharacteristics();
127      SetEnabledStateOfControls();
128    }
129
130    private void xValidateButton_Click(object sender, EventArgs e) {
131      var recommender = (IAlgorithmInstanceRecommender)recommenderComboBox.SelectedItem;
132      var features = characteristics.CheckedItems.Select(x => x.Value.Value).ToArray();
133
134      var trainingSet = Content.ProblemInstances.Where(x => !Content.IsCurrentInstance(x)).ToArray();
135
136      // leave one out crossvalidation
137      foreach (var pi in trainingSet) {
138        var model = recommender.TrainModel(trainingSet.Where(x => x != pi).ToArray(), Content, features);
139        var ranking = model.GetRanking(pi).ToList();
140        var realPerformance = Content.GetAlgorithmPerformance(pi);
141        // absolute error predicting ert
142        // confusion matrix predicting class
143        // Kendall's tau
144        // average NCDG ... relevance determined by clustering (unsuccessful algorithms being penalized with negative relevance)
145        // mean reciprocal rank
146        // optional: expected reciprocal rank
147      }
148    }
149
150    private static double AbsoluteError(Dictionary<IAlgorithm, double> performance, List<Tuple<IAlgorithm, double>> ranking) {
151      var error = 0.0;
152      foreach (var tuple in ranking) {
153        double actual;
154        if (!performance.TryGetValue(tuple.Item1, out actual)) continue;
155        error += Math.Abs(actual - tuple.Item2);
156      }
157      return error;
158    }
159
160    private static int[,] ConfusionMatrix(Dictionary<IAlgorithm, double> performance, List<Tuple<IAlgorithm, double>> ranking) {
161      var confMatrix = new int[5,5];
162      return confMatrix;
163    }
164    #endregion
165
166    #region Other Event Handlers
167    private void CharacteristicsOnCheckedItemsChanged(object sender, EventArgs e) {
168      SetEnabledStateOfControls();
169    }
170    #endregion
171  }
172}
Note: See TracBrowser for help on using the repository browser.