Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CEDMA-Refactoring-Ticket419/HeuristicLab.CEDMA.Core/Problem.cs @ 1214

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

changed model attribute TargetVariable to store the name of the variable instead of the index. #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    public string[] VariableNames {
57      get { return DataSet.VariableNames; }
58    }
59
60    private int trainingSamplesStart;
61    public int TrainingSamplesStart {
62      get { return trainingSamplesStart; }
63      set { trainingSamplesStart = value; }
64    }
65
66    private int trainingSamplesEnd;
67    public int TrainingSamplesEnd {
68      get { return trainingSamplesEnd; }
69      set { trainingSamplesEnd = value; }
70    }
71
72    private int validationSamplesStart;
73    public int ValidationSamplesStart {
74      get { return validationSamplesStart; }
75      set { validationSamplesStart = value; }
76    }
77
78    private int validationSamplesEnd;
79    public int ValidationSamplesEnd {
80      get { return validationSamplesEnd; }
81      set { validationSamplesEnd = value; }
82    }
83
84    private int testSamplesStart;
85    public int TestSamplesStart {
86      get { return testSamplesStart; }
87      set { testSamplesStart = value; }
88    }
89
90    private int testSamplesEnd;
91    public int TestSamplesEnd {
92      get { return testSamplesEnd; }
93      set { testSamplesEnd = value; }
94    }
95
96    private List<int> allowedInputVariables;
97    public List<int> AllowedInputVariables {
98      get { return allowedInputVariables; }
99    }
100
101    private List<int> allowedTargetVariables;
102    public List<int> AllowedTargetVariables {
103      get { return allowedTargetVariables; }
104    }
105
106    private bool autoRegressive;
107    public bool AutoRegressive {
108      get { return autoRegressive; }
109      set { autoRegressive = value; }
110    }
111
112    private LearningTask learningTask;
113    public LearningTask LearningTask {
114      get { return learningTask; }
115      set { learningTask = value; }
116    }
117
118    public Problem()
119      : base() {
120      dataset = new DataAnalysis.Dataset();
121      allowedInputVariables = new List<int>();
122      allowedTargetVariables = new List<int>();
123    }
124
125    public override IView CreateView() {
126      return new ProblemView(this);
127    }
128
129    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
130      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
131      node.AppendChild(PersistenceManager.Persist("DataSet", dataset, document, persistedObjects));
132      XmlAttribute trainingSamplesStartAttr = document.CreateAttribute("TrainingSamplesStart");
133      trainingSamplesStartAttr.Value = TrainingSamplesStart.ToString();
134      XmlAttribute trainingSamplesEndAttr = document.CreateAttribute("TrainingSamplesEnd");
135      trainingSamplesEndAttr.Value = TrainingSamplesEnd.ToString();
136      XmlAttribute validationSamplesStartAttr = document.CreateAttribute("ValidationSamplesStart");
137      validationSamplesStartAttr.Value = ValidationSamplesStart.ToString();
138      XmlAttribute validationSamplesEndAttr = document.CreateAttribute("ValidationSamplesEnd");
139      validationSamplesEndAttr.Value = ValidationSamplesEnd.ToString();
140      XmlAttribute testSamplesStartAttr = document.CreateAttribute("TestSamplesStart");
141      testSamplesStartAttr.Value = TestSamplesStart.ToString();
142      XmlAttribute testSamplesEndAttr = document.CreateAttribute("TestSamplesEnd");
143      testSamplesEndAttr.Value = TestSamplesEnd.ToString();
144      XmlAttribute learningTaskAttr = document.CreateAttribute("LearningTask");
145      learningTaskAttr.Value = LearningTask.ToString();
146      XmlAttribute autoRegressiveAttr = document.CreateAttribute("AutoRegressive");
147      autoRegressiveAttr.Value = AutoRegressive.ToString();
148
149      node.Attributes.Append(trainingSamplesStartAttr);
150      node.Attributes.Append(trainingSamplesEndAttr);
151      node.Attributes.Append(validationSamplesStartAttr);
152      node.Attributes.Append(validationSamplesEndAttr);
153      node.Attributes.Append(testSamplesStartAttr);
154      node.Attributes.Append(testSamplesEndAttr);
155      node.Attributes.Append(learningTaskAttr);
156      node.Attributes.Append(autoRegressiveAttr);
157
158      XmlElement targetVariablesElement = document.CreateElement("AllowedTargetVariables");
159      targetVariablesElement.InnerText = SemiColonSeparatedList(AllowedTargetVariables);
160      XmlElement inputVariablesElement = document.CreateElement("AllowedInputVariables");
161      inputVariablesElement.InnerText = SemiColonSeparatedList(AllowedInputVariables);
162      node.AppendChild(targetVariablesElement);
163      node.AppendChild(inputVariablesElement);
164      return node;
165    }
166
167    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
168      base.Populate(node, restoredObjects);
169      dataset = (HeuristicLab.DataAnalysis.Dataset)PersistenceManager.Restore(node.SelectSingleNode("DataSet"), restoredObjects);
170      TrainingSamplesStart = int.Parse(node.Attributes["TrainingSamplesStart"].Value);
171      TrainingSamplesEnd = int.Parse(node.Attributes["TrainingSamplesEnd"].Value);
172      ValidationSamplesStart = int.Parse(node.Attributes["ValidationSamplesStart"].Value);
173      ValidationSamplesEnd = int.Parse(node.Attributes["ValidationSamplesEnd"].Value);
174      TestSamplesStart = int.Parse(node.Attributes["TestSamplesStart"].Value);
175      TestSamplesEnd = int.Parse(node.Attributes["TestSamplesEnd"].Value);
176      LearningTask = (LearningTask)Enum.Parse(typeof(LearningTask), node.Attributes["LearningTask"].Value);
177      AutoRegressive = bool.Parse(node.Attributes["AutoRegressive"].Value);
178      allowedTargetVariables.Clear();
179      foreach (string tok in node.SelectSingleNode("AllowedTargetVariables").InnerText.Split(new string[]{";"}, StringSplitOptions.RemoveEmptyEntries))
180        allowedTargetVariables.Add(int.Parse(tok));
181      allowedInputVariables.Clear();
182      foreach (string tok in node.SelectSingleNode("AllowedInputVariables").InnerText.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries))
183        allowedInputVariables.Add(int.Parse(tok));
184    }
185
186    private string SemiColonSeparatedList(List<int> xs) {
187      StringBuilder b = new StringBuilder();
188      foreach (int x in xs) {
189        b = b.Append(x).Append(";");
190      }
191      if (xs.Count > 0) b.Remove(b.Length - 1, 1); // remove last ';'
192      return b.ToString();
193    }
194  }
195}
Note: See TracBrowser for help on using the repository browser.