1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Threading;
|
---|
25 | using HeuristicLab.Optimization;
|
---|
26 | using HeuristicLab.Problems.Instances;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Encodings.ParameterConfigurationEncoding {
|
---|
29 | public class ExperimentFactory {
|
---|
30 | private double experimentGenerationProgress;
|
---|
31 | public double ExperimentGenerationProgress {
|
---|
32 | get { return experimentGenerationProgress; }
|
---|
33 | private set {
|
---|
34 | if (experimentGenerationProgress != value) {
|
---|
35 | experimentGenerationProgress = value;
|
---|
36 | OnExperimentGenerationProgressChanged();
|
---|
37 | }
|
---|
38 | }
|
---|
39 | }
|
---|
40 |
|
---|
41 | public event EventHandler ExperimentGenerationProgressChanged;
|
---|
42 | private void OnExperimentGenerationProgressChanged() {
|
---|
43 | var handler = ExperimentGenerationProgressChanged;
|
---|
44 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
45 | }
|
---|
46 |
|
---|
47 | public Experiment GenerateExperiment(IAlgorithm algorithm, ParameterConfigurationTree configuration, bool createBatchRuns, int repetitions, Dictionary<IProblemInstanceProvider, HashSet<IDataDescriptor>> problemInstances, CancellationToken ct) {
|
---|
48 | var experiment = new Experiment();
|
---|
49 | var algorithms = new List<IAlgorithm>(1 + problemInstances.Values.Count) { (IAlgorithm)algorithm.Clone() };
|
---|
50 | foreach (var provider in problemInstances) {
|
---|
51 | foreach (var descriptor in provider.Value) {
|
---|
52 | var alg = (IAlgorithm)algorithm.Clone();
|
---|
53 | ProblemInstanceManager.LoadData(provider.Key, descriptor, (IProblemInstanceConsumer)alg.Problem);
|
---|
54 | algorithms.Add(alg);
|
---|
55 | }
|
---|
56 | }
|
---|
57 | ExperimentGenerationProgress = 0;
|
---|
58 | foreach (var alg in algorithms) {
|
---|
59 | foreach (ParameterizedValueConfiguration combination in configuration) {
|
---|
60 | ct.ThrowIfCancellationRequested();
|
---|
61 | var clonedAlg = (IAlgorithm)alg.Clone();
|
---|
62 | clonedAlg.Name = combination.ParameterInfoString;
|
---|
63 | combination.Parameterize(clonedAlg);
|
---|
64 | clonedAlg.StoreAlgorithmInEachRun = false;
|
---|
65 | if (createBatchRuns) {
|
---|
66 | var batchRun = new BatchRun(string.Format("BatchRun: {0}", combination.ParameterInfoString));
|
---|
67 | batchRun.Optimizer = clonedAlg;
|
---|
68 | batchRun.Repetitions = repetitions;
|
---|
69 | experiment.Optimizers.Add(batchRun);
|
---|
70 | } else {
|
---|
71 | experiment.Optimizers.Add(clonedAlg);
|
---|
72 | }
|
---|
73 | ExperimentGenerationProgress = (double)experiment.Optimizers.Count / (configuration.GetCombinationCount(0) * algorithms.Count);
|
---|
74 | }
|
---|
75 | }
|
---|
76 | return experiment;
|
---|
77 | }
|
---|
78 |
|
---|
79 | public Experiment GenerateExperiment(IAlgorithm algorithm, ParameterConfigurationTree configuration) {
|
---|
80 | return GenerateExperiment(algorithm, configuration, false, 0, null, CancellationToken.None);
|
---|
81 | }
|
---|
82 |
|
---|
83 | public Experiment GenerateExperiment(IAlgorithm algorithm, ParameterConfigurationTree configuration, bool createBatchRuns, int repetitions) {
|
---|
84 | return GenerateExperiment(algorithm, configuration, createBatchRuns, repetitions, null, CancellationToken.None);
|
---|
85 | }
|
---|
86 | }
|
---|
87 | }
|
---|