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;
|
---|
34 | using HeuristicLab.GP.StructureIdentification;
|
---|
35 | using HeuristicLab.Data;
|
---|
36 | using HeuristicLab.Core;
|
---|
37 | using HeuristicLab.Modeling;
|
---|
38 |
|
---|
39 | namespace HeuristicLab.CEDMA.Server {
|
---|
40 | public abstract class DispatcherBase : IDispatcher {
|
---|
41 | private IStore store;
|
---|
42 | public DispatcherBase(IStore store) {
|
---|
43 | this.store = store;
|
---|
44 | }
|
---|
45 |
|
---|
46 | public Execution GetNextJob() {
|
---|
47 | // find and select a dataset
|
---|
48 | var dataSetVar = new HeuristicLab.CEDMA.DB.Interfaces.Variable("DataSet");
|
---|
49 | var dataSetQuery = new Statement[] {
|
---|
50 | new Statement(dataSetVar, Ontology.PredicateInstanceOf, Ontology.TypeDataSet)
|
---|
51 | };
|
---|
52 |
|
---|
53 | Entity[] datasets = store.Query("?DataSet <" + Ontology.PredicateInstanceOf.Uri + "> <" + Ontology.TypeDataSet.Uri + "> .", 0, 100)
|
---|
54 | .Select(x => (Entity)x.Get("DataSet"))
|
---|
55 | .ToArray();
|
---|
56 |
|
---|
57 | // no datasets => do nothing
|
---|
58 | if (datasets.Length == 0) return null;
|
---|
59 |
|
---|
60 | Entity dataSetEntity = SelectDataSet(datasets);
|
---|
61 | DataSet dataSet = new DataSet(store, dataSetEntity);
|
---|
62 |
|
---|
63 | int targetVariable = SelectTargetVariable(dataSetEntity, dataSet.Problem.AllowedTargetVariables.ToArray());
|
---|
64 | IAlgorithm selectedAlgorithm = SelectAlgorithm(dataSetEntity, targetVariable, dataSet.Problem.LearningTask);
|
---|
65 | string targetVariableName = dataSet.Problem.GetVariableName(targetVariable);
|
---|
66 |
|
---|
67 |
|
---|
68 | if (selectedAlgorithm != null) {
|
---|
69 | Execution exec = CreateExecution(dataSet.Problem, targetVariable, selectedAlgorithm);
|
---|
70 | exec.DataSetEntity = dataSetEntity;
|
---|
71 | exec.TargetVariable = targetVariableName;
|
---|
72 | return exec;
|
---|
73 | } else return null;
|
---|
74 | }
|
---|
75 |
|
---|
76 | public abstract Entity SelectDataSet(Entity[] datasets);
|
---|
77 | public abstract int SelectTargetVariable(Entity dataSet, int[] targetVariables);
|
---|
78 | public abstract IAlgorithm SelectAlgorithm(Entity dataSet, int targetVariable, LearningTask learningTask);
|
---|
79 |
|
---|
80 | private Execution CreateExecution(Problem problem, int targetVariable, IAlgorithm algorithm) {
|
---|
81 | SetProblemParameters(algorithm, problem, targetVariable);
|
---|
82 | Execution exec = new Execution(algorithm.Engine);
|
---|
83 | exec.Description = algorithm.Name;
|
---|
84 | return exec;
|
---|
85 | }
|
---|
86 |
|
---|
87 | private void SetProblemParameters(IAlgorithm algo, Problem problem, int targetVariable) {
|
---|
88 | algo.ProblemInjector.GetVariable("Dataset").Value = problem.DataSet;
|
---|
89 | algo.ProblemInjector.GetVariable("TargetVariable").GetValue<IntData>().Data = targetVariable;
|
---|
90 | algo.ProblemInjector.GetVariable("TrainingSamplesStart").GetValue<IntData>().Data = problem.TrainingSamplesStart;
|
---|
91 | algo.ProblemInjector.GetVariable("TrainingSamplesEnd").GetValue<IntData>().Data = problem.TrainingSamplesEnd;
|
---|
92 | algo.ProblemInjector.GetVariable("ValidationSamplesStart").GetValue<IntData>().Data = problem.ValidationSamplesStart;
|
---|
93 | algo.ProblemInjector.GetVariable("ValidationSamplesEnd").GetValue<IntData>().Data = problem.ValidationSamplesEnd;
|
---|
94 | algo.ProblemInjector.GetVariable("TestSamplesStart").GetValue<IntData>().Data = problem.TestSamplesStart;
|
---|
95 | algo.ProblemInjector.GetVariable("TestSamplesEnd").GetValue<IntData>().Data = problem.TestSamplesEnd;
|
---|
96 | ItemList<IntData> allowedFeatures = algo.ProblemInjector.GetVariable("AllowedFeatures").GetValue<ItemList<IntData>>();
|
---|
97 | foreach (int allowedFeature in problem.AllowedInputVariables) allowedFeatures.Add(new IntData(allowedFeature));
|
---|
98 |
|
---|
99 | if (problem.LearningTask == LearningTask.TimeSeries) {
|
---|
100 | algo.ProblemInjector.GetVariable("Autoregressive").GetValue<BoolData>().Data = problem.AutoRegressive;
|
---|
101 | algo.ProblemInjector.GetVariable("MinTimeOffset").GetValue<IntData>().Data = problem.MinTimeOffset;
|
---|
102 | algo.ProblemInjector.GetVariable("MaxTimeOffset").GetValue<IntData>().Data = problem.MaxTimeOffset;
|
---|
103 | } else if (problem.LearningTask == LearningTask.Classification) {
|
---|
104 | ItemList<DoubleData> classValues = algo.ProblemInjector.GetVariable("TargetClassValues").GetValue<ItemList<DoubleData>>();
|
---|
105 | foreach (double classValue in GetDifferentClassValues(problem.DataSet, targetVariable)) classValues.Add(new DoubleData(classValue));
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | private IEnumerable<double> GetDifferentClassValues(HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable) {
|
---|
110 | return Enumerable.Range(0, dataset.Rows).Select(x => dataset.GetValue(x, targetVariable)).Distinct();
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|