Free cookie consent management tool by TermsFeed Policy Generator

Changeset 2283


Ignore:
Timestamp:
08/13/09 14:16:40 (15 years ago)
Author:
mkommend
Message:

added implementation for HeuristicLab.SparseMatrix and SparseMatrixRow (ticket #701)

Location:
trunk/sources/HeuristicLab.SparseMatrix/3.2
Files:
1 added
2 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.SparseMatrix/3.2/HeuristicLab.SparseMatrix-3.2.csproj

    r2280 r2283  
    8282    <Compile Include="HeuristicLabSparseMatrixPlugin.cs" />
    8383    <Compile Include="Properties\AssemblyInfo.cs" />
     84    <Compile Include="SparseMatrix.cs" />
     85    <Compile Include="SparseMatrixRow.cs" />
    8486  </ItemGroup>
    8587  <ItemGroup>
     
    8890  </ItemGroup>
    8991  <ItemGroup>
     92    <ProjectReference Include="..\..\HeuristicLab.Core\3.2\HeuristicLab.Core-3.2.csproj">
     93      <Project>{F43B59AB-2B8C-4570-BC1E-15592086517C}</Project>
     94      <Name>HeuristicLab.Core-3.2</Name>
     95    </ProjectReference>
    9096    <ProjectReference Include="..\..\HeuristicLab.PluginInfrastructure\HeuristicLab.PluginInfrastructure.csproj">
    9197      <Project>{94186A6A-5176-4402-AE83-886557B53CCA}</Project>
  • trunk/sources/HeuristicLab.SparseMatrix/3.2/HeuristicLabSparseMatrixPlugin.cs

    r2280 r2283  
    2929  [ClassInfo(Name = "HeuristicLab.SparseMatrix-3.2")]
    3030  [PluginFile(Filename = "HeuristicLab.SparseMatrix-3.2.dll", Filetype = PluginFileType.Assembly)]
     31  [Dependency(Dependency = "HeuristicLab.Core-3.2")] 
    3132  public class HeuristicLabSparseMatrixPlugin : PluginBase {
    3233  }
  • trunk/sources/HeuristicLab.SparseMatrix/3.2/SparseMatrix.cs

    r2279 r2283  
    2020#endregion
    2121
    22 using System;
    2322using System.Collections.Generic;
    24 using System.Linq;
    25 using System.Text;
     23using System.Collections.ObjectModel;
    2624using HeuristicLab.Core;
    27 using System.Collections;
    28 using System.Xml;
    29 using System.Runtime.Serialization;
    30 using System.IO;
    31 using HeuristicLab.PluginInfrastructure;
    32 using HeuristicLab.Data;
    33 using HeuristicLab.DataAnalysis;
    34 using System.Drawing;
    35 using HeuristicLab.Modeling.Database;
    3625
    37 namespace 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       }
     26namespace HeuristicLab.SparseMatrix {   
     27  public class SparseMatrix : ItemBase {
     28    public SparseMatrix() {
     29      this.rows = new List<SparseMatrixRow>();
    4730    }
    4831
    49     private string[] ordinalVariables = null;
    50     public string[] OrdinalVariables {
    51       get {
    52         if (ordinalVariables == null) {
    53           LoadModelAttributes();
    54         }
    55         return ordinalVariables;
    56       }
     32    public SparseMatrix(IEnumerable<SparseMatrixRow> rows)
     33      : this() {
     34      this.rows.AddRange(rows);
    5735    }
    5836
    59     private string[] multiDimensionalOrdinalVariables;
    60     public string[] MultiDimensionalOrdinalVariables {
    61       get { return multiDimensionalOrdinalVariables; }
     37    private List<SparseMatrixRow> rows;
     38    public ReadOnlyCollection<SparseMatrixRow> GetRows() {
     39      return rows.AsReadOnly();
    6240    }
    6341
    64     private string[] multiDimensionalCategoricalVariables = new string[] { "VariableImpacts: InputVariableName" };
    65     public string[] MultiDimensionalCategoricalVariables {
    66       get { return multiDimensionalCategoricalVariables; }
     42    public void AddRow(SparseMatrixRow row) {
     43      this.rows.Add(row);
    6744    }
    6845
    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();
     46    public void RemoveRow(SparseMatrixRow row) {
     47      this.rows.Remove(row);
    7648    }
    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.Connect();     
    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         modelEntry.Set("Algorithm", model.Algorithm.Name);
    97         Dictionary<HeuristicLab.Modeling.Database.IVariable, ResultsEntry> inputVariableResultsEntries =
    98           new Dictionary<HeuristicLab.Modeling.Database.IVariable, ResultsEntry>();
    99 
    100         foreach (IInputVariableResult inputVariableResult in database.GetInputVariableResults(model)) {
    101           if (!inputVariableResultsEntries.ContainsKey(inputVariableResult.Variable)) {
    102             inputVariableResultsEntries[inputVariableResult.Variable] = new ResultsEntry();
    103             inputVariableResultsEntries[inputVariableResult.Variable].Set("InputVariableName", inputVariableResult.Variable.Name);
    104           }
    105           inputVariableResultsEntries[inputVariableResult.Variable].Set(inputVariableResult.Result.Name, inputVariableResult.Value);
    106         }
    107         modelEntry.Set("VariableImpacts", inputVariableResultsEntries.Values);
    108         entries.Add(modelEntry);
    109       }
    110       database.Disconnect();
    111       FireChanged();
    112       cached = true;
    113       return entries;
    114     }
    115 
    116     private bool IsAlmost(double x, double y) {
    117       return Math.Abs(x - y) < 1.0E-12;
    118     }
    119 
    120     internal IEnumerable<string> SelectModelAttributes() {
    121       return CategoricalVariables.Concat(OrdinalVariables);
    122     }
    123 
    124     private void LoadModelAttributes() {
    125       ordinalVariables = database.GetAllResults().Select(r => r.Name).ToArray();
    126       categoricalVariables = new string[] { "TargetVariable", "Algorithm" };
    127     }
    128 
    129     public double IndexOfCategoricalValue(string variable, object value) {
    130       if (value == null) return double.NaN;
    131       Dictionary<object, double> valueToIndexMap;
    132       if (categoricalValueIndices.ContainsKey(variable)) {
    133         valueToIndexMap = categoricalValueIndices[variable];
    134       } else {
    135         valueToIndexMap = new Dictionary<object, double>();
    136         categoricalValueIndices[variable] = valueToIndexMap;
    137       }
    138       if (!valueToIndexMap.ContainsKey(value)) {
    139         if (valueToIndexMap.Values.Count == 0) valueToIndexMap[value] = 1.0;
    140         else valueToIndexMap[value] = 1.0 + valueToIndexMap.Values.Max();
    141       }
    142       return valueToIndexMap[value];
    143     }
     49   
    14450  }
    14551}
Note: See TracChangeset for help on using the changeset viewer.