Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.CEDMA.Server/DispatcherBase.cs @ 1322

Last change on this file since 1322 was 1287, checked in by gkronber, 16 years ago

Merged change sets from CEDMA branch to trunk:

File size: 10.3 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Text;
25using System.Windows.Forms;
26using HeuristicLab.PluginInfrastructure;
27using System.Net;
28using System.ServiceModel;
29using HeuristicLab.CEDMA.DB.Interfaces;
30using HeuristicLab.CEDMA.DB;
31using System.ServiceModel.Description;
32using System.Linq;
33using HeuristicLab.CEDMA.Core;
34using HeuristicLab.GP.StructureIdentification;
35using HeuristicLab.Data;
36using HeuristicLab.Core;
37
38namespace HeuristicLab.CEDMA.Server {
39  public abstract class DispatcherBase : IDispatcher {
40    public enum ModelComplexity { Low, Medium, High };
41    public enum Algorithm { StandardGpRegression, OffspringGpRegression, StandardGpClassification, OffspringGpClassification, StandardGpForecasting, OffspringGpForecasting };
42
43    private IStore store;
44    private ModelComplexity[] possibleComplexities = new ModelComplexity[] { ModelComplexity.Low, ModelComplexity.Medium, ModelComplexity.High };
45    private Dictionary<LearningTask, Algorithm[]> possibleAlgorithms = new Dictionary<LearningTask, Algorithm[]>() {
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 }}
49    };
50
51    private static int MaxGenerations {
52      get { return 3; }
53    }
54
55    private static int MaxEvaluatedSolutions {
56      get { return 3000; }
57    }
58
59    public DispatcherBase(IStore store) {
60      this.store = store;
61    }
62
63    public Execution GetNextJob() {
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
70      Entity[] datasets = store.Query("?DataSet <" + Ontology.PredicateInstanceOf.Uri + "> <" + Ontology.TypeDataSet.Uri + "> .")
71        .Select(x => (Entity)x.Get("DataSet"))
72        .ToArray();
73
74      // no datasets => do nothing
75      if (datasets.Length == 0) return null;
76
77      Entity dataSetEntity = SelectDataSet(datasets);
78      DataSet dataSet = new DataSet(store, dataSetEntity);
79
80      int targetVariable = SelectTargetVariable(dataSet, dataSet.Problem.AllowedTargetVariables.ToArray());
81      Algorithm selectedAlgorithm = SelectAlgorithm(dataSet, targetVariable, possibleAlgorithms[dataSet.Problem.LearningTask]);
82      string targetVariableName = dataSet.Problem.GetVariableName(targetVariable);
83      ModelComplexity selectedComplexity = SelectComplexity(dataSet, targetVariable, selectedAlgorithm, possibleComplexities);
84
85      Execution exec = CreateExecution(dataSet.Problem, targetVariable, selectedAlgorithm, selectedComplexity);
86      if (exec != null) {
87        exec.DataSetEntity = dataSetEntity;
88        exec.TargetVariable = targetVariableName;
89      }
90      return exec;
91    }
92
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);
97
98    private Execution CreateExecution(Problem problem, int targetVariable, Algorithm algorithm, ModelComplexity complexity) {
99      switch (algorithm) {
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;
109          }
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          }
157        default: {
158            return null;
159          }
160      }
161    }
162
163    private void SetComplexityParameters(AlgorithmBase algo, ModelComplexity complexity) {
164      switch (complexity) {
165        case ModelComplexity.Low: {
166            algo.MaxTreeHeight = 5;
167            algo.MaxTreeSize = 20;
168            break;
169          }
170        case ModelComplexity.Medium: {
171            algo.MaxTreeHeight = 10;
172            algo.MaxTreeSize = 100;
173            break;
174          }
175        case ModelComplexity.High: {
176            algo.MaxTreeHeight = 12;
177            algo.MaxTreeSize = 200;
178            break;
179          }
180      }
181    }
182
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      }
203    }
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    }
208  }
209}
Note: See TracBrowser for help on using the repository browser.