Free cookie consent management tool by TermsFeed Policy Generator

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

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

worked on #419 (Refactor CEDMA plugins):

  • increased number of sessions for the grid service from 20 to 100
  • implemented results query
  • fixed a bug in the Dispatcher
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      // find and select a dataset
54      var dataSetVar = new HeuristicLab.CEDMA.DB.Interfaces.Variable("DataSet");
55      var dataSetQuery = new Statement[] {
56        new Statement(dataSetVar, Ontology.PredicateInstanceOf, Ontology.TypeDataSet)
57      };
58
59      var dataSetBindings = store.Query("?DataSet <"+Ontology.PredicateInstanceOf.Uri+"> <"+Ontology.TypeDataSet.Uri+"> .");
60
61      // no datasets => do nothing
62      if (dataSetBindings.Count() == 0) return;
63
64      // assume last dataset is the most interesting one
65      // find and select all results for this dataset
66      var dataSetEntity = (Entity)dataSetBindings.Last().Get("DataSet");
67      var targetVar = new HeuristicLab.CEDMA.DB.Interfaces.Variable("TargetVariable");
68      var modelVar = new HeuristicLab.CEDMA.DB.Interfaces.Variable("Model");
69      var modelMAPE = new HeuristicLab.CEDMA.DB.Interfaces.Variable("ModelMAPE");
70
71      var query = "<" + dataSetEntity.Uri + "> <" + Ontology.PredicateHasModel.Uri + "> ?Model ." + Environment.NewLine +
72        "?Model <" + Ontology.TargetVariable.Uri + "> ?TargetVariable ." + Environment.NewLine +
73        "?Model <" + Ontology.ValidationMeanAbsolutePercentageError.Uri + "> ?ModelMAPE .";
74
75
76
77      var bindings = store.Query(query);
78      DataSet dataSet = new DataSet(store, dataSetEntity);
79      double[] utilization = new double[dataSet.Problem.AllowedTargetVariables.Count];
80      int i = 0;
81      int totalN = bindings.Count();
82      foreach (int targetVariable in dataSet.Problem.AllowedTargetVariables) {
83        var targetVarBindings = bindings.Where(x => (int)((Literal)x.Get("TargetVariable")).Value == targetVariable);
84        if (targetVarBindings.Count() == 0) {
85          utilization[i++] = double.PositiveInfinity;
86        } else {
87          double averageMape = targetVarBindings.Average(x => (double)((Literal)x.Get("ModelMAPE")).Value);
88          double n = targetVarBindings.Count();
89          utilization[i++] = -averageMape + Math.Sqrt(Math.Log(totalN) / n) * 0.1;
90        }
91      }
92      int[] idx = Enumerable.Range(0, utilization.Length).ToArray();
93      Array.Sort(utilization, idx);
94      int nConfigurations = utilization.Length;
95      for (int j = nConfigurations - 1; j > nConfigurations * 0.8; j--) {
96        int targetVariable = dataSet.Problem.AllowedTargetVariables[idx[j]];
97        IEngine engine = CreateEngine(dataSet.Problem, targetVariable);
98        if (engine != null) {
99          QueueJob(new Execution(dataSetEntity, engine, targetVariable));
100        }
101      }
102    }
103
104    private void QueueJob(Execution execution) {
105      dispatchQueue.Add(execution);
106    }
107
108    public Execution GetNextJob() {
109      if (dispatchQueue.Count == 0) {
110        FillDispatchQueue();
111      }
112      if (dispatchQueue.Count > 0) {
113        Execution next = dispatchQueue[0];
114        dispatchQueue.RemoveAt(0);
115        return next;
116      } else
117        return null;
118    }
119
120    internal void Start() {
121      FillDispatchQueue();
122    }
123
124    private IEngine CreateEngine(Problem problem, int targetVariable) {
125      switch (problem.LearningTask) {
126        case LearningTask.Classification: return null;
127        case LearningTask.Regression: {
128            return CreateStandardGp(problem, targetVariable).Engine;
129          }
130        case LearningTask.TimeSeries: return null;
131        case LearningTask.Clustering: return null;
132        default: return null;
133      }
134    }
135
136    private StandardGP CreateStandardGp(Problem problem, int targetVariable) {
137      ProblemInjector probInjector = new ProblemInjector(problem);
138      probInjector.TargetVariable = targetVariable;
139      StandardGP sgp = new StandardGP();
140      sgp.SetSeedRandomly = true;
141      sgp.MaxGenerations = 300;
142      sgp.PopulationSize = 10000;
143      sgp.Elites = 1;
144      sgp.ProblemInjector = probInjector;
145      return sgp;
146    }
147  }
148}
Note: See TracBrowser for help on using the repository browser.