Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2290 was 2290, checked in by gkronber, 15 years ago
  • introduced a variablename to index mapping for SVM models (to make sure we can use the model for prediction in the model analyzer)
  • added support to enable and disable algorithms in the dispatcher and removed DispatcherBase
  • fixed bugs when calculating variable impacts and reading the final model of GP algorithms

#722 (IModel should provide a Predict() method to get predicted values for an input vector)

File size: 12.5 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        algo.ProblemInjector.GetVariable("Autoregressive").GetValue<BoolData>().Data = problem.AutoRegressive;
190        algo.ProblemInjector.GetVariable("MinTimeOffset").GetValue<IntData>().Data = problem.MinTimeOffset;
191        algo.ProblemInjector.GetVariable("MaxTimeOffset").GetValue<IntData>().Data = problem.MaxTimeOffset;
192        if (problem.AutoRegressive) {
193          allowedFeatures.Add(new IntData(targetVariable));
194        }
195      } else if (problem.LearningTask == LearningTask.Classification) {
196        ItemList<DoubleData> classValues = algo.ProblemInjector.GetVariable("TargetClassValues").GetValue<ItemList<DoubleData>>();
197        foreach (double classValue in GetDifferentClassValues(problem.Dataset, targetVariable)) classValues.Add(new DoubleData(classValue));
198      }
199    }
200
201    private IEnumerable<double> GetDifferentClassValues(HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable) {
202      return Enumerable.Range(0, dataset.Rows).Select(x => dataset.GetValue(x, targetVariable)).Distinct();
203    }
204
205    private void AddDispatchedRun(string targetVariable, IEnumerable<string> inputVariables, string algorithm) {
206      AddDispatchedRun(
207        Problem.Dataset.GetVariableIndex(targetVariable),
208        inputVariables.Select(x => Problem.Dataset.GetVariableIndex(x)).ToArray(),
209        algorithm);
210    }
211
212    private void AddDispatchedRun(int targetVariable, int[] inputVariables, string algoName) {
213      if (!finishedAndDispatchedRuns.ContainsKey(targetVariable)) {
214        finishedAndDispatchedRuns[targetVariable] = new List<AlgorithmConfiguration>();
215      }
216      AlgorithmConfiguration conf = new AlgorithmConfiguration();
217      conf.name = algoName;
218      conf.inputVariables = new List<int>(inputVariables);
219      conf.targetVariable = targetVariable;
220      finishedAndDispatchedRuns[targetVariable].Add(conf);
221    }
222
223    private bool AlgorithmFinishedOrDispatched(int targetVariable, int[] inputVariables, string algoName) {
224      return
225        finishedAndDispatchedRuns.ContainsKey(targetVariable) &&
226        finishedAndDispatchedRuns[targetVariable].Any(x => targetVariable == x.targetVariable &&
227                                                           algoName == x.name &&
228                                                           inputVariables.Count() == x.inputVariables.Count() &&
229                                                           inputVariables.All(v => x.inputVariables.Contains(v)));
230    }
231
232    public void EnableAlgorithm(HeuristicLab.Modeling.IAlgorithm algo) {
233      lock (locker) {
234        if (!activeAlgorithms.Contains(algo)) activeAlgorithms.Add(algo);
235      }
236    }
237
238    public void DisableAlgorithm(HeuristicLab.Modeling.IAlgorithm algo) {
239      lock (locker) {
240        while (activeAlgorithms.Remove(algo)) ;
241      }
242    }
243
244    internal void EnableTargetVariable(string name) {
245      int varIndex = problem.Dataset.GetVariableIndex(name);
246      lock (locker)
247        if (!allowedTargetVariables.Contains(varIndex)) allowedTargetVariables.Add(varIndex);
248    }
249
250    internal void DisableTargetVariable(string name) {
251      int varIndex = problem.Dataset.GetVariableIndex(name);
252      lock (locker)
253        while (allowedTargetVariables.Remove(varIndex)) ;
254    }
255
256    internal void EnableInputVariable(string target, string name) {
257      int targetIndex = problem.Dataset.GetVariableIndex(target);
258      int inputIndex = problem.Dataset.GetVariableIndex(name);
259      lock (locker) {
260        if (!activeInputVariables.ContainsKey(targetIndex)) activeInputVariables[targetIndex] = new List<int>();
261        if (!activeInputVariables[targetIndex].Contains(inputIndex)) {
262          activeInputVariables[targetIndex].Add(inputIndex);
263        }
264      }
265    }
266
267    internal void DisableInputVariable(string target, string name) {
268      int targetIndex = problem.Dataset.GetVariableIndex(target);
269      int inputIndex = problem.Dataset.GetVariableIndex(name);
270      lock (locker) {
271        if (!activeInputVariables.ContainsKey(targetIndex)) activeInputVariables[targetIndex] = new List<int>();
272        while (activeInputVariables[targetIndex].Remove(inputIndex)) ;
273      }
274    }
275
276    public void OnChanged() {
277      if (Changed != null) Changed(this, new EventArgs());
278    }
279
280    internal IEnumerable<string> GetInputVariables(string target) {
281      int targetIndex = problem.Dataset.GetVariableIndex(target);
282      lock (locker) {
283        if (!activeInputVariables.ContainsKey(targetIndex)) activeInputVariables[targetIndex] = new List<int>();
284        return activeInputVariables[targetIndex]
285          .Select(i => problem.Dataset.GetVariableName(i));
286      }
287    }
288
289
290    #region IViewable Members
291
292    public virtual IView CreateView() {
293      return new DispatcherView(this);
294    }
295
296    #endregion
297
298
299  }
300}
Note: See TracBrowser for help on using the repository browser.