Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HL-3.2-MonoMigration/HeuristicLab.CEDMA.Charting/ResultList.cs @ 1520

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

added functionality to open the algorithm that generated any model

File size: 9.0 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 generatedByPredicate = new Entity(cedmaNS + "GeneratedBy");
66    private readonly Entity anyEntity = new Entity(null);
67    private Dictionary<Record, Dataset> datasets;
68
69    private IStore store;
70    public IStore Store {
71      get { return store; }
72      set {
73        store = value;
74        Action reloadList = ReloadList;
75        reloadList.BeginInvoke(null, null);
76      }
77    }
78
79    private List<string> variableNames = new List<string>() { Record.TARGET_VARIABLE, Record.TREE_SIZE, Record.TREE_HEIGHT, Record.SELECTIONPRESSURE,
80    Record.MAPE_TRAINING, Record.MAPE_VALIDATION, Record.MAPE_TEST,
81    Record.R2_TRAINING, Record.R2_VALIDATION, Record.R2_TEST};
82    public string[] VariableNames {
83      get {
84        return variableNames.ToArray();
85      }
86    }
87
88    private Dictionary<Entity, string> predicateToVariableName;
89
90    public event EventHandler<RecordAddedEventArgs> OnRecordAdded;
91
92    private List<Record> records;
93    public List<Record> Records {
94      get {
95        List<Record> result = new List<Record>();
96        lock(records) {
97          result.AddRange(records);
98        }
99        return result;
100      }
101    }
102    private void ReloadList() {
103      var results = store.Select(new Statement(anyEntity, new Entity(cedmaNS + "instanceOf"), new Literal("class:GpFunctionTree")))
104      .Select(x => store.Select(new SelectFilter(
105        new Entity[] { new Entity(x.Subject.Uri) },
106        new Entity[] { targetVariablePredicate, treeSizePredicate, treeHeightPredicate, selectionPressurePredicate,
107          trainingMAPEPredicate, validationMAPEPredicate, testMAPEPredicate,
108          trainingR2Predicate, validationR2Predicate, testR2Predicate },
109          new Resource[] { anyEntity })));
110
111      Random random = new Random();
112      foreach(Statement[] ss in results) {
113        if(ss.Length > 0) {
114          Record r = new Record(this, ss[0].Subject.Uri);
115          r.Set(Record.X_JITTER, random.NextDouble() * 2.0 - 1.0);
116          r.Set(Record.Y_JITTER, random.NextDouble() * 2.0 - 1.0);
117          foreach(Statement s in ss) {
118            string varName;
119            predicateToVariableName.TryGetValue(s.Predicate, out varName);
120            if(varName != null) {
121              if(varName == Record.TREE_HEIGHT || varName == Record.TREE_SIZE || varName == Record.TARGET_VARIABLE) {
122                r.Set(varName, (double)(int)((Literal)s.Property).Value);
123              } else {
124                r.Set(varName, (double)((Literal)s.Property).Value);
125              }
126            }
127          }
128          lock(records) {
129            records.Add(r);
130          }
131          FireRecordAdded(r);
132        }
133      }
134      FireChanged();
135    }
136
137    private void FireRecordAdded(Record r) {
138      if(OnRecordAdded != null) OnRecordAdded(this, new RecordAddedEventArgs(r));
139    }
140
141    public ResultList()
142      : base() {
143      records = new List<Record>();
144      datasets = new Dictionary<Record, Dataset>();
145      predicateToVariableName = new Dictionary<Entity, string>();
146      predicateToVariableName[targetVariablePredicate] = Record.TARGET_VARIABLE;
147      predicateToVariableName[treeSizePredicate] = Record.TREE_SIZE;
148      predicateToVariableName[treeHeightPredicate] = Record.TREE_HEIGHT;
149      predicateToVariableName[selectionPressurePredicate] = Record.SELECTIONPRESSURE;
150      predicateToVariableName[trainingMAPEPredicate] = Record.MAPE_TRAINING;
151      predicateToVariableName[validationMAPEPredicate] = Record.MAPE_VALIDATION;
152      predicateToVariableName[testMAPEPredicate] = Record.MAPE_TEST;
153      predicateToVariableName[trainingR2Predicate] = Record.R2_TRAINING;
154      predicateToVariableName[validationR2Predicate] = Record.R2_VALIDATION;
155      predicateToVariableName[testR2Predicate] = Record.R2_TEST;
156    }
157
158    public override IView CreateView() {
159      return new ResultListView(this);
160    }
161
162    internal void OpenModel(Record record) {
163      IList<Statement> modelResults = store.Select(new Statement(new Entity(record.Uri), rawDataPredicate, anyEntity));
164      if(modelResults.Count == 1) {
165        string rawData = ((SerializedLiteral)modelResults[0].Property).RawData;
166        XmlDocument doc = new XmlDocument();
167        doc.LoadXml(rawData);
168        IFunctionTree tree = (IFunctionTree)PersistenceManager.Restore(doc.ChildNodes[1], new Dictionary<Guid, IStorable>());
169        int targetVariable = (int)record.Get(Record.TARGET_VARIABLE);
170        Dataset dataset = GetDataset(record);
171
172        ModelView modelView = new ModelView(record, dataset, tree, targetVariable);
173        PluginManager.ControlManager.ShowControl(modelView);
174      }
175    }
176
177    private Dataset GetDataset(Record record) {
178      if(!datasets.ContainsKey(record)) {
179        IList<Statement> result = store.Select(new Statement(anyEntity, hasModelPredicate, new Entity(record.Uri)));
180        if(result.Count == 1) {
181          IList<Statement> datasetResult = store.Select(new Statement(result[0].Subject, rawDataPredicate, anyEntity));
182          if(datasetResult.Count == 1) {
183            string rawData = ((SerializedLiteral)datasetResult[0].Property).RawData;
184            XmlDocument doc = new XmlDocument();
185            doc.LoadXml(rawData);
186            Dataset dataset = (Dataset)PersistenceManager.Restore(doc.ChildNodes[1], new Dictionary<Guid, IStorable>());
187            datasets.Add(record, dataset);
188          }
189        }
190      }
191      return datasets[record];
192    }
193
194    internal void OpenAlgorithm(Record record) {
195      IList<Statement> generatedBy = store.Select(new Statement(new Entity(record.Uri), generatedByPredicate, anyEntity));
196      if(generatedBy.Count == 1) {
197        IList<Statement> algoResult = store.Select(new Statement((Entity)generatedBy[0].Property, rawDataPredicate, anyEntity));
198        if(algoResult.Count == 1) {
199          string rawData = ((SerializedLiteral)algoResult[0].Property).RawData;
200          XmlDocument doc = new XmlDocument();
201          doc.LoadXml(rawData);
202          IItem algo = (IItem)PersistenceManager.Restore(doc.ChildNodes[1], new Dictionary<Guid, IStorable>());
203          PluginManager.ControlManager.ShowControl(algo.CreateView());
204        }
205      }
206    }
207  }
208}
Note: See TracBrowser for help on using the repository browser.