using System; using System.ComponentModel; using System.Linq; using System.Windows.Forms; using HeuristicLab.Algorithms.DataAnalysis; using HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm; using HeuristicLab.Common; using HeuristicLab.Data; using HeuristicLab.Encodings.ParameterConfigurationTreeEncoding; using HeuristicLab.Optimization; using HeuristicLab.Problems.DataAnalysis; using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression; namespace HeuristicLab.ExperimentGeneration.DataAnalysis.ExperimentWizard { public partial class MediumAnalysisPage : HeuristicLab.ExperimentGeneration.DataAnalysis.Wizard.WizardPage { private const int nrOfRepetitions = 10; private const string gaCrossover = "SubtreeSwappingCrossover"; private const string gaMutator = "MultiSymbolicExpressionTreeManipulator"; private const string gaSelector = "GenderSpecificSelection"; private BackgroundWorker worker; private DataAnalysisWizardContext context; public DataAnalysisWizardContext Context { get { return context; } } public MediumAnalysisPage(DataAnalysisWizardContext context) { InitializeComponent(); this.context = context; worker = new BackgroundWorker(); worker.DoWork += new DoWorkEventHandler(GenerateExperimentsTask); worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(WorkerCompleted); worker.WorkerReportsProgress = true; worker.ProgressChanged += new ProgressChangedEventHandler(WorkerProgressChanged); worker.WorkerSupportsCancellation = true; } private void GenerateExperimentsTask(object sender, DoWorkEventArgs e) { DataAnalysisWizardContext c = e.Argument as DataAnalysisWizardContext; IProblem problem = c.Problem; if (worker.CancellationPending) { e.Cancel = true; } else { lblProgress.Invoke(new Action(() => { lblProgress.Text = "Generating Support Vector Regression Experiment ..."; })); } // SVR Experiment ------------------------------------------------------- //Experiment svrExperiment = null; //if (!e.Cancel) { // SupportVectorRegression svr = new SupportVectorRegression(); // svr.Problem = (IRegressionProblem)problem; // ParameterConfigurationTree vc = new ParameterConfigurationTree(svr, problem); // var nuRange = new DoubleValueRange(new DoubleValue(0.1), new DoubleValue(0.9), new DoubleValue(0.1)); // SetParameterRangeContraint(vc, "Nu", nuRange); // var costRange = new DoubleValueFactorRange(new DoubleValue(0.03125), new DoubleValue(32768), new DoubleValue(4)); // SetParameterRangeContraint(vc, "Cost", costRange); // var gammaRange = new DoubleValueFactorRange(new DoubleValue(6.10352E-05), new DoubleValue(64), new DoubleValue(4)); // SetParameterRangeContraint(vc, "Gamma", gammaRange); // worker.ReportProgress(5); // svrExperiment = vc.GenerateExperiment(svr); //} // ====================================================================== if (worker.CancellationPending) { e.Cancel = true; } else { worker.ReportProgress(20); lblProgress.Invoke(new Action(() => { lblProgress.Text = "Generating Random Forest Regression Experiment ..."; })); } // RF Experiment -------------------------------------------------------- //Experiment rfrExperiment = null; //if (!e.Cancel) { // RandomForestRegression rfr = new RandomForestRegression(); // rfr.Problem = (IRegressionProblem)problem; // ParameterConfigurationTree rfConfig = new ParameterConfigurationTree(rfr, problem); // var rRange = new DoubleValueRange(new DoubleValue(0.1), new DoubleValue(0.7), new DoubleValue(0.1)); // SetParameterRangeContraint(rfConfig, "R", rRange); // var treeRange = new IntValueRange(new IntValue(250), new IntValue(250), new IntValue(250)); // SetParameterRangeContraint(rfConfig, "Number of trees", treeRange); // rfrExperiment = rfConfig.GenerateExperiment(rfr); //} // ====================================================================== if (worker.CancellationPending) { e.Cancel = true; } else { worker.ReportProgress(40); lblProgress.Invoke(new Action(() => { lblProgress.Text = "Generating Neural Network Regression Experiment ..."; })); } // NN Experiment -------------------------------------------------------- //Experiment nnrExperiment = null; //if (!e.Cancel) { // NeuralNetworkRegression nnr = new NeuralNetworkRegression(); // nnr.Problem = (IRegressionProblem)problem; // ParameterConfigurationTree nnrConfig = new ParameterConfigurationTree(nnr, problem); // var decayRange = new DoubleValueFactorRange(new DoubleValue(0.001), new DoubleValue(100), new DoubleValue(10)); // SetParameterRangeContraint(nnrConfig, "Decay", decayRange); // var nodesRange = new IntValueFactorRange(new IntValue(1), new IntValue(100), new IntValue(3)); // SetParameterRangeContraint(nnrConfig, "NodesInFirstHiddenLayer", nodesRange); // nnrExperiment = nnrConfig.GenerateExperiment(nnr); //} // ====================================================================== if (worker.CancellationPending) { e.Cancel = true; } else { worker.ReportProgress(60); lblProgress.Invoke(new Action(() => { lblProgress.Text = "Generating Offspring Selection Genetic Algorithm Experiment ..."; })); } // GP Experiment -------------------------------------------------------- Experiment gpExperiment = new Experiment(); if (!e.Cancel) { OffspringSelectionGeneticAlgorithm osga = new OffspringSelectionGeneticAlgorithm(); var prob = new SymbolicRegressionSingleObjectiveProblem(); prob.ProblemData = ((IRegressionProblem)problem).ProblemData; //prob.SolutionCreator = new MultiSymbolicDataAnalysisExpressionCreator(); osga.Problem = prob; osga.ComparisonFactorLowerBound.Value = 1; osga.Crossover = osga.Problem.Operators.OfType().FirstOrDefault(x => x.Name == gaCrossover); osga.MaximumEvaluatedSolutions.Value = 500000; osga.MaximumGenerations.Value = 100; osga.MutationProbability = new PercentValue(0.15); osga.Mutator = osga.Problem.Operators.OfType().FirstOrDefault(x => x.Name == gaMutator); osga.PopulationSize.Value = 500; osga.Selector = osga.SelectorParameter.ValidValues.FirstOrDefault(x => x.Name == gaSelector); CrossValidation crossSmall = new CrossValidation(); crossSmall.Algorithm = osga; BatchRun batchSmall = new BatchRun("small"); batchSmall.Repetitions = nrOfRepetitions; batchSmall.Optimizer = crossSmall; CrossValidation crossMedium = (CrossValidation)crossSmall.Clone(new Cloner()); var probMedium = (SymbolicRegressionSingleObjectiveProblem)prob.Clone(new Cloner()); probMedium.MaximumSymbolicExpressionTreeDepth.Value = 12; probMedium.MaximumSymbolicExpressionTreeLength.Value = 125; crossMedium.Algorithm.Problem = probMedium; BatchRun batchMedium = new BatchRun("medium"); batchMedium.Repetitions = nrOfRepetitions; batchMedium.Optimizer = crossMedium; CrossValidation crossLarge = (CrossValidation)crossSmall.Clone(new Cloner()); var probLarge = (SymbolicRegressionSingleObjectiveProblem)prob.Clone(new Cloner()); probLarge.MaximumSymbolicExpressionTreeDepth.Value = 17; probLarge.MaximumSymbolicExpressionTreeLength.Value = 250; crossLarge.Algorithm.Problem = probLarge; BatchRun batchLarge = new BatchRun("large"); batchLarge.Repetitions = nrOfRepetitions; batchLarge.Optimizer = crossLarge; //Experiment gpExperiment = new Experiment(); gpExperiment.Optimizers.Add(batchSmall); gpExperiment.Optimizers.Add(batchMedium); gpExperiment.Optimizers.Add(batchLarge); } // ====================================================================== if (worker.CancellationPending) { e.Cancel = true; } else { worker.ReportProgress(80); lblProgress.Invoke(new Action(() => { lblProgress.Text = "Packing Data Analysis Experiment ..."; })); } Experiment experiment = new Experiment("Data Analysis Experiment"); if (!e.Cancel) { //Experiment experiment = new Experiment("Data Analysis Experiment"); //experiment.Optimizers.Add(svrExperiment); //experiment.Optimizers.Add(rfrExperiment); //experiment.Optimizers.Add(nnrExperiment); experiment.Optimizers.Add(gpExperiment); } if (worker.CancellationPending) { e.Cancel = true; } else { worker.ReportProgress(100); lblProgress.Invoke(new Action(() => { lblProgress.Text = "Experiment generation completed"; })); } e.Result = experiment; } private void SetParameterRangeContraint(ParameterConfigurationTree pct, string parameterName, IRange rangeContraint) { var pc = pct.AlgorithmConfiguration.ParameterConfigurations.Where(x => x.ParameterName == parameterName).SingleOrDefault(); pc.Optimize = true; var vc = (RangeValueConfiguration)pc.ValueConfigurations.First(); vc.Optimize = true; vc.RangeConstraint = rangeContraint; } private void WorkerProgressChanged(object sender, ProgressChangedEventArgs e) { progressBar1.Value = e.ProgressPercentage; } private void WorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { if (e.Error != null) { SetWizardButton(Wizard.WizardButtons.Back); MessageBox.Show(e.Error.Message, "Error occurred while generating experiments", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (e.Cancelled) { // nothing to do here ... } else { SetWizardButton(Wizard.WizardButtons.Next); Experiment experiment = (Experiment)e.Result; context.Experiment = experiment; } } private void MediumAnalysisPage_WizardBack(object sender, Wizard.WizardPageEventArgs e) { e.NewPage = "SelectAnalysisPage"; } private void MediumAnalysisPage_SetActive(object sender, System.ComponentModel.CancelEventArgs e) { SetWizardButton(Wizard.WizardButtons.None); worker.RunWorkerAsync(context); } private void MediumAnalysisPage_WizardCancel(object sender, CancelEventArgs e) { worker.CancelAsync(); } } }