Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1861 was 1857, checked in by gkronber, 16 years ago

Worked on lose coupling of CEDMA and GP/SVR with plugin HeuristicLab.Modeling as common bridge. #635

File size: 6.2 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;
37using HeuristicLab.Modeling;
38
39namespace HeuristicLab.CEDMA.Server {
40  public abstract class DispatcherBase : IDispatcher {
41    private IStore store;
42
43    private static int MaxGenerations {
44      get { return 3; }
45    }
46
47    private static int MaxEvaluatedSolutions {
48      get { return 3000; }
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 + "> .", 0, 100)
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.AllowedTargetVariables.ToArray());
73      IAlgorithm selectedAlgorithm = SelectAlgorithm(dataSet, targetVariable, dataSet.Problem.LearningTask);
74      string targetVariableName = dataSet.Problem.GetVariableName(targetVariable);
75
76
77      if (selectedAlgorithm != null) {
78        Execution exec = CreateExecution(dataSet.Problem, targetVariable, selectedAlgorithm);
79        exec.DataSetEntity = dataSetEntity;
80        exec.TargetVariable = targetVariableName;
81        return exec;
82      } else return null;
83    }
84
85    public abstract Entity SelectDataSet(Entity[] datasets);
86    public abstract int SelectTargetVariable(DataSet dataSet, int[] targetVariables);
87    public abstract IAlgorithm SelectAlgorithm(DataSet dataSet, int targetVariable, LearningTask learningTask);
88
89    private Execution CreateExecution(Problem problem, int targetVariable, IAlgorithm algorithm) {
90      //switch (algorithm) {
91      //  case Algorithm.StandardGpRegression: {
92      //      var algo = new HeuristicLab.GP.StructureIdentification.StandardGP();
93      //      SetComplexityParameters(algo, complexity);
94      //      SetProblemParameters(algo, problem, targetVariable);
95      //      algo.PopulationSize = 10000;
96      //      algo.MaxGenerations = MaxGenerations;
97      //      Execution exec = new Execution(algo.Engine);
98      //      exec.Description = "StandardGP - Complexity: " + complexity;
99      //      return exec;
100      //    }
101      SetProblemParameters(algorithm, problem, targetVariable);
102      Execution exec = new Execution(algorithm.Engine);
103      exec.Description = algorithm.Name;
104      return exec;
105    }
106
107    private void SetProblemParameters(IAlgorithm algo, Problem problem, int targetVariable) {
108      algo.ProblemInjector.GetVariable("Dataset").Value = problem.DataSet;
109      algo.ProblemInjector.GetVariable("TargetVariable").GetValue<IntData>().Data = targetVariable;
110      algo.ProblemInjector.GetVariable("TrainingSamplesStart").GetValue<IntData>().Data = problem.TrainingSamplesStart;
111      algo.ProblemInjector.GetVariable("TrainingSamplesEnd").GetValue<IntData>().Data = problem.TrainingSamplesEnd;
112      algo.ProblemInjector.GetVariable("ValidationSamplesStart").GetValue<IntData>().Data = problem.ValidationSamplesStart;
113      algo.ProblemInjector.GetVariable("ValidationSamplesEnd").GetValue<IntData>().Data = problem.ValidationSamplesEnd;
114      algo.ProblemInjector.GetVariable("TestSamplesStart").GetValue<IntData>().Data = problem.TestSamplesStart;
115      algo.ProblemInjector.GetVariable("TestSamplesEnd").GetValue<IntData>().Data = problem.TestSamplesEnd;
116      ItemList<IntData> allowedFeatures = algo.ProblemInjector.GetVariable("AllowedFeatures").GetValue<ItemList<IntData>>();
117      foreach (int allowedFeature in problem.AllowedInputVariables) allowedFeatures.Add(new IntData(allowedFeature));
118
119      if (problem.LearningTask == LearningTask.TimeSeries) {
120        algo.ProblemInjector.GetVariable("Autoregressive").GetValue<BoolData>().Data = problem.AutoRegressive;
121        algo.ProblemInjector.GetVariable("MinTimeOffset").GetValue<IntData>().Data = problem.MinTimeOffset;
122        algo.ProblemInjector.GetVariable("MaxTimeOffset").GetValue<IntData>().Data = problem.MaxTimeOffset;
123      } else if (problem.LearningTask == LearningTask.Classification) {
124        ItemList<DoubleData> classValues = algo.ProblemInjector.GetVariable("TargetClassValues").GetValue<ItemList<DoubleData>>();
125        foreach (double classValue in GetDifferentClassValues(problem.DataSet, targetVariable)) classValues.Add(new DoubleData(classValue));
126      }
127    }
128
129    private IEnumerable<double> GetDifferentClassValues(HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable) {
130      return Enumerable.Range(0, dataset.Rows).Select(x => dataset.GetValue(x, targetVariable)).Distinct();
131    }
132  }
133}
Note: See TracBrowser for help on using the repository browser.