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.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<string> allowedFeatures = new List<string>();
|
---|
182 | foreach (string inputVariable in spec.InputVariables) {
|
---|
183 | if (inputVariable != spec.TargetVariable) {
|
---|
184 | allowedFeatures.Add(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.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 | 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;
|
---|
254 | }
|
---|
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;
|
---|
270 | }
|
---|
271 |
|
---|
272 | public void FireChanged() {
|
---|
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
|
---|
283 | }
|
---|
284 | }
|
---|