[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 HeuristicLab.CEDMA.DB.Interfaces;
|
---|
| 30 | using HeuristicLab.CEDMA.DB;
|
---|
| 31 | using System.ServiceModel.Description;
|
---|
| 32 | using System.Linq;
|
---|
| 33 | using HeuristicLab.CEDMA.Core;
|
---|
[1053] | 34 | using HeuristicLab.GP.StructureIdentification;
|
---|
| 35 | using HeuristicLab.Data;
|
---|
[1060] | 36 | using HeuristicLab.Core;
|
---|
[1857] | 37 | using HeuristicLab.Modeling;
|
---|
[1044] | 38 |
|
---|
| 39 | namespace HeuristicLab.CEDMA.Server {
|
---|
[1873] | 40 | public class SimpleDispatcher : DispatcherBase {
|
---|
[2119] | 41 | private class AlgorithmConfiguration {
|
---|
| 42 | public string name;
|
---|
| 43 | public int targetVariable;
|
---|
| 44 | public List<int> inputVariables;
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[1217] | 47 | private Random random;
|
---|
[1873] | 48 | private IStore store;
|
---|
[2119] | 49 | private Dictionary<int, List<AlgorithmConfiguration>> finishedAndDispatchedRuns;
|
---|
[1873] | 50 |
|
---|
| 51 | public SimpleDispatcher(IStore store)
|
---|
[1217] | 52 | : base(store) {
|
---|
[1873] | 53 | this.store = store;
|
---|
[1217] | 54 | random = new Random();
|
---|
[2119] | 55 | finishedAndDispatchedRuns = new Dictionary<int, List<AlgorithmConfiguration>>();
|
---|
[1873] | 56 | PopulateFinishedRuns();
|
---|
[1044] | 57 | }
|
---|
| 58 |
|
---|
[2153] | 59 | public override IAlgorithm SelectAndConfigureAlgorithm(int targetVariable, int[] inputVariables, Problem problem) {
|
---|
[1857] | 60 | DiscoveryService ds = new DiscoveryService();
|
---|
| 61 | IAlgorithm[] algos = ds.GetInstances<IAlgorithm>();
|
---|
[1873] | 62 | IAlgorithm selectedAlgorithm = null;
|
---|
[2119] | 63 | switch (problem.LearningTask) {
|
---|
[1857] | 64 | case LearningTask.Regression: {
|
---|
| 65 | var regressionAlgos = algos.Where(a => (a as IClassificationAlgorithm) == null && (a as ITimeSeriesAlgorithm) == null);
|
---|
[2119] | 66 | selectedAlgorithm = ChooseDeterministic(targetVariable, inputVariables, regressionAlgos) ?? ChooseStochastic(regressionAlgos);
|
---|
[1873] | 67 | break;
|
---|
[1857] | 68 | }
|
---|
| 69 | case LearningTask.Classification: {
|
---|
| 70 | var classificationAlgos = algos.Where(a => (a as IClassificationAlgorithm) != null);
|
---|
[2119] | 71 | selectedAlgorithm = ChooseDeterministic(targetVariable, inputVariables, classificationAlgos) ?? ChooseStochastic(classificationAlgos);
|
---|
[1873] | 72 | break;
|
---|
[1857] | 73 | }
|
---|
| 74 | case LearningTask.TimeSeries: {
|
---|
| 75 | var timeSeriesAlgos = algos.Where(a => (a as ITimeSeriesAlgorithm) != null);
|
---|
[2119] | 76 | selectedAlgorithm = ChooseDeterministic(targetVariable, inputVariables, timeSeriesAlgos) ?? ChooseStochastic(timeSeriesAlgos);
|
---|
[1873] | 77 | break;
|
---|
[1857] | 78 | }
|
---|
| 79 | }
|
---|
[2119] | 80 |
|
---|
| 81 |
|
---|
[1873] | 82 | if (selectedAlgorithm != null) {
|
---|
[2152] | 83 | SetProblemParameters(selectedAlgorithm, problem, targetVariable, inputVariables);
|
---|
[2119] | 84 | AddDispatchedRun(targetVariable, inputVariables, selectedAlgorithm.Name);
|
---|
[1873] | 85 | }
|
---|
| 86 | return selectedAlgorithm;
|
---|
[1044] | 87 | }
|
---|
| 88 |
|
---|
[2119] | 89 | private IAlgorithm ChooseDeterministic(int targetVariable, int[] inputVariables, IEnumerable<IAlgorithm> algos) {
|
---|
[1873] | 90 | var deterministicAlgos = algos
|
---|
| 91 | .Where(a => (a as IStochasticAlgorithm) == null)
|
---|
[2119] | 92 | .Where(a => AlgorithmFinishedOrDispatched(targetVariable, inputVariables, a.Name) == false);
|
---|
[1873] | 93 |
|
---|
| 94 | if (deterministicAlgos.Count() == 0) return null;
|
---|
| 95 | return deterministicAlgos.ElementAt(random.Next(deterministicAlgos.Count()));
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | private IAlgorithm ChooseStochastic(IEnumerable<IAlgorithm> regressionAlgos) {
|
---|
| 99 | var stochasticAlgos = regressionAlgos.Where(a => (a as IStochasticAlgorithm) != null);
|
---|
| 100 | if (stochasticAlgos.Count() == 0) return null;
|
---|
| 101 | return stochasticAlgos.ElementAt(random.Next(stochasticAlgos.Count()));
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | private void PopulateFinishedRuns() {
|
---|
[2119] | 105 | Dictionary<Entity, Entity> processedModels = new Dictionary<Entity, Entity>();
|
---|
[2049] | 106 | var datasetBindings = store
|
---|
[2012] | 107 | .Query(
|
---|
[2047] | 108 | "?Dataset <" + Ontology.InstanceOf + "> <" + Ontology.TypeDataSet + "> .", 0, 1)
|
---|
[2049] | 109 | .Select(x => (Entity)x.Get("Dataset"));
|
---|
[2012] | 110 |
|
---|
[2049] | 111 | if (datasetBindings.Count() > 0) {
|
---|
| 112 | var datasetEntity = datasetBindings.ElementAt(0);
|
---|
[1873] | 113 |
|
---|
[2049] | 114 | DataSet ds = new DataSet(store, datasetEntity);
|
---|
| 115 | var result = store
|
---|
| 116 | .Query(
|
---|
| 117 | "?Model <" + Ontology.TargetVariable + "> ?TargetVariable ." + Environment.NewLine +
|
---|
| 118 | "?Model <" + Ontology.Name + "> ?AlgoName .",
|
---|
| 119 | 0, 1000)
|
---|
[2119] | 120 | .Select(x => new Resource[] { (Literal)x.Get("TargetVariable"), (Literal)x.Get("AlgoName"), (Entity)x.Get("Model") });
|
---|
[2012] | 121 |
|
---|
[2049] | 122 | foreach (Resource[] row in result) {
|
---|
[2119] | 123 | Entity model = ((Entity)row[2]);
|
---|
| 124 | if (!processedModels.ContainsKey(model)) {
|
---|
| 125 | processedModels.Add(model, model);
|
---|
[2049] | 126 |
|
---|
[2119] | 127 | string targetVariable = (string)((Literal)row[0]).Value;
|
---|
| 128 | string algoName = (string)((Literal)row[1]).Value;
|
---|
| 129 | int targetVariableIndex = ds.Problem.Dataset.GetVariableIndex(targetVariable);
|
---|
| 130 |
|
---|
| 131 | var inputVariableLiterals = store
|
---|
| 132 | .Query(
|
---|
| 133 | "<" + model.Uri + "> <" + Ontology.HasInputVariable + "> ?InputVariable ." + Environment.NewLine +
|
---|
| 134 | "?InputVariable <" + Ontology.Name + "> ?Name .",
|
---|
| 135 | 0, 1000)
|
---|
| 136 | .Select(x => (Literal)x.Get("Name"))
|
---|
| 137 | .Select(l => (string)l.Value)
|
---|
| 138 | .Distinct();
|
---|
| 139 |
|
---|
| 140 | List<int> inputVariables = new List<int>();
|
---|
| 141 | foreach (string variableName in inputVariableLiterals) {
|
---|
| 142 | int variableIndex = ds.Problem.Dataset.GetVariableIndex(variableName);
|
---|
| 143 | inputVariables.Add(variableIndex);
|
---|
| 144 | }
|
---|
| 145 | if (!AlgorithmFinishedOrDispatched(targetVariableIndex, inputVariables.ToArray(), algoName)) {
|
---|
| 146 | AddDispatchedRun(targetVariableIndex, inputVariables.ToArray(), algoName);
|
---|
| 147 | }
|
---|
| 148 | }
|
---|
[2049] | 149 | }
|
---|
[1873] | 150 | }
|
---|
| 151 | }
|
---|
| 152 |
|
---|
[2119] | 153 | private void SetProblemParameters(IAlgorithm algo, Problem problem, int targetVariable, int[] inputVariables) {
|
---|
| 154 | algo.Dataset = problem.Dataset;
|
---|
| 155 | algo.TargetVariable = targetVariable;
|
---|
| 156 | algo.ProblemInjector.GetVariable("TrainingSamplesStart").GetValue<IntData>().Data = problem.TrainingSamplesStart;
|
---|
| 157 | algo.ProblemInjector.GetVariable("TrainingSamplesEnd").GetValue<IntData>().Data = problem.TrainingSamplesEnd;
|
---|
| 158 | algo.ProblemInjector.GetVariable("ValidationSamplesStart").GetValue<IntData>().Data = problem.ValidationSamplesStart;
|
---|
| 159 | algo.ProblemInjector.GetVariable("ValidationSamplesEnd").GetValue<IntData>().Data = problem.ValidationSamplesEnd;
|
---|
| 160 | algo.ProblemInjector.GetVariable("TestSamplesStart").GetValue<IntData>().Data = problem.TestSamplesStart;
|
---|
| 161 | algo.ProblemInjector.GetVariable("TestSamplesEnd").GetValue<IntData>().Data = problem.TestSamplesEnd;
|
---|
| 162 | ItemList<IntData> allowedFeatures = algo.ProblemInjector.GetVariable("AllowedFeatures").GetValue<ItemList<IntData>>();
|
---|
[2130] | 163 | foreach (int inputVariable in inputVariables) {
|
---|
| 164 | if (inputVariable != targetVariable) {
|
---|
| 165 | allowedFeatures.Add(new IntData(inputVariable));
|
---|
| 166 | }
|
---|
| 167 | }
|
---|
[2119] | 168 |
|
---|
| 169 | if (problem.LearningTask == LearningTask.TimeSeries) {
|
---|
| 170 | algo.ProblemInjector.GetVariable("Autoregressive").GetValue<BoolData>().Data = problem.AutoRegressive;
|
---|
| 171 | algo.ProblemInjector.GetVariable("MinTimeOffset").GetValue<IntData>().Data = problem.MinTimeOffset;
|
---|
| 172 | algo.ProblemInjector.GetVariable("MaxTimeOffset").GetValue<IntData>().Data = problem.MaxTimeOffset;
|
---|
[2130] | 173 | if (problem.AutoRegressive) {
|
---|
| 174 | allowedFeatures.Add(new IntData(targetVariable));
|
---|
| 175 | }
|
---|
[2119] | 176 | } else if (problem.LearningTask == LearningTask.Classification) {
|
---|
| 177 | ItemList<DoubleData> classValues = algo.ProblemInjector.GetVariable("TargetClassValues").GetValue<ItemList<DoubleData>>();
|
---|
| 178 | foreach (double classValue in GetDifferentClassValues(problem.Dataset, targetVariable)) classValues.Add(new DoubleData(classValue));
|
---|
| 179 | }
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | private IEnumerable<double> GetDifferentClassValues(HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable) {
|
---|
| 183 | return Enumerable.Range(0, dataset.Rows).Select(x => dataset.GetValue(x, targetVariable)).Distinct();
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | private void AddDispatchedRun(int targetVariable, int[] inputVariables, string algoName) {
|
---|
[2012] | 187 | if (!finishedAndDispatchedRuns.ContainsKey(targetVariable)) {
|
---|
[2119] | 188 | finishedAndDispatchedRuns[targetVariable] = new List<AlgorithmConfiguration>();
|
---|
[1873] | 189 | }
|
---|
[2119] | 190 | AlgorithmConfiguration conf = new AlgorithmConfiguration();
|
---|
| 191 | conf.name = algoName;
|
---|
| 192 | conf.inputVariables = new List<int>(inputVariables);
|
---|
| 193 | conf.targetVariable = targetVariable;
|
---|
| 194 | finishedAndDispatchedRuns[targetVariable].Add(conf);
|
---|
[1873] | 195 | }
|
---|
| 196 |
|
---|
[2119] | 197 | private bool AlgorithmFinishedOrDispatched(int targetVariable, int[] inputVariables, string algoName) {
|
---|
[1873] | 198 | return
|
---|
[2012] | 199 | finishedAndDispatchedRuns.ContainsKey(targetVariable) &&
|
---|
[2119] | 200 | finishedAndDispatchedRuns[targetVariable].Any(x => targetVariable == x.targetVariable &&
|
---|
| 201 | algoName == x.name &&
|
---|
| 202 | inputVariables.Count() == x.inputVariables.Count() &&
|
---|
| 203 | inputVariables.All(v => x.inputVariables.Contains(v)));
|
---|
[1873] | 204 | }
|
---|
[1044] | 205 | }
|
---|
| 206 | }
|
---|