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;
|
---|
32 | using HeuristicLab.Data;
|
---|
33 | using HeuristicLab.Core;
|
---|
34 | using HeuristicLab.Modeling;
|
---|
35 | using HeuristicLab.Modeling.Database;
|
---|
36 | using HeuristicLab.DataAnalysis;
|
---|
37 |
|
---|
38 | namespace HeuristicLab.CEDMA.Server {
|
---|
39 | public class SimpleDispatcher : IDispatcher, IViewable {
|
---|
40 | private class AlgorithmConfiguration {
|
---|
41 | public string name;
|
---|
42 | public ProblemSpecification problemSpecification;
|
---|
43 | }
|
---|
44 |
|
---|
45 | internal event EventHandler Changed;
|
---|
46 |
|
---|
47 | private IModelingDatabase database;
|
---|
48 | public IModelingDatabase Database {
|
---|
49 | get {
|
---|
50 | return database;
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | private Dataset dataset;
|
---|
55 | public Dataset Dataset {
|
---|
56 | get {
|
---|
57 | return dataset;
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | public IEnumerable<string> TargetVariables {
|
---|
62 | get {
|
---|
63 | return Enumerable.Range(0, Dataset.Columns).Select(x => Dataset.GetVariableName(x));
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | public IEnumerable<string> Variables {
|
---|
68 | get {
|
---|
69 | return TargetVariables;
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
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 | }
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
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[] { };
|
---|
98 | }
|
---|
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;
|
---|
104 | private object locker = new object();
|
---|
105 |
|
---|
106 | public SimpleDispatcher(IModelingDatabase database, Dataset dataset) {
|
---|
107 | this.dataset = dataset;
|
---|
108 | this.database = database;
|
---|
109 | dataset.Changed += (sender, args) => FireChanged();
|
---|
110 | random = new Random();
|
---|
111 |
|
---|
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 |
|
---|
117 | DiscoveryService ds = new DiscoveryService();
|
---|
118 | defaultAlgorithms = ds.GetInstances<HeuristicLab.Modeling.IAlgorithm>();
|
---|
119 |
|
---|
120 | // PopulateFinishedRuns();
|
---|
121 | }
|
---|
122 |
|
---|
123 | public HeuristicLab.Modeling.IAlgorithm GetNextJob() {
|
---|
124 | lock (locker) {
|
---|
125 | if (activeVariables.Count > 0) {
|
---|
126 | string[] targetVariables = activeVariables.Keys.ToArray();
|
---|
127 | string targetVariable = SelectTargetVariable(targetVariables);
|
---|
128 | HeuristicLab.Modeling.IAlgorithm selectedAlgorithm = SelectAndConfigureAlgorithm(targetVariable);
|
---|
129 |
|
---|
130 | return selectedAlgorithm;
|
---|
131 | } else return null;
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | public virtual string SelectTargetVariable(string[] targetVariables) {
|
---|
136 | return targetVariables[random.Next(targetVariables.Length)];
|
---|
137 | }
|
---|
138 |
|
---|
139 | public HeuristicLab.Modeling.IAlgorithm SelectAndConfigureAlgorithm(string targetVariable) {
|
---|
140 | HeuristicLab.Modeling.IAlgorithm selectedAlgorithm = null;
|
---|
141 | var possibleAlgos =
|
---|
142 | algorithms[targetVariable]
|
---|
143 | .Where(x =>
|
---|
144 | ((x is IStochasticAlgorithm) || !AlgorithmFinishedOrDispatched(problemSpecifications[targetVariable], x.Name)));
|
---|
145 | if (possibleAlgos.Count() > 0) selectedAlgorithm = possibleAlgos.ElementAt(random.Next(possibleAlgos.Count()));
|
---|
146 | if (selectedAlgorithm != null) {
|
---|
147 | // create a clone of the algorithm template before setting the parameters
|
---|
148 | selectedAlgorithm = (HeuristicLab.Modeling.IAlgorithm)selectedAlgorithm.Clone();
|
---|
149 | SetProblemParameters(selectedAlgorithm, problemSpecifications[targetVariable]);
|
---|
150 | if (!(selectedAlgorithm is IStochasticAlgorithm))
|
---|
151 | AddDispatchedRun(problemSpecifications[targetVariable], selectedAlgorithm.Name);
|
---|
152 | }
|
---|
153 | return selectedAlgorithm;
|
---|
154 | }
|
---|
155 |
|
---|
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 | //}
|
---|
170 |
|
---|
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));
|
---|
184 | }
|
---|
185 | }
|
---|
186 |
|
---|
187 | if (spec.LearningTask == LearningTask.TimeSeries) {
|
---|
188 | ITimeSeriesAlgorithm timeSeriesAlgo = (ITimeSeriesAlgorithm)algo;
|
---|
189 | timeSeriesAlgo.MinTimeOffset = spec.MinTimeOffset;
|
---|
190 | timeSeriesAlgo.MaxTimeOffset = spec.MaxTimeOffset;
|
---|
191 | timeSeriesAlgo.TrainingSamplesStart = spec.TrainingSamplesStart - spec.MinTimeOffset +1 ; // first possible index is 1 because of differential symbol
|
---|
192 | if (spec.AutoRegressive) {
|
---|
193 | allowedFeatures.Add(spec.Dataset.GetVariableIndex(spec.TargetVariable));
|
---|
194 | }
|
---|
195 | }
|
---|
196 | algo.AllowedVariables = allowedFeatures;
|
---|
197 | }
|
---|
198 |
|
---|
199 |
|
---|
200 | private void AddDispatchedRun(ProblemSpecification specification, string algorithm) {
|
---|
201 | AlgorithmConfiguration conf = new AlgorithmConfiguration();
|
---|
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);
|
---|
207 | }
|
---|
208 |
|
---|
209 | private bool AlgorithmFinishedOrDispatched(ProblemSpecification specification, string algoName) {
|
---|
210 | return
|
---|
211 | finishedAndDispatchedRuns.ContainsKey(specification.TargetVariable) &&
|
---|
212 | finishedAndDispatchedRuns[specification.TargetVariable].Any(x =>
|
---|
213 | algoName == x.name &&
|
---|
214 | specification.Equals(x.problemSpecification));
|
---|
215 | }
|
---|
216 |
|
---|
217 | internal void EnableTargetVariable(string name) {
|
---|
218 | activeVariables[name] = true;
|
---|
219 | }
|
---|
220 |
|
---|
221 | internal void DisableTargetVariable(string name) {
|
---|
222 | activeVariables[name] = false;
|
---|
223 | }
|
---|
224 |
|
---|
225 | public void EnableAlgorithm(string targetVariable, HeuristicLab.Modeling.IAlgorithm algo) {
|
---|
226 | if (!algorithms.ContainsKey(targetVariable)) algorithms.Add(targetVariable, new List<HeuristicLab.Modeling.IAlgorithm>());
|
---|
227 | if(!algorithms[targetVariable].Contains(algo))
|
---|
228 | algorithms[targetVariable].Add(algo);
|
---|
229 | }
|
---|
230 |
|
---|
231 | public void DisableAlgorithm(string targetVariable, HeuristicLab.Modeling.IAlgorithm algo) {
|
---|
232 | algorithms[targetVariable].Remove(algo);
|
---|
233 | }
|
---|
234 |
|
---|
235 | public ProblemSpecification GetProblemSpecification(string targetVariable) {
|
---|
236 | if (!problemSpecifications.ContainsKey(targetVariable))
|
---|
237 | problemSpecifications[targetVariable] = CreateDefaultProblemSpecification(targetVariable);
|
---|
238 |
|
---|
239 | return problemSpecifications[targetVariable];
|
---|
240 | }
|
---|
241 |
|
---|
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;
|
---|
278 | }
|
---|
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;
|
---|
294 | }
|
---|
295 |
|
---|
296 | public void FireChanged() {
|
---|
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
|
---|
307 | }
|
---|
308 | }
|
---|