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