Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.CEDMA.Charting/ResultList.cs @ 575

Last change on this file since 575 was 575, checked in by gkronber, 16 years ago

fixed a problem with brushing in histograms and added variable selection pressure

File size: 8.2 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 HeuristicLab.CEDMA.DB.Interfaces;
29using System.Xml;
30using System.Runtime.Serialization;
31using System.IO;
32using HeuristicLab.PluginInfrastructure;
33using HeuristicLab.Logging;
34using HeuristicLab.Data;
35using HeuristicLab.DataAnalysis;
36using HeuristicLab.Functions;
37using HeuristicLab.Charting.Data;
38using System.Drawing;
39
40namespace HeuristicLab.CEDMA.Charting {
41
42  public class RecordAddedEventArgs : EventArgs {
43    private Record record;
44    public Record Record { get { return record; } }
45    public RecordAddedEventArgs(Record r)
46      : base() {
47      this.record = r;
48    }
49  }
50
51  public class ResultList : ItemBase {
52    private const string cedmaNS = "http://www.heuristiclab.com/cedma/";
53    private readonly Entity targetVariablePredicate = new Entity(cedmaNS + "TargetVariable");
54    private readonly Entity trainingMAPEPredicate = new Entity(cedmaNS + "MeanAbsolutePercentageErrorTraining");
55    private readonly Entity validationMAPEPredicate = new Entity(cedmaNS + "MeanAbsolutePercentageErrorValidation");
56    private readonly Entity testMAPEPredicate = new Entity(cedmaNS + "MeanAbsolutePercentageErrorTest");
57    private readonly Entity trainingR2Predicate = new Entity(cedmaNS + "CoefficientOfDeterminationTraining");
58    private readonly Entity validationR2Predicate = new Entity(cedmaNS + "CoefficientOfDeterminationValidation");
59    private readonly Entity testR2Predicate = new Entity(cedmaNS + "CoefficientOfDeterminationTest");
60    private readonly Entity treeSizePredicate = new Entity(cedmaNS + "TreeSize");
61    private readonly Entity treeHeightPredicate = new Entity(cedmaNS + "TreeHeight");
62    private readonly Entity selectionPressurePredicate = new Entity(cedmaNS + "SelectionPressure");
63    private readonly Entity rawDataPredicate = new Entity(cedmaNS + "RawData");
64    private readonly Entity hasModelPredicate = new Entity(cedmaNS + "Model");
65    private readonly Entity anyEntity = new Entity(null);
66    private Dictionary<Record, Dataset> datasets;
67
68    private IStore store;
69    public IStore Store {
70      get { return store; }
71      set {
72        store = value;
73        Action reloadList = ReloadList;
74        reloadList.BeginInvoke(null, null);
75      }
76    }
77
78    private List<string> variableNames = new List<string>() { Record.TARGET_VARIABLE, Record.TREE_SIZE, Record.TREE_HEIGHT, Record.SELECTIONPRESSURE,
79    Record.MAPE_TRAINING, Record.MAPE_VALIDATION, Record.MAPE_TEST,
80    Record.R2_TRAINING, Record.R2_VALIDATION, Record.R2_TEST};
81    public string[] VariableNames {
82      get {
83        return variableNames.ToArray();
84      }
85    }
86
87    private Dictionary<Entity, string> predicateToVariableName;
88
89    public event EventHandler<RecordAddedEventArgs> OnRecordAdded;
90
91    private List<Record> records;
92    public List<Record> Records {
93      get {
94        List<Record> result = new List<Record>();
95        lock(records) {
96          result.AddRange(records);
97        }
98        return result;
99      }
100    }
101    private void ReloadList() {
102      var results = store.Select(new Statement(anyEntity, new Entity(cedmaNS + "instanceOf"), new Literal("class:GpFunctionTree")))
103      .Select(x => store.Select(new SelectFilter(
104        new Entity[] { new Entity(x.Subject.Uri) },
105        new Entity[] { targetVariablePredicate, treeSizePredicate, treeHeightPredicate, selectionPressurePredicate,
106          trainingMAPEPredicate, validationMAPEPredicate, testMAPEPredicate,
107          trainingR2Predicate, validationR2Predicate, testR2Predicate },
108          new Resource[] { anyEntity })));
109
110      Random random = new Random();
111      foreach(Statement[] ss in results) {
112        if(ss.Length > 0) {
113          Record r = new Record(this, ss[0].Subject.Uri);
114          r.Set(Record.X_JITTER, random.NextDouble() * 2.0 - 1.0);
115          r.Set(Record.Y_JITTER, random.NextDouble() * 2.0 - 1.0);
116          foreach(Statement s in ss) {
117            string varName;
118            predicateToVariableName.TryGetValue(s.Predicate, out varName);
119            if(varName != null) {
120              if(varName == Record.TREE_HEIGHT || varName == Record.TREE_SIZE || varName == Record.TARGET_VARIABLE) {
121                r.Set(varName, (double)(int)((Literal)s.Property).Value);
122              } else {
123                r.Set(varName, (double)((Literal)s.Property).Value);
124              }
125            }
126          }
127          lock(records) {
128            records.Add(r);
129          }
130          FireRecordAdded(r);
131        }
132      }
133      FireChanged();
134    }
135
136    private void FireRecordAdded(Record r) {
137      if(OnRecordAdded != null) OnRecordAdded(this, new RecordAddedEventArgs(r));
138    }
139
140    public ResultList()
141      : base() {
142      records = new List<Record>();
143      datasets = new Dictionary<Record, Dataset>();
144      predicateToVariableName = new Dictionary<Entity, string>();
145      predicateToVariableName[targetVariablePredicate] = Record.TARGET_VARIABLE;
146      predicateToVariableName[treeSizePredicate] = Record.TREE_SIZE;
147      predicateToVariableName[treeHeightPredicate] = Record.TREE_HEIGHT;
148      predicateToVariableName[selectionPressurePredicate] = Record.SELECTIONPRESSURE;
149      predicateToVariableName[trainingMAPEPredicate] = Record.MAPE_TRAINING;
150      predicateToVariableName[validationMAPEPredicate] = Record.MAPE_VALIDATION;
151      predicateToVariableName[testMAPEPredicate] = Record.MAPE_TEST;
152      predicateToVariableName[trainingR2Predicate] = Record.R2_TRAINING;
153      predicateToVariableName[validationR2Predicate] = Record.R2_VALIDATION;
154      predicateToVariableName[testR2Predicate] = Record.R2_TEST;
155    }
156
157    public override IView CreateView() {
158      return new ResultListView(this);
159    }
160
161    internal void OpenModel(Record record) {
162      IList<Statement> modelResults = store.Select(new Statement(new Entity(record.Uri), rawDataPredicate, anyEntity));
163      if(modelResults.Count == 1) {
164        string rawData = ((SerializedLiteral)modelResults[0].Property).RawData;
165        XmlDocument doc = new XmlDocument();
166        doc.LoadXml(rawData);
167        IFunctionTree tree = (IFunctionTree)PersistenceManager.Restore(doc.ChildNodes[1], new Dictionary<Guid, IStorable>());
168        int targetVariable = (int)record.Get(Record.TARGET_VARIABLE);
169        Dataset dataset = GetDataset(record);
170
171        ModelView modelView = new ModelView(dataset, tree, targetVariable);
172        PluginManager.ControlManager.ShowControl(modelView);
173      }
174    }
175
176    private Dataset GetDataset(Record record) {
177      if(!datasets.ContainsKey(record)) {
178        IList<Statement> result = store.Select(new Statement(anyEntity, hasModelPredicate, new Entity(record.Uri)));
179        if(result.Count == 1) {
180          IList<Statement> datasetResult = store.Select(new Statement(result[0].Subject, rawDataPredicate, anyEntity));
181          if(datasetResult.Count == 1) {
182            string rawData = ((SerializedLiteral)datasetResult[0].Property).RawData;
183            XmlDocument doc = new XmlDocument();
184            doc.LoadXml(rawData);
185            Dataset dataset = (Dataset)PersistenceManager.Restore(doc.ChildNodes[1], new Dictionary<Guid, IStorable>());
186            datasets.Add(record, dataset);
187          }
188        }
189      }
190      return datasets[record];
191    }
192  }
193}
Note: See TracBrowser for help on using the repository browser.