[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 {
|
---|
[1217] | 41 | private Random random;
|
---|
[1873] | 42 | private IStore store;
|
---|
| 43 | private Dictionary<Entity, Dictionary<int, List<string>>> finishedAndDispatchedRuns;
|
---|
| 44 |
|
---|
| 45 | public SimpleDispatcher(IStore store)
|
---|
[1217] | 46 | : base(store) {
|
---|
[1873] | 47 | this.store = store;
|
---|
[1217] | 48 | random = new Random();
|
---|
[1873] | 49 | finishedAndDispatchedRuns = new Dictionary<Entity, Dictionary<int, List<string>>>();
|
---|
| 50 | PopulateFinishedRuns();
|
---|
[1044] | 51 | }
|
---|
| 52 |
|
---|
[1873] | 53 | public override IAlgorithm SelectAlgorithm(Entity dataSetEntity, int targetVariable, LearningTask learningTask) {
|
---|
[1857] | 54 | DiscoveryService ds = new DiscoveryService();
|
---|
| 55 | IAlgorithm[] algos = ds.GetInstances<IAlgorithm>();
|
---|
[1873] | 56 | IAlgorithm selectedAlgorithm = null;
|
---|
[1857] | 57 | switch (learningTask) {
|
---|
| 58 | case LearningTask.Regression: {
|
---|
| 59 | var regressionAlgos = algos.Where(a => (a as IClassificationAlgorithm) == null && (a as ITimeSeriesAlgorithm) == null);
|
---|
[1873] | 60 | selectedAlgorithm = ChooseDeterministic(dataSetEntity, targetVariable, regressionAlgos) ?? ChooseStochastic(regressionAlgos);
|
---|
| 61 | break;
|
---|
[1857] | 62 | }
|
---|
| 63 | case LearningTask.Classification: {
|
---|
| 64 | var classificationAlgos = algos.Where(a => (a as IClassificationAlgorithm) != null);
|
---|
[1873] | 65 | selectedAlgorithm = ChooseDeterministic(dataSetEntity, targetVariable, classificationAlgos) ?? ChooseStochastic(classificationAlgos);
|
---|
| 66 | break;
|
---|
[1857] | 67 | }
|
---|
| 68 | case LearningTask.TimeSeries: {
|
---|
| 69 | var timeSeriesAlgos = algos.Where(a => (a as ITimeSeriesAlgorithm) != null);
|
---|
[1873] | 70 | selectedAlgorithm = ChooseDeterministic(dataSetEntity, targetVariable, timeSeriesAlgos) ?? ChooseStochastic(timeSeriesAlgos);
|
---|
| 71 | break;
|
---|
[1857] | 72 | }
|
---|
| 73 | }
|
---|
[1873] | 74 | if (selectedAlgorithm != null) {
|
---|
| 75 | AddDispatchedRun(dataSetEntity, targetVariable, selectedAlgorithm.Name);
|
---|
| 76 | }
|
---|
| 77 | return selectedAlgorithm;
|
---|
[1044] | 78 | }
|
---|
| 79 |
|
---|
[1873] | 80 | private IAlgorithm ChooseDeterministic(Entity dataSetEntity, int targetVariable, IEnumerable<IAlgorithm> algos) {
|
---|
| 81 | var deterministicAlgos = algos
|
---|
| 82 | .Where(a => (a as IStochasticAlgorithm) == null)
|
---|
| 83 | .Where(a => AlgorithmFinishedOrDispatched(dataSetEntity, targetVariable, a.Name) == false);
|
---|
| 84 |
|
---|
| 85 | if (deterministicAlgos.Count() == 0) return null;
|
---|
| 86 | return deterministicAlgos.ElementAt(random.Next(deterministicAlgos.Count()));
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | private IAlgorithm ChooseStochastic(IEnumerable<IAlgorithm> regressionAlgos) {
|
---|
| 90 | var stochasticAlgos = regressionAlgos.Where(a => (a as IStochasticAlgorithm) != null);
|
---|
| 91 | if (stochasticAlgos.Count() == 0) return null;
|
---|
| 92 | return stochasticAlgos.ElementAt(random.Next(stochasticAlgos.Count()));
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[1217] | 95 | public override Entity SelectDataSet(Entity[] datasets) {
|
---|
| 96 | return datasets[random.Next(datasets.Length)];
|
---|
[1060] | 97 | }
|
---|
| 98 |
|
---|
[1873] | 99 | public override int SelectTargetVariable(Entity dataSet, int[] targetVariables) {
|
---|
[1217] | 100 | return targetVariables[random.Next(targetVariables.Length)];
|
---|
[1060] | 101 | }
|
---|
[1873] | 102 |
|
---|
| 103 | private void PopulateFinishedRuns() {
|
---|
| 104 | var result = store
|
---|
| 105 | .Query("?DataSet <" + Ontology.PredicateInstanceOf.Uri + "> <" + Ontology.TypeDataSet.Uri + "> ." + Environment.NewLine +
|
---|
| 106 | "?DataSet <" + Ontology.PredicateHasModel + "> ?Model ." + Environment.NewLine +
|
---|
| 107 | "?Model <" + Ontology.TargetVariable + "> ?TargetVariable ." + Environment.NewLine +
|
---|
| 108 | "?Model <" + Ontology.AlgorithmName + "> ?AlgoName .",
|
---|
| 109 | 0, 1000)
|
---|
| 110 | .Select(x => new Resource[] { (Entity)x.Get("DataSet"), (Literal)x.Get("TargetVariable"), (Literal)x.Get("AlgoName") });
|
---|
| 111 |
|
---|
| 112 | foreach (Resource[] row in result) {
|
---|
| 113 | Entity dataset = (Entity)row[0];
|
---|
| 114 | int targetVariable = (int)((Literal)row[1]).Value;
|
---|
| 115 | string algoName = (string)((Literal)row[2]).Value;
|
---|
| 116 | if (!AlgorithmFinishedOrDispatched(dataset, targetVariable, algoName))
|
---|
| 117 | AddDispatchedRun(dataset, targetVariable, algoName);
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | private void AddDispatchedRun(Entity dataSetEntity, int targetVariable, string algoName) {
|
---|
| 122 | if (!finishedAndDispatchedRuns.ContainsKey(dataSetEntity)) {
|
---|
| 123 | finishedAndDispatchedRuns[dataSetEntity] = new Dictionary<int, List<string>>();
|
---|
| 124 | }
|
---|
| 125 | if (!finishedAndDispatchedRuns[dataSetEntity].ContainsKey(targetVariable)) {
|
---|
| 126 | finishedAndDispatchedRuns[dataSetEntity][targetVariable] = new List<string>();
|
---|
| 127 | }
|
---|
| 128 | finishedAndDispatchedRuns[dataSetEntity][targetVariable].Add(algoName);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | private bool AlgorithmFinishedOrDispatched(Entity dataSetEntity, int targetVariable, string algoName) {
|
---|
| 132 | return
|
---|
| 133 | finishedAndDispatchedRuns.ContainsKey(dataSetEntity) &&
|
---|
| 134 | finishedAndDispatchedRuns[dataSetEntity].ContainsKey(targetVariable) &&
|
---|
| 135 | finishedAndDispatchedRuns[dataSetEntity][targetVariable].Contains(algoName);
|
---|
| 136 | }
|
---|
[1044] | 137 | }
|
---|
| 138 | }
|
---|