Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.CEDMA.Core/3.3/Results.cs @ 2133

Last change on this file since 2133 was 2131, checked in by gkronber, 15 years ago

Implemented basic visualization of variable impacts in the CEDMA bubble chart. #286 (Variable-usage diagrams for the CEDMA frontend)

File size: 7.8 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      bool newEntry = false;
96      if (store == null) yield break;
97      entries = new List<ResultsEntry>();
98      do {
99        var allBindings = store.Query(
100          "?Model <" + Ontology.InstanceOf + "> <" + Ontology.TypeModel + "> ." +
101          "?Model ?Attribute ?Value .",
102          page, PAGE_SIZE);
103        var allModelBindings = allBindings.GroupBy(x => (Entity)x.Get("Model"));
104        resultsReturned = allBindings.Count;
105
106        foreach (var modelBindings in allModelBindings) {
107          ResultsEntry entry = entries.FirstOrDefault(x => x.Uri == modelBindings.Key.Uri);
108          newEntry = false;
109          if (entry == null) {
110            entry = new ResultsEntry();
111            entry.Uri = modelBindings.Key.Uri;
112            entries.Add(entry);
113            newEntry = true;
114            entry.Set("VariableImpacts", SelectVariableImpacts(entry.Uri));
115          }
116          foreach (var binding in modelBindings) {
117            if (binding.Get("Value") is Literal) {
118              string name = ((Entity)binding.Get("Attribute")).Uri.Replace(Ontology.CedmaNameSpace, "");
119              if (entry.Get(name) == null) {
120                object value = ((Literal)binding.Get("Value")).Value;
121                entry.Set(name, value);
122              }
123            }
124          }
125          if (newEntry) yield return entry;
126        }
127        page++;
128      } while (resultsReturned == PAGE_SIZE);
129
130      FireChanged();
131      cached = true;
132    }
133
134    private IEnumerable<ResultsEntry> SelectVariableImpacts(string modelUri) {
135      var allBindings = store.Query(
136          "<" + modelUri + "> <" + Ontology.HasInputVariable + "> ?InputVariable ." +
137          "?InputVariable <" + Ontology.Name + "> ?InputName ." +
138          "?InputVariable <" + Ontology.QualityImpact + "> ?QualityImpact ." +
139          "?InputVariable <" + Ontology.EvaluationImpact + "> ?EvaluationImpact .",
140          0, PAGE_SIZE);
141      var allInputVariableBindings = allBindings.GroupBy(x => (Entity)x.Get("InputVariable"));
142      List<ResultsEntry> variableImpacts = new List<ResultsEntry>();
143
144      foreach (var inputVariableBinding in allInputVariableBindings) {
145        ResultsEntry entry = new ResultsEntry();
146        VariableBindings binding = inputVariableBinding.First();
147        double evaluationImpact = (double)((Literal)binding.Get("EvaluationImpact")).Value;
148        double qualityImpact = (double)((Literal)binding.Get("QualityImpact")).Value;
149        if (binding.Get("InputName") != null) entry.Set("InputVariableName", ((Literal)binding.Get("InputName")).Value);
150        if (binding.Get("QualityImpact") != null) entry.Set("QualityImpact", qualityImpact);
151        if (binding.Get("EvaluationImpact") != null) entry.Set("EvaluationImpact", evaluationImpact);
152        if (!IsAlmost(evaluationImpact, 0.0) && !IsAlmost(qualityImpact, 1.0)) {
153          variableImpacts.Add(entry);
154        }
155      }
156      return variableImpacts;
157    }
158
159    private bool IsAlmost(double x, double y) {
160      return (y + 1.0E-7 > x) && (y - 1.0E-7 < x);
161    }
162
163    internal IEnumerable<string> SelectModelAttributes() {
164      return CategoricalVariables.Concat(OrdinalVariables);
165    }
166
167    private void LoadModelAttributes() {
168      this.ordinalVariables =
169        store
170          .Query(
171            "?ModelAttribute <" + Ontology.InstanceOf + "> <" + Ontology.TypeModelAttribute + "> ." + Environment.NewLine +
172            "?ModelAttribute <" + Ontology.InstanceOf + "> <" + Ontology.TypeOrdinalAttribute + "> .", 0, 100)
173          .Select(s => ((Entity)s.Get("ModelAttribute")).Uri.Replace(Ontology.CedmaNameSpace, ""))
174          .Distinct()
175          .ToArray();
176      this.categoricalVariables =
177        store
178          .Query(
179            "?ModelAttribute <" + Ontology.InstanceOf + "> <" + Ontology.TypeModelAttribute + "> ." + Environment.NewLine +
180            "?ModelAttribute <" + Ontology.InstanceOf + "> <" + Ontology.TypeCategoricalAttribute + "> .", 0, 100)
181          .Select(s => ((Entity)s.Get("ModelAttribute")).Uri.Replace(Ontology.CedmaNameSpace, ""))
182          .Distinct()
183          .ToArray();
184    }
185
186    public double IndexOfCategoricalValue(string variable, object value) {
187      if (value == null) return double.NaN;
188      Dictionary<object, double> valueToIndexMap;
189      if (categoricalValueIndices.ContainsKey(variable)) {
190        valueToIndexMap = categoricalValueIndices[variable];
191      } else {
192        valueToIndexMap = new Dictionary<object, double>();
193        categoricalValueIndices[variable] = valueToIndexMap;
194      }
195      if (!valueToIndexMap.ContainsKey(value)) {
196        if (valueToIndexMap.Values.Count == 0) valueToIndexMap[value] = 1.0;
197        else valueToIndexMap[value] = 1.0 + valueToIndexMap.Values.Max();
198      }
199      return valueToIndexMap[value];
200    }
201  }
202}
Note: See TracBrowser for help on using the repository browser.