source: branches/CEDMA-Refactoring-Ticket419/HeuristicLab.CEDMA.Server/Dispatcher.cs @ 1129

Last change on this file since 1129 was 1129, checked in by gkronber, 12 years ago

worked on RDF backend. Implemented query support to load datasets. Queries for dispatcher and results still missing. (#419)

File size: 5.5 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;
37
38namespace HeuristicLab.CEDMA.Server {
39  public class Dispatcher {
40    private List<Execution> dispatchQueue;
41    public IList<string> DispatchQueue {
42      get { return dispatchQueue.Select(t => "StandardGP").ToList(); }
43    }
44
45    private IStore store;
46
47    public Dispatcher(IStore store) {
48      this.store = store;
49      this.dispatchQueue = new List<Execution>();
50    }
51
52    private void FillDispatchQueue() {
53      //Dictionary<Entity, Dictionary<int, int>> numberOfModelsOfTargetVariableOfDataSet = new Dictionary<Entity, Dictionary<int, int>>();
54      //var datasetStatements = store.Select(new Statement(Ontology.AnyEntity, Ontology.PredicateInstanceOf, Ontology.TypeDataSet));
55      //foreach (Statement datasetStatement in datasetStatements) {
56      //  numberOfModelsOfTargetVariableOfDataSet.Add(datasetStatement.Subject, new Dictionary<int, int>());
57      //  var modelStatements = store.Select(new Statement(datasetStatement.Subject, Ontology.PredicateHasModel, Ontology.AnyEntity));
58      //  foreach (Statement modelStatement in modelStatements) {
59      //    var modelAttributeStatements = store.Select(new Statement((Entity)modelStatement.Property, Ontology.PredicateModelAttribute, Ontology.AnyEntity));
60      //    foreach (Statement modelAttrStatement in modelAttributeStatements) {
61      //      var targetVariableStatements = store.Select(new Statement((Entity)modelAttrStatement.Property, Ontology.PredicateModelAttributeName, Ontology.AnyEntity))
62      //         .Where(t => (string)((Literal)t.Property).Value == "TargetVariable")
63      //         .SelectMany(t => store.Select(new Statement((Entity)modelAttrStatement.Property, Ontology.PredicateModelAttributeValue, Ontology.AnyEntity)))
64      //         .GroupBy(t => (int)((Literal)t.Property).Value);
65      //      foreach (var targetVariable in targetVariableStatements) {
66      //        if (numberOfModelsOfTargetVariableOfDataSet[datasetStatement.Subject].ContainsKey(targetVariable.Key)) {
67      //          numberOfModelsOfTargetVariableOfDataSet[datasetStatement.Subject][targetVariable.Key] = targetVariable.Count();
68      //        } else {
69      //          numberOfModelsOfTargetVariableOfDataSet[datasetStatement.Subject].Add(targetVariable.Key, targetVariable.Count());
70      //        }
71      //      }
72      //    }
73      //  }
74      //}
75      //foreach (KeyValuePair<Entity, Dictionary<int, int>> dataSetEntry in numberOfModelsOfTargetVariableOfDataSet) {
76      //  DataSet dataSet = new DataSet(store, dataSetEntry.Key);
77      //  foreach (int targetVariable in dataSet.Problem.AllowedTargetVariables)
78      //    if (!dataSetEntry.Value.ContainsKey(targetVariable) || dataSetEntry.Value[targetVariable] < 10) {
79      //      IEngine engine = CreateEngine(dataSet.Problem, targetVariable);
80      //      if (engine != null) {
81      //        QueueJob(new Execution(dataSetEntry.Key, engine, targetVariable));
82      //      }
83      //    }
84      //}
85    }
86
87    private void QueueJob(Execution execution) {
88      dispatchQueue.Add(execution);
89    }
90
91    public Execution GetNextJob() {
92      if (dispatchQueue.Count == 0) FillDispatchQueue();
93      if (dispatchQueue.Count > 0) {
94        Execution next = dispatchQueue[0];
95        dispatchQueue.RemoveAt(0);
96        return next;
97      } else
98        return null;
99    }
100
101    internal void Start() {
102      FillDispatchQueue();
103    }
104
105    private IEngine CreateEngine(Problem problem, int targetVariable) {
106      switch (problem.LearningTask) {
107        case LearningTask.Classification: return null;
108        case LearningTask.Regression: {
109            return CreateStandardGp(problem, targetVariable).Engine;
110          }
111        case LearningTask.TimeSeries: return null;
112        case LearningTask.Clustering: return null;
113        default: return null;
114      }
115    }
116
117    private StandardGP CreateStandardGp(Problem problem, int targetVariable) {
118      ProblemInjector probInjector = new ProblemInjector(problem);
119      probInjector.TargetVariable = targetVariable;
120      StandardGP sgp = new StandardGP();
121      sgp.SetSeedRandomly = true;
122      sgp.MaxGenerations = 100;
123      sgp.PopulationSize = 1000;
124      sgp.Elites = 1;
125      sgp.ProblemInjector = probInjector;
126      return sgp;
127    }
128  }
129}
Note: See TracBrowser for help on using the repository browser.