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 |
|
---|
37 | namespace HeuristicLab.CEDMA.Server {
|
---|
38 | public class SimpleDispatcher : IDispatcher {
|
---|
39 | private class AlgorithmConfiguration {
|
---|
40 | public string name;
|
---|
41 | public int targetVariable;
|
---|
42 | public List<int> inputVariables;
|
---|
43 | }
|
---|
44 |
|
---|
45 | private IModelingDatabase database;
|
---|
46 | public IModelingDatabase Database {
|
---|
47 | get {
|
---|
48 | return database;
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | private Problem problem;
|
---|
53 | public Problem Problem {
|
---|
54 | get {
|
---|
55 | return problem;
|
---|
56 | }
|
---|
57 | }
|
---|
58 | internal event EventHandler Changed;
|
---|
59 |
|
---|
60 | public IEnumerable<string> TargetVariables {
|
---|
61 | get {
|
---|
62 | return Enumerable.Range(0, problem.Dataset.Columns).Select(x => problem.Dataset.GetVariableName(x));
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | public IEnumerable<string> InputVariables {
|
---|
67 | get {
|
---|
68 | return TargetVariables;
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | private HeuristicLab.Modeling.IAlgorithm[] algorithms;
|
---|
73 | public IEnumerable<HeuristicLab.Modeling.IAlgorithm> Algorithms {
|
---|
74 | get {
|
---|
75 | switch (Problem.LearningTask) {
|
---|
76 | case LearningTask.Regression: {
|
---|
77 | return algorithms.Where(a => (a as IClassificationAlgorithm) == null && (a as ITimeSeriesAlgorithm) == null);
|
---|
78 | }
|
---|
79 | case LearningTask.Classification: {
|
---|
80 | return algorithms.Where(a => (a as IClassificationAlgorithm) != null);
|
---|
81 | }
|
---|
82 | case LearningTask.TimeSeries: {
|
---|
83 | return algorithms.Where(a => (a as ITimeSeriesAlgorithm) != null);
|
---|
84 | }
|
---|
85 | }
|
---|
86 | return new HeuristicLab.Modeling.IAlgorithm[] { };
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | private List<HeuristicLab.Modeling.IAlgorithm> activeAlgorithms;
|
---|
91 | public IEnumerable<HeuristicLab.Modeling.IAlgorithm> ActiveAlgorithms {
|
---|
92 | get { return activeAlgorithms; }
|
---|
93 | }
|
---|
94 |
|
---|
95 | private Random random;
|
---|
96 | private List<int> allowedTargetVariables;
|
---|
97 | private Dictionary<int, List<int>> activeInputVariables;
|
---|
98 | private Dictionary<int, List<AlgorithmConfiguration>> finishedAndDispatchedRuns;
|
---|
99 | private object locker = new object();
|
---|
100 |
|
---|
101 | public SimpleDispatcher(IModelingDatabase database, Problem problem) {
|
---|
102 | this.problem = problem;
|
---|
103 | this.database = database;
|
---|
104 | this.random = new Random();
|
---|
105 |
|
---|
106 | this.finishedAndDispatchedRuns = new Dictionary<int, List<AlgorithmConfiguration>>();
|
---|
107 | this.allowedTargetVariables = new List<int>();
|
---|
108 | this.activeInputVariables = new Dictionary<int, List<int>>();
|
---|
109 | this.activeAlgorithms = new List<HeuristicLab.Modeling.IAlgorithm>();
|
---|
110 | DiscoveryService ds = new DiscoveryService();
|
---|
111 | this.algorithms = ds.GetInstances<HeuristicLab.Modeling.IAlgorithm>();
|
---|
112 | problem.Changed += (sender, args) => {
|
---|
113 | lock (locker) {
|
---|
114 | allowedTargetVariables.Clear();
|
---|
115 | activeInputVariables.Clear();
|
---|
116 | activeAlgorithms.Clear();
|
---|
117 | }
|
---|
118 | OnChanged();
|
---|
119 | };
|
---|
120 |
|
---|
121 | PopulateFinishedRuns();
|
---|
122 | }
|
---|
123 |
|
---|
124 | public HeuristicLab.Modeling.IAlgorithm GetNextJob() {
|
---|
125 | lock (locker) {
|
---|
126 | if (allowedTargetVariables.Count > 0) {
|
---|
127 | int[] targetVariables = allowedTargetVariables.ToArray();
|
---|
128 | int targetVariable = SelectTargetVariable(targetVariables);
|
---|
129 | int[] inputVariables = activeInputVariables[targetVariable].ToArray();
|
---|
130 |
|
---|
131 | HeuristicLab.Modeling.IAlgorithm selectedAlgorithm = SelectAndConfigureAlgorithm(targetVariable, inputVariables, problem);
|
---|
132 |
|
---|
133 | return selectedAlgorithm;
|
---|
134 | } else return null;
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | public virtual int SelectTargetVariable(int[] targetVariables) {
|
---|
139 | return targetVariables[random.Next(targetVariables.Length)];
|
---|
140 | }
|
---|
141 |
|
---|
142 | public HeuristicLab.Modeling.IAlgorithm SelectAndConfigureAlgorithm(int targetVariable, int[] inputVariables, Problem problem) {
|
---|
143 | HeuristicLab.Modeling.IAlgorithm selectedAlgorithm = null;
|
---|
144 | DiscoveryService ds = new DiscoveryService();
|
---|
145 | var allAlgorithms = ds.GetInstances<HeuristicLab.Modeling.IAlgorithm>();
|
---|
146 | var allowedAlgorithmTypes = activeAlgorithms.Select(x => x.GetType());
|
---|
147 | var possibleAlgos =
|
---|
148 | allAlgorithms
|
---|
149 | .Where(x => allowedAlgorithmTypes.Contains(x.GetType()) &&
|
---|
150 | ((x is IStochasticAlgorithm) || !AlgorithmFinishedOrDispatched(targetVariable, inputVariables, x.Name)));
|
---|
151 | if (possibleAlgos.Count() > 0) selectedAlgorithm = possibleAlgos.ElementAt(random.Next(possibleAlgos.Count()));
|
---|
152 | if (selectedAlgorithm != null) {
|
---|
153 | SetProblemParameters(selectedAlgorithm, problem, targetVariable, inputVariables);
|
---|
154 | if (!(selectedAlgorithm is IStochasticAlgorithm))
|
---|
155 | AddDispatchedRun(targetVariable, inputVariables, selectedAlgorithm.Name);
|
---|
156 | }
|
---|
157 | return selectedAlgorithm;
|
---|
158 | }
|
---|
159 |
|
---|
160 | private void PopulateFinishedRuns() {
|
---|
161 | var dispatchedAlgos = from model in Database.GetAllModels()
|
---|
162 | select new {
|
---|
163 | TargetVariable = model.TargetVariable.Name,
|
---|
164 | Algorithm = model.Algorithm.Name,
|
---|
165 | Inputvariables = Database.GetInputVariableResults(model).Select(x => x.Variable.Name).Distinct()
|
---|
166 | };
|
---|
167 | foreach (var algo in dispatchedAlgos) {
|
---|
168 | AddDispatchedRun(algo.TargetVariable, algo.Inputvariables, algo.Algorithm);
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | private void SetProblemParameters(HeuristicLab.Modeling.IAlgorithm algo, Problem problem, int targetVariable, int[] inputVariables) {
|
---|
173 | algo.Dataset = problem.Dataset;
|
---|
174 | algo.TargetVariable = targetVariable;
|
---|
175 | algo.ProblemInjector.GetVariable("TrainingSamplesStart").GetValue<IntData>().Data = problem.TrainingSamplesStart;
|
---|
176 | algo.ProblemInjector.GetVariable("TrainingSamplesEnd").GetValue<IntData>().Data = problem.TrainingSamplesEnd;
|
---|
177 | algo.ProblemInjector.GetVariable("ValidationSamplesStart").GetValue<IntData>().Data = problem.ValidationSamplesStart;
|
---|
178 | algo.ProblemInjector.GetVariable("ValidationSamplesEnd").GetValue<IntData>().Data = problem.ValidationSamplesEnd;
|
---|
179 | algo.ProblemInjector.GetVariable("TestSamplesStart").GetValue<IntData>().Data = problem.TestSamplesStart;
|
---|
180 | algo.ProblemInjector.GetVariable("TestSamplesEnd").GetValue<IntData>().Data = problem.TestSamplesEnd;
|
---|
181 | ItemList<IntData> allowedFeatures = algo.ProblemInjector.GetVariable("AllowedFeatures").GetValue<ItemList<IntData>>();
|
---|
182 | foreach (int inputVariable in inputVariables) {
|
---|
183 | if (inputVariable != targetVariable) {
|
---|
184 | allowedFeatures.Add(new IntData(inputVariable));
|
---|
185 | }
|
---|
186 | }
|
---|
187 |
|
---|
188 | if (problem.LearningTask == LearningTask.TimeSeries) {
|
---|
189 | algo.ProblemInjector.GetVariable("Autoregressive").GetValue<BoolData>().Data = problem.AutoRegressive;
|
---|
190 | algo.ProblemInjector.GetVariable("MinTimeOffset").GetValue<IntData>().Data = problem.MinTimeOffset;
|
---|
191 | algo.ProblemInjector.GetVariable("MaxTimeOffset").GetValue<IntData>().Data = problem.MaxTimeOffset;
|
---|
192 | if (problem.AutoRegressive) {
|
---|
193 | allowedFeatures.Add(new IntData(targetVariable));
|
---|
194 | }
|
---|
195 | } else if (problem.LearningTask == LearningTask.Classification) {
|
---|
196 | ItemList<DoubleData> classValues = algo.ProblemInjector.GetVariable("TargetClassValues").GetValue<ItemList<DoubleData>>();
|
---|
197 | foreach (double classValue in GetDifferentClassValues(problem.Dataset, targetVariable)) classValues.Add(new DoubleData(classValue));
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 | private IEnumerable<double> GetDifferentClassValues(HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable) {
|
---|
202 | return Enumerable.Range(0, dataset.Rows).Select(x => dataset.GetValue(x, targetVariable)).Distinct();
|
---|
203 | }
|
---|
204 |
|
---|
205 | private void AddDispatchedRun(string targetVariable, IEnumerable<string> inputVariables, string algorithm) {
|
---|
206 | AddDispatchedRun(
|
---|
207 | Problem.Dataset.GetVariableIndex(targetVariable),
|
---|
208 | inputVariables.Select(x => Problem.Dataset.GetVariableIndex(x)).ToArray(),
|
---|
209 | algorithm);
|
---|
210 | }
|
---|
211 |
|
---|
212 | private void AddDispatchedRun(int targetVariable, int[] inputVariables, string algoName) {
|
---|
213 | if (!finishedAndDispatchedRuns.ContainsKey(targetVariable)) {
|
---|
214 | finishedAndDispatchedRuns[targetVariable] = new List<AlgorithmConfiguration>();
|
---|
215 | }
|
---|
216 | AlgorithmConfiguration conf = new AlgorithmConfiguration();
|
---|
217 | conf.name = algoName;
|
---|
218 | conf.inputVariables = new List<int>(inputVariables);
|
---|
219 | conf.targetVariable = targetVariable;
|
---|
220 | finishedAndDispatchedRuns[targetVariable].Add(conf);
|
---|
221 | }
|
---|
222 |
|
---|
223 | private bool AlgorithmFinishedOrDispatched(int targetVariable, int[] inputVariables, string algoName) {
|
---|
224 | return
|
---|
225 | finishedAndDispatchedRuns.ContainsKey(targetVariable) &&
|
---|
226 | finishedAndDispatchedRuns[targetVariable].Any(x => targetVariable == x.targetVariable &&
|
---|
227 | algoName == x.name &&
|
---|
228 | inputVariables.Count() == x.inputVariables.Count() &&
|
---|
229 | inputVariables.All(v => x.inputVariables.Contains(v)));
|
---|
230 | }
|
---|
231 |
|
---|
232 | public void EnableAlgorithm(HeuristicLab.Modeling.IAlgorithm algo) {
|
---|
233 | lock (locker) {
|
---|
234 | if (!activeAlgorithms.Contains(algo)) activeAlgorithms.Add(algo);
|
---|
235 | }
|
---|
236 | }
|
---|
237 |
|
---|
238 | public void DisableAlgorithm(HeuristicLab.Modeling.IAlgorithm algo) {
|
---|
239 | lock (locker) {
|
---|
240 | while (activeAlgorithms.Remove(algo)) ;
|
---|
241 | }
|
---|
242 | }
|
---|
243 |
|
---|
244 | internal void EnableTargetVariable(string name) {
|
---|
245 | int varIndex = problem.Dataset.GetVariableIndex(name);
|
---|
246 | lock (locker)
|
---|
247 | if (!allowedTargetVariables.Contains(varIndex)) allowedTargetVariables.Add(varIndex);
|
---|
248 | }
|
---|
249 |
|
---|
250 | internal void DisableTargetVariable(string name) {
|
---|
251 | int varIndex = problem.Dataset.GetVariableIndex(name);
|
---|
252 | lock (locker)
|
---|
253 | while (allowedTargetVariables.Remove(varIndex)) ;
|
---|
254 | }
|
---|
255 |
|
---|
256 | internal void EnableInputVariable(string target, string name) {
|
---|
257 | int targetIndex = problem.Dataset.GetVariableIndex(target);
|
---|
258 | int inputIndex = problem.Dataset.GetVariableIndex(name);
|
---|
259 | lock (locker) {
|
---|
260 | if (!activeInputVariables.ContainsKey(targetIndex)) activeInputVariables[targetIndex] = new List<int>();
|
---|
261 | if (!activeInputVariables[targetIndex].Contains(inputIndex)) {
|
---|
262 | activeInputVariables[targetIndex].Add(inputIndex);
|
---|
263 | }
|
---|
264 | }
|
---|
265 | }
|
---|
266 |
|
---|
267 | internal void DisableInputVariable(string target, string name) {
|
---|
268 | int targetIndex = problem.Dataset.GetVariableIndex(target);
|
---|
269 | int inputIndex = problem.Dataset.GetVariableIndex(name);
|
---|
270 | lock (locker) {
|
---|
271 | if (!activeInputVariables.ContainsKey(targetIndex)) activeInputVariables[targetIndex] = new List<int>();
|
---|
272 | while (activeInputVariables[targetIndex].Remove(inputIndex)) ;
|
---|
273 | }
|
---|
274 | }
|
---|
275 |
|
---|
276 | public void OnChanged() {
|
---|
277 | if (Changed != null) Changed(this, new EventArgs());
|
---|
278 | }
|
---|
279 |
|
---|
280 | internal IEnumerable<string> GetInputVariables(string target) {
|
---|
281 | int targetIndex = problem.Dataset.GetVariableIndex(target);
|
---|
282 | lock (locker) {
|
---|
283 | if (!activeInputVariables.ContainsKey(targetIndex)) activeInputVariables[targetIndex] = new List<int>();
|
---|
284 | return activeInputVariables[targetIndex]
|
---|
285 | .Select(i => problem.Dataset.GetVariableName(i));
|
---|
286 | }
|
---|
287 | }
|
---|
288 |
|
---|
289 |
|
---|
290 | #region IViewable Members
|
---|
291 |
|
---|
292 | public virtual IView CreateView() {
|
---|
293 | return new DispatcherView(this);
|
---|
294 | }
|
---|
295 |
|
---|
296 | #endregion
|
---|
297 |
|
---|
298 |
|
---|
299 | }
|
---|
300 | }
|
---|