Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CEDMA-Refactoring-Ticket419/HeuristicLab.CEDMA.Server/DispatcherBase.cs @ 1217

Last change on this file since 1217 was 1217, checked in by gkronber, 15 years ago

Improved dispatcher code. Added an interface and a base class to make it easier to try out different dispatch strategies in the future. #419 (Refactor CEDMA plugins)

File size: 5.3 KB
RevLine 
[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
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;
[1053]34using HeuristicLab.GP.StructureIdentification;
35using HeuristicLab.Data;
[1060]36using HeuristicLab.Core;
[1044]37
38namespace HeuristicLab.CEDMA.Server {
[1217]39  public abstract class DispatcherBase :IDispatcher {
40    public enum ModelComplexity { Low, Medium, High };
41    public enum Algorithm { StandardGP };
[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[]>() {
46      {LearningTask.Classification, new Algorithm[] {}},
47      {LearningTask.Regression, new Algorithm[] { Algorithm.StandardGP }},
48      {LearningTask.TimeSeries, new Algorithm[] { }}
49    };
[1044]50
[1217]51    public DispatcherBase(IStore store) {
[1044]52      this.store = store;
53    }
54
[1217]55    public Execution GetNextJob() {
[1130]56      // find and select a dataset
57      var dataSetVar = new HeuristicLab.CEDMA.DB.Interfaces.Variable("DataSet");
58      var dataSetQuery = new Statement[] {
59        new Statement(dataSetVar, Ontology.PredicateInstanceOf, Ontology.TypeDataSet)
60      };
61
[1217]62      Entity[] datasets = store.Query("?DataSet <" + Ontology.PredicateInstanceOf.Uri + "> <" + Ontology.TypeDataSet.Uri + "> .")
63        .Select(x => (Entity)x.Get("DataSet"))
64        .ToArray();
[1130]65
66      // no datasets => do nothing
[1217]67      if (datasets.Length == 0) return null;
[1130]68
[1217]69      Entity dataSetEntity = SelectDataSet(datasets);
[1130]70      DataSet dataSet = new DataSet(store, dataSetEntity);
[1217]71
72      int targetVariable = SelectTargetVariable(dataSet, dataSet.Problem.AllowedInputVariables.ToArray());
73      Algorithm selectedAlgorithm = SelectAlgorithm(dataSet, targetVariable, possibleAlgorithms[dataSet.Problem.LearningTask]);
[1216]74      string targetVariableName = dataSet.Problem.GetVariableName(targetVariable);
[1217]75      ModelComplexity selectedComplexity = SelectComplexity(dataSet, targetVariable, selectedAlgorithm, possibleComplexities);
76
77      Execution exec = CreateExecution(dataSet.Problem, targetVariable, selectedAlgorithm, selectedComplexity);
[1216]78      if (exec != null) {
79        exec.DataSetEntity = dataSetEntity;
80        exec.TargetVariable = targetVariableName;
[1130]81      }
[1217]82      return exec;
[1044]83    }
84
[1217]85    public abstract Entity SelectDataSet(Entity[] datasets);
86    public abstract int SelectTargetVariable(DataSet dataSet, int[] targetVariables);
87    public abstract Algorithm SelectAlgorithm(DataSet dataSet, int targetVariable, Algorithm[] possibleAlgorithms);
88    public abstract ModelComplexity SelectComplexity(DataSet dataSet, int targetVariable, Algorithm algorithm, ModelComplexity[] possibleComplexities);
[1060]89
[1217]90    private Execution CreateExecution(Problem problem, int targetVariable, Algorithm algorithm, ModelComplexity complexity) {
91      switch (algorithm) {
92        case Algorithm.StandardGP: {
93            return CreateStandardGpExecution(problem, targetVariable, complexity);
[1060]94          }
[1217]95        default: {
96            return null;
97          }
[1060]98      }
99    }
100
[1217]101    private Execution CreateStandardGpExecution(Problem problem, int targetVariable, ModelComplexity complexity) {
[1060]102      ProblemInjector probInjector = new ProblemInjector(problem);
[1053]103      probInjector.TargetVariable = targetVariable;
104      StandardGP sgp = new StandardGP();
105      sgp.SetSeedRandomly = true;
[1216]106      sgp.MaxGenerations = 2;
107      sgp.PopulationSize = 100;
[1053]108      sgp.Elites = 1;
109      sgp.ProblemInjector = probInjector;
[1217]110
111      int maxTreeHeight = 10;
112      int maxTreeSize = 100;
113      switch (complexity) {
114        case ModelComplexity.Low: {
115            maxTreeHeight = 5;
116            maxTreeSize = 20;
117            break;
118          }
119        case ModelComplexity.Medium: {
120            maxTreeHeight = 10;
121            maxTreeSize = 100;
122            break;
123          }
124        case ModelComplexity.High: {
125            maxTreeHeight = 12;
126            maxTreeSize = 200;
127            break;
128          }
129      }
130
[1216]131      sgp.MaxTreeHeight = maxTreeHeight;
132      sgp.MaxTreeSize = maxTreeSize;
133      Execution exec = new Execution(sgp.Engine);
[1217]134      exec.Description = "StandardGP - Complexity: " + complexity;
[1216]135      return exec;
[1053]136    }
[1044]137  }
138}
Note: See TracBrowser for help on using the repository browser.