Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.CEDMA.Server/3.3/SimpleDispatcher.cs @ 5681

Last change on this file since 5681 was 2843, checked in by gkronber, 14 years ago

Removed max. and min. time offset constraints as algorithm parameters and from all engines. The time constraints were added to the relevant terminal symbols (variable & differential) instead. The time offset constraint can be changed by editing the symbols in the function library. #880 (Max and min time offsets for variable symbols are not set correctly by FunctionLibraryInjectors)

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