#region License Information
/* HeuristicLab
* Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using HeuristicLab.PluginInfrastructure;
using System.Net;
using System.ServiceModel;
using HeuristicLab.CEDMA.DB.Interfaces;
using HeuristicLab.CEDMA.DB;
using System.ServiceModel.Description;
using System.Linq;
using HeuristicLab.CEDMA.Core;
using HeuristicLab.GP.StructureIdentification;
using HeuristicLab.Data;
using HeuristicLab.Core;
namespace HeuristicLab.CEDMA.Server {
public class Dispatcher {
private List dispatchQueue;
public IList DispatchQueue {
get { return dispatchQueue.Select(t => "StandardGP").ToList(); }
}
private IStore store;
public Dispatcher(IStore store) {
this.store = store;
this.dispatchQueue = new List();
}
private void FillDispatchQueue() {
// find and select a dataset
var dataSetVar = new HeuristicLab.CEDMA.DB.Interfaces.Variable("DataSet");
var dataSetQuery = new Statement[] {
new Statement(dataSetVar, Ontology.PredicateInstanceOf, Ontology.TypeDataSet)
};
var dataSetBindings = store.Query("?DataSet <"+Ontology.PredicateInstanceOf.Uri+"> <"+Ontology.TypeDataSet.Uri+"> .");
// no datasets => do nothing
if (dataSetBindings.Count() == 0) return;
// assume last dataset is the most interesting one
// find and select all results for this dataset
var dataSetEntity = (Entity)dataSetBindings.Last().Get("DataSet");
var targetVar = new HeuristicLab.CEDMA.DB.Interfaces.Variable("TargetVariable");
var modelVar = new HeuristicLab.CEDMA.DB.Interfaces.Variable("Model");
var modelMAPE = new HeuristicLab.CEDMA.DB.Interfaces.Variable("ModelMAPE");
var query = "<" + dataSetEntity.Uri + "> <" + Ontology.PredicateHasModel.Uri + "> ?Model ." + Environment.NewLine +
"?Model <" + Ontology.TargetVariable.Uri + "> ?TargetVariable ." + Environment.NewLine +
"?Model <" + Ontology.ValidationMeanAbsolutePercentageError.Uri + "> ?ModelMAPE .";
var bindings = store.Query(query);
DataSet dataSet = new DataSet(store, dataSetEntity);
double[] utilization = new double[dataSet.Problem.AllowedTargetVariables.Count];
int i = 0;
int totalN = bindings.Count();
foreach (int targetVariable in dataSet.Problem.AllowedTargetVariables) {
var targetVarBindings = bindings.Where(x => (int)((Literal)x.Get("TargetVariable")).Value == targetVariable);
if (targetVarBindings.Count() == 0) {
utilization[i++] = double.PositiveInfinity;
} else {
double averageMape = targetVarBindings.Average(x => (double)((Literal)x.Get("ModelMAPE")).Value);
double n = targetVarBindings.Count();
utilization[i++] = -averageMape + Math.Sqrt(Math.Log(totalN) / n) * 0.1;
}
}
int[] idx = Enumerable.Range(0, utilization.Length).ToArray();
Array.Sort(utilization, idx);
int nConfigurations = utilization.Length;
for (int j = nConfigurations - 1; j > nConfigurations * 0.8; j--) {
int targetVariable = dataSet.Problem.AllowedTargetVariables[idx[j]];
IEngine engine = CreateEngine(dataSet.Problem, targetVariable);
if (engine != null) {
QueueJob(new Execution(dataSetEntity, engine, targetVariable));
}
}
}
private void QueueJob(Execution execution) {
dispatchQueue.Add(execution);
}
public Execution GetNextJob() {
if (dispatchQueue.Count == 0) {
FillDispatchQueue();
}
if (dispatchQueue.Count > 0) {
Execution next = dispatchQueue[0];
dispatchQueue.RemoveAt(0);
return next;
} else
return null;
}
internal void Start() {
FillDispatchQueue();
}
private IEngine CreateEngine(Problem problem, int targetVariable) {
switch (problem.LearningTask) {
case LearningTask.Classification: return null;
case LearningTask.Regression: {
return CreateStandardGp(problem, targetVariable).Engine;
}
case LearningTask.TimeSeries: return null;
case LearningTask.Clustering: return null;
default: return null;
}
}
private StandardGP CreateStandardGp(Problem problem, int targetVariable) {
ProblemInjector probInjector = new ProblemInjector(problem);
probInjector.TargetVariable = targetVariable;
StandardGP sgp = new StandardGP();
sgp.SetSeedRandomly = true;
sgp.MaxGenerations = 300;
sgp.PopulationSize = 10000;
sgp.Elites = 1;
sgp.ProblemInjector = probInjector;
return sgp;
}
}
}