Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.CEDMA.Core/3.3/Problem.cs @ 2188

Last change on this file since 2188 was 2000, checked in by gkronber, 15 years ago

Refactoring: changed CEDMA backend to use a single data set per RDF results database. #656 (CEDMA server should handle only one data set (problem) at a time)

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