Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/RegressionWorkbench.cs @ 6593

Last change on this file since 6593 was 6587, checked in by gkronber, 13 years ago

#1552: implemented first version of an optimizer for regression analysis experiments

File size: 7.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Drawing;
25using System.Linq;
26using HeuristicLab.Collections;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Data;
30using HeuristicLab.Optimization;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.Problems.DataAnalysis;
33using HeuristicLab.Problems.DataAnalysis.Symbolic;
34using HeuristicLab.Parameters;
35
36namespace HeuristicLab.Algorithms.DataAnalysis {
37  [Item("Regression Workbench", "Experiment containing multiple algorithms for regression analysis.")]
38  [Creatable("Data Analysis")]
39  [StorableClass]
40  public sealed class RegressionWorkbench : ParameterizedNamedItem, IOptimizer, IStorableContent {
41    public string Filename { get; set; }
42
43    private const string ProblemDataParameterName = "ProblemData";
44
45    #region parameter properties
46    public IValueParameter<IRegressionProblemData> ProblemDataParameter {
47      get { return (IValueParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName]; }
48    }
49    #endregion
50    #region properties
51    public IRegressionProblemData ProblemData {
52      get { return ProblemDataParameter.Value; }
53      set { ProblemDataParameter.Value = value; }
54    }
55    #endregion
56    [Storable]
57    private Experiment experiment;
58
59    [StorableConstructor]
60    private RegressionWorkbench(bool deserializing)
61      : base(deserializing) {
62    }
63    private RegressionWorkbench(RegressionWorkbench original, Cloner cloner)
64      : base(original, cloner) {
65      experiment = cloner.Clone(original.experiment);
66      RegisterEventHandlers();
67    }
68    public RegressionWorkbench()
69      : base() {
70      name = ItemName;
71      description = ItemDescription;
72
73      Parameters.Add(new ValueParameter<IRegressionProblemData>(ProblemDataParameterName, "The regression problem data that should be used for modeling.", new RegressionProblemData()));
74
75      experiment = new Experiment();
76
77      //var svmExperiments = CreateSvmExperiment();
78      var rfExperiments = CreateRandomForestExperiments();
79
80      experiment.Optimizers.Add(new LinearRegression());
81      experiment.Optimizers.Add(rfExperiments);
82      //experiment.Optimizers.Add(svmExperiments);
83
84      RegisterEventHandlers();
85    }
86
87    [StorableHook(HookType.AfterDeserialization)]
88    private void AfterDeserialization() {
89      RegisterEventHandlers();
90    }
91
92    public override IDeepCloneable Clone(Cloner cloner) {
93      return new RegressionWorkbench(this, cloner);
94    }
95
96    private void RegisterEventHandlers() {
97      ProblemDataParameter.ValueChanged += ProblemDataParameterValueChanged;
98
99      experiment.ExceptionOccurred += (sender, e) => OnExceptionOccured(e);
100      experiment.ExecutionStateChanged += (sender, e) => OnExecutionStateChanged(e);
101      experiment.ExecutionTimeChanged += (sender, e) => OnExecutionTimeChanged(e);
102      experiment.Paused += (sender, e) => OnPaused(e);
103      experiment.Prepared += (sender, e) => OnPrepared(e);
104      experiment.Started += (sender, e) => OnStarted(e);
105      experiment.Stopped += (sender, e) => OnStopped(e);
106    }
107
108    private IOptimizer CreateRandomForestExperiments() {
109      var exp = new Experiment();
110      double[] rs = new double[] { 0.2, 0.3, 0.4, 0.5, 0.6, 0.65 };
111      foreach (var r in rs) {
112        var cv = new CrossValidation();
113        var rf = new RandomForestRegression();
114        rf.R = r;
115        cv.Algorithm = rf;
116        cv.Folds.Value = 5;
117        exp.Optimizers.Add(cv);
118      }
119      return exp;
120    }
121
122    private IOptimizer CreateSvmExperiment() {
123      var exp = new Experiment();
124      var costs = new double[] { Math.Pow(2, -5), Math.Pow(2, -3), Math.Pow(2, -1), Math.Pow(2, 1), Math.Pow(2, 3), Math.Pow(2, 5), Math.Pow(2, 7), Math.Pow(2, 9), Math.Pow(2, 11), Math.Pow(2, 13), Math.Pow(2, 15) };
125      var gammas = new double[] { Math.Pow(2, -15), Math.Pow(2, -13), Math.Pow(2, -11), Math.Pow(2, -9), Math.Pow(2, -7), Math.Pow(2, -5), Math.Pow(2, -3), Math.Pow(2, -1), Math.Pow(2, 1), Math.Pow(2, 3) };
126      var nus = new double[] { 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9 };
127      foreach (var gamma in gammas)
128        foreach (var cost in costs)
129          foreach (var nu in nus) {
130            var cv = new CrossValidation();
131            var svr = new SupportVectorRegression();       
132            svr.Nu.Value = nu;
133            svr.Cost.Value = cost;
134            svr.Gamma.Value = gamma;
135            cv.Algorithm = svr;
136            cv.Folds.Value = 5;
137            exp.Optimizers.Add(cv);
138          }
139      return exp;
140    }
141
142    public RunCollection Runs {
143      get { return experiment.Runs; }
144    }
145
146    public void Prepare(bool clearRuns) {
147      experiment.Prepare(clearRuns);
148    }
149
150    public IEnumerable<IOptimizer> NestedOptimizers {
151      get { return experiment.NestedOptimizers; }
152    }
153
154    public ExecutionState ExecutionState {
155      get { return experiment.ExecutionState; }
156    }
157
158    public TimeSpan ExecutionTime {
159      get { return experiment.ExecutionTime; }
160    }
161
162    public void Prepare() {
163      experiment.Prepare();
164    }
165
166    public void Start() {
167      experiment.Start();
168    }
169
170    public void Pause() {
171      experiment.Pause();
172    }
173
174    public void Stop() {
175      experiment.Stop();
176    }
177
178    public event EventHandler ExecutionStateChanged;
179    private void OnExecutionStateChanged(EventArgs e) {
180      var handler = ExecutionStateChanged;
181      if (handler != null) handler(this, e);
182    }
183
184    public event EventHandler ExecutionTimeChanged;
185    private void OnExecutionTimeChanged(EventArgs e) {
186      var handler = ExecutionTimeChanged;
187      if (handler != null) handler(this, e);
188    }
189
190    public event EventHandler Prepared;
191    private void OnPrepared(EventArgs e) {
192      var handler = Prepared;
193      if (handler != null) handler(this, e);
194    }
195
196    public event EventHandler Started;
197    private void OnStarted(EventArgs e) {
198      var handler = Started;
199      if (handler != null) handler(this, e);
200    }
201
202    public event EventHandler Paused;
203    private void OnPaused(EventArgs e) {
204      var handler = Paused;
205      if (handler != null) handler(this, e);
206    }
207
208    public event EventHandler Stopped;
209    private void OnStopped(EventArgs e) {
210      var handler = Stopped;
211      if (handler != null) handler(this, e);
212    }
213
214    public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
215    private void OnExceptionOccured(EventArgs<Exception> e) {
216      var handler = ExceptionOccurred;
217      if (handler != null) handler(this, e);
218    }
219
220    public void ProblemDataParameterValueChanged(object source, EventArgs e) {
221      foreach (var op in NestedOptimizers.OfType<IDataAnalysisAlgorithm<IRegressionProblem>>()) {
222        op.Problem.ProblemDataParameter.Value = ProblemData;
223      }
224    }
225  }
226}
Note: See TracBrowser for help on using the repository browser.