Free cookie consent management tool by TermsFeed Policy Generator

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

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

#650 (IAlgorithm and derived interfaces should provide properties to retrieve results):

  • Implemented properties to retrieve model quality
  • Changed CEDMA executor to retrieve results via properties
  • Removed obsolete class Execution in CEDMA (replaced by the interface IAlgorithm)
File size: 5.0 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    public DispatcherBase(IStore store) {
43      this.store = store;
44    }
45
46    public IAlgorithm 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        SetProblemParameters(selectedAlgorithm, dataSet.Problem, targetVariable);
70      }
71      return selectedAlgorithm;
72    }
73
74    public abstract Entity SelectDataSet(Entity[] datasets);
75    public abstract int SelectTargetVariable(Entity dataSet, int[] targetVariables);
76    public abstract IAlgorithm SelectAlgorithm(Entity dataSet, int targetVariable, LearningTask learningTask);
77
78    private void SetProblemParameters(IAlgorithm algo, Problem problem, int targetVariable) {
79      algo.Dataset = problem.DataSet;
80      algo.TargetVariable = targetVariable;
81      algo.ProblemInjector.GetVariable("TrainingSamplesStart").GetValue<IntData>().Data = problem.TrainingSamplesStart;
82      algo.ProblemInjector.GetVariable("TrainingSamplesEnd").GetValue<IntData>().Data = problem.TrainingSamplesEnd;
83      algo.ProblemInjector.GetVariable("ValidationSamplesStart").GetValue<IntData>().Data = problem.ValidationSamplesStart;
84      algo.ProblemInjector.GetVariable("ValidationSamplesEnd").GetValue<IntData>().Data = problem.ValidationSamplesEnd;
85      algo.ProblemInjector.GetVariable("TestSamplesStart").GetValue<IntData>().Data = problem.TestSamplesStart;
86      algo.ProblemInjector.GetVariable("TestSamplesEnd").GetValue<IntData>().Data = problem.TestSamplesEnd;
87      ItemList<IntData> allowedFeatures = algo.ProblemInjector.GetVariable("AllowedFeatures").GetValue<ItemList<IntData>>();
88      foreach (int allowedFeature in problem.AllowedInputVariables) allowedFeatures.Add(new IntData(allowedFeature));
89
90      if (problem.LearningTask == LearningTask.TimeSeries) {
91        algo.ProblemInjector.GetVariable("Autoregressive").GetValue<BoolData>().Data = problem.AutoRegressive;
92        algo.ProblemInjector.GetVariable("MinTimeOffset").GetValue<IntData>().Data = problem.MinTimeOffset;
93        algo.ProblemInjector.GetVariable("MaxTimeOffset").GetValue<IntData>().Data = problem.MaxTimeOffset;
94      } else if (problem.LearningTask == LearningTask.Classification) {
95        ItemList<DoubleData> classValues = algo.ProblemInjector.GetVariable("TargetClassValues").GetValue<ItemList<DoubleData>>();
96        foreach (double classValue in GetDifferentClassValues(problem.DataSet, targetVariable)) classValues.Add(new DoubleData(classValue));
97      }
98    }
99
100    private IEnumerable<double> GetDifferentClassValues(HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable) {
101      return Enumerable.Range(0, dataset.Rows).Select(x => dataset.GetValue(x, targetVariable)).Distinct();
102    }
103  }
104}
Note: See TracBrowser for help on using the repository browser.