Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone3/sources/HeuristicLab.CEDMA.Server/3.3/SimpleDispatcher.cs @ 6479

Last change on this file since 6479 was 2049, checked in by gkronber, 16 years ago

Fixed a bug that is triggered when the results DB is still empty. #656

File size: 5.6 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 Random random;
42    private IStore store;
43    private Dictionary<int, List<string>> finishedAndDispatchedRuns;
44
45    public SimpleDispatcher(IStore store)
46      : base(store) {
47      this.store = store;
48      random = new Random();
49      finishedAndDispatchedRuns = new Dictionary<int, List<string>>();
50      PopulateFinishedRuns();
51    }
52
53    public override IAlgorithm SelectAlgorithm(int targetVariable, LearningTask learningTask) {
54      DiscoveryService ds = new DiscoveryService();
55      IAlgorithm[] algos = ds.GetInstances<IAlgorithm>();
56      IAlgorithm selectedAlgorithm = null;
57      switch (learningTask) {
58        case LearningTask.Regression: {
59            var regressionAlgos = algos.Where(a => (a as IClassificationAlgorithm) == null && (a as ITimeSeriesAlgorithm) == null);
60            selectedAlgorithm = ChooseDeterministic(targetVariable, regressionAlgos) ?? ChooseStochastic(regressionAlgos);
61            break;
62          }
63        case LearningTask.Classification: {
64            var classificationAlgos = algos.Where(a => (a as IClassificationAlgorithm) != null);
65            selectedAlgorithm = ChooseDeterministic(targetVariable, classificationAlgos) ?? ChooseStochastic(classificationAlgos);
66            break;
67          }
68        case LearningTask.TimeSeries: {
69            var timeSeriesAlgos = algos.Where(a => (a as ITimeSeriesAlgorithm) != null);
70            selectedAlgorithm = ChooseDeterministic(targetVariable, timeSeriesAlgos) ?? ChooseStochastic(timeSeriesAlgos);
71            break;
72          }
73      }
74      if (selectedAlgorithm != null) {
75        AddDispatchedRun(targetVariable, selectedAlgorithm.Name);
76      }
77      return selectedAlgorithm;
78    }
79
80    private IAlgorithm ChooseDeterministic(int targetVariable, IEnumerable<IAlgorithm> algos) {
81      var deterministicAlgos = algos
82        .Where(a => (a as IStochasticAlgorithm) == null)
83        .Where(a => AlgorithmFinishedOrDispatched(targetVariable, a.Name) == false);
84
85      if (deterministicAlgos.Count() == 0) return null;
86      return deterministicAlgos.ElementAt(random.Next(deterministicAlgos.Count()));
87    }
88
89    private IAlgorithm ChooseStochastic(IEnumerable<IAlgorithm> regressionAlgos) {
90      var stochasticAlgos = regressionAlgos.Where(a => (a as IStochasticAlgorithm) != null);
91      if (stochasticAlgos.Count() == 0) return null;
92      return stochasticAlgos.ElementAt(random.Next(stochasticAlgos.Count()));
93    }
94
95    public override int SelectTargetVariable(int[] targetVariables) {
96      return targetVariables[random.Next(targetVariables.Length)];
97    }
98
99    private void PopulateFinishedRuns() {
100      var datasetBindings = store
101        .Query(
102        "?Dataset <" + Ontology.InstanceOf + "> <" + Ontology.TypeDataSet + "> .", 0, 1)
103        .Select(x => (Entity)x.Get("Dataset"));
104
105      if (datasetBindings.Count() > 0) {
106        var datasetEntity = datasetBindings.ElementAt(0);
107
108        DataSet ds = new DataSet(store, datasetEntity);
109        var result = store
110          .Query(
111          "?Model <" + Ontology.TargetVariable + "> ?TargetVariable ." + Environment.NewLine +
112          "?Model <" + Ontology.Name + "> ?AlgoName .",
113          0, 1000)
114          .Select(x => new Resource[] { (Literal)x.Get("TargetVariable"), (Literal)x.Get("AlgoName") });
115
116        foreach (Resource[] row in result) {
117          string targetVariable = (string)((Literal)row[0]).Value;
118          string algoName = (string)((Literal)row[1]).Value;
119
120          int targetVariableIndex = ds.Problem.Dataset.GetVariableIndex(targetVariable);
121          if (!AlgorithmFinishedOrDispatched(targetVariableIndex, algoName))
122            AddDispatchedRun(targetVariableIndex, algoName);
123        }
124      }
125    }
126
127    private void AddDispatchedRun(int targetVariable, string algoName) {
128      if (!finishedAndDispatchedRuns.ContainsKey(targetVariable)) {
129        finishedAndDispatchedRuns[targetVariable] = new List<string>();
130      }
131      finishedAndDispatchedRuns[targetVariable].Add(algoName);
132    }
133
134    private bool AlgorithmFinishedOrDispatched(int targetVariable, string algoName) {
135      return
136        finishedAndDispatchedRuns.ContainsKey(targetVariable) &&
137        finishedAndDispatchedRuns[targetVariable].Contains(algoName);
138    }
139  }
140}
Note: See TracBrowser for help on using the repository browser.