1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 | using HeuristicLab.Problems.Instances;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
31 | [StorableClass]
|
---|
32 | [Item("Classification Problem", "A general classification problem.")]
|
---|
33 | [Creatable("Problems")]
|
---|
34 | public class ClassificationProblem : DataAnalysisProblem<IClassificationProblemData>, IClassificationProblem, IStorableContent,
|
---|
35 | IProblemInstanceConsumer<ClassificationData>, IProblemInstanceExporter<ClassificationData> {
|
---|
36 | public string Filename { get; set; }
|
---|
37 |
|
---|
38 | [StorableConstructor]
|
---|
39 | protected ClassificationProblem(bool deserializing) : base(deserializing) { }
|
---|
40 | protected ClassificationProblem(ClassificationProblem original, Cloner cloner) : base(original, cloner) { }
|
---|
41 | public override IDeepCloneable Clone(Cloner cloner) { return new ClassificationProblem(this, cloner); }
|
---|
42 |
|
---|
43 | public ClassificationProblem()
|
---|
44 | : base() {
|
---|
45 | ProblemData = new ClassificationProblemData();
|
---|
46 | }
|
---|
47 |
|
---|
48 | public override void ImportProblemDataFromFile(string fileName) {
|
---|
49 | ClassificationProblemData problemData = ClassificationProblemData.ImportFromFile(fileName);
|
---|
50 | ProblemData = problemData;
|
---|
51 | }
|
---|
52 |
|
---|
53 | public void Load(ClassificationData data) {
|
---|
54 | Name = data.Name;
|
---|
55 | Description = data.Description;
|
---|
56 | Dataset dataset = new Dataset(data.InputVariables, data.Values);
|
---|
57 | ProblemData = new ClassificationProblemData(dataset, data.AllowedInputVariables, data.TargetVariable);
|
---|
58 | ProblemData.TrainingPartition.Start = data.TrainingPartitionStart;
|
---|
59 | ProblemData.TrainingPartition.End = data.TrainingPartitionEnd;
|
---|
60 | ProblemData.TestPartition.Start = data.TestPartitionStart;
|
---|
61 | ProblemData.TestPartition.End = data.TestPartitionEnd;
|
---|
62 | OnReset();
|
---|
63 | }
|
---|
64 |
|
---|
65 | public ClassificationData Export() {
|
---|
66 | if (!ProblemData.InputVariables.Count.Equals(ProblemData.Dataset.DoubleVariables.Count()))
|
---|
67 | throw new ArgumentException("Not all input variables are double variables! (Export only works with double variables)");
|
---|
68 |
|
---|
69 | ClassificationData claData = new ClassificationData();
|
---|
70 | claData.Name = Name;
|
---|
71 | claData.Description = Description;
|
---|
72 | claData.TargetVariable = ProblemData.TargetVariable;
|
---|
73 | claData.InputVariables = ProblemData.InputVariables.Select(x => x.Value).ToArray();
|
---|
74 | claData.AllowedInputVariables = ProblemData.AllowedInputVariables.ToArray();
|
---|
75 | claData.TrainingPartitionStart = ProblemData.TrainingPartition.Start;
|
---|
76 | claData.TrainingPartitionEnd = ProblemData.TrainingPartition.End;
|
---|
77 | claData.TestPartitionStart = ProblemData.TestPartition.Start;
|
---|
78 | claData.TestPartitionEnd = ProblemData.TestPartition.End;
|
---|
79 |
|
---|
80 | List<List<double>> data = new List<List<double>>();
|
---|
81 | foreach (var variable in ProblemData.Dataset.DoubleVariables) {
|
---|
82 | data.Add(ProblemData.Dataset.GetDoubleValues(variable).ToList());
|
---|
83 | }
|
---|
84 | claData.Values = Transformer.Transformation(data);
|
---|
85 |
|
---|
86 | return claData;
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|