Free cookie consent management tool by TermsFeed Policy Generator

source: branches/plugins/HeuristicLab.CEDMA.Core/3.2/Problem.cs @ 3039

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

removed property VariableNames and added Get- and SetVariableName(index) methods instead. #419 (Refactor CEDMA plugins)

File size: 7.8 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.Xml;
28using HeuristicLab.CEDMA.DB.Interfaces;
29using HeuristicLab.Operators;
30
31namespace HeuristicLab.CEDMA.Core {
32
33  public enum LearningTask {
34    Classification,
35    Regression,
36    TimeSeries,
37    Clustering
38  }
39
40  /// <summary>
41  /// Problem describes the data mining task.
42  /// Contains the actual data and meta-data:
43  ///  * which variables should be modelled
44  ///  * regression, time-series or classification problem
45  /// </summary>
46  public class Problem : ItemBase {
47    private string name;
48    public string Name {
49      get { return name; }
50    }
51    private HeuristicLab.DataAnalysis.Dataset dataset;
52    public HeuristicLab.DataAnalysis.Dataset DataSet {
53      get { return dataset; }
54    }
55
56    private int trainingSamplesStart;
57    public int TrainingSamplesStart {
58      get { return trainingSamplesStart; }
59      set { trainingSamplesStart = value; }
60    }
61
62    private int trainingSamplesEnd;
63    public int TrainingSamplesEnd {
64      get { return trainingSamplesEnd; }
65      set { trainingSamplesEnd = value; }
66    }
67
68    private int validationSamplesStart;
69    public int ValidationSamplesStart {
70      get { return validationSamplesStart; }
71      set { validationSamplesStart = value; }
72    }
73
74    private int validationSamplesEnd;
75    public int ValidationSamplesEnd {
76      get { return validationSamplesEnd; }
77      set { validationSamplesEnd = value; }
78    }
79
80    private int testSamplesStart;
81    public int TestSamplesStart {
82      get { return testSamplesStart; }
83      set { testSamplesStart = value; }
84    }
85
86    private int testSamplesEnd;
87    public int TestSamplesEnd {
88      get { return testSamplesEnd; }
89      set { testSamplesEnd = value; }
90    }
91
92    private List<int> allowedInputVariables;
93    public List<int> AllowedInputVariables {
94      get { return allowedInputVariables; }
95    }
96
97    private List<int> allowedTargetVariables;
98    public List<int> AllowedTargetVariables {
99      get { return allowedTargetVariables; }
100    }
101
102    private bool autoRegressive;
103    public bool AutoRegressive {
104      get { return autoRegressive; }
105      set { autoRegressive = value; }
106    }
107
108    private LearningTask learningTask;
109    public LearningTask LearningTask {
110      get { return learningTask; }
111      set { learningTask = value; }
112    }
113
114    public Problem()
115      : base() {
116      dataset = new DataAnalysis.Dataset();
117      allowedInputVariables = new List<int>();
118      allowedTargetVariables = new List<int>();
119    }
120
121
122    public string GetVariableName(int index) {
123      return dataset.GetVariableName(index);
124    }
125
126    public override IView CreateView() {
127      return new ProblemView(this);
128    }
129
130    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
131      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
132      node.AppendChild(PersistenceManager.Persist("DataSet", dataset, document, persistedObjects));
133      XmlAttribute trainingSamplesStartAttr = document.CreateAttribute("TrainingSamplesStart");
134      trainingSamplesStartAttr.Value = TrainingSamplesStart.ToString();
135      XmlAttribute trainingSamplesEndAttr = document.CreateAttribute("TrainingSamplesEnd");
136      trainingSamplesEndAttr.Value = TrainingSamplesEnd.ToString();
137      XmlAttribute validationSamplesStartAttr = document.CreateAttribute("ValidationSamplesStart");
138      validationSamplesStartAttr.Value = ValidationSamplesStart.ToString();
139      XmlAttribute validationSamplesEndAttr = document.CreateAttribute("ValidationSamplesEnd");
140      validationSamplesEndAttr.Value = ValidationSamplesEnd.ToString();
141      XmlAttribute testSamplesStartAttr = document.CreateAttribute("TestSamplesStart");
142      testSamplesStartAttr.Value = TestSamplesStart.ToString();
143      XmlAttribute testSamplesEndAttr = document.CreateAttribute("TestSamplesEnd");
144      testSamplesEndAttr.Value = TestSamplesEnd.ToString();
145      XmlAttribute learningTaskAttr = document.CreateAttribute("LearningTask");
146      learningTaskAttr.Value = LearningTask.ToString();
147      XmlAttribute autoRegressiveAttr = document.CreateAttribute("AutoRegressive");
148      autoRegressiveAttr.Value = AutoRegressive.ToString();
149
150      node.Attributes.Append(trainingSamplesStartAttr);
151      node.Attributes.Append(trainingSamplesEndAttr);
152      node.Attributes.Append(validationSamplesStartAttr);
153      node.Attributes.Append(validationSamplesEndAttr);
154      node.Attributes.Append(testSamplesStartAttr);
155      node.Attributes.Append(testSamplesEndAttr);
156      node.Attributes.Append(learningTaskAttr);
157      node.Attributes.Append(autoRegressiveAttr);
158
159      XmlElement targetVariablesElement = document.CreateElement("AllowedTargetVariables");
160      targetVariablesElement.InnerText = SemiColonSeparatedList(AllowedTargetVariables);
161      XmlElement inputVariablesElement = document.CreateElement("AllowedInputVariables");
162      inputVariablesElement.InnerText = SemiColonSeparatedList(AllowedInputVariables);
163      node.AppendChild(targetVariablesElement);
164      node.AppendChild(inputVariablesElement);
165      return node;
166    }
167
168    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
169      base.Populate(node, restoredObjects);
170      dataset = (HeuristicLab.DataAnalysis.Dataset)PersistenceManager.Restore(node.SelectSingleNode("DataSet"), restoredObjects);
171      TrainingSamplesStart = int.Parse(node.Attributes["TrainingSamplesStart"].Value);
172      TrainingSamplesEnd = int.Parse(node.Attributes["TrainingSamplesEnd"].Value);
173      ValidationSamplesStart = int.Parse(node.Attributes["ValidationSamplesStart"].Value);
174      ValidationSamplesEnd = int.Parse(node.Attributes["ValidationSamplesEnd"].Value);
175      TestSamplesStart = int.Parse(node.Attributes["TestSamplesStart"].Value);
176      TestSamplesEnd = int.Parse(node.Attributes["TestSamplesEnd"].Value);
177      LearningTask = (LearningTask)Enum.Parse(typeof(LearningTask), node.Attributes["LearningTask"].Value);
178      AutoRegressive = bool.Parse(node.Attributes["AutoRegressive"].Value);
179      allowedTargetVariables.Clear();
180      foreach (string tok in node.SelectSingleNode("AllowedTargetVariables").InnerText.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries))
181        allowedTargetVariables.Add(int.Parse(tok));
182      allowedInputVariables.Clear();
183      foreach (string tok in node.SelectSingleNode("AllowedInputVariables").InnerText.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries))
184        allowedInputVariables.Add(int.Parse(tok));
185    }
186
187    private string SemiColonSeparatedList(List<int> xs) {
188      StringBuilder b = new StringBuilder();
189      foreach (int x in xs) {
190        b = b.Append(x).Append(";");
191      }
192      if (xs.Count > 0) b.Remove(b.Length - 1, 1); // remove last ';'
193      return b.ToString();
194    }
195  }
196}
Note: See TracBrowser for help on using the repository browser.