Free cookie consent management tool by TermsFeed Policy Generator

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

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

Refactored CEDMA dispatcher and CEDMA server view to allow different modeling scenarios for each variable. #754

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