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