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
RevLine 
[560]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;
[562]31using HeuristicLab.PluginInfrastructure;
[567]32using HeuristicLab.Data;
33using HeuristicLab.DataAnalysis;
34using System.Drawing;
[2194]35using HeuristicLab.Modeling.Database;
[560]36
[1073]37namespace HeuristicLab.CEDMA.Core {
[1287]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    }
[560]48
[1287]49    private string[] ordinalVariables = null;
50    public string[] OrdinalVariables {
51      get {
52        if (ordinalVariables == null) {
53          LoadModelAttributes();
54        }
55        return ordinalVariables;
56      }
57    }
58
[2207]59    private string[] multiDimensionalOrdinalVariables;
[2131]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
[2190]69    private IModelingDatabase database;
[561]70
[1426]71    private Dictionary<string, Dictionary<object, double>> categoricalValueIndices = new Dictionary<string, Dictionary<object, double>>();
72
[2190]73    public Results(IModelingDatabase database) {
74      this.database = database;
[2207]75      multiDimensionalOrdinalVariables = database.GetAllResultsForInputVariables().Select(x => "VariableImpacts: "+ x.Name).ToArray();
[560]76    }
77
[1287]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();
[560]84    }
85
[1287]86    private IEnumerable<ResultsEntry> SelectRows() {
[2217]87      database.GetAllModelResults();
[1287]88      entries = new List<ResultsEntry>();
[2190]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);
[1287]93        }
[2217]94        modelEntry.Set("PersistedData", database.GetModelData(model));
[2190]95        modelEntry.Set("TargetVariable", model.TargetVariable.Name);
[2207]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);
[2217]103          }
104          inputVariableResultsEntries[inputVariableResult.Variable].Set(inputVariableResult.Result.Name, inputVariableResult.Value);
[2207]105        }
106        modelEntry.Set("VariableImpacts", inputVariableResultsEntries.Values);
[2193]107        entries.Add(modelEntry);
[2190]108      }
109     
[1287]110      FireChanged();
111      cached = true;
[2137]112      return entries;
[560]113    }
[567]114
[2131]115    private bool IsAlmost(double x, double y) {
[2137]116      return Math.Abs(x - y) < 1.0E-12;
[2131]117    }
118
[1287]119    internal IEnumerable<string> SelectModelAttributes() {
120      return CategoricalVariables.Concat(OrdinalVariables);
[567]121    }
[576]122
[1287]123    private void LoadModelAttributes() {
[2194]124      ordinalVariables = database.GetAllResults().Select(r => r.Name).ToArray();
[2190]125      categoricalVariables = new string[] { "TargetVariable" };
[576]126    }
[1424]127
128    public double IndexOfCategoricalValue(string variable, object value) {
[1426]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];
[1424]142    }
[560]143  }
144}
Note: See TracBrowser for help on using the repository browser.