Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysisService/HeuristicLab.ExperimentGeneration.DataAnalysis.ExperimentWizard/3.3/MediumAnalysisPage.cs @ 12966

Last change on this file since 12966 was 8024, checked in by spimming, 13 years ago

#1807:

  • fire event when Cancel button clicked
  • support cancellation when generating experiments
  • fixed page sizes
  • plugin dependencies updated
File size: 10.7 KB
Line 
1
2using System;
3using System.ComponentModel;
4using System.Linq;
5using System.Windows.Forms;
6using HeuristicLab.Algorithms.DataAnalysis;
7using HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm;
8using HeuristicLab.Common;
9using HeuristicLab.Data;
10using HeuristicLab.Encodings.ParameterConfigurationTreeEncoding;
11using HeuristicLab.Optimization;
12using HeuristicLab.Problems.DataAnalysis;
13using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
14namespace HeuristicLab.ExperimentGeneration.DataAnalysis.ExperimentWizard {
15  public partial class MediumAnalysisPage : HeuristicLab.ExperimentGeneration.DataAnalysis.Wizard.WizardPage {
16    private const int nrOfRepetitions = 10;
17    private const string gaCrossover = "SubtreeSwappingCrossover";
18    private const string gaMutator = "MultiSymbolicExpressionTreeManipulator";
19    private const string gaSelector = "GenderSpecificSelection";
20
21    private BackgroundWorker worker;
22
23    private DataAnalysisWizardContext context;
24    public DataAnalysisWizardContext Context {
25      get { return context; }
26    }
27
28    public MediumAnalysisPage(DataAnalysisWizardContext context) {
29      InitializeComponent();
30      this.context = context;
31      worker = new BackgroundWorker();
32      worker.DoWork += new DoWorkEventHandler(GenerateExperimentsTask);
33      worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(WorkerCompleted);
34      worker.WorkerReportsProgress = true;
35      worker.ProgressChanged += new ProgressChangedEventHandler(WorkerProgressChanged);
36      worker.WorkerSupportsCancellation = true;
37    }
38
39    private void GenerateExperimentsTask(object sender, DoWorkEventArgs e) {
40      DataAnalysisWizardContext c = e.Argument as DataAnalysisWizardContext;
41      IProblem problem = c.Problem;
42      if (worker.CancellationPending) {
43        e.Cancel = true;
44      } else {
45        lblProgress.Invoke(new Action(() => { lblProgress.Text = "Generating Support Vector Regression Experiment ..."; }));
46      }
47
48      // SVR Experiment -------------------------------------------------------
49      //Experiment svrExperiment = null;
50      //if (!e.Cancel) {
51      //  SupportVectorRegression svr = new SupportVectorRegression();
52      //  svr.Problem = (IRegressionProblem)problem;
53      //  ParameterConfigurationTree vc = new ParameterConfigurationTree(svr, problem);
54
55      //  var nuRange = new DoubleValueRange(new DoubleValue(0.1), new DoubleValue(0.9), new DoubleValue(0.1));
56      //  SetParameterRangeContraint(vc, "Nu", nuRange);
57
58      //  var costRange = new DoubleValueFactorRange(new DoubleValue(0.03125), new DoubleValue(32768), new DoubleValue(4));
59      //  SetParameterRangeContraint(vc, "Cost", costRange);
60
61      //  var gammaRange = new DoubleValueFactorRange(new DoubleValue(6.10352E-05), new DoubleValue(64), new DoubleValue(4));
62      //  SetParameterRangeContraint(vc, "Gamma", gammaRange);
63      //  worker.ReportProgress(5);
64      //  svrExperiment = vc.GenerateExperiment(svr);
65      //}
66      // ======================================================================
67
68      if (worker.CancellationPending) {
69        e.Cancel = true;
70      } else {
71        worker.ReportProgress(20);
72        lblProgress.Invoke(new Action(() => { lblProgress.Text = "Generating Random Forest Regression Experiment ..."; }));
73      }
74
75
76      // RF Experiment --------------------------------------------------------
77      //Experiment rfrExperiment = null;
78      //if (!e.Cancel) {
79      //  RandomForestRegression rfr = new RandomForestRegression();
80      //  rfr.Problem = (IRegressionProblem)problem;
81      //  ParameterConfigurationTree rfConfig = new ParameterConfigurationTree(rfr, problem);
82      //  var rRange = new DoubleValueRange(new DoubleValue(0.1), new DoubleValue(0.7), new DoubleValue(0.1));
83      //  SetParameterRangeContraint(rfConfig, "R", rRange);
84      //  var treeRange = new IntValueRange(new IntValue(250), new IntValue(250), new IntValue(250));
85      //  SetParameterRangeContraint(rfConfig, "Number of trees", treeRange);
86      //  rfrExperiment = rfConfig.GenerateExperiment(rfr);
87      //}
88      // ======================================================================
89
90      if (worker.CancellationPending) {
91        e.Cancel = true;
92      } else {
93        worker.ReportProgress(40);
94        lblProgress.Invoke(new Action(() => { lblProgress.Text = "Generating Neural Network Regression Experiment ..."; }));
95      }
96
97
98      // NN Experiment --------------------------------------------------------
99      //Experiment nnrExperiment = null;
100      //if (!e.Cancel) {
101      //  NeuralNetworkRegression nnr = new NeuralNetworkRegression();
102      //  nnr.Problem = (IRegressionProblem)problem;
103      //  ParameterConfigurationTree nnrConfig = new ParameterConfigurationTree(nnr, problem);
104      //  var decayRange = new DoubleValueFactorRange(new DoubleValue(0.001), new DoubleValue(100), new DoubleValue(10));
105      //  SetParameterRangeContraint(nnrConfig, "Decay", decayRange);
106      //  var nodesRange = new IntValueFactorRange(new IntValue(1), new IntValue(100), new IntValue(3));
107      //  SetParameterRangeContraint(nnrConfig, "NodesInFirstHiddenLayer", nodesRange);
108      //  nnrExperiment = nnrConfig.GenerateExperiment(nnr);
109      //}
110      // ======================================================================
111
112      if (worker.CancellationPending) {
113        e.Cancel = true;
114      } else {
115        worker.ReportProgress(60);
116        lblProgress.Invoke(new Action(() => { lblProgress.Text = "Generating Offspring Selection Genetic Algorithm Experiment ..."; }));
117      }
118
119      // GP Experiment --------------------------------------------------------
120      Experiment gpExperiment = new Experiment();
121      if (!e.Cancel) {
122        OffspringSelectionGeneticAlgorithm osga = new OffspringSelectionGeneticAlgorithm();
123        var prob = new SymbolicRegressionSingleObjectiveProblem();
124        prob.ProblemData = ((IRegressionProblem)problem).ProblemData;
125        //prob.SolutionCreator = new MultiSymbolicDataAnalysisExpressionCreator();
126        osga.Problem = prob;
127        osga.ComparisonFactorLowerBound.Value = 1;
128        osga.Crossover = osga.Problem.Operators.OfType<ICrossover>().FirstOrDefault(x => x.Name == gaCrossover);
129        osga.MaximumEvaluatedSolutions.Value = 500000;
130        osga.MaximumGenerations.Value = 100;
131        osga.MutationProbability = new PercentValue(0.15);
132        osga.Mutator = osga.Problem.Operators.OfType<IManipulator>().FirstOrDefault(x => x.Name == gaMutator);
133        osga.PopulationSize.Value = 500;
134        osga.Selector = osga.SelectorParameter.ValidValues.FirstOrDefault(x => x.Name == gaSelector);
135
136        CrossValidation crossSmall = new CrossValidation();
137        crossSmall.Algorithm = osga;
138        BatchRun batchSmall = new BatchRun("small");
139        batchSmall.Repetitions = nrOfRepetitions;
140        batchSmall.Optimizer = crossSmall;
141
142        CrossValidation crossMedium = (CrossValidation)crossSmall.Clone(new Cloner());
143        var probMedium = (SymbolicRegressionSingleObjectiveProblem)prob.Clone(new Cloner());
144        probMedium.MaximumSymbolicExpressionTreeDepth.Value = 12;
145        probMedium.MaximumSymbolicExpressionTreeLength.Value = 125;
146        crossMedium.Algorithm.Problem = probMedium;
147        BatchRun batchMedium = new BatchRun("medium");
148        batchMedium.Repetitions = nrOfRepetitions;
149        batchMedium.Optimizer = crossMedium;
150
151        CrossValidation crossLarge = (CrossValidation)crossSmall.Clone(new Cloner());
152        var probLarge = (SymbolicRegressionSingleObjectiveProblem)prob.Clone(new Cloner());
153        probLarge.MaximumSymbolicExpressionTreeDepth.Value = 17;
154        probLarge.MaximumSymbolicExpressionTreeLength.Value = 250;
155        crossLarge.Algorithm.Problem = probLarge;
156        BatchRun batchLarge = new BatchRun("large");
157        batchLarge.Repetitions = nrOfRepetitions;
158        batchLarge.Optimizer = crossLarge;
159
160        //Experiment gpExperiment = new Experiment();
161        gpExperiment.Optimizers.Add(batchSmall);
162        gpExperiment.Optimizers.Add(batchMedium);
163        gpExperiment.Optimizers.Add(batchLarge);
164      }
165      // ======================================================================
166      if (worker.CancellationPending) {
167        e.Cancel = true;
168      } else {
169        worker.ReportProgress(80);
170        lblProgress.Invoke(new Action(() => { lblProgress.Text = "Packing Data Analysis Experiment ..."; }));
171      }
172
173      Experiment experiment = new Experiment("Data Analysis Experiment");
174      if (!e.Cancel) {
175        //Experiment experiment = new Experiment("Data Analysis Experiment");
176        //experiment.Optimizers.Add(svrExperiment);
177        //experiment.Optimizers.Add(rfrExperiment);
178        //experiment.Optimizers.Add(nnrExperiment);
179        experiment.Optimizers.Add(gpExperiment);
180      }
181
182      if (worker.CancellationPending) {
183        e.Cancel = true;
184      } else {
185        worker.ReportProgress(100);
186        lblProgress.Invoke(new Action(() => { lblProgress.Text = "Experiment generation completed"; }));
187      }
188
189      e.Result = experiment;
190    }
191
192    private void SetParameterRangeContraint(ParameterConfigurationTree pct, string parameterName, IRange rangeContraint) {
193      var pc = pct.AlgorithmConfiguration.ParameterConfigurations.Where(x => x.ParameterName == parameterName).SingleOrDefault();
194      pc.Optimize = true;
195      var vc = (RangeValueConfiguration)pc.ValueConfigurations.First();
196      vc.Optimize = true;
197      vc.RangeConstraint = rangeContraint;
198
199    }
200
201    private void WorkerProgressChanged(object sender, ProgressChangedEventArgs e) {
202      progressBar1.Value = e.ProgressPercentage;
203    }
204
205    private void WorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
206      if (e.Error != null) {
207        SetWizardButton(Wizard.WizardButtons.Back);
208        MessageBox.Show(e.Error.Message, "Error occurred while generating experiments", MessageBoxButtons.OK, MessageBoxIcon.Error);
209      } else if (e.Cancelled) {
210        // nothing to do here ...
211      } else {
212        SetWizardButton(Wizard.WizardButtons.Next);
213        Experiment experiment = (Experiment)e.Result;
214        context.Experiment = experiment;
215      }
216    }
217
218    private void MediumAnalysisPage_WizardBack(object sender, Wizard.WizardPageEventArgs e) {
219      e.NewPage = "SelectAnalysisPage";
220    }
221
222    private void MediumAnalysisPage_SetActive(object sender, System.ComponentModel.CancelEventArgs e) {
223      SetWizardButton(Wizard.WizardButtons.None);
224      worker.RunWorkerAsync(context);
225    }
226
227    private void MediumAnalysisPage_WizardCancel(object sender, CancelEventArgs e) {
228      worker.CancelAsync();
229    }
230  }
231}
Note: See TracBrowser for help on using the repository browser.