Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Algorithms.DataAnalysis/3.4/RegressionWorkbench.cs @ 10355

Last change on this file since 10355 was 6760, checked in by epitzer, 13 years ago

#1530 integrate changes from trunk

File size: 7.7 KB
RevLine 
[5617]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;
[5886]33using HeuristicLab.Problems.DataAnalysis.Symbolic;
[6587]34using HeuristicLab.Parameters;
[5617]35
36namespace HeuristicLab.Algorithms.DataAnalysis {
[6587]37  [Item("Regression Workbench", "Experiment containing multiple algorithms for regression analysis.")]
[5617]38  [Creatable("Data Analysis")]
39  [StorableClass]
[6587]40  public sealed class RegressionWorkbench : ParameterizedNamedItem, IOptimizer, IStorableContent {
41    public string Filename { get; set; }
[5617]42
[6587]43    private const string ProblemDataParameterName = "ProblemData";
[5617]44
[6587]45    #region parameter properties
46    public IValueParameter<IRegressionProblemData> ProblemDataParameter {
47      get { return (IValueParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName]; }
[5617]48    }
[6587]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;
[5617]58
59    [StorableConstructor]
[6587]60    private RegressionWorkbench(bool deserializing)
[5617]61      : base(deserializing) {
62    }
[6587]63    private RegressionWorkbench(RegressionWorkbench original, Cloner cloner)
64      : base(original, cloner) {
65      experiment = cloner.Clone(original.experiment);
66      RegisterEventHandlers();
[5617]67    }
[6587]68    public RegressionWorkbench()
69      : base() {
70      name = ItemName;
71      description = ItemDescription;
[5617]72
[6587]73      Parameters.Add(new ValueParameter<IRegressionProblemData>(ProblemDataParameterName, "The regression problem data that should be used for modeling.", new RegressionProblemData()));
[5617]74
[6587]75      experiment = new Experiment();
[5617]76
[6587]77      //var svmExperiments = CreateSvmExperiment();
78      var rfExperiments = CreateRandomForestExperiments();
[5617]79
[6587]80      experiment.Optimizers.Add(new LinearRegression());
81      experiment.Optimizers.Add(rfExperiments);
82      //experiment.Optimizers.Add(svmExperiments);
[5617]83
[6587]84      RegisterEventHandlers();
[5617]85    }
86
[6587]87    [StorableHook(HookType.AfterDeserialization)]
88    private void AfterDeserialization() {
89      RegisterEventHandlers();
90    }
[5617]91
[6587]92    public override IDeepCloneable Clone(Cloner cloner) {
93      return new RegressionWorkbench(this, cloner);
[5617]94    }
95
[6587]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);
[5617]106    }
107
[6587]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);
[5617]118      }
[6587]119      return exp;
[5617]120    }
121
[6587]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;
[5617]140    }
141
[6587]142    public RunCollection Runs {
143      get { return experiment.Runs; }
[5617]144    }
145
[6587]146    public void Prepare(bool clearRuns) {
147      experiment.Prepare(clearRuns);
[5617]148    }
149
[6587]150    public IEnumerable<IOptimizer> NestedOptimizers {
151      get { return experiment.NestedOptimizers; }
[5617]152    }
153
154    public ExecutionState ExecutionState {
[6587]155      get { return experiment.ExecutionState; }
[5617]156    }
157
158    public TimeSpan ExecutionTime {
[6587]159      get { return experiment.ExecutionTime; }
[5617]160    }
161
162    public void Prepare() {
[6587]163      experiment.Prepare();
[5617]164    }
165
166    public void Start() {
[6587]167      experiment.Start();
[5617]168    }
169
170    public void Pause() {
[6587]171      experiment.Pause();
[5617]172    }
173
174    public void Stop() {
[6587]175      experiment.Stop();
[5617]176    }
177
[6587]178    public event EventHandler ExecutionStateChanged;
179    private void OnExecutionStateChanged(EventArgs e) {
180      var handler = ExecutionStateChanged;
181      if (handler != null) handler(this, e);
[5617]182    }
183
[6587]184    public event EventHandler ExecutionTimeChanged;
185    private void OnExecutionTimeChanged(EventArgs e) {
186      var handler = ExecutionTimeChanged;
187      if (handler != null) handler(this, e);
[6566]188    }
189
[6587]190    public event EventHandler Prepared;
191    private void OnPrepared(EventArgs e) {
192      var handler = Prepared;
193      if (handler != null) handler(this, e);
[5617]194    }
195
[6587]196    public event EventHandler Started;
197    private void OnStarted(EventArgs e) {
198      var handler = Started;
199      if (handler != null) handler(this, e);
[6184]200    }
201
[6587]202    public event EventHandler Paused;
203    private void OnPaused(EventArgs e) {
204      var handler = Paused;
205      if (handler != null) handler(this, e);
[6239]206    }
207
[6587]208    public event EventHandler Stopped;
209    private void OnStopped(EventArgs e) {
210      var handler = Stopped;
211      if (handler != null) handler(this, e);
[6250]212    }
213
[6587]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);
[5617]218    }
219
[6587]220    public void ProblemDataParameterValueChanged(object source, EventArgs e) {
221      foreach (var op in NestedOptimizers.OfType<IDataAnalysisAlgorithm<IRegressionProblem>>()) {
222        op.Problem.ProblemDataParameter.Value = ProblemData;
[5617]223      }
224    }
225  }
226}
Note: See TracBrowser for help on using the repository browser.