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
RevLine 
[1044]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;
[1053]32using HeuristicLab.Data;
[1060]33using HeuristicLab.Core;
[1857]34using HeuristicLab.Modeling;
[2223]35using HeuristicLab.Modeling.Database;
[1044]36
37namespace HeuristicLab.CEDMA.Server {
[1873]38  public class SimpleDispatcher : DispatcherBase {
[2119]39    private class AlgorithmConfiguration {
40      public string name;
41      public int targetVariable;
42      public List<int> inputVariables;
43    }
44
[2223]45    private Random random;   
[2119]46    private Dictionary<int, List<AlgorithmConfiguration>> finishedAndDispatchedRuns;
[1873]47
[2223]48    public SimpleDispatcher(IModelingDatabase database, Problem problem)
49      : base(database, problem) {     
[1217]50      random = new Random();
[2119]51      finishedAndDispatchedRuns = new Dictionary<int, List<AlgorithmConfiguration>>();
[1873]52      PopulateFinishedRuns();
[1044]53    }
54
[2223]55    public override HeuristicLab.Modeling.IAlgorithm SelectAndConfigureAlgorithm(int targetVariable, int[] inputVariables, Problem problem) {
[1857]56      DiscoveryService ds = new DiscoveryService();
[2223]57      HeuristicLab.Modeling.IAlgorithm[] algos = ds.GetInstances<HeuristicLab.Modeling.IAlgorithm>();
58      HeuristicLab.Modeling.IAlgorithm selectedAlgorithm = null;
[2119]59      switch (problem.LearningTask) {
[1857]60        case LearningTask.Regression: {
61            var regressionAlgos = algos.Where(a => (a as IClassificationAlgorithm) == null && (a as ITimeSeriesAlgorithm) == null);
[2258]62            selectedAlgorithm = ChooseDeterministic(targetVariable, inputVariables, regressionAlgos); //  ?? ChooseStochastic(regressionAlgos);
[1873]63            break;
[1857]64          }
65        case LearningTask.Classification: {
66            var classificationAlgos = algos.Where(a => (a as IClassificationAlgorithm) != null);
[2119]67            selectedAlgorithm = ChooseDeterministic(targetVariable, inputVariables, classificationAlgos) ?? ChooseStochastic(classificationAlgos);
[1873]68            break;
[1857]69          }
70        case LearningTask.TimeSeries: {
71            var timeSeriesAlgos = algos.Where(a => (a as ITimeSeriesAlgorithm) != null);
[2119]72            selectedAlgorithm = ChooseDeterministic(targetVariable, inputVariables, timeSeriesAlgos) ?? ChooseStochastic(timeSeriesAlgos);
[1873]73            break;
[1857]74          }
75      }
[2119]76
77
[1873]78      if (selectedAlgorithm != null) {
[2152]79        SetProblemParameters(selectedAlgorithm, problem, targetVariable, inputVariables);
[2119]80        AddDispatchedRun(targetVariable, inputVariables, selectedAlgorithm.Name);
[1873]81      }
82      return selectedAlgorithm;
[1044]83    }
84
[2223]85    private HeuristicLab.Modeling.IAlgorithm ChooseDeterministic(int targetVariable, int[] inputVariables, IEnumerable<HeuristicLab.Modeling.IAlgorithm> algos) {
[1873]86      var deterministicAlgos = algos
87        .Where(a => (a as IStochasticAlgorithm) == null)
[2119]88        .Where(a => AlgorithmFinishedOrDispatched(targetVariable, inputVariables, a.Name) == false);
[1873]89
90      if (deterministicAlgos.Count() == 0) return null;
91      return deterministicAlgos.ElementAt(random.Next(deterministicAlgos.Count()));
92    }
93
[2223]94    private HeuristicLab.Modeling.IAlgorithm ChooseStochastic(IEnumerable<HeuristicLab.Modeling.IAlgorithm> regressionAlgos) {
[1873]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() {
[2258]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      }
[1873]109    }
110
[2223]111    private void SetProblemParameters(HeuristicLab.Modeling.IAlgorithm algo, Problem problem, int targetVariable, int[] inputVariables) {
[2119]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>>();
[2130]121      foreach (int inputVariable in inputVariables) {
122        if (inputVariable != targetVariable) {
123          allowedFeatures.Add(new IntData(inputVariable));
124        }
125      }
[2119]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;
[2130]131        if (problem.AutoRegressive) {
132          allowedFeatures.Add(new IntData(targetVariable));
133        }
[2119]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
[2258]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
[2119]151    private void AddDispatchedRun(int targetVariable, int[] inputVariables, string algoName) {
[2012]152      if (!finishedAndDispatchedRuns.ContainsKey(targetVariable)) {
[2119]153        finishedAndDispatchedRuns[targetVariable] = new List<AlgorithmConfiguration>();
[1873]154      }
[2119]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);
[1873]160    }
161
[2119]162    private bool AlgorithmFinishedOrDispatched(int targetVariable, int[] inputVariables, string algoName) {
[1873]163      return
[2012]164        finishedAndDispatchedRuns.ContainsKey(targetVariable) &&
[2119]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)));
[1873]169    }
[1044]170  }
171}
Note: See TracBrowser for help on using the repository browser.