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