[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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Text;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using System.Collections;
|
---|
| 28 | using System.Xml;
|
---|
| 29 | using System.Runtime.Serialization;
|
---|
| 30 | using System.IO;
|
---|
[562] | 31 | using HeuristicLab.PluginInfrastructure;
|
---|
[567] | 32 | using HeuristicLab.Data;
|
---|
| 33 | using HeuristicLab.DataAnalysis;
|
---|
| 34 | using System.Drawing;
|
---|
[2223] | 35 | using HeuristicLab.Modeling.Database;
|
---|
[560] | 36 |
|
---|
[1073] | 37 | namespace 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 |
|
---|
[2223] | 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 |
|
---|
[2223] | 69 | private IModelingDatabase database;
|
---|
[561] | 70 |
|
---|
[1426] | 71 | private Dictionary<string, Dictionary<object, double>> categoricalValueIndices = new Dictionary<string, Dictionary<object, double>>();
|
---|
| 72 |
|
---|
[2223] | 73 | public Results(IModelingDatabase database) {
|
---|
| 74 | this.database = database;
|
---|
| 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() {
|
---|
[2223] | 87 | database.Connect();
|
---|
[1287] | 88 | entries = new List<ResultsEntry>();
|
---|
[2223] | 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);
|
---|
[1417] | 103 | }
|
---|
[2223] | 104 | inputVariableResultsEntries[inputVariableResult.Variable].Set(inputVariableResult.Result.Name, inputVariableResult.Value);
|
---|
[1287] | 105 | }
|
---|
[2223] | 106 | modelEntry.Set("VariableImpacts", inputVariableResultsEntries.Values);
|
---|
| 107 | entries.Add(modelEntry);
|
---|
| 108 | }
|
---|
| 109 | database.Disconnect();
|
---|
[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() {
|
---|
[2223] | 124 | ordinalVariables = database.GetAllResults().Select(r => r.Name).ToArray();
|
---|
| 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 | }
|
---|