Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CEDMA-Exporter-715/sources/HeuristicLab.CEDMA.Server/3.3/SimpleDispatcher.cs @ 2227

Last change on this file since 2227 was 2153, checked in by gkronber, 15 years ago

Changed CEDMA dispatcher to allow different input variable sets for each target variable. #676 (Cockpit for the CEDMA Server to control algorithm settings)

File size: 9.8 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Text;
25using System.Windows.Forms;
26using HeuristicLab.PluginInfrastructure;
27using System.Net;
28using System.ServiceModel;
29using HeuristicLab.CEDMA.DB.Interfaces;
30using HeuristicLab.CEDMA.DB;
31using System.ServiceModel.Description;
32using System.Linq;
33using HeuristicLab.CEDMA.Core;
34using HeuristicLab.GP.StructureIdentification;
35using HeuristicLab.Data;
36using HeuristicLab.Core;
37using HeuristicLab.Modeling;
38
39namespace HeuristicLab.CEDMA.Server {
40  public class SimpleDispatcher : DispatcherBase {
41    private class AlgorithmConfiguration {
42      public string name;
43      public int targetVariable;
44      public List<int> inputVariables;
45    }
46
47    private Random random;
48    private IStore store;
49    private Dictionary<int, List<AlgorithmConfiguration>> finishedAndDispatchedRuns;
50
51    public SimpleDispatcher(IStore store)
52      : base(store) {
53      this.store = store;
54      random = new Random();
55      finishedAndDispatchedRuns = new Dictionary<int, List<AlgorithmConfiguration>>();
56      PopulateFinishedRuns();
57    }
58
59    public override IAlgorithm SelectAndConfigureAlgorithm(int targetVariable, int[] inputVariables, Problem problem) {
60      DiscoveryService ds = new DiscoveryService();
61      IAlgorithm[] algos = ds.GetInstances<IAlgorithm>();
62      IAlgorithm selectedAlgorithm = null;
63      switch (problem.LearningTask) {
64        case LearningTask.Regression: {
65            var regressionAlgos = algos.Where(a => (a as IClassificationAlgorithm) == null && (a as ITimeSeriesAlgorithm) == null);
66            selectedAlgorithm = ChooseDeterministic(targetVariable, inputVariables, regressionAlgos) ?? ChooseStochastic(regressionAlgos);
67            break;
68          }
69        case LearningTask.Classification: {
70            var classificationAlgos = algos.Where(a => (a as IClassificationAlgorithm) != null);
71            selectedAlgorithm = ChooseDeterministic(targetVariable, inputVariables, classificationAlgos) ?? ChooseStochastic(classificationAlgos);
72            break;
73          }
74        case LearningTask.TimeSeries: {
75            var timeSeriesAlgos = algos.Where(a => (a as ITimeSeriesAlgorithm) != null);
76            selectedAlgorithm = ChooseDeterministic(targetVariable, inputVariables, timeSeriesAlgos) ?? ChooseStochastic(timeSeriesAlgos);
77            break;
78          }
79      }
80
81
82      if (selectedAlgorithm != null) {
83        SetProblemParameters(selectedAlgorithm, problem, targetVariable, inputVariables);
84        AddDispatchedRun(targetVariable, inputVariables, selectedAlgorithm.Name);
85      }
86      return selectedAlgorithm;
87    }
88
89    private IAlgorithm ChooseDeterministic(int targetVariable, int[] inputVariables, IEnumerable<IAlgorithm> algos) {
90      var deterministicAlgos = algos
91        .Where(a => (a as IStochasticAlgorithm) == null)
92        .Where(a => AlgorithmFinishedOrDispatched(targetVariable, inputVariables, a.Name) == false);
93
94      if (deterministicAlgos.Count() == 0) return null;
95      return deterministicAlgos.ElementAt(random.Next(deterministicAlgos.Count()));
96    }
97
98    private IAlgorithm ChooseStochastic(IEnumerable<IAlgorithm> regressionAlgos) {
99      var stochasticAlgos = regressionAlgos.Where(a => (a as IStochasticAlgorithm) != null);
100      if (stochasticAlgos.Count() == 0) return null;
101      return stochasticAlgos.ElementAt(random.Next(stochasticAlgos.Count()));
102    }
103
104    private void PopulateFinishedRuns() {
105      Dictionary<Entity, Entity> processedModels = new Dictionary<Entity, Entity>();
106      var datasetBindings = store
107        .Query(
108        "?Dataset <" + Ontology.InstanceOf + "> <" + Ontology.TypeDataSet + "> .", 0, 1)
109        .Select(x => (Entity)x.Get("Dataset"));
110
111      if (datasetBindings.Count() > 0) {
112        var datasetEntity = datasetBindings.ElementAt(0);
113
114        DataSet ds = new DataSet(store, datasetEntity);
115        var result = store
116          .Query(
117          "?Model <" + Ontology.TargetVariable + "> ?TargetVariable ." + Environment.NewLine +
118          "?Model <" + Ontology.Name + "> ?AlgoName .",
119          0, 1000)
120          .Select(x => new Resource[] { (Literal)x.Get("TargetVariable"), (Literal)x.Get("AlgoName"), (Entity)x.Get("Model") });
121
122        foreach (Resource[] row in result) {
123          Entity model = ((Entity)row[2]);
124          if (!processedModels.ContainsKey(model)) {
125            processedModels.Add(model, model);
126
127            string targetVariable = (string)((Literal)row[0]).Value;
128            string algoName = (string)((Literal)row[1]).Value;
129            int targetVariableIndex = ds.Problem.Dataset.GetVariableIndex(targetVariable);
130
131            var inputVariableLiterals = store
132              .Query(
133                "<" + model.Uri + "> <" + Ontology.HasInputVariable + "> ?InputVariable ." + Environment.NewLine +
134                "?InputVariable <" + Ontology.Name + "> ?Name .",
135                0, 1000)
136              .Select(x => (Literal)x.Get("Name"))
137              .Select(l => (string)l.Value)
138              .Distinct();
139
140            List<int> inputVariables = new List<int>();
141            foreach (string variableName in inputVariableLiterals) {
142              int variableIndex = ds.Problem.Dataset.GetVariableIndex(variableName);
143              inputVariables.Add(variableIndex);
144            }
145            if (!AlgorithmFinishedOrDispatched(targetVariableIndex, inputVariables.ToArray(), algoName)) {
146              AddDispatchedRun(targetVariableIndex, inputVariables.ToArray(), algoName);
147            }
148          }
149        }
150      }
151    }
152
153    private void SetProblemParameters(IAlgorithm algo, Problem problem, int targetVariable, int[] inputVariables) {
154      algo.Dataset = problem.Dataset;
155      algo.TargetVariable = targetVariable;
156      algo.ProblemInjector.GetVariable("TrainingSamplesStart").GetValue<IntData>().Data = problem.TrainingSamplesStart;
157      algo.ProblemInjector.GetVariable("TrainingSamplesEnd").GetValue<IntData>().Data = problem.TrainingSamplesEnd;
158      algo.ProblemInjector.GetVariable("ValidationSamplesStart").GetValue<IntData>().Data = problem.ValidationSamplesStart;
159      algo.ProblemInjector.GetVariable("ValidationSamplesEnd").GetValue<IntData>().Data = problem.ValidationSamplesEnd;
160      algo.ProblemInjector.GetVariable("TestSamplesStart").GetValue<IntData>().Data = problem.TestSamplesStart;
161      algo.ProblemInjector.GetVariable("TestSamplesEnd").GetValue<IntData>().Data = problem.TestSamplesEnd;
162      ItemList<IntData> allowedFeatures = algo.ProblemInjector.GetVariable("AllowedFeatures").GetValue<ItemList<IntData>>();
163      foreach (int inputVariable in inputVariables) {
164        if (inputVariable != targetVariable) {
165          allowedFeatures.Add(new IntData(inputVariable));
166        }
167      }
168
169      if (problem.LearningTask == LearningTask.TimeSeries) {
170        algo.ProblemInjector.GetVariable("Autoregressive").GetValue<BoolData>().Data = problem.AutoRegressive;
171        algo.ProblemInjector.GetVariable("MinTimeOffset").GetValue<IntData>().Data = problem.MinTimeOffset;
172        algo.ProblemInjector.GetVariable("MaxTimeOffset").GetValue<IntData>().Data = problem.MaxTimeOffset;
173        if (problem.AutoRegressive) {
174          allowedFeatures.Add(new IntData(targetVariable));
175        }
176      } else if (problem.LearningTask == LearningTask.Classification) {
177        ItemList<DoubleData> classValues = algo.ProblemInjector.GetVariable("TargetClassValues").GetValue<ItemList<DoubleData>>();
178        foreach (double classValue in GetDifferentClassValues(problem.Dataset, targetVariable)) classValues.Add(new DoubleData(classValue));
179      }
180    }
181
182    private IEnumerable<double> GetDifferentClassValues(HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable) {
183      return Enumerable.Range(0, dataset.Rows).Select(x => dataset.GetValue(x, targetVariable)).Distinct();
184    }
185
186    private void AddDispatchedRun(int targetVariable, int[] inputVariables, string algoName) {
187      if (!finishedAndDispatchedRuns.ContainsKey(targetVariable)) {
188        finishedAndDispatchedRuns[targetVariable] = new List<AlgorithmConfiguration>();
189      }
190      AlgorithmConfiguration conf = new AlgorithmConfiguration();
191      conf.name = algoName;
192      conf.inputVariables = new List<int>(inputVariables);
193      conf.targetVariable = targetVariable;
194      finishedAndDispatchedRuns[targetVariable].Add(conf);
195    }
196
197    private bool AlgorithmFinishedOrDispatched(int targetVariable, int[] inputVariables, string algoName) {
198      return
199        finishedAndDispatchedRuns.ContainsKey(targetVariable) &&
200        finishedAndDispatchedRuns[targetVariable].Any(x => targetVariable == x.targetVariable &&
201                                                           algoName == x.name &&
202                                                           inputVariables.Count() == x.inputVariables.Count() &&
203                                                           inputVariables.All(v => x.inputVariables.Contains(v)));
204    }
205  }
206}
Note: See TracBrowser for help on using the repository browser.