Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1130 was 1130, checked in by gkronber, 15 years ago
  • re-enabled security for all WCF services;
  • debugged rdf queries
  • added tracing for WCF services
  • implemented simple version of quality based dispatching
  • extended ontology to include a number of predefined model-attributes (quality, size, evaluated solutions etc.)

ticket: #419 (Refactor CEDMA plugins)

File size: 5.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;
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 = new Statement[] {
72      //  new Statement(dataSetEntity, Ontology.PredicateHasModel, modelVar),
73      //  new Statement(modelVar, Ontology.TargetVariable, targetVar),
74      //  new Statement(modelVar, Ontology.ValidationMeanAbsolutePercentageError, modelMAPE)
75      //};
76
77      var query = "<" + dataSetEntity.Uri + "> <" + Ontology.PredicateHasModel.Uri + "> ?Model ." + Environment.NewLine +
78        "?Model <" + Ontology.TargetVariable.Uri + "> ?TargetVariable ." + Environment.NewLine +
79        "?Model <" + Ontology.ValidationMeanAbsolutePercentageError.Uri + "> ?ModelMAPE .";
80
81
82
83      var bindings = store.Query(query);
84      DataSet dataSet = new DataSet(store, dataSetEntity);
85      double[] utilization = new double[dataSet.Problem.AllowedTargetVariables.Count];
86      int i = 0;
87      int totalN = bindings.Count();
88      foreach (int targetVariable in dataSet.Problem.AllowedTargetVariables) {
89        var targetVarBindings = bindings.Where(x => (int)((Literal)x.Get("TargetVariable")).Value == targetVariable);
90        if (targetVarBindings.Count() == 0) {
91          utilization[i++] = double.PositiveInfinity;
92        } else {
93          double averageMape = targetVarBindings.Average(x => (double)((Literal)x.Get("ModelMAPE")).Value);
94          double n = targetVarBindings.Count();
95          utilization[i++] = -averageMape + Math.Sqrt(Math.Log(totalN) / n) * 0.1;
96        }
97      }
98      int[] idx = Enumerable.Range(0, utilization.Length).ToArray();
99      Array.Sort(utilization, idx);
100      int nConfigurations = utilization.Length;
101      for (int j = nConfigurations - 1; j > nConfigurations * 0.8; j--) {
102        int targetVariable = dataSet.Problem.AllowedTargetVariables[j];
103        IEngine engine = CreateEngine(dataSet.Problem, targetVariable);
104        if (engine != null) {
105          QueueJob(new Execution(dataSetEntity, engine, targetVariable));
106        }
107      }
108    }
109
110    private void QueueJob(Execution execution) {
111      dispatchQueue.Add(execution);
112    }
113
114    public Execution GetNextJob() {
115      if (dispatchQueue.Count == 0) {
116        FillDispatchQueue();
117      }
118      if (dispatchQueue.Count > 0) {
119        Execution next = dispatchQueue[0];
120        dispatchQueue.RemoveAt(0);
121        return next;
122      } else
123        return null;
124    }
125
126    internal void Start() {
127      FillDispatchQueue();
128    }
129
130    private IEngine CreateEngine(Problem problem, int targetVariable) {
131      switch (problem.LearningTask) {
132        case LearningTask.Classification: return null;
133        case LearningTask.Regression: {
134            return CreateStandardGp(problem, targetVariable).Engine;
135          }
136        case LearningTask.TimeSeries: return null;
137        case LearningTask.Clustering: return null;
138        default: return null;
139      }
140    }
141
142    private StandardGP CreateStandardGp(Problem problem, int targetVariable) {
143      ProblemInjector probInjector = new ProblemInjector(problem);
144      probInjector.TargetVariable = targetVariable;
145      StandardGP sgp = new StandardGP();
146      sgp.SetSeedRandomly = true;
147      sgp.MaxGenerations = 300;
148      sgp.PopulationSize = 10000;
149      sgp.Elites = 1;
150      sgp.ProblemInjector = probInjector;
151      return sgp;
152    }
153  }
154}
Note: See TracBrowser for help on using the repository browser.