Free cookie consent management tool by TermsFeed Policy Generator

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

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

Copied refactored plugin infrastructure from branch and merged changeset r2586:2589 from branch into the trunk. #799

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.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      IGeneticProgrammingAlgorithm structIdAlgo = algo as IGeneticProgrammingAlgorithm;
203      if (structIdAlgo != null) {
204        var funLib = SelectRandomFunctionLibrary();
205        structIdAlgo.FunctionLibraryInjector = funLib;
206      }
207    }
208
209    private IOperator SelectRandomFunctionLibrary() {
210      var injectors = from injector in ApplicationManager.Manager.GetInstances<FunctionLibraryInjectorBase>()
211                      where injector.GetType().GetCustomAttributes(typeof(SymbolicRegressionFunctionLibraryInjectorAttribute), true).Count() > 0
212                      select injector;
213
214      return injectors.ElementAt(random.Next(injectors.Count()));
215    }
216
217
218    private void AddDispatchedRun(ProblemSpecification specification, string algorithm) {
219      AlgorithmConfiguration conf = new AlgorithmConfiguration();
220      conf.name = algorithm;
221      conf.problemSpecification = new ProblemSpecification(specification);
222      if (!finishedAndDispatchedRuns.ContainsKey(specification.TargetVariable))
223        finishedAndDispatchedRuns.Add(specification.TargetVariable, new List<AlgorithmConfiguration>());
224      finishedAndDispatchedRuns[specification.TargetVariable].Add(conf);
225    }
226
227    private bool AlgorithmFinishedOrDispatched(ProblemSpecification specification, string algoName) {
228      return
229        finishedAndDispatchedRuns.ContainsKey(specification.TargetVariable) &&
230        finishedAndDispatchedRuns[specification.TargetVariable].Any(x =>
231                                                           algoName == x.name &&
232                                                           specification.Equals(x.problemSpecification));
233    }
234
235    internal void EnableTargetVariable(string name) {
236      activeVariables[name] = true;
237    }
238
239    internal void DisableTargetVariable(string name) {
240      activeVariables[name] = false;
241    }
242
243    public void EnableAlgorithm(string targetVariable, HeuristicLab.Modeling.IAlgorithm algo) {
244      if (!algorithms.ContainsKey(targetVariable)) algorithms.Add(targetVariable, new List<HeuristicLab.Modeling.IAlgorithm>());
245      if (!algorithms[targetVariable].Contains(algo))
246        algorithms[targetVariable].Add(algo);
247    }
248
249    public void DisableAlgorithm(string targetVariable, HeuristicLab.Modeling.IAlgorithm algo) {
250      algorithms[targetVariable].Remove(algo);
251    }
252
253    public ProblemSpecification GetProblemSpecification(string targetVariable) {
254      if (!problemSpecifications.ContainsKey(targetVariable))
255        problemSpecifications[targetVariable] = CreateDefaultProblemSpecification(targetVariable);
256
257      return problemSpecifications[targetVariable];
258    }
259
260    private ProblemSpecification CreateDefaultProblemSpecification(string targetVariable) {
261      ProblemSpecification spec = new ProblemSpecification();
262      spec.Dataset = dataset;
263      spec.TargetVariable = targetVariable;
264      spec.LearningTask = LearningTask.Regression;
265      int targetColumn = dataset.GetVariableIndex(targetVariable);
266      // find index of first correct target value
267      int firstValueIndex;
268      for (firstValueIndex = 0; firstValueIndex < dataset.Rows; firstValueIndex++) {
269        double x = dataset.GetValue(firstValueIndex, targetColumn);
270        if (!(double.IsNaN(x) || double.IsInfinity(x))) break;
271      }
272      // find index of last correct target value
273      int lastValueIndex;
274      for (lastValueIndex = dataset.Rows - 1; lastValueIndex > firstValueIndex; lastValueIndex--) {
275        double x = dataset.GetValue(lastValueIndex, targetColumn);
276        if (!(double.IsNaN(x) || double.IsInfinity(x))) break;
277      }
278
279      int validTargetRange = lastValueIndex - firstValueIndex;
280      spec.TrainingSamplesStart = firstValueIndex;
281      spec.TrainingSamplesEnd = firstValueIndex + (int)Math.Floor(validTargetRange * 0.5);
282      spec.ValidationSamplesStart = spec.TrainingSamplesEnd;
283      spec.ValidationSamplesEnd = firstValueIndex + (int)Math.Floor(validTargetRange * 0.75);
284      spec.TestSamplesStart = spec.ValidationSamplesEnd;
285      spec.TestSamplesEnd = lastValueIndex;
286      return spec;
287    }
288
289    public void FireChanged() {
290      if (Changed != null) Changed(this, new EventArgs());
291    }
292
293    #region IViewable Members
294
295    public virtual IView CreateView() {
296      return new DispatcherView(this);
297    }
298
299    #endregion
300  }
301}
Note: See TracBrowser for help on using the repository browser.