[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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Text;
|
---|
| 25 | using System.Windows.Forms;
|
---|
| 26 | using HeuristicLab.PluginInfrastructure;
|
---|
| 27 | using System.Net;
|
---|
| 28 | using System.ServiceModel;
|
---|
| 29 | using System.ServiceModel.Description;
|
---|
| 30 | using System.Linq;
|
---|
| 31 | using HeuristicLab.CEDMA.Core;
|
---|
[1053] | 32 | using HeuristicLab.Data;
|
---|
[1060] | 33 | using HeuristicLab.Core;
|
---|
[1857] | 34 | using HeuristicLab.Modeling;
|
---|
[2223] | 35 | using HeuristicLab.Modeling.Database;
|
---|
[1044] | 36 |
|
---|
| 37 | namespace 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 |
|
---|
[2270] | 45 | private Random random;
|
---|
[2119] | 46 | private Dictionary<int, List<AlgorithmConfiguration>> finishedAndDispatchedRuns;
|
---|
[1873] | 47 |
|
---|
[2223] | 48 | public SimpleDispatcher(IModelingDatabase database, Problem problem)
|
---|
[2270] | 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()
|
---|
[2270] | 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 | };
|
---|
[2258] | 107 | foreach (var algo in dispatchedAlgos) {
|
---|
| 108 | AddDispatchedRun(algo.TargetVariable, algo.Inputvariables, algo.Algorithm);
|
---|
| 109 | }
|
---|
[1873] | 110 | }
|
---|
| 111 |
|
---|
[2223] | 112 | private void SetProblemParameters(HeuristicLab.Modeling.IAlgorithm algo, Problem problem, int targetVariable, int[] inputVariables) {
|
---|
[2119] | 113 | algo.Dataset = problem.Dataset;
|
---|
| 114 | algo.TargetVariable = targetVariable;
|
---|
| 115 | algo.ProblemInjector.GetVariable("TrainingSamplesStart").GetValue<IntData>().Data = problem.TrainingSamplesStart;
|
---|
| 116 | algo.ProblemInjector.GetVariable("TrainingSamplesEnd").GetValue<IntData>().Data = problem.TrainingSamplesEnd;
|
---|
| 117 | algo.ProblemInjector.GetVariable("ValidationSamplesStart").GetValue<IntData>().Data = problem.ValidationSamplesStart;
|
---|
| 118 | algo.ProblemInjector.GetVariable("ValidationSamplesEnd").GetValue<IntData>().Data = problem.ValidationSamplesEnd;
|
---|
| 119 | algo.ProblemInjector.GetVariable("TestSamplesStart").GetValue<IntData>().Data = problem.TestSamplesStart;
|
---|
| 120 | algo.ProblemInjector.GetVariable("TestSamplesEnd").GetValue<IntData>().Data = problem.TestSamplesEnd;
|
---|
| 121 | ItemList<IntData> allowedFeatures = algo.ProblemInjector.GetVariable("AllowedFeatures").GetValue<ItemList<IntData>>();
|
---|
[2130] | 122 | foreach (int inputVariable in inputVariables) {
|
---|
| 123 | if (inputVariable != targetVariable) {
|
---|
| 124 | allowedFeatures.Add(new IntData(inputVariable));
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
[2119] | 127 |
|
---|
| 128 | if (problem.LearningTask == LearningTask.TimeSeries) {
|
---|
| 129 | algo.ProblemInjector.GetVariable("Autoregressive").GetValue<BoolData>().Data = problem.AutoRegressive;
|
---|
| 130 | algo.ProblemInjector.GetVariable("MinTimeOffset").GetValue<IntData>().Data = problem.MinTimeOffset;
|
---|
| 131 | algo.ProblemInjector.GetVariable("MaxTimeOffset").GetValue<IntData>().Data = problem.MaxTimeOffset;
|
---|
[2130] | 132 | if (problem.AutoRegressive) {
|
---|
| 133 | allowedFeatures.Add(new IntData(targetVariable));
|
---|
| 134 | }
|
---|
[2119] | 135 | } else if (problem.LearningTask == LearningTask.Classification) {
|
---|
| 136 | ItemList<DoubleData> classValues = algo.ProblemInjector.GetVariable("TargetClassValues").GetValue<ItemList<DoubleData>>();
|
---|
| 137 | foreach (double classValue in GetDifferentClassValues(problem.Dataset, targetVariable)) classValues.Add(new DoubleData(classValue));
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | private IEnumerable<double> GetDifferentClassValues(HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable) {
|
---|
| 142 | return Enumerable.Range(0, dataset.Rows).Select(x => dataset.GetValue(x, targetVariable)).Distinct();
|
---|
| 143 | }
|
---|
| 144 |
|
---|
[2258] | 145 | private void AddDispatchedRun(string targetVariable, IEnumerable<string> inputVariables, string algorithm) {
|
---|
| 146 | AddDispatchedRun(
|
---|
[2270] | 147 | Problem.Dataset.GetVariableIndex(targetVariable),
|
---|
| 148 | inputVariables.Select(x => Problem.Dataset.GetVariableIndex(x)).ToArray(),
|
---|
[2258] | 149 | algorithm);
|
---|
| 150 | }
|
---|
| 151 |
|
---|
[2119] | 152 | private void AddDispatchedRun(int targetVariable, int[] inputVariables, string algoName) {
|
---|
[2012] | 153 | if (!finishedAndDispatchedRuns.ContainsKey(targetVariable)) {
|
---|
[2119] | 154 | finishedAndDispatchedRuns[targetVariable] = new List<AlgorithmConfiguration>();
|
---|
[1873] | 155 | }
|
---|
[2119] | 156 | AlgorithmConfiguration conf = new AlgorithmConfiguration();
|
---|
| 157 | conf.name = algoName;
|
---|
| 158 | conf.inputVariables = new List<int>(inputVariables);
|
---|
| 159 | conf.targetVariable = targetVariable;
|
---|
| 160 | finishedAndDispatchedRuns[targetVariable].Add(conf);
|
---|
[1873] | 161 | }
|
---|
| 162 |
|
---|
[2119] | 163 | private bool AlgorithmFinishedOrDispatched(int targetVariable, int[] inputVariables, string algoName) {
|
---|
[1873] | 164 | return
|
---|
[2012] | 165 | finishedAndDispatchedRuns.ContainsKey(targetVariable) &&
|
---|
[2119] | 166 | finishedAndDispatchedRuns[targetVariable].Any(x => targetVariable == x.targetVariable &&
|
---|
| 167 | algoName == x.name &&
|
---|
| 168 | inputVariables.Count() == x.inputVariables.Count() &&
|
---|
| 169 | inputVariables.All(v => x.inputVariables.Contains(v)));
|
---|
[1873] | 170 | }
|
---|
[1044] | 171 | }
|
---|
| 172 | }
|
---|