Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Modeling Database Backend/sources/HeuristicLab.CEDMA.Core/3.3/Results.cs @ 2169

Last change on this file since 2169 was 2146, checked in by gkronber, 15 years ago

Removed obsolete performance measurement code. #691 (CEDMA result views should allow filtering of displayed results)

File size: 9.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Linq;
25using System.Text;
26using HeuristicLab.Core;
27using System.Collections;
28using HeuristicLab.CEDMA.DB.Interfaces;
29using System.Xml;
30using System.Runtime.Serialization;
31using System.IO;
32using HeuristicLab.PluginInfrastructure;
33using HeuristicLab.Data;
34using HeuristicLab.DataAnalysis;
35using System.Drawing;
36
37namespace HeuristicLab.CEDMA.Core {
38  public class Results : ItemBase {
39    private const int PAGE_SIZE = 1000;
40    private string[] categoricalVariables = null;
41    public string[] CategoricalVariables {
42      get {
43        if (categoricalVariables == null) {
44          LoadModelAttributes();
45        }
46        return categoricalVariables;
47      }
48    }
49
50    private string[] ordinalVariables = null;
51    public string[] OrdinalVariables {
52      get {
53        if (ordinalVariables == null) {
54          LoadModelAttributes();
55        }
56        return ordinalVariables;
57      }
58    }
59
60    private string[] multiDimensionalOrdinalVariables = new string[] { "VariableImpacts: EvaluationImpact", "VariableImpacts: QualityImpact" };
61    public string[] MultiDimensionalOrdinalVariables {
62      get { return multiDimensionalOrdinalVariables; }
63    }
64
65    private string[] multiDimensionalCategoricalVariables = new string[] { "VariableImpacts: InputVariableName" };
66    public string[] MultiDimensionalCategoricalVariables {
67      get { return multiDimensionalCategoricalVariables; }
68    }
69
70    private IStore store;
71    public IStore Store {
72      get { return store; }
73      set {
74        store = value;
75      }
76    }
77
78    private Dictionary<string, Dictionary<object, double>> categoricalValueIndices = new Dictionary<string, Dictionary<object, double>>();
79
80    public Results(IStore store) {
81      this.store = store;
82    }
83
84    private List<ResultsEntry> entries = null;
85    private bool cached = false;
86    public IEnumerable<ResultsEntry> GetEntries() {
87      if (!cached)
88        return SelectRows();
89      return entries.AsEnumerable();
90    }
91
92    private IEnumerable<ResultsEntry> SelectRows() {
93      int page = 0;
94      int resultsReturned = 0;
95      entries = new List<ResultsEntry>();
96      if (store == null) return entries;
97      do {
98        var allModels = store.Query(
99          "?Model <" + Ontology.InstanceOf + "> <" + Ontology.TypeModel + "> ." +
100          "?Model <" + Ontology.TargetVariable + "> ?TargetVariable ." +
101          "?Model <" + Ontology.TestMeanSquaredError + "> ?TestMSE .",
102          0, 3000)
103          .Select(modelBinding => new {
104            Model = ((Entity)modelBinding.Get("Model")).Uri,
105            TestMSE = (double)((Literal)modelBinding.Get("TestMSE")).Value,
106            TargetVariable = (string)((Literal)modelBinding.Get("TargetVariable")).Value
107          })
108          .Distinct()
109          .GroupBy(m => m.TargetVariable)
110          .Select(grouping => grouping.OrderBy(m => m.TestMSE));
111        foreach (var targetVariableBindings in allModels) {
112          resultsReturned = targetVariableBindings.Count();
113          int nModels = Math.Max(10, resultsReturned * 10 / 100);
114          nModels = Math.Min(nModels, resultsReturned);
115          foreach (var modelBindings in targetVariableBindings.Take(nModels)) {
116            ResultsEntry entry = new ResultsEntry();
117            entry.Uri = modelBindings.Model;
118            entries.Add(entry);
119            SetModelAttributes(entry, modelBindings.Model);
120            entry.Set("VariableImpacts", SelectVariableImpacts(modelBindings.Model));
121          }
122        }
123        page++;
124      } while (resultsReturned == PAGE_SIZE);
125      FireChanged();
126      cached = true;
127      return entries;
128    }
129
130    private void SetModelAttributes(ResultsEntry entry, string modelUri) {
131      var modelBindings = store.Query(
132        "<" + modelUri + "> ?Attribute ?Value .",
133        0, PAGE_SIZE);
134      foreach (var binding in modelBindings) {
135        if (binding.Get("Value") is Literal) {
136          string name = ((Entity)binding.Get("Attribute")).Uri.Replace(Ontology.CedmaNameSpace, "");
137          if (entry.Get(name) == null) {
138            object value = ((Literal)binding.Get("Value")).Value;
139            entry.Set(name, value);
140          }
141        }
142      }
143    }
144
145    private IEnumerable<ResultsEntry> SelectVariableImpacts(string modelUri) {
146      var inputVariableNameBindings = store.Query(
147          "<" + modelUri + "> <" + Ontology.HasInputVariable + "> ?InputVariable ." +
148          "?InputVariable <" + Ontology.Name + "> ?InputName .",
149          0, PAGE_SIZE);
150
151      var qualityImpactBindings = store.Query(
152          "<" + modelUri + "> <" + Ontology.HasInputVariable + "> ?InputVariable ." +
153          "?InputVariable <" + Ontology.QualityImpact + "> ?QualityImpact .",
154          0, PAGE_SIZE);
155
156      var evaluationImpactBindings = store.Query(
157           "<" + modelUri + "> <" + Ontology.HasInputVariable + "> ?InputVariable ." +
158           "?InputVariable <" + Ontology.EvaluationImpact + "> ?EvaluationImpact .",
159           0, PAGE_SIZE);
160      Dictionary<object, ResultsEntry> inputVariableAttributes = new Dictionary<object, ResultsEntry>();
161
162      foreach (var inputVariableNameBinding in inputVariableNameBindings) {
163        object inputVariable = inputVariableNameBinding.Get("InputVariable");
164        object name = ((Literal)inputVariableNameBinding.Get("InputName")).Value;
165        if (!inputVariableAttributes.ContainsKey(inputVariable)) {
166          inputVariableAttributes[inputVariable] = new ResultsEntry();
167          inputVariableAttributes[inputVariable].Set("InputVariableName", name);
168        }
169      }
170
171      foreach (var qualityImpactBinding in qualityImpactBindings) {
172        double qualityImpact = (double)((Literal)qualityImpactBinding.Get("QualityImpact")).Value;
173        object inputVariable = qualityImpactBinding.Get("InputVariable");
174        if (!IsAlmost(qualityImpact, 1.0)) {
175          if (inputVariableAttributes[inputVariable].Get("QualityImpact") == null)
176            inputVariableAttributes[inputVariable].Set("QualityImpact", qualityImpact);
177        } else inputVariableAttributes.Remove(inputVariable);
178      }
179
180      foreach (var evaluationImpactBinding in evaluationImpactBindings) {
181        double evaluationImpact = (double)((Literal)evaluationImpactBinding.Get("EvaluationImpact")).Value;
182        object inputVariable = evaluationImpactBinding.Get("InputVariable");
183        if (!IsAlmost(evaluationImpact, 0.0)) {
184          if (inputVariableAttributes.ContainsKey(inputVariable) && inputVariableAttributes[inputVariable].Get("EvaluationImpact") == null)
185            inputVariableAttributes[inputVariable].Set("EvaluationImpact", evaluationImpact);
186        } else inputVariableAttributes.Remove(inputVariable);
187      }
188
189      return inputVariableAttributes.Values;
190    }
191
192    private bool IsAlmost(double x, double y) {
193      return Math.Abs(x - y) < 1.0E-12;
194    }
195
196    internal IEnumerable<string> SelectModelAttributes() {
197      return CategoricalVariables.Concat(OrdinalVariables);
198    }
199
200    private void LoadModelAttributes() {
201      this.ordinalVariables =
202        store
203          .Query(
204            "?ModelAttribute <" + Ontology.InstanceOf + "> <" + Ontology.TypeModelAttribute + "> ." + Environment.NewLine +
205            "?ModelAttribute <" + Ontology.InstanceOf + "> <" + Ontology.TypeOrdinalAttribute + "> .", 0, 100)
206          .Select(s => ((Entity)s.Get("ModelAttribute")).Uri.Replace(Ontology.CedmaNameSpace, ""))
207          .Distinct()
208          .ToArray();
209      this.categoricalVariables =
210        store
211          .Query(
212            "?ModelAttribute <" + Ontology.InstanceOf + "> <" + Ontology.TypeModelAttribute + "> ." + Environment.NewLine +
213            "?ModelAttribute <" + Ontology.InstanceOf + "> <" + Ontology.TypeCategoricalAttribute + "> .", 0, 100)
214          .Select(s => ((Entity)s.Get("ModelAttribute")).Uri.Replace(Ontology.CedmaNameSpace, ""))
215          .Distinct()
216          .ToArray();
217    }
218
219    public double IndexOfCategoricalValue(string variable, object value) {
220      if (value == null) return double.NaN;
221      Dictionary<object, double> valueToIndexMap;
222      if (categoricalValueIndices.ContainsKey(variable)) {
223        valueToIndexMap = categoricalValueIndices[variable];
224      } else {
225        valueToIndexMap = new Dictionary<object, double>();
226        categoricalValueIndices[variable] = valueToIndexMap;
227      }
228      if (!valueToIndexMap.ContainsKey(value)) {
229        if (valueToIndexMap.Values.Count == 0) valueToIndexMap[value] = 1.0;
230        else valueToIndexMap[value] = 1.0 + valueToIndexMap.Values.Max();
231      }
232      return valueToIndexMap[value];
233    }
234  }
235}
Note: See TracBrowser for help on using the repository browser.