Free cookie consent management tool by TermsFeed Policy Generator

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

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

Removed random selection of function library injectors from CEDMA dispatcher. #877 (Predefined GP engines for time series prognosis are defect because of the removal of local variables of function library injectors in #748)

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.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    public SimpleDispatcher(IModelingDatabase database, Dataset dataset) {
109      this.dataset = dataset;
110      this.database = database;
111      dataset.Changed += (sender, args) => FireChanged();
112      random = new Random();
113
114      activeVariables = new Dictionary<string, bool>();
115      problemSpecifications = new Dictionary<string, ProblemSpecification>();
116      algorithms = new Dictionary<string, List<HeuristicLab.Modeling.IAlgorithm>>();
117      finishedAndDispatchedRuns = new Dictionary<string, List<AlgorithmConfiguration>>();
118
119     
120      defaultAlgorithms = ApplicationManager.Manager.GetInstances<HeuristicLab.Modeling.IAlgorithm>().ToArray();
121
122      // PopulateFinishedRuns();
123    }
124
125    public HeuristicLab.Modeling.IAlgorithm GetNextJob() {
126      lock (locker) {
127        if (activeVariables.Where(x => x.Value == true).Count() > 0) {
128          string[] targetVariables = (from pair in activeVariables
129                                      where pair.Value == true
130                                      select pair.Key).ToArray();
131          string targetVariable = SelectTargetVariable(targetVariables);
132          HeuristicLab.Modeling.IAlgorithm selectedAlgorithm = SelectAndConfigureAlgorithm(targetVariable);
133
134          return selectedAlgorithm;
135        } else return null;
136      }
137    }
138
139    public virtual string SelectTargetVariable(string[] targetVariables) {
140      return targetVariables[random.Next(targetVariables.Length)];
141    }
142
143    public HeuristicLab.Modeling.IAlgorithm SelectAndConfigureAlgorithm(string targetVariable) {
144      HeuristicLab.Modeling.IAlgorithm selectedAlgorithm = null;
145      var possibleAlgos =
146        algorithms[targetVariable]
147        .Where(x =>
148          ((x is IStochasticAlgorithm) || !AlgorithmFinishedOrDispatched(problemSpecifications[targetVariable], x.Name)));
149      if (possibleAlgos.Count() > 0) selectedAlgorithm = possibleAlgos.ElementAt(random.Next(possibleAlgos.Count()));
150      if (selectedAlgorithm != null) {
151        // create a clone of the algorithm template before setting the parameters
152        selectedAlgorithm = (HeuristicLab.Modeling.IAlgorithm)selectedAlgorithm.Clone();
153        SetProblemParameters(selectedAlgorithm, problemSpecifications[targetVariable]);
154        if (!(selectedAlgorithm is IStochasticAlgorithm))
155          AddDispatchedRun(problemSpecifications[targetVariable], 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    //    ProblemSpecification spec = new ProblemSpecification();
169    //    spec.TargetVariable = algo.TargetVariable;
170    //    foreach (string variable in algo.InputVariables) spec.AddInputVariable(variable);
171    //    AddDispatchedRun(spec, algo.Algorithm);
172    //  }
173    //}
174
175    private void SetProblemParameters(HeuristicLab.Modeling.IAlgorithm algo, ProblemSpecification spec) {
176      algo.Dataset = spec.Dataset;
177      algo.TargetVariable = spec.TargetVariable;
178      algo.TrainingSamplesStart = spec.TrainingSamplesStart;
179      algo.TrainingSamplesEnd = spec.TrainingSamplesEnd;
180      algo.ValidationSamplesStart = spec.ValidationSamplesStart;
181      algo.ValidationSamplesEnd = spec.ValidationSamplesEnd;
182      algo.TestSamplesStart = spec.TestSamplesStart;
183      algo.TestSamplesEnd = spec.TestSamplesEnd;
184      List<string> allowedFeatures = new List<string>();
185      foreach (string inputVariable in spec.InputVariables) {
186        if (inputVariable != spec.TargetVariable) {
187          allowedFeatures.Add(inputVariable);
188        }
189      }
190
191      if (spec.LearningTask == LearningTask.TimeSeries) {
192        ITimeSeriesAlgorithm timeSeriesAlgo = (ITimeSeriesAlgorithm)algo;
193        timeSeriesAlgo.MinTimeOffset = spec.MinTimeOffset;
194        timeSeriesAlgo.MaxTimeOffset = spec.MaxTimeOffset;
195        timeSeriesAlgo.TrainingSamplesStart = spec.TrainingSamplesStart - spec.MinTimeOffset + 1; // first possible index is 1 because of differential symbol
196        if (spec.AutoRegressive) {
197          allowedFeatures.Add(spec.TargetVariable);
198        }
199      }
200      algo.AllowedVariables = allowedFeatures;
201    }
202
203    private void AddDispatchedRun(ProblemSpecification specification, string algorithm) {
204      AlgorithmConfiguration conf = new AlgorithmConfiguration();
205      conf.name = algorithm;
206      conf.problemSpecification = new ProblemSpecification(specification);
207      if (!finishedAndDispatchedRuns.ContainsKey(specification.TargetVariable))
208        finishedAndDispatchedRuns.Add(specification.TargetVariable, new List<AlgorithmConfiguration>());
209      finishedAndDispatchedRuns[specification.TargetVariable].Add(conf);
210    }
211
212    private bool AlgorithmFinishedOrDispatched(ProblemSpecification specification, string algoName) {
213      return
214        finishedAndDispatchedRuns.ContainsKey(specification.TargetVariable) &&
215        finishedAndDispatchedRuns[specification.TargetVariable].Any(x =>
216                                                           algoName == x.name &&
217                                                           specification.Equals(x.problemSpecification));
218    }
219
220    internal void EnableTargetVariable(string name) {
221      activeVariables[name] = true;
222    }
223
224    internal void DisableTargetVariable(string name) {
225      activeVariables[name] = false;
226    }
227
228    public void EnableAlgorithm(string targetVariable, HeuristicLab.Modeling.IAlgorithm algo) {
229      if (!algorithms.ContainsKey(targetVariable)) algorithms.Add(targetVariable, new List<HeuristicLab.Modeling.IAlgorithm>());
230      if (!algorithms[targetVariable].Contains(algo))
231        algorithms[targetVariable].Add(algo);
232    }
233
234    public void DisableAlgorithm(string targetVariable, HeuristicLab.Modeling.IAlgorithm algo) {
235      algorithms[targetVariable].Remove(algo);
236    }
237
238    public ProblemSpecification GetProblemSpecification(string targetVariable) {
239      if (!problemSpecifications.ContainsKey(targetVariable))
240        problemSpecifications[targetVariable] = CreateDefaultProblemSpecification(targetVariable);
241
242      return problemSpecifications[targetVariable];
243    }
244
245    private ProblemSpecification CreateDefaultProblemSpecification(string targetVariable) {
246      ProblemSpecification spec = new ProblemSpecification();
247      spec.Dataset = dataset;
248      spec.TargetVariable = targetVariable;
249      spec.LearningTask = LearningTask.Regression;
250      int targetColumn = dataset.GetVariableIndex(targetVariable);
251      // find index of first correct target value
252      int firstValueIndex;
253      for (firstValueIndex = 0; firstValueIndex < dataset.Rows; firstValueIndex++) {
254        double x = dataset.GetValue(firstValueIndex, targetColumn);
255        if (!(double.IsNaN(x) || double.IsInfinity(x))) break;
256      }
257      // find index of last correct target value
258      int lastValueIndex;
259      for (lastValueIndex = dataset.Rows - 1; lastValueIndex > firstValueIndex; lastValueIndex--) {
260        double x = dataset.GetValue(lastValueIndex, targetColumn);
261        if (!(double.IsNaN(x) || double.IsInfinity(x))) break;
262      }
263
264      int validTargetRange = lastValueIndex - firstValueIndex;
265      spec.TrainingSamplesStart = firstValueIndex;
266      spec.TrainingSamplesEnd = firstValueIndex + (int)Math.Floor(validTargetRange * 0.5);
267      spec.ValidationSamplesStart = spec.TrainingSamplesEnd;
268      spec.ValidationSamplesEnd = firstValueIndex + (int)Math.Floor(validTargetRange * 0.75);
269      spec.TestSamplesStart = spec.ValidationSamplesEnd;
270      spec.TestSamplesEnd = lastValueIndex;
271      return spec;
272    }
273
274    public void FireChanged() {
275      if (Changed != null) Changed(this, new EventArgs());
276    }
277
278    #region IViewable Members
279
280    public virtual IView CreateView() {
281      return new DispatcherView(this);
282    }
283
284    #endregion
285  }
286}
Note: See TracBrowser for help on using the repository browser.