Free cookie consent management tool by TermsFeed Policy Generator

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

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

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