Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.CEDMA.Server/3.3/SimpleDispatcher.cs @ 2366

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

Fixed incompatibility between CEDMA server and time-series algorithms. #746 (CEDMA server is not compatible with new data-modeling algorithms)

File size: 11.8 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 System.ServiceModel.Description;
30using System.Linq;
31using HeuristicLab.CEDMA.Core;
32using HeuristicLab.Data;
33using HeuristicLab.Core;
34using HeuristicLab.Modeling;
35using HeuristicLab.Modeling.Database;
36
37namespace HeuristicLab.CEDMA.Server {
38  public class SimpleDispatcher : IDispatcher {
39    private class AlgorithmConfiguration {
40      public string name;
41      public int targetVariable;
42      public List<int> inputVariables;
43    }
44
45    private IModelingDatabase database;
46    public IModelingDatabase Database {
47      get {
48        return database;
49      }
50    }
51
52    private Problem problem;
53    public Problem Problem {
54      get {
55        return problem;
56      }
57    }
58    internal event EventHandler Changed;
59
60    public IEnumerable<string> TargetVariables {
61      get {
62        return Enumerable.Range(0, problem.Dataset.Columns).Select(x => problem.Dataset.GetVariableName(x));
63      }
64    }
65
66    public IEnumerable<string> InputVariables {
67      get {
68        return TargetVariables;
69      }
70    }
71
72    private HeuristicLab.Modeling.IAlgorithm[] algorithms;
73    public IEnumerable<HeuristicLab.Modeling.IAlgorithm> Algorithms {
74      get {
75        switch (Problem.LearningTask) {
76          case LearningTask.Regression: {
77              return algorithms.Where(a => (a as IClassificationAlgorithm) == null && (a as ITimeSeriesAlgorithm) == null);
78            }
79          case LearningTask.Classification: {
80              return algorithms.Where(a => (a as IClassificationAlgorithm) != null);
81            }
82          case LearningTask.TimeSeries: {
83              return algorithms.Where(a => (a as ITimeSeriesAlgorithm) != null);
84            }
85        }
86        return new HeuristicLab.Modeling.IAlgorithm[] { };
87      }
88    }
89
90    private List<HeuristicLab.Modeling.IAlgorithm> activeAlgorithms;
91    public IEnumerable<HeuristicLab.Modeling.IAlgorithm> ActiveAlgorithms {
92      get { return activeAlgorithms; }
93    }
94
95    private Random random;
96    private List<int> allowedTargetVariables;
97    private Dictionary<int, List<int>> activeInputVariables;
98    private Dictionary<int, List<AlgorithmConfiguration>> finishedAndDispatchedRuns;
99    private object locker = new object();
100
101    public SimpleDispatcher(IModelingDatabase database, Problem problem) {
102      this.problem = problem;
103      this.database = database;
104      this.random = new Random();
105
106      this.finishedAndDispatchedRuns = new Dictionary<int, List<AlgorithmConfiguration>>();
107      this.allowedTargetVariables = new List<int>();
108      this.activeInputVariables = new Dictionary<int, List<int>>();
109      this.activeAlgorithms = new List<HeuristicLab.Modeling.IAlgorithm>();
110      DiscoveryService ds = new DiscoveryService();
111      this.algorithms = ds.GetInstances<HeuristicLab.Modeling.IAlgorithm>();
112      problem.Changed += (sender, args) => {
113        lock (locker) {
114          allowedTargetVariables.Clear();
115          activeInputVariables.Clear();
116          activeAlgorithms.Clear();
117        }
118        OnChanged();
119      };
120
121      PopulateFinishedRuns();
122    }
123
124    public HeuristicLab.Modeling.IAlgorithm GetNextJob() {
125      lock (locker) {
126        if (allowedTargetVariables.Count > 0) {
127          int[] targetVariables = allowedTargetVariables.ToArray();
128          int targetVariable = SelectTargetVariable(targetVariables);
129          int[] inputVariables = activeInputVariables[targetVariable].ToArray();
130
131          HeuristicLab.Modeling.IAlgorithm selectedAlgorithm = SelectAndConfigureAlgorithm(targetVariable, inputVariables, problem);
132
133          return selectedAlgorithm;
134        } else return null;
135      }
136    }
137
138    public virtual int SelectTargetVariable(int[] targetVariables) {
139      return targetVariables[random.Next(targetVariables.Length)];
140    }
141
142    public HeuristicLab.Modeling.IAlgorithm SelectAndConfigureAlgorithm(int targetVariable, int[] inputVariables, Problem problem) {
143      HeuristicLab.Modeling.IAlgorithm selectedAlgorithm = null;
144      DiscoveryService ds = new DiscoveryService();
145      var allAlgorithms = ds.GetInstances<HeuristicLab.Modeling.IAlgorithm>();
146      var allowedAlgorithmTypes = activeAlgorithms.Select(x => x.GetType());
147      var possibleAlgos =
148        allAlgorithms
149        .Where(x => allowedAlgorithmTypes.Contains(x.GetType()) &&
150          ((x is IStochasticAlgorithm) || !AlgorithmFinishedOrDispatched(targetVariable, inputVariables, x.Name)));
151      if (possibleAlgos.Count() > 0) selectedAlgorithm = possibleAlgos.ElementAt(random.Next(possibleAlgos.Count()));
152      if (selectedAlgorithm != null) {
153        SetProblemParameters(selectedAlgorithm, problem, targetVariable, inputVariables);
154        if (!(selectedAlgorithm is IStochasticAlgorithm))
155          AddDispatchedRun(targetVariable, inputVariables, selectedAlgorithm.Name);
156      }
157      return selectedAlgorithm;
158    }
159
160    private void PopulateFinishedRuns() {
161      var dispatchedAlgos = from model in Database.GetAllModels()
162                            select new {
163                              TargetVariable = model.TargetVariable.Name,
164                              Algorithm = model.Algorithm.Name,
165                              Inputvariables = Database.GetInputVariableResults(model).Select(x => x.Variable.Name).Distinct()
166                            };
167      foreach (var algo in dispatchedAlgos) {
168        AddDispatchedRun(algo.TargetVariable, algo.Inputvariables, algo.Algorithm);
169      }
170    }
171
172    private void SetProblemParameters(HeuristicLab.Modeling.IAlgorithm algo, Problem problem, int targetVariable, int[] inputVariables) {
173      algo.Dataset = problem.Dataset;
174      algo.TargetVariable = targetVariable;
175      algo.ProblemInjector.GetVariable("TrainingSamplesStart").GetValue<IntData>().Data = problem.TrainingSamplesStart;
176      algo.ProblemInjector.GetVariable("TrainingSamplesEnd").GetValue<IntData>().Data = problem.TrainingSamplesEnd;
177      algo.ProblemInjector.GetVariable("ValidationSamplesStart").GetValue<IntData>().Data = problem.ValidationSamplesStart;
178      algo.ProblemInjector.GetVariable("ValidationSamplesEnd").GetValue<IntData>().Data = problem.ValidationSamplesEnd;
179      algo.ProblemInjector.GetVariable("TestSamplesStart").GetValue<IntData>().Data = problem.TestSamplesStart;
180      algo.ProblemInjector.GetVariable("TestSamplesEnd").GetValue<IntData>().Data = problem.TestSamplesEnd;
181      ItemList<IntData> allowedFeatures = algo.ProblemInjector.GetVariable("AllowedFeatures").GetValue<ItemList<IntData>>();
182      foreach (int inputVariable in inputVariables) {
183        if (inputVariable != targetVariable) {
184          allowedFeatures.Add(new IntData(inputVariable));
185        }
186      }
187
188      if (problem.LearningTask == LearningTask.TimeSeries) {
189        ITimeSeriesAlgorithm timeSeriesAlgo = (ITimeSeriesAlgorithm)algo;
190        timeSeriesAlgo.MinTimeOffset = problem.MinTimeOffset;
191        timeSeriesAlgo.MaxTimeOffset = problem.MaxTimeOffset;
192        if (problem.AutoRegressive) {
193          allowedFeatures.Add(new IntData(targetVariable));
194        }
195      }
196    }
197
198
199    private void AddDispatchedRun(string targetVariable, IEnumerable<string> inputVariables, string algorithm) {
200      AddDispatchedRun(
201        Problem.Dataset.GetVariableIndex(targetVariable),
202        inputVariables.Select(x => Problem.Dataset.GetVariableIndex(x)).ToArray(),
203        algorithm);
204    }
205
206    private void AddDispatchedRun(int targetVariable, int[] inputVariables, string algoName) {
207      if (!finishedAndDispatchedRuns.ContainsKey(targetVariable)) {
208        finishedAndDispatchedRuns[targetVariable] = new List<AlgorithmConfiguration>();
209      }
210      AlgorithmConfiguration conf = new AlgorithmConfiguration();
211      conf.name = algoName;
212      conf.inputVariables = new List<int>(inputVariables);
213      conf.targetVariable = targetVariable;
214      finishedAndDispatchedRuns[targetVariable].Add(conf);
215    }
216
217    private bool AlgorithmFinishedOrDispatched(int targetVariable, int[] inputVariables, string algoName) {
218      return
219        finishedAndDispatchedRuns.ContainsKey(targetVariable) &&
220        finishedAndDispatchedRuns[targetVariable].Any(x => targetVariable == x.targetVariable &&
221                                                           algoName == x.name &&
222                                                           inputVariables.Count() == x.inputVariables.Count() &&
223                                                           inputVariables.All(v => x.inputVariables.Contains(v)));
224    }
225
226    public void EnableAlgorithm(HeuristicLab.Modeling.IAlgorithm algo) {
227      lock (locker) {
228        if (!activeAlgorithms.Contains(algo)) activeAlgorithms.Add(algo);
229      }
230    }
231
232    public void DisableAlgorithm(HeuristicLab.Modeling.IAlgorithm algo) {
233      lock (locker) {
234        while (activeAlgorithms.Remove(algo)) ;
235      }
236    }
237
238    internal void EnableTargetVariable(string name) {
239      int varIndex = problem.Dataset.GetVariableIndex(name);
240      lock (locker)
241        if (!allowedTargetVariables.Contains(varIndex)) allowedTargetVariables.Add(varIndex);
242    }
243
244    internal void DisableTargetVariable(string name) {
245      int varIndex = problem.Dataset.GetVariableIndex(name);
246      lock (locker)
247        while (allowedTargetVariables.Remove(varIndex)) ;
248    }
249
250    internal void EnableInputVariable(string target, string name) {
251      int targetIndex = problem.Dataset.GetVariableIndex(target);
252      int inputIndex = problem.Dataset.GetVariableIndex(name);
253      lock (locker) {
254        if (!activeInputVariables.ContainsKey(targetIndex)) activeInputVariables[targetIndex] = new List<int>();
255        if (!activeInputVariables[targetIndex].Contains(inputIndex)) {
256          activeInputVariables[targetIndex].Add(inputIndex);
257        }
258      }
259    }
260
261    internal void DisableInputVariable(string target, string name) {
262      int targetIndex = problem.Dataset.GetVariableIndex(target);
263      int inputIndex = problem.Dataset.GetVariableIndex(name);
264      lock (locker) {
265        if (!activeInputVariables.ContainsKey(targetIndex)) activeInputVariables[targetIndex] = new List<int>();
266        while (activeInputVariables[targetIndex].Remove(inputIndex)) ;
267      }
268    }
269
270    public void OnChanged() {
271      if (Changed != null) Changed(this, new EventArgs());
272    }
273
274    internal IEnumerable<string> GetInputVariables(string target) {
275      int targetIndex = problem.Dataset.GetVariableIndex(target);
276      lock (locker) {
277        if (!activeInputVariables.ContainsKey(targetIndex)) activeInputVariables[targetIndex] = new List<int>();
278        return activeInputVariables[targetIndex]
279          .Select(i => problem.Dataset.GetVariableName(i));
280      }
281    }
282
283
284    #region IViewable Members
285
286    public virtual IView CreateView() {
287      return new DispatcherView(this);
288    }
289
290    #endregion
291
292
293  }
294}
Note: See TracBrowser for help on using the repository browser.