Free cookie consent management tool by TermsFeed Policy Generator

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

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

Refactored CEDMA plugins to work with new modeling backend. #712

File size: 7.2 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 IModelingDatabase database;
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(IModelingDatabase database) {
81      this.database = database;
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      entries = new List<ResultsEntry>();
94      foreach (var model in database.GetAllModels()) {
95        ResultsEntry modelEntry = new ResultsEntry();
96        foreach(var modelResult in database.GetModelResults(model)) {
97          modelEntry.Set(modelResult.Result.Name, modelResult.Value);
98        }
99        modelEntry.Set("PersistedData", model.Data);
100        modelEntry.Set("TargetVariable", model.TargetVariable.Name);
101      }
102     
103      FireChanged();
104      cached = true;
105      return entries;
106    }
107
108    //private IEnumerable<ResultsEntry> SelectVariableImpacts(string modelUri) {
109    //  var inputVariableNameBindings = store.Query(
110    //      "<" + modelUri + "> <" + Ontology.HasInputVariable + "> ?InputVariable ." +
111    //      "?InputVariable <" + Ontology.Name + "> ?InputName .",
112    //      0, PAGE_SIZE);
113
114    //  var qualityImpactBindings = store.Query(
115    //      "<" + modelUri + "> <" + Ontology.HasInputVariable + "> ?InputVariable ." +
116    //      "?InputVariable <" + Ontology.QualityImpact + "> ?QualityImpact .",
117    //      0, PAGE_SIZE);
118
119    //  var evaluationImpactBindings = store.Query(
120    //       "<" + modelUri + "> <" + Ontology.HasInputVariable + "> ?InputVariable ." +
121    //       "?InputVariable <" + Ontology.EvaluationImpact + "> ?EvaluationImpact .",
122    //       0, PAGE_SIZE);
123    //  Dictionary<object, ResultsEntry> inputVariableAttributes = new Dictionary<object, ResultsEntry>();
124
125    //  foreach (var inputVariableNameBinding in inputVariableNameBindings) {
126    //    object inputVariable = inputVariableNameBinding.Get("InputVariable");
127    //    object name = ((Literal)inputVariableNameBinding.Get("InputName")).Value;
128    //    if (!inputVariableAttributes.ContainsKey(inputVariable)) {
129    //      inputVariableAttributes[inputVariable] = new ResultsEntry();
130    //      inputVariableAttributes[inputVariable].Set("InputVariableName", name);
131    //    }
132    //  }
133
134    //  foreach (var qualityImpactBinding in qualityImpactBindings) {
135    //    double qualityImpact = (double)((Literal)qualityImpactBinding.Get("QualityImpact")).Value;
136    //    object inputVariable = qualityImpactBinding.Get("InputVariable");
137    //    if (!IsAlmost(qualityImpact, 1.0)) {
138    //      if (inputVariableAttributes[inputVariable].Get("QualityImpact") == null)
139    //        inputVariableAttributes[inputVariable].Set("QualityImpact", qualityImpact);
140    //    } else inputVariableAttributes.Remove(inputVariable);
141    //  }
142
143    //  foreach (var evaluationImpactBinding in evaluationImpactBindings) {
144    //    double evaluationImpact = (double)((Literal)evaluationImpactBinding.Get("EvaluationImpact")).Value;
145    //    object inputVariable = evaluationImpactBinding.Get("InputVariable");
146    //    if (!IsAlmost(evaluationImpact, 0.0)) {
147    //      if (inputVariableAttributes.ContainsKey(inputVariable) && inputVariableAttributes[inputVariable].Get("EvaluationImpact") == null)
148    //        inputVariableAttributes[inputVariable].Set("EvaluationImpact", evaluationImpact);
149    //    } else inputVariableAttributes.Remove(inputVariable);
150    //  }
151
152    //  return inputVariableAttributes.Values;
153    //}
154
155    private bool IsAlmost(double x, double y) {
156      return Math.Abs(x - y) < 1.0E-12;
157    }
158
159    internal IEnumerable<string> SelectModelAttributes() {
160      return CategoricalVariables.Concat(OrdinalVariables);
161    }
162
163    private void LoadModelAttributes() {
164      ordinalVariables = database.GetAllResults().Select(r => r.Name);
165      categoricalVariables = new string[] { "TargetVariable" };
166    }
167
168    public double IndexOfCategoricalValue(string variable, object value) {
169      if (value == null) return double.NaN;
170      Dictionary<object, double> valueToIndexMap;
171      if (categoricalValueIndices.ContainsKey(variable)) {
172        valueToIndexMap = categoricalValueIndices[variable];
173      } else {
174        valueToIndexMap = new Dictionary<object, double>();
175        categoricalValueIndices[variable] = valueToIndexMap;
176      }
177      if (!valueToIndexMap.ContainsKey(value)) {
178        if (valueToIndexMap.Values.Count == 0) valueToIndexMap[value] = 1.0;
179        else valueToIndexMap[value] = 1.0 + valueToIndexMap.Values.Max();
180      }
181      return valueToIndexMap[value];
182    }
183  }
184}
Note: See TracBrowser for help on using the repository browser.