#region License Information /* HeuristicLab * Copyright (C) 2002-2012 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.Threading; using HeuristicLab.Optimization; using HeuristicLab.Problems.Instances; namespace HeuristicLab.Encodings.ParameterConfigurationEncoding { public class ExperimentFactory { private double experimentGenerationProgress; public double ExperimentGenerationProgress { get { return experimentGenerationProgress; } private set { if (experimentGenerationProgress != value) { experimentGenerationProgress = value; OnExperimentGenerationProgressChanged(); } } } public event EventHandler ExperimentGenerationProgressChanged; private void OnExperimentGenerationProgressChanged() { var handler = ExperimentGenerationProgressChanged; if (handler != null) handler(this, EventArgs.Empty); } public Experiment GenerateExperiment(IAlgorithm algorithm, ParameterConfigurationTree configuration, bool createBatchRuns, int repetitions, Dictionary> problemInstances, CancellationToken ct) { var experiment = new Experiment(); var algorithms = new List(1 + problemInstances.Values.Count) { (IAlgorithm)algorithm.Clone() }; foreach (var provider in problemInstances) { foreach (var descriptor in provider.Value) { var alg = (IAlgorithm)algorithm.Clone(); ProblemInstanceManager.LoadData(provider.Key, descriptor, (IProblemInstanceConsumer)alg.Problem); algorithms.Add(alg); } } ExperimentGenerationProgress = 0; foreach (var alg in algorithms) { foreach (ParameterizedValueConfiguration combination in configuration) { ct.ThrowIfCancellationRequested(); var clonedAlg = (IAlgorithm)alg.Clone(); clonedAlg.Name = combination.ParameterInfoString; combination.Parameterize(clonedAlg); clonedAlg.StoreAlgorithmInEachRun = false; if (createBatchRuns) { var batchRun = new BatchRun(string.Format("BatchRun: {0}", combination.ParameterInfoString)); batchRun.Optimizer = clonedAlg; batchRun.Repetitions = repetitions; experiment.Optimizers.Add(batchRun); } else { experiment.Optimizers.Add(clonedAlg); } ExperimentGenerationProgress = (double)experiment.Optimizers.Count / (configuration.GetCombinationCount(0) * algorithms.Count); } } return experiment; } public Experiment GenerateExperiment(IAlgorithm algorithm, ParameterConfigurationTree configuration) { return GenerateExperiment(algorithm, configuration, false, 0, null, CancellationToken.None); } public Experiment GenerateExperiment(IAlgorithm algorithm, ParameterConfigurationTree configuration, bool createBatchRuns, int repetitions) { return GenerateExperiment(algorithm, configuration, createBatchRuns, repetitions, null, CancellationToken.None); } } }