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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using System.Xml;
|
---|
28 | using System.Diagnostics;
|
---|
29 | using HeuristicLab.DataAnalysis;
|
---|
30 | using HeuristicLab.Data;
|
---|
31 | using HeuristicLab.Operators;
|
---|
32 | using HeuristicLab.GP.StructureIdentification;
|
---|
33 | using HeuristicLab.Modeling;
|
---|
34 | using HeuristicLab.GP;
|
---|
35 | using HeuristicLab.Random;
|
---|
36 | using HeuristicLab.GP.Interfaces;
|
---|
37 |
|
---|
38 | namespace HeuristicLab.LinearRegression {
|
---|
39 | public class LinearRegression : ItemBase, IEditable, IAlgorithm {
|
---|
40 |
|
---|
41 | public virtual string Name { get { return "LinearRegression"; } }
|
---|
42 | public virtual string Description { get { return "TODO"; } }
|
---|
43 |
|
---|
44 | private SequentialEngine.SequentialEngine engine;
|
---|
45 | public virtual IEngine Engine {
|
---|
46 | get { return engine; }
|
---|
47 | }
|
---|
48 |
|
---|
49 | public virtual Dataset Dataset {
|
---|
50 | get { return ProblemInjector.GetVariableValue<Dataset>("Dataset", null, false); }
|
---|
51 | set { ProblemInjector.GetVariable("Dataset").Value = value; }
|
---|
52 | }
|
---|
53 |
|
---|
54 | public virtual int TargetVariable {
|
---|
55 | get { return ProblemInjector.GetVariableValue<IntData>("TargetVariable", null, false).Data; }
|
---|
56 | set { ProblemInjector.GetVariableValue<IntData>("TargetVariable", null, false).Data = value; }
|
---|
57 | }
|
---|
58 |
|
---|
59 | public virtual IOperator ProblemInjector {
|
---|
60 | get {
|
---|
61 | IOperator main = GetMainOperator();
|
---|
62 | return main.SubOperators[1];
|
---|
63 | }
|
---|
64 | set {
|
---|
65 | IOperator main = GetMainOperator();
|
---|
66 | main.RemoveSubOperator(1);
|
---|
67 | main.AddSubOperator(value, 1);
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | public virtual IAnalyzerModel Model {
|
---|
72 | get {
|
---|
73 | if (!engine.Terminated) throw new InvalidOperationException("The algorithm is still running. Wait until the algorithm is terminated to retrieve the result.");
|
---|
74 | IScope bestModelScope = engine.GlobalScope;
|
---|
75 | return CreateLRModel(bestModelScope);
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | public LinearRegression() {
|
---|
80 | engine = new SequentialEngine.SequentialEngine();
|
---|
81 | CombinedOperator algo = CreateAlgorithm();
|
---|
82 | engine.OperatorGraph.AddOperator(algo);
|
---|
83 | engine.OperatorGraph.InitialOperator = algo;
|
---|
84 | }
|
---|
85 |
|
---|
86 | protected virtual CombinedOperator CreateAlgorithm() {
|
---|
87 | CombinedOperator algo = new CombinedOperator();
|
---|
88 | SequentialProcessor seq = new SequentialProcessor();
|
---|
89 | algo.Name = "LinearRegression";
|
---|
90 | seq.Name = "LinearRegression";
|
---|
91 |
|
---|
92 | var randomInjector = new RandomInjector();
|
---|
93 | randomInjector.Name = "Random Injector";
|
---|
94 | IOperator globalInjector = CreateGlobalInjector();
|
---|
95 | ProblemInjector problemInjector = new ProblemInjector();
|
---|
96 | problemInjector.GetVariableInfo("MaxNumberOfTrainingSamples").Local = true;
|
---|
97 | problemInjector.AddVariable(new HeuristicLab.Core.Variable("MaxNumberOfTrainingSamples", new IntData(5000)));
|
---|
98 |
|
---|
99 | HL2TreeEvaluatorInjector treeEvaluatorInjector = new HL2TreeEvaluatorInjector();
|
---|
100 |
|
---|
101 | IOperator shuffler = new DatasetShuffler();
|
---|
102 | shuffler.GetVariableInfo("ShuffleStart").ActualName = "TrainingSamplesStart";
|
---|
103 | shuffler.GetVariableInfo("ShuffleEnd").ActualName = "TrainingSamplesEnd";
|
---|
104 |
|
---|
105 | LinearRegressionOperator lrOperator = new LinearRegressionOperator();
|
---|
106 | lrOperator.GetVariableInfo("SamplesStart").ActualName = "ActualTrainingSamplesStart";
|
---|
107 | lrOperator.GetVariableInfo("SamplesEnd").ActualName = "ActualTrainingSamplesEnd";
|
---|
108 |
|
---|
109 | seq.AddSubOperator(randomInjector);
|
---|
110 | seq.AddSubOperator(problemInjector);
|
---|
111 | seq.AddSubOperator(globalInjector);
|
---|
112 | seq.AddSubOperator(treeEvaluatorInjector);
|
---|
113 | seq.AddSubOperator(shuffler);
|
---|
114 | seq.AddSubOperator(lrOperator);
|
---|
115 | seq.AddSubOperator(CreateModelAnalyser());
|
---|
116 |
|
---|
117 | algo.OperatorGraph.InitialOperator = seq;
|
---|
118 | algo.OperatorGraph.AddOperator(seq);
|
---|
119 |
|
---|
120 | return algo;
|
---|
121 | }
|
---|
122 |
|
---|
123 | protected virtual VariableInjector CreateGlobalInjector() {
|
---|
124 | VariableInjector injector = new VariableInjector();
|
---|
125 | injector.AddVariable(new HeuristicLab.Core.Variable("PunishmentFactor", new DoubleData(1000)));
|
---|
126 | injector.AddVariable(new HeuristicLab.Core.Variable("TotalEvaluatedNodes", new DoubleData(0)));
|
---|
127 |
|
---|
128 | return injector;
|
---|
129 | }
|
---|
130 |
|
---|
131 | protected virtual IOperator CreateModelAnalyser() {
|
---|
132 | CombinedOperator op = new CombinedOperator();
|
---|
133 | op.AddVariableInfo(new VariableInfo("FunctionTree", "The model to analyze", typeof(IGeneticProgrammingModel), VariableKind.In));
|
---|
134 | op.Name = "Model Analyzer";
|
---|
135 | op.GetVariableInfo("FunctionTree").ActualName = "LinearRegressionModel";
|
---|
136 |
|
---|
137 | IOperator maOp = DefaultStructureIdentificationAlgorithmOperators.CreatePostProcessingOperator();
|
---|
138 | op.OperatorGraph.AddOperator(maOp);
|
---|
139 | op.OperatorGraph.InitialOperator = maOp;
|
---|
140 | return op;
|
---|
141 | }
|
---|
142 |
|
---|
143 | protected virtual IAnalyzerModel CreateLRModel(IScope bestModelScope) {
|
---|
144 | return DefaultStructureIdentificationAlgorithmOperators.CreateGPModel(bestModelScope);
|
---|
145 | }
|
---|
146 |
|
---|
147 | protected virtual IOperator GetMainOperator() {
|
---|
148 | CombinedOperator lr = (CombinedOperator)Engine.OperatorGraph.InitialOperator;
|
---|
149 | return lr.OperatorGraph.InitialOperator;
|
---|
150 | }
|
---|
151 |
|
---|
152 | public override IView CreateView() {
|
---|
153 | return engine.CreateView();
|
---|
154 | }
|
---|
155 |
|
---|
156 | #region IEditable Members
|
---|
157 |
|
---|
158 | public virtual IEditor CreateEditor() {
|
---|
159 | return engine.CreateEditor();
|
---|
160 | }
|
---|
161 |
|
---|
162 | #endregion
|
---|
163 | }
|
---|
164 | }
|
---|