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 HeuristicLab.CEDMA.DB.Interfaces;
|
---|
30 | using HeuristicLab.CEDMA.DB;
|
---|
31 | using System.ServiceModel.Description;
|
---|
32 | using System.Linq;
|
---|
33 | using HeuristicLab.CEDMA.Core;
|
---|
34 | using HeuristicLab.GP.StructureIdentification;
|
---|
35 | using HeuristicLab.Data;
|
---|
36 | using HeuristicLab.Core;
|
---|
37 |
|
---|
38 | namespace 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 | IList<Statement> 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 | IList<Statement> modelStatements = store.Select(new Statement(datasetStatement.Subject, Ontology.PredicateHasModel, Ontology.AnyEntity));
|
---|
58 | foreach (Statement modelStatement in modelStatements) {
|
---|
59 | IList<Statement> 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 | numberOfModelsOfTargetVariableOfDataSet[datasetStatement.Subject].Add(targetVariable.Key, targetVariable.Count());
|
---|
67 | }
|
---|
68 | }
|
---|
69 | }
|
---|
70 | }
|
---|
71 | foreach (KeyValuePair<Entity, Dictionary<int, int>> dataSetEntry in numberOfModelsOfTargetVariableOfDataSet) {
|
---|
72 | DataSet dataSet = new DataSet(store, dataSetEntry.Key);
|
---|
73 | foreach (int targetVariable in dataSet.Problem.AllowedTargetVariables)
|
---|
74 | if (!dataSetEntry.Value.ContainsKey(targetVariable) || dataSetEntry.Value[targetVariable] < 10) {
|
---|
75 | IEngine engine = CreateEngine(dataSet.Problem, targetVariable);
|
---|
76 | if (engine != null) {
|
---|
77 | QueueJob(new Execution(dataSetEntry.Key, engine, targetVariable));
|
---|
78 | }
|
---|
79 | }
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | private void QueueJob(Execution execution) {
|
---|
84 | dispatchQueue.Add(execution);
|
---|
85 | }
|
---|
86 |
|
---|
87 | public Execution GetNextJob() {
|
---|
88 | if (dispatchQueue.Count == 0) FillDispatchQueue();
|
---|
89 | Execution next = dispatchQueue[0];
|
---|
90 | dispatchQueue.RemoveAt(0);
|
---|
91 | return next;
|
---|
92 | }
|
---|
93 |
|
---|
94 | internal void Start() {
|
---|
95 | FillDispatchQueue();
|
---|
96 | }
|
---|
97 |
|
---|
98 | private IEngine CreateEngine(Problem problem, int targetVariable) {
|
---|
99 | switch (problem.LearningTask) {
|
---|
100 | case LearningTask.Classification: return null;
|
---|
101 | case LearningTask.Regression: {
|
---|
102 | return CreateStandardGp(problem, targetVariable).Engine;
|
---|
103 | }
|
---|
104 | case LearningTask.TimeSeries: return null;
|
---|
105 | case LearningTask.Clustering: return null;
|
---|
106 | default: return null;
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | private StandardGP CreateStandardGp(Problem problem, int targetVariable) {
|
---|
111 | ProblemInjector probInjector = new ProblemInjector(problem);
|
---|
112 | probInjector.TargetVariable = targetVariable;
|
---|
113 | StandardGP sgp = new StandardGP();
|
---|
114 | sgp.SetSeedRandomly = true;
|
---|
115 | sgp.MaxGenerations = 100;
|
---|
116 | sgp.PopulationSize = 10000;
|
---|
117 | sgp.Elites = 1;
|
---|
118 | sgp.ProblemInjector = probInjector;
|
---|
119 | return sgp;
|
---|
120 | }
|
---|
121 | }
|
---|
122 | }
|
---|