[1044] | 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.Text;
|
---|
| 25 | using System.Windows.Forms;
|
---|
| 26 | using HeuristicLab.PluginInfrastructure;
|
---|
| 27 | using System.Net;
|
---|
| 28 | using System.ServiceModel;
|
---|
| 29 | using HeuristicLab.CEDMA.DB.Interfaces;
|
---|
| 30 | using HeuristicLab.CEDMA.DB;
|
---|
| 31 | using System.ServiceModel.Description;
|
---|
| 32 | using System.Linq;
|
---|
| 33 | using HeuristicLab.CEDMA.Core;
|
---|
[1053] | 34 | using HeuristicLab.GP.StructureIdentification;
|
---|
| 35 | using HeuristicLab.Data;
|
---|
[1060] | 36 | using HeuristicLab.Core;
|
---|
[1044] | 37 |
|
---|
| 38 | namespace HeuristicLab.CEDMA.Server {
|
---|
[1287] | 39 | public abstract class DispatcherBase : IDispatcher {
|
---|
[1217] | 40 | public enum ModelComplexity { Low, Medium, High };
|
---|
[1287] | 41 | public enum Algorithm { StandardGpRegression, OffspringGpRegression, StandardGpClassification, OffspringGpClassification, StandardGpForecasting, OffspringGpForecasting };
|
---|
[1044] | 42 |
|
---|
| 43 | private IStore store;
|
---|
[1217] | 44 | private ModelComplexity[] possibleComplexities = new ModelComplexity[] { ModelComplexity.Low, ModelComplexity.Medium, ModelComplexity.High };
|
---|
| 45 | private Dictionary<LearningTask, Algorithm[]> possibleAlgorithms = new Dictionary<LearningTask, Algorithm[]>() {
|
---|
[1287] | 46 | {LearningTask.Classification, new Algorithm[] { Algorithm.StandardGpClassification, Algorithm.OffspringGpClassification }},
|
---|
| 47 | {LearningTask.Regression, new Algorithm[] { Algorithm.StandardGpRegression, Algorithm.OffspringGpRegression }},
|
---|
| 48 | {LearningTask.TimeSeries, new Algorithm[] { Algorithm.StandardGpForecasting, Algorithm.OffspringGpForecasting }}
|
---|
[1217] | 49 | };
|
---|
[1044] | 50 |
|
---|
[1287] | 51 | private static int MaxGenerations {
|
---|
| 52 | get { return 3; }
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | private static int MaxEvaluatedSolutions {
|
---|
| 56 | get { return 3000; }
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[1217] | 59 | public DispatcherBase(IStore store) {
|
---|
[1044] | 60 | this.store = store;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[1217] | 63 | public Execution GetNextJob() {
|
---|
[1130] | 64 | // find and select a dataset
|
---|
| 65 | var dataSetVar = new HeuristicLab.CEDMA.DB.Interfaces.Variable("DataSet");
|
---|
| 66 | var dataSetQuery = new Statement[] {
|
---|
| 67 | new Statement(dataSetVar, Ontology.PredicateInstanceOf, Ontology.TypeDataSet)
|
---|
| 68 | };
|
---|
| 69 |
|
---|
[1417] | 70 | Entity[] datasets = store.Query("?DataSet <" + Ontology.PredicateInstanceOf.Uri + "> <" + Ontology.TypeDataSet.Uri + "> .", 0, 100)
|
---|
[1217] | 71 | .Select(x => (Entity)x.Get("DataSet"))
|
---|
| 72 | .ToArray();
|
---|
[1130] | 73 |
|
---|
| 74 | // no datasets => do nothing
|
---|
[1217] | 75 | if (datasets.Length == 0) return null;
|
---|
[1130] | 76 |
|
---|
[1217] | 77 | Entity dataSetEntity = SelectDataSet(datasets);
|
---|
[1130] | 78 | DataSet dataSet = new DataSet(store, dataSetEntity);
|
---|
[1217] | 79 |
|
---|
[1287] | 80 | int targetVariable = SelectTargetVariable(dataSet, dataSet.Problem.AllowedTargetVariables.ToArray());
|
---|
[1217] | 81 | Algorithm selectedAlgorithm = SelectAlgorithm(dataSet, targetVariable, possibleAlgorithms[dataSet.Problem.LearningTask]);
|
---|
[1216] | 82 | string targetVariableName = dataSet.Problem.GetVariableName(targetVariable);
|
---|
[1217] | 83 | ModelComplexity selectedComplexity = SelectComplexity(dataSet, targetVariable, selectedAlgorithm, possibleComplexities);
|
---|
| 84 |
|
---|
| 85 | Execution exec = CreateExecution(dataSet.Problem, targetVariable, selectedAlgorithm, selectedComplexity);
|
---|
[1216] | 86 | if (exec != null) {
|
---|
| 87 | exec.DataSetEntity = dataSetEntity;
|
---|
| 88 | exec.TargetVariable = targetVariableName;
|
---|
[1130] | 89 | }
|
---|
[1217] | 90 | return exec;
|
---|
[1044] | 91 | }
|
---|
| 92 |
|
---|
[1217] | 93 | public abstract Entity SelectDataSet(Entity[] datasets);
|
---|
| 94 | public abstract int SelectTargetVariable(DataSet dataSet, int[] targetVariables);
|
---|
| 95 | public abstract Algorithm SelectAlgorithm(DataSet dataSet, int targetVariable, Algorithm[] possibleAlgorithms);
|
---|
| 96 | public abstract ModelComplexity SelectComplexity(DataSet dataSet, int targetVariable, Algorithm algorithm, ModelComplexity[] possibleComplexities);
|
---|
[1060] | 97 |
|
---|
[1217] | 98 | private Execution CreateExecution(Problem problem, int targetVariable, Algorithm algorithm, ModelComplexity complexity) {
|
---|
| 99 | switch (algorithm) {
|
---|
[1287] | 100 | case Algorithm.StandardGpRegression: {
|
---|
| 101 | var algo = new HeuristicLab.GP.StructureIdentification.StandardGP();
|
---|
| 102 | SetComplexityParameters(algo, complexity);
|
---|
| 103 | SetProblemParameters(algo, problem, targetVariable);
|
---|
| 104 | algo.PopulationSize = 10000;
|
---|
| 105 | algo.MaxGenerations = MaxGenerations;
|
---|
| 106 | Execution exec = new Execution(algo.Engine);
|
---|
| 107 | exec.Description = "StandardGP - Complexity: " + complexity;
|
---|
| 108 | return exec;
|
---|
[1060] | 109 | }
|
---|
[1287] | 110 | case Algorithm.OffspringGpRegression: {
|
---|
| 111 | var algo = new HeuristicLab.GP.StructureIdentification.OffspringSelectionGP();
|
---|
| 112 | SetComplexityParameters(algo, complexity);
|
---|
| 113 | SetProblemParameters(algo, problem, targetVariable);
|
---|
| 114 | algo.MaxEvaluatedSolutions = MaxEvaluatedSolutions;
|
---|
| 115 | Execution exec = new Execution(algo.Engine);
|
---|
| 116 | exec.Description = "OffspringGP - Complexity: " + complexity;
|
---|
| 117 | return exec;
|
---|
| 118 | }
|
---|
| 119 | case Algorithm.StandardGpClassification: {
|
---|
| 120 | var algo = new HeuristicLab.GP.StructureIdentification.Classification.StandardGP();
|
---|
| 121 | SetComplexityParameters(algo, complexity);
|
---|
| 122 | SetProblemParameters(algo, problem, targetVariable);
|
---|
| 123 | algo.PopulationSize = 10000;
|
---|
| 124 | algo.MaxGenerations = MaxGenerations;
|
---|
| 125 | Execution exec = new Execution(algo.Engine);
|
---|
| 126 | exec.Description = "StandardGP - Complexity: " + complexity;
|
---|
| 127 | return exec;
|
---|
| 128 | }
|
---|
| 129 | case Algorithm.OffspringGpClassification: {
|
---|
| 130 | var algo = new HeuristicLab.GP.StructureIdentification.Classification.OffspringSelectionGP();
|
---|
| 131 | SetComplexityParameters(algo, complexity);
|
---|
| 132 | SetProblemParameters(algo, problem, targetVariable);
|
---|
| 133 | algo.MaxEvaluatedSolutions = MaxEvaluatedSolutions;
|
---|
| 134 | Execution exec = new Execution(algo.Engine);
|
---|
| 135 | exec.Description = "OffspringGP - Complexity: " + complexity;
|
---|
| 136 | return exec;
|
---|
| 137 | }
|
---|
| 138 | case Algorithm.StandardGpForecasting: {
|
---|
| 139 | var algo = new HeuristicLab.GP.StructureIdentification.TimeSeries.StandardGP();
|
---|
| 140 | SetComplexityParameters(algo, complexity);
|
---|
| 141 | SetProblemParameters(algo, problem, targetVariable);
|
---|
| 142 | algo.PopulationSize = 10000;
|
---|
| 143 | algo.MaxGenerations = MaxGenerations;
|
---|
| 144 | Execution exec = new Execution(algo.Engine);
|
---|
| 145 | exec.Description = "StandardGP - Complexity: " + complexity;
|
---|
| 146 | return exec;
|
---|
| 147 | }
|
---|
| 148 | case Algorithm.OffspringGpForecasting: {
|
---|
| 149 | var algo = new HeuristicLab.GP.StructureIdentification.TimeSeries.OffspringSelectionGP();
|
---|
| 150 | SetComplexityParameters(algo, complexity);
|
---|
| 151 | SetProblemParameters(algo, problem, targetVariable);
|
---|
| 152 | algo.MaxEvaluatedSolutions = MaxEvaluatedSolutions;
|
---|
| 153 | Execution exec = new Execution(algo.Engine);
|
---|
| 154 | exec.Description = "OffspringGP - Complexity: " + complexity;
|
---|
| 155 | return exec;
|
---|
| 156 | }
|
---|
[1217] | 157 | default: {
|
---|
| 158 | return null;
|
---|
| 159 | }
|
---|
[1060] | 160 | }
|
---|
| 161 | }
|
---|
| 162 |
|
---|
[1287] | 163 | private void SetComplexityParameters(AlgorithmBase algo, ModelComplexity complexity) {
|
---|
[1217] | 164 | switch (complexity) {
|
---|
| 165 | case ModelComplexity.Low: {
|
---|
[1287] | 166 | algo.MaxTreeHeight = 5;
|
---|
| 167 | algo.MaxTreeSize = 20;
|
---|
[1217] | 168 | break;
|
---|
| 169 | }
|
---|
| 170 | case ModelComplexity.Medium: {
|
---|
[1287] | 171 | algo.MaxTreeHeight = 10;
|
---|
| 172 | algo.MaxTreeSize = 100;
|
---|
[1217] | 173 | break;
|
---|
| 174 | }
|
---|
| 175 | case ModelComplexity.High: {
|
---|
[1287] | 176 | algo.MaxTreeHeight = 12;
|
---|
| 177 | algo.MaxTreeSize = 200;
|
---|
[1217] | 178 | break;
|
---|
| 179 | }
|
---|
| 180 | }
|
---|
[1287] | 181 | }
|
---|
[1217] | 182 |
|
---|
[1287] | 183 | private void SetProblemParameters(AlgorithmBase algo, Problem problem, int targetVariable) {
|
---|
| 184 | algo.ProblemInjector.GetVariable("Dataset").Value = problem.DataSet;
|
---|
| 185 | algo.ProblemInjector.GetVariable("TargetVariable").GetValue<IntData>().Data = targetVariable;
|
---|
| 186 | algo.ProblemInjector.GetVariable("TrainingSamplesStart").GetValue<IntData>().Data = problem.TrainingSamplesStart;
|
---|
| 187 | algo.ProblemInjector.GetVariable("TrainingSamplesEnd").GetValue<IntData>().Data = problem.TrainingSamplesEnd;
|
---|
| 188 | algo.ProblemInjector.GetVariable("ValidationSamplesStart").GetValue<IntData>().Data = problem.ValidationSamplesStart;
|
---|
| 189 | algo.ProblemInjector.GetVariable("ValidationSamplesEnd").GetValue<IntData>().Data = problem.ValidationSamplesEnd;
|
---|
| 190 | algo.ProblemInjector.GetVariable("TestSamplesStart").GetValue<IntData>().Data = problem.TestSamplesStart;
|
---|
| 191 | algo.ProblemInjector.GetVariable("TestSamplesEnd").GetValue<IntData>().Data = problem.TestSamplesEnd;
|
---|
| 192 | ItemList<IntData> allowedFeatures = algo.ProblemInjector.GetVariable("AllowedFeatures").GetValue<ItemList<IntData>>();
|
---|
| 193 | foreach (int allowedFeature in problem.AllowedInputVariables) allowedFeatures.Add(new IntData(allowedFeature));
|
---|
| 194 |
|
---|
| 195 | if (problem.LearningTask == LearningTask.TimeSeries) {
|
---|
| 196 | algo.ProblemInjector.GetVariable("Autoregressive").GetValue<BoolData>().Data = problem.AutoRegressive;
|
---|
| 197 | algo.ProblemInjector.GetVariable("MinTimeOffset").GetValue<IntData>().Data = problem.MinTimeOffset;
|
---|
| 198 | algo.ProblemInjector.GetVariable("MaxTimeOffset").GetValue<IntData>().Data = problem.MaxTimeOffset;
|
---|
| 199 | } else if (problem.LearningTask == LearningTask.Classification) {
|
---|
| 200 | ItemList<DoubleData> classValues = algo.ProblemInjector.GetVariable("TargetClassValues").GetValue<ItemList<DoubleData>>();
|
---|
| 201 | foreach (double classValue in GetDifferentClassValues(problem.DataSet, targetVariable)) classValues.Add(new DoubleData(classValue));
|
---|
| 202 | }
|
---|
[1053] | 203 | }
|
---|
[1287] | 204 |
|
---|
| 205 | private IEnumerable<double> GetDifferentClassValues(HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable) {
|
---|
| 206 | return Enumerable.Range(0, dataset.Rows).Select(x => dataset.GetValue(x, targetVariable)).Distinct();
|
---|
| 207 | }
|
---|
[1044] | 208 | }
|
---|
| 209 | }
|
---|