Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2258 was 2258, checked in by gkronber, 15 years ago
  • Reimplemented method to read a list of already executed algorithms and configurations from the results DB
  • Fixed a bug in the GridExecuter
  • Added a button in the dispatcher view to speed up configuration of input variables

#712

File size: 8.6 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 : DispatcherBase {
39    private class AlgorithmConfiguration {
40      public string name;
41      public int targetVariable;
42      public List<int> inputVariables;
43    }
44
45    private Random random;   
46    private Dictionary<int, List<AlgorithmConfiguration>> finishedAndDispatchedRuns;
47
48    public SimpleDispatcher(IModelingDatabase database, Problem problem)
49      : base(database, problem) {     
50      random = new Random();
51      finishedAndDispatchedRuns = new Dictionary<int, List<AlgorithmConfiguration>>();
52      PopulateFinishedRuns();
53    }
54
55    public override HeuristicLab.Modeling.IAlgorithm SelectAndConfigureAlgorithm(int targetVariable, int[] inputVariables, Problem problem) {
56      DiscoveryService ds = new DiscoveryService();
57      HeuristicLab.Modeling.IAlgorithm[] algos = ds.GetInstances<HeuristicLab.Modeling.IAlgorithm>();
58      HeuristicLab.Modeling.IAlgorithm selectedAlgorithm = null;
59      switch (problem.LearningTask) {
60        case LearningTask.Regression: {
61            var regressionAlgos = algos.Where(a => (a as IClassificationAlgorithm) == null && (a as ITimeSeriesAlgorithm) == null);
62            selectedAlgorithm = ChooseDeterministic(targetVariable, inputVariables, regressionAlgos); //  ?? ChooseStochastic(regressionAlgos);
63            break;
64          }
65        case LearningTask.Classification: {
66            var classificationAlgos = algos.Where(a => (a as IClassificationAlgorithm) != null);
67            selectedAlgorithm = ChooseDeterministic(targetVariable, inputVariables, classificationAlgos) ?? ChooseStochastic(classificationAlgos);
68            break;
69          }
70        case LearningTask.TimeSeries: {
71            var timeSeriesAlgos = algos.Where(a => (a as ITimeSeriesAlgorithm) != null);
72            selectedAlgorithm = ChooseDeterministic(targetVariable, inputVariables, timeSeriesAlgos) ?? ChooseStochastic(timeSeriesAlgos);
73            break;
74          }
75      }
76
77
78      if (selectedAlgorithm != null) {
79        SetProblemParameters(selectedAlgorithm, problem, targetVariable, inputVariables);
80        AddDispatchedRun(targetVariable, inputVariables, selectedAlgorithm.Name);
81      }
82      return selectedAlgorithm;
83    }
84
85    private HeuristicLab.Modeling.IAlgorithm ChooseDeterministic(int targetVariable, int[] inputVariables, IEnumerable<HeuristicLab.Modeling.IAlgorithm> algos) {
86      var deterministicAlgos = algos
87        .Where(a => (a as IStochasticAlgorithm) == null)
88        .Where(a => AlgorithmFinishedOrDispatched(targetVariable, inputVariables, a.Name) == false);
89
90      if (deterministicAlgos.Count() == 0) return null;
91      return deterministicAlgos.ElementAt(random.Next(deterministicAlgos.Count()));
92    }
93
94    private HeuristicLab.Modeling.IAlgorithm ChooseStochastic(IEnumerable<HeuristicLab.Modeling.IAlgorithm> regressionAlgos) {
95      var stochasticAlgos = regressionAlgos.Where(a => (a as IStochasticAlgorithm) != null);
96      if (stochasticAlgos.Count() == 0) return null;
97      return stochasticAlgos.ElementAt(random.Next(stochasticAlgos.Count()));
98    }
99
100    private void PopulateFinishedRuns() {
101      var dispatchedAlgos = from model in Database.GetAllModels()
102                            select new {
103                              TargetVariable = model.TargetVariable.Name,
104                              Algorithm = model.Algorithm.Name,
105                              Inputvariables = Database.GetInputVariableResults(model).Select(x => x.Variable.Name).Distinct() };
106      foreach (var algo in dispatchedAlgos) {
107        AddDispatchedRun(algo.TargetVariable, algo.Inputvariables, algo.Algorithm);
108      }
109    }
110
111    private void SetProblemParameters(HeuristicLab.Modeling.IAlgorithm algo, Problem problem, int targetVariable, int[] inputVariables) {
112      algo.Dataset = problem.Dataset;
113      algo.TargetVariable = targetVariable;
114      algo.ProblemInjector.GetVariable("TrainingSamplesStart").GetValue<IntData>().Data = problem.TrainingSamplesStart;
115      algo.ProblemInjector.GetVariable("TrainingSamplesEnd").GetValue<IntData>().Data = problem.TrainingSamplesEnd;
116      algo.ProblemInjector.GetVariable("ValidationSamplesStart").GetValue<IntData>().Data = problem.ValidationSamplesStart;
117      algo.ProblemInjector.GetVariable("ValidationSamplesEnd").GetValue<IntData>().Data = problem.ValidationSamplesEnd;
118      algo.ProblemInjector.GetVariable("TestSamplesStart").GetValue<IntData>().Data = problem.TestSamplesStart;
119      algo.ProblemInjector.GetVariable("TestSamplesEnd").GetValue<IntData>().Data = problem.TestSamplesEnd;
120      ItemList<IntData> allowedFeatures = algo.ProblemInjector.GetVariable("AllowedFeatures").GetValue<ItemList<IntData>>();
121      foreach (int inputVariable in inputVariables) {
122        if (inputVariable != targetVariable) {
123          allowedFeatures.Add(new IntData(inputVariable));
124        }
125      }
126
127      if (problem.LearningTask == LearningTask.TimeSeries) {
128        algo.ProblemInjector.GetVariable("Autoregressive").GetValue<BoolData>().Data = problem.AutoRegressive;
129        algo.ProblemInjector.GetVariable("MinTimeOffset").GetValue<IntData>().Data = problem.MinTimeOffset;
130        algo.ProblemInjector.GetVariable("MaxTimeOffset").GetValue<IntData>().Data = problem.MaxTimeOffset;
131        if (problem.AutoRegressive) {
132          allowedFeatures.Add(new IntData(targetVariable));
133        }
134      } else if (problem.LearningTask == LearningTask.Classification) {
135        ItemList<DoubleData> classValues = algo.ProblemInjector.GetVariable("TargetClassValues").GetValue<ItemList<DoubleData>>();
136        foreach (double classValue in GetDifferentClassValues(problem.Dataset, targetVariable)) classValues.Add(new DoubleData(classValue));
137      }
138    }
139
140    private IEnumerable<double> GetDifferentClassValues(HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable) {
141      return Enumerable.Range(0, dataset.Rows).Select(x => dataset.GetValue(x, targetVariable)).Distinct();
142    }
143
144    private void AddDispatchedRun(string targetVariable, IEnumerable<string> inputVariables, string algorithm) {
145      AddDispatchedRun(
146        Problem.Dataset.GetVariableIndex(targetVariable),
147        inputVariables.Select(x => Problem.Dataset.GetVariableIndex(x)).ToArray(),
148        algorithm);
149    }
150
151    private void AddDispatchedRun(int targetVariable, int[] inputVariables, string algoName) {
152      if (!finishedAndDispatchedRuns.ContainsKey(targetVariable)) {
153        finishedAndDispatchedRuns[targetVariable] = new List<AlgorithmConfiguration>();
154      }
155      AlgorithmConfiguration conf = new AlgorithmConfiguration();
156      conf.name = algoName;
157      conf.inputVariables = new List<int>(inputVariables);
158      conf.targetVariable = targetVariable;
159      finishedAndDispatchedRuns[targetVariable].Add(conf);
160    }
161
162    private bool AlgorithmFinishedOrDispatched(int targetVariable, int[] inputVariables, string algoName) {
163      return
164        finishedAndDispatchedRuns.ContainsKey(targetVariable) &&
165        finishedAndDispatchedRuns[targetVariable].Any(x => targetVariable == x.targetVariable &&
166                                                           algoName == x.name &&
167                                                           inputVariables.Count() == x.inputVariables.Count() &&
168                                                           inputVariables.All(v => x.inputVariables.Contains(v)));
169    }
170  }
171}
Note: See TracBrowser for help on using the repository browser.