Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2194 was 2194, checked in by mkommend, 15 years ago

adapted HeuristicLab.Modeling.Database and Database.SQLServerCompact (ticket #712)

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