#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 abstract class DispatcherBase :IDispatcher { public enum ModelComplexity { Low, Medium, High }; public enum Algorithm { StandardGP }; private IStore store; private ModelComplexity[] possibleComplexities = new ModelComplexity[] { ModelComplexity.Low, ModelComplexity.Medium, ModelComplexity.High }; private Dictionary possibleAlgorithms = new Dictionary() { {LearningTask.Classification, new Algorithm[] {}}, {LearningTask.Regression, new Algorithm[] { Algorithm.StandardGP }}, {LearningTask.TimeSeries, new Algorithm[] { }} }; public DispatcherBase(IStore store) { this.store = store; } public Execution GetNextJob() { // 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) }; Entity[] datasets = store.Query("?DataSet <" + Ontology.PredicateInstanceOf.Uri + "> <" + Ontology.TypeDataSet.Uri + "> .") .Select(x => (Entity)x.Get("DataSet")) .ToArray(); // no datasets => do nothing if (datasets.Length == 0) return null; Entity dataSetEntity = SelectDataSet(datasets); DataSet dataSet = new DataSet(store, dataSetEntity); int targetVariable = SelectTargetVariable(dataSet, dataSet.Problem.AllowedInputVariables.ToArray()); Algorithm selectedAlgorithm = SelectAlgorithm(dataSet, targetVariable, possibleAlgorithms[dataSet.Problem.LearningTask]); string targetVariableName = dataSet.Problem.GetVariableName(targetVariable); ModelComplexity selectedComplexity = SelectComplexity(dataSet, targetVariable, selectedAlgorithm, possibleComplexities); Execution exec = CreateExecution(dataSet.Problem, targetVariable, selectedAlgorithm, selectedComplexity); if (exec != null) { exec.DataSetEntity = dataSetEntity; exec.TargetVariable = targetVariableName; } return exec; } public abstract Entity SelectDataSet(Entity[] datasets); public abstract int SelectTargetVariable(DataSet dataSet, int[] targetVariables); public abstract Algorithm SelectAlgorithm(DataSet dataSet, int targetVariable, Algorithm[] possibleAlgorithms); public abstract ModelComplexity SelectComplexity(DataSet dataSet, int targetVariable, Algorithm algorithm, ModelComplexity[] possibleComplexities); private Execution CreateExecution(Problem problem, int targetVariable, Algorithm algorithm, ModelComplexity complexity) { switch (algorithm) { case Algorithm.StandardGP: { return CreateStandardGpExecution(problem, targetVariable, complexity); } default: { return null; } } } private Execution CreateStandardGpExecution(Problem problem, int targetVariable, ModelComplexity complexity) { ProblemInjector probInjector = new ProblemInjector(problem); probInjector.TargetVariable = targetVariable; StandardGP sgp = new StandardGP(); sgp.SetSeedRandomly = true; sgp.MaxGenerations = 2; sgp.PopulationSize = 100; sgp.Elites = 1; sgp.ProblemInjector = probInjector; int maxTreeHeight = 10; int maxTreeSize = 100; switch (complexity) { case ModelComplexity.Low: { maxTreeHeight = 5; maxTreeSize = 20; break; } case ModelComplexity.Medium: { maxTreeHeight = 10; maxTreeSize = 100; break; } case ModelComplexity.High: { maxTreeHeight = 12; maxTreeSize = 200; break; } } sgp.MaxTreeHeight = maxTreeHeight; sgp.MaxTreeSize = maxTreeSize; Execution exec = new Execution(sgp.Engine); exec.Description = "StandardGP - Complexity: " + complexity; return exec; } } }