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