[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;
|
---|
[1053] | 31 | using HeuristicLab.Data;
|
---|
[1060] | 32 | using HeuristicLab.Core;
|
---|
[1857] | 33 | using HeuristicLab.Modeling;
|
---|
[2223] | 34 | using HeuristicLab.Modeling.Database;
|
---|
[2375] | 35 | using HeuristicLab.DataAnalysis;
|
---|
[1044] | 36 |
|
---|
| 37 | namespace HeuristicLab.CEDMA.Server {
|
---|
[2375] | 38 | public class SimpleDispatcher : IDispatcher, IViewable {
|
---|
[2119] | 39 | private class AlgorithmConfiguration {
|
---|
| 40 | public string name;
|
---|
[2375] | 41 | public ProblemSpecification problemSpecification;
|
---|
[2119] | 42 | }
|
---|
| 43 |
|
---|
[2375] | 44 | internal event EventHandler Changed;
|
---|
| 45 |
|
---|
[2290] | 46 | private IModelingDatabase database;
|
---|
| 47 | public IModelingDatabase Database {
|
---|
| 48 | get {
|
---|
| 49 | return database;
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[2375] | 53 | private Dataset dataset;
|
---|
| 54 | public Dataset Dataset {
|
---|
[2290] | 55 | get {
|
---|
[2375] | 56 | return dataset;
|
---|
[2290] | 57 | }
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | public IEnumerable<string> TargetVariables {
|
---|
| 61 | get {
|
---|
[2375] | 62 | return Enumerable.Range(0, Dataset.Columns).Select(x => Dataset.GetVariableName(x));
|
---|
[2290] | 63 | }
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[2375] | 66 | public IEnumerable<string> Variables {
|
---|
[2290] | 67 | get {
|
---|
| 68 | return TargetVariables;
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[2375] | 72 | private HeuristicLab.Modeling.IAlgorithm[] defaultAlgorithms;
|
---|
| 73 | public IEnumerable<HeuristicLab.Modeling.IAlgorithm> GetAlgorithms(LearningTask task) {
|
---|
| 74 | switch (task) {
|
---|
| 75 | case LearningTask.Regression: {
|
---|
| 76 | return defaultAlgorithms.Where(a => (a as IClassificationAlgorithm) == null && (a as ITimeSeriesAlgorithm) == null);
|
---|
| 77 | }
|
---|
| 78 | case LearningTask.Classification: {
|
---|
| 79 | return defaultAlgorithms.Where(a => (a as IClassificationAlgorithm) != null);
|
---|
| 80 | }
|
---|
| 81 | case LearningTask.TimeSeries: {
|
---|
| 82 | return defaultAlgorithms.Where(a => (a as ITimeSeriesAlgorithm) != null);
|
---|
| 83 | }
|
---|
| 84 | default: {
|
---|
| 85 | return new HeuristicLab.Modeling.IAlgorithm[] { };
|
---|
| 86 | }
|
---|
[2290] | 87 | }
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[2375] | 90 | private Random random;
|
---|
| 91 | private Dictionary<string, ProblemSpecification> problemSpecifications;
|
---|
| 92 | private Dictionary<string, List<HeuristicLab.Modeling.IAlgorithm>> algorithms;
|
---|
| 93 | public IEnumerable<HeuristicLab.Modeling.IAlgorithm> GetAllowedAlgorithms(string targetVariable) {
|
---|
| 94 | if (algorithms.ContainsKey(targetVariable))
|
---|
| 95 | return algorithms[targetVariable];
|
---|
| 96 | else return new HeuristicLab.Modeling.IAlgorithm[] { };
|
---|
[2290] | 97 | }
|
---|
[2375] | 98 | private Dictionary<string, bool> activeVariables;
|
---|
| 99 | public IEnumerable<string> AllowedTargetVariables {
|
---|
| 100 | get { return activeVariables.Where(x => x.Value).Select(x => x.Key); }
|
---|
| 101 | }
|
---|
| 102 | private Dictionary<string, List<AlgorithmConfiguration>> finishedAndDispatchedRuns;
|
---|
[2290] | 103 | private object locker = new object();
|
---|
[1873] | 104 |
|
---|
[2375] | 105 | public SimpleDispatcher(IModelingDatabase database, Dataset dataset) {
|
---|
| 106 | this.dataset = dataset;
|
---|
[2290] | 107 | this.database = database;
|
---|
[2375] | 108 | dataset.Changed += (sender, args) => FireChanged();
|
---|
| 109 | random = new Random();
|
---|
[2290] | 110 |
|
---|
[2375] | 111 | activeVariables = new Dictionary<string, bool>();
|
---|
| 112 | problemSpecifications = new Dictionary<string, ProblemSpecification>();
|
---|
| 113 | algorithms = new Dictionary<string, List<HeuristicLab.Modeling.IAlgorithm>>();
|
---|
| 114 | finishedAndDispatchedRuns = new Dictionary<string, List<AlgorithmConfiguration>>();
|
---|
| 115 |
|
---|
[2290] | 116 | DiscoveryService ds = new DiscoveryService();
|
---|
[2375] | 117 | defaultAlgorithms = ds.GetInstances<HeuristicLab.Modeling.IAlgorithm>();
|
---|
[2290] | 118 |
|
---|
[2375] | 119 | // PopulateFinishedRuns();
|
---|
[1044] | 120 | }
|
---|
| 121 |
|
---|
[2290] | 122 | public HeuristicLab.Modeling.IAlgorithm GetNextJob() {
|
---|
| 123 | lock (locker) {
|
---|
[2422] | 124 | if (activeVariables.Where(x => x.Value == true).Count() > 0) {
|
---|
| 125 | string[] targetVariables = (from pair in activeVariables
|
---|
| 126 | where pair.Value == true
|
---|
| 127 | select pair.Key).ToArray();
|
---|
[2375] | 128 | string targetVariable = SelectTargetVariable(targetVariables);
|
---|
| 129 | HeuristicLab.Modeling.IAlgorithm selectedAlgorithm = SelectAndConfigureAlgorithm(targetVariable);
|
---|
[2290] | 130 |
|
---|
| 131 | return selectedAlgorithm;
|
---|
| 132 | } else return null;
|
---|
[1857] | 133 | }
|
---|
[2290] | 134 | }
|
---|
[2119] | 135 |
|
---|
[2375] | 136 | public virtual string SelectTargetVariable(string[] targetVariables) {
|
---|
[2290] | 137 | return targetVariables[random.Next(targetVariables.Length)];
|
---|
| 138 | }
|
---|
[2119] | 139 |
|
---|
[2375] | 140 | public HeuristicLab.Modeling.IAlgorithm SelectAndConfigureAlgorithm(string targetVariable) {
|
---|
[2290] | 141 | HeuristicLab.Modeling.IAlgorithm selectedAlgorithm = null;
|
---|
| 142 | var possibleAlgos =
|
---|
[2375] | 143 | algorithms[targetVariable]
|
---|
| 144 | .Where(x =>
|
---|
| 145 | ((x is IStochasticAlgorithm) || !AlgorithmFinishedOrDispatched(problemSpecifications[targetVariable], x.Name)));
|
---|
[2290] | 146 | if (possibleAlgos.Count() > 0) selectedAlgorithm = possibleAlgos.ElementAt(random.Next(possibleAlgos.Count()));
|
---|
[1873] | 147 | if (selectedAlgorithm != null) {
|
---|
[2377] | 148 | // create a clone of the algorithm template before setting the parameters
|
---|
[2422] | 149 | selectedAlgorithm = (HeuristicLab.Modeling.IAlgorithm)selectedAlgorithm.Clone();
|
---|
[2375] | 150 | SetProblemParameters(selectedAlgorithm, problemSpecifications[targetVariable]);
|
---|
[2290] | 151 | if (!(selectedAlgorithm is IStochasticAlgorithm))
|
---|
[2375] | 152 | AddDispatchedRun(problemSpecifications[targetVariable], selectedAlgorithm.Name);
|
---|
[1873] | 153 | }
|
---|
| 154 | return selectedAlgorithm;
|
---|
[1044] | 155 | }
|
---|
| 156 |
|
---|
[2375] | 157 | //private void PopulateFinishedRuns() {
|
---|
| 158 | // var dispatchedAlgos = from model in Database.GetAllModels()
|
---|
| 159 | // select new {
|
---|
| 160 | // TargetVariable = model.TargetVariable.Name,
|
---|
| 161 | // Algorithm = model.Algorithm.Name,
|
---|
| 162 | // InputVariables = Database.GetInputVariableResults(model).Select(x => x.Variable.Name).Distinct(),
|
---|
| 163 | // };
|
---|
| 164 | // foreach (var algo in dispatchedAlgos) {
|
---|
| 165 | // ProblemSpecification spec = new ProblemSpecification();
|
---|
| 166 | // spec.TargetVariable = algo.TargetVariable;
|
---|
| 167 | // foreach (string variable in algo.InputVariables) spec.AddInputVariable(variable);
|
---|
| 168 | // AddDispatchedRun(spec, algo.Algorithm);
|
---|
| 169 | // }
|
---|
| 170 | //}
|
---|
[1873] | 171 |
|
---|
[2375] | 172 | private void SetProblemParameters(HeuristicLab.Modeling.IAlgorithm algo, ProblemSpecification spec) {
|
---|
| 173 | algo.Dataset = spec.Dataset;
|
---|
[2440] | 174 | algo.TargetVariable = spec.TargetVariable;
|
---|
[2375] | 175 | algo.TrainingSamplesStart = spec.TrainingSamplesStart;
|
---|
| 176 | algo.TrainingSamplesEnd = spec.TrainingSamplesEnd;
|
---|
| 177 | algo.ValidationSamplesStart = spec.ValidationSamplesStart;
|
---|
| 178 | algo.ValidationSamplesEnd = spec.ValidationSamplesEnd;
|
---|
| 179 | algo.TestSamplesStart = spec.TestSamplesStart;
|
---|
| 180 | algo.TestSamplesEnd = spec.TestSamplesEnd;
|
---|
[2440] | 181 | List<string> allowedFeatures = new List<string>();
|
---|
[2375] | 182 | foreach (string inputVariable in spec.InputVariables) {
|
---|
| 183 | if (inputVariable != spec.TargetVariable) {
|
---|
[2440] | 184 | allowedFeatures.Add(inputVariable);
|
---|
[2130] | 185 | }
|
---|
| 186 | }
|
---|
[2119] | 187 |
|
---|
[2375] | 188 | if (spec.LearningTask == LearningTask.TimeSeries) {
|
---|
[2366] | 189 | ITimeSeriesAlgorithm timeSeriesAlgo = (ITimeSeriesAlgorithm)algo;
|
---|
[2375] | 190 | timeSeriesAlgo.MinTimeOffset = spec.MinTimeOffset;
|
---|
| 191 | timeSeriesAlgo.MaxTimeOffset = spec.MaxTimeOffset;
|
---|
[2422] | 192 | timeSeriesAlgo.TrainingSamplesStart = spec.TrainingSamplesStart - spec.MinTimeOffset + 1; // first possible index is 1 because of differential symbol
|
---|
[2375] | 193 | if (spec.AutoRegressive) {
|
---|
[2440] | 194 | allowedFeatures.Add(spec.TargetVariable);
|
---|
[2130] | 195 | }
|
---|
[2119] | 196 | }
|
---|
[2375] | 197 | algo.AllowedVariables = allowedFeatures;
|
---|
[2119] | 198 | }
|
---|
| 199 |
|
---|
| 200 |
|
---|
[2375] | 201 | private void AddDispatchedRun(ProblemSpecification specification, string algorithm) {
|
---|
[2119] | 202 | AlgorithmConfiguration conf = new AlgorithmConfiguration();
|
---|
[2375] | 203 | conf.name = algorithm;
|
---|
| 204 | conf.problemSpecification = new ProblemSpecification(specification);
|
---|
| 205 | if (!finishedAndDispatchedRuns.ContainsKey(specification.TargetVariable))
|
---|
| 206 | finishedAndDispatchedRuns.Add(specification.TargetVariable, new List<AlgorithmConfiguration>());
|
---|
| 207 | finishedAndDispatchedRuns[specification.TargetVariable].Add(conf);
|
---|
[1873] | 208 | }
|
---|
| 209 |
|
---|
[2375] | 210 | private bool AlgorithmFinishedOrDispatched(ProblemSpecification specification, string algoName) {
|
---|
[1873] | 211 | return
|
---|
[2375] | 212 | finishedAndDispatchedRuns.ContainsKey(specification.TargetVariable) &&
|
---|
| 213 | finishedAndDispatchedRuns[specification.TargetVariable].Any(x =>
|
---|
[2119] | 214 | algoName == x.name &&
|
---|
[2375] | 215 | specification.Equals(x.problemSpecification));
|
---|
[1873] | 216 | }
|
---|
[2290] | 217 |
|
---|
[2375] | 218 | internal void EnableTargetVariable(string name) {
|
---|
| 219 | activeVariables[name] = true;
|
---|
[2290] | 220 | }
|
---|
| 221 |
|
---|
[2375] | 222 | internal void DisableTargetVariable(string name) {
|
---|
| 223 | activeVariables[name] = false;
|
---|
[2290] | 224 | }
|
---|
| 225 |
|
---|
[2375] | 226 | public void EnableAlgorithm(string targetVariable, HeuristicLab.Modeling.IAlgorithm algo) {
|
---|
| 227 | if (!algorithms.ContainsKey(targetVariable)) algorithms.Add(targetVariable, new List<HeuristicLab.Modeling.IAlgorithm>());
|
---|
[2422] | 228 | if (!algorithms[targetVariable].Contains(algo))
|
---|
| 229 | algorithms[targetVariable].Add(algo);
|
---|
[2290] | 230 | }
|
---|
| 231 |
|
---|
[2375] | 232 | public void DisableAlgorithm(string targetVariable, HeuristicLab.Modeling.IAlgorithm algo) {
|
---|
| 233 | algorithms[targetVariable].Remove(algo);
|
---|
[2290] | 234 | }
|
---|
| 235 |
|
---|
[2375] | 236 | public ProblemSpecification GetProblemSpecification(string targetVariable) {
|
---|
| 237 | if (!problemSpecifications.ContainsKey(targetVariable))
|
---|
| 238 | problemSpecifications[targetVariable] = CreateDefaultProblemSpecification(targetVariable);
|
---|
| 239 |
|
---|
| 240 | return problemSpecifications[targetVariable];
|
---|
[2290] | 241 | }
|
---|
| 242 |
|
---|
[2375] | 243 | private ProblemSpecification CreateDefaultProblemSpecification(string targetVariable) {
|
---|
| 244 | ProblemSpecification spec = new ProblemSpecification();
|
---|
| 245 | spec.Dataset = dataset;
|
---|
| 246 | spec.TargetVariable = targetVariable;
|
---|
| 247 | spec.LearningTask = LearningTask.Regression;
|
---|
| 248 | int targetColumn = dataset.GetVariableIndex(targetVariable);
|
---|
| 249 | // find index of first correct target value
|
---|
| 250 | int firstValueIndex;
|
---|
| 251 | for (firstValueIndex = 0; firstValueIndex < dataset.Rows; firstValueIndex++) {
|
---|
| 252 | double x = dataset.GetValue(firstValueIndex, targetColumn);
|
---|
| 253 | if (!(double.IsNaN(x) || double.IsInfinity(x))) break;
|
---|
[2290] | 254 | }
|
---|
[2375] | 255 | // find index of last correct target value
|
---|
| 256 | int lastValueIndex;
|
---|
| 257 | for (lastValueIndex = dataset.Rows - 1; lastValueIndex > firstValueIndex; lastValueIndex--) {
|
---|
| 258 | double x = dataset.GetValue(lastValueIndex, targetColumn);
|
---|
| 259 | if (!(double.IsNaN(x) || double.IsInfinity(x))) break;
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | int validTargetRange = lastValueIndex - firstValueIndex;
|
---|
| 263 | spec.TrainingSamplesStart = firstValueIndex;
|
---|
| 264 | spec.TrainingSamplesEnd = firstValueIndex + (int)Math.Floor(validTargetRange * 0.5);
|
---|
| 265 | spec.ValidationSamplesStart = spec.TrainingSamplesEnd;
|
---|
| 266 | spec.ValidationSamplesEnd = firstValueIndex + (int)Math.Floor(validTargetRange * 0.75);
|
---|
| 267 | spec.TestSamplesStart = spec.ValidationSamplesEnd;
|
---|
| 268 | spec.TestSamplesEnd = lastValueIndex;
|
---|
| 269 | return spec;
|
---|
[2290] | 270 | }
|
---|
| 271 |
|
---|
[2375] | 272 | public void FireChanged() {
|
---|
[2290] | 273 | if (Changed != null) Changed(this, new EventArgs());
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | #region IViewable Members
|
---|
| 277 |
|
---|
| 278 | public virtual IView CreateView() {
|
---|
| 279 | return new DispatcherView(this);
|
---|
| 280 | }
|
---|
| 281 |
|
---|
| 282 | #endregion
|
---|
[1044] | 283 | }
|
---|
| 284 | }
|
---|