Free cookie consent management tool by TermsFeed Policy Generator

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

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

first part of performance improvements (ticket #712)

File size: 5.4 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 System.Xml;
29using System.Runtime.Serialization;
30using System.IO;
31using HeuristicLab.PluginInfrastructure;
32using HeuristicLab.Data;
33using HeuristicLab.DataAnalysis;
34using System.Drawing;
35using HeuristicLab.Modeling.Database;
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;
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      multiDimensionalOrdinalVariables = database.GetAllResultsForInputVariables().Select(x => "VariableImpacts: "+ x.Name).ToArray();
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      database.GetAllModelResults();
88      entries = new List<ResultsEntry>();
89      foreach (var model in database.GetAllModels()) {
90        ResultsEntry modelEntry = new ResultsEntry();
91        foreach(var modelResult in database.GetModelResults(model)) {
92          modelEntry.Set(modelResult.Result.Name, modelResult.Value);
93        }
94        modelEntry.Set("PersistedData", database.GetModelData(model));
95        modelEntry.Set("TargetVariable", model.TargetVariable.Name);
96        Dictionary<HeuristicLab.Modeling.Database.IVariable, ResultsEntry> inputVariableResultsEntries =
97          new Dictionary<HeuristicLab.Modeling.Database.IVariable, ResultsEntry>();
98
99        foreach (IInputVariableResult inputVariableResult in database.GetInputVariableResults(model)) {
100          if (!inputVariableResultsEntries.ContainsKey(inputVariableResult.Variable)) {
101            inputVariableResultsEntries[inputVariableResult.Variable] = new ResultsEntry();
102            inputVariableResultsEntries[inputVariableResult.Variable].Set("InputVariableName", inputVariableResult.Variable.Name);
103          }
104          inputVariableResultsEntries[inputVariableResult.Variable].Set(inputVariableResult.Result.Name, inputVariableResult.Value);
105        }
106        modelEntry.Set("VariableImpacts", inputVariableResultsEntries.Values);
107        entries.Add(modelEntry);
108      }
109     
110      FireChanged();
111      cached = true;
112      return entries;
113    }
114
115    private bool IsAlmost(double x, double y) {
116      return Math.Abs(x - y) < 1.0E-12;
117    }
118
119    internal IEnumerable<string> SelectModelAttributes() {
120      return CategoricalVariables.Concat(OrdinalVariables);
121    }
122
123    private void LoadModelAttributes() {
124      ordinalVariables = database.GetAllResults().Select(r => r.Name).ToArray();
125      categoricalVariables = new string[] { "TargetVariable" };
126    }
127
128    public double IndexOfCategoricalValue(string variable, object value) {
129      if (value == null) return double.NaN;
130      Dictionary<object, double> valueToIndexMap;
131      if (categoricalValueIndices.ContainsKey(variable)) {
132        valueToIndexMap = categoricalValueIndices[variable];
133      } else {
134        valueToIndexMap = new Dictionary<object, double>();
135        categoricalValueIndices[variable] = valueToIndexMap;
136      }
137      if (!valueToIndexMap.ContainsKey(value)) {
138        if (valueToIndexMap.Values.Count == 0) valueToIndexMap[value] = 1.0;
139        else valueToIndexMap[value] = 1.0 + valueToIndexMap.Values.Max();
140      }
141      return valueToIndexMap[value];
142    }
143  }
144}
Note: See TracBrowser for help on using the repository browser.