Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1053 was 1053, checked in by gkronber, 15 years ago

added problem injector for CEDMA problems and to create standard GP runs in the CEDMA dispatcher. #419 (Refactor CEDMA plugins)

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