Free cookie consent management tool by TermsFeed Policy Generator

source: branches/plugins/HeuristicLab.CEDMA.DB.Server/3.2/DispatcherBase.cs @ 3494

Last change on this file since 3494 was 1217, checked in by gkronber, 16 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
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 { StandardGP };
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[] {}},
47      {LearningTask.Regression, new Algorithm[] { Algorithm.StandardGP }},
48      {LearningTask.TimeSeries, new Algorithm[] { }}
49    };
50
51    public DispatcherBase(IStore store) {
52      this.store = store;
53    }
54
55    public Execution GetNextJob() {
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
62      Entity[] datasets = store.Query("?DataSet <" + Ontology.PredicateInstanceOf.Uri + "> <" + Ontology.TypeDataSet.Uri + "> .")
63        .Select(x => (Entity)x.Get("DataSet"))
64        .ToArray();
65
66      // no datasets => do nothing
67      if (datasets.Length == 0) return null;
68
69      Entity dataSetEntity = SelectDataSet(datasets);
70      DataSet dataSet = new DataSet(store, dataSetEntity);
71
72      int targetVariable = SelectTargetVariable(dataSet, dataSet.Problem.AllowedInputVariables.ToArray());
73      Algorithm selectedAlgorithm = SelectAlgorithm(dataSet, targetVariable, possibleAlgorithms[dataSet.Problem.LearningTask]);
74      string targetVariableName = dataSet.Problem.GetVariableName(targetVariable);
75      ModelComplexity selectedComplexity = SelectComplexity(dataSet, targetVariable, selectedAlgorithm, possibleComplexities);
76
77      Execution exec = CreateExecution(dataSet.Problem, targetVariable, selectedAlgorithm, selectedComplexity);
78      if (exec != null) {
79        exec.DataSetEntity = dataSetEntity;
80        exec.TargetVariable = targetVariableName;
81      }
82      return exec;
83    }
84
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);
89
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);
94          }
95        default: {
96            return null;
97          }
98      }
99    }
100
101    private Execution CreateStandardGpExecution(Problem problem, int targetVariable, ModelComplexity complexity) {
102      ProblemInjector probInjector = new ProblemInjector(problem);
103      probInjector.TargetVariable = targetVariable;
104      StandardGP sgp = new StandardGP();
105      sgp.SetSeedRandomly = true;
106      sgp.MaxGenerations = 2;
107      sgp.PopulationSize = 100;
108      sgp.Elites = 1;
109      sgp.ProblemInjector = probInjector;
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
131      sgp.MaxTreeHeight = maxTreeHeight;
132      sgp.MaxTreeSize = maxTreeSize;
133      Execution exec = new Execution(sgp.Engine);
134      exec.Description = "StandardGP - Complexity: " + complexity;
135      return exec;
136    }
137  }
138}
Note: See TracBrowser for help on using the repository browser.