Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone3/sources/HeuristicLab.CEDMA.Server/3.3/DispatcherBase.cs @ 4342

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

Fixed updating of executer and dispatcher views and renamed ServerForm to ServerView. #676 (Cockpit for the CEDMA Server to control algorithm settings)

File size: 5.4 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    private DataSet dataset;
43    internal event EventHandler Changed;
44    public IEnumerable<string> AllowedTargetVariables {
45      get {
46        if (dataset != null) {
47          return dataset.Problem.AllowedTargetVariables.Select(x => dataset.Problem.Dataset.GetVariableName(x));
48        } else return new string[0];
49      }
50    }
51
52    public IEnumerable<string> AllowedInputVariables {
53      get {
54        if (dataset != null) {
55          return dataset.Problem.AllowedInputVariables.Select(x => dataset.Problem.Dataset.GetVariableName(x));
56        } else return new string[0];
57      }
58    }
59
60    public DispatcherBase(IStore store) {
61      this.store = store;
62    }
63
64    public IAlgorithm GetNextJob() {
65      if (dataset == null) {
66        var datasetEntities = store.Query("?DataSet <" + Ontology.InstanceOf.Uri + "> <" + Ontology.TypeDataSet.Uri + "> .", 0, 1)
67          .Select(x => (Entity)x.Get("DataSet"));
68        if (datasetEntities.Count() == 0) return null;
69        dataset = new DataSet(store, datasetEntities.ElementAt(0));
70        OnChanged();
71      }
72      int targetVariable = SelectTargetVariable(dataset.Problem.AllowedTargetVariables.ToArray());
73      IAlgorithm selectedAlgorithm = SelectAlgorithm(targetVariable, dataset.Problem.LearningTask);
74      string targetVariableName = dataset.Problem.GetVariableName(targetVariable);
75
76      if (selectedAlgorithm != null) {
77        SetProblemParameters(selectedAlgorithm, dataset.Problem, targetVariable);
78      }
79      return selectedAlgorithm;
80    }
81
82    public abstract int SelectTargetVariable(int[] targetVariables);
83    public abstract IAlgorithm SelectAlgorithm(int targetVariable, LearningTask learningTask);
84
85    private void SetProblemParameters(IAlgorithm algo, Problem problem, int targetVariable) {
86      algo.Dataset = problem.Dataset;
87      algo.TargetVariable = targetVariable;
88      algo.ProblemInjector.GetVariable("TrainingSamplesStart").GetValue<IntData>().Data = problem.TrainingSamplesStart;
89      algo.ProblemInjector.GetVariable("TrainingSamplesEnd").GetValue<IntData>().Data = problem.TrainingSamplesEnd;
90      algo.ProblemInjector.GetVariable("ValidationSamplesStart").GetValue<IntData>().Data = problem.ValidationSamplesStart;
91      algo.ProblemInjector.GetVariable("ValidationSamplesEnd").GetValue<IntData>().Data = problem.ValidationSamplesEnd;
92      algo.ProblemInjector.GetVariable("TestSamplesStart").GetValue<IntData>().Data = problem.TestSamplesStart;
93      algo.ProblemInjector.GetVariable("TestSamplesEnd").GetValue<IntData>().Data = problem.TestSamplesEnd;
94      ItemList<IntData> allowedFeatures = algo.ProblemInjector.GetVariable("AllowedFeatures").GetValue<ItemList<IntData>>();
95      foreach (int allowedFeature in problem.AllowedInputVariables) allowedFeatures.Add(new IntData(allowedFeature));
96
97      if (problem.LearningTask == LearningTask.TimeSeries) {
98        algo.ProblemInjector.GetVariable("Autoregressive").GetValue<BoolData>().Data = problem.AutoRegressive;
99        algo.ProblemInjector.GetVariable("MinTimeOffset").GetValue<IntData>().Data = problem.MinTimeOffset;
100        algo.ProblemInjector.GetVariable("MaxTimeOffset").GetValue<IntData>().Data = problem.MaxTimeOffset;
101      } else if (problem.LearningTask == LearningTask.Classification) {
102        ItemList<DoubleData> classValues = algo.ProblemInjector.GetVariable("TargetClassValues").GetValue<ItemList<DoubleData>>();
103        foreach (double classValue in GetDifferentClassValues(problem.Dataset, targetVariable)) classValues.Add(new DoubleData(classValue));
104      }
105    }
106
107    private IEnumerable<double> GetDifferentClassValues(HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable) {
108      return Enumerable.Range(0, dataset.Rows).Select(x => dataset.GetValue(x, targetVariable)).Distinct();
109    }
110
111    private void OnChanged() {
112      if (Changed != null) Changed(this, new EventArgs());
113    }
114
115    #region IViewable Members
116
117    public IView CreateView() {
118      return new DispatcherView(this);
119    }
120
121    #endregion
122  }
123}
Note: See TracBrowser for help on using the repository browser.