Free cookie consent management tool by TermsFeed Policy Generator

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

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

removed obsolete usings in CEDMA.Core and CEDMA.Server (ticket #712)

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