1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 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.Linq;
|
---|
24 | using HeuristicLab.Algorithms.GeneticAlgorithm;
|
---|
25 | using HeuristicLab.Collections;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 | using HeuristicLab.PluginInfrastructure;
|
---|
33 | using HeuristicLab.Problems.DataAnalysis;
|
---|
34 | using HeuristicLab.Problems.TestFunctions;
|
---|
35 |
|
---|
36 | namespace HeuristicLab.Problems.MetaOptimization {
|
---|
37 | [Item("Meta Optimization Problem", "Represents a Meta Optimization Problem.")]
|
---|
38 | [Creatable("Problems")]
|
---|
39 | [StorableClass]
|
---|
40 | public sealed class MetaOptimizationProblem : SingleObjectiveHeuristicOptimizationProblem<IParameterConfigurationEvaluator, IParameterConfigurationCreator>, IStorableContent {
|
---|
41 | public string Filename { get; set; }
|
---|
42 |
|
---|
43 | public const string AlgorithmTypeParameterName = "AlgorithmType";
|
---|
44 | public const string ProblemTypeParameterName = "ProblemType";
|
---|
45 | public const string ProblemsParameterName = "Problems";
|
---|
46 | public const string ParameterConfigurationTreeParameterName = "ParameterConfiguration";
|
---|
47 | public const string RepetitionsParameterName = "Repetitions";
|
---|
48 |
|
---|
49 | public const string IntValueManipulatorParameterName = "IntValueManipulator";
|
---|
50 | public const string DoubleValueManipulatorParameterName = "DoubleValueManipulator";
|
---|
51 | public const string IntValueCrossoverParameterName = "IntValueCrossover";
|
---|
52 | public const string DoubleValueCrossoverParameterName = "DoubleValueCrossover";
|
---|
53 |
|
---|
54 | #region Parameter Properties
|
---|
55 | public IValueParameter<ConstrainedTypeValue<IAlgorithm>> AlgorithmTypeParameter {
|
---|
56 | get { return (ValueParameter<ConstrainedTypeValue<IAlgorithm>>)Parameters[AlgorithmTypeParameterName]; }
|
---|
57 | }
|
---|
58 | public IValueParameter<ConstrainedTypeValue<IProblem>> ProblemTypeParameter {
|
---|
59 | get { return (ValueParameter<ConstrainedTypeValue<IProblem>>)Parameters[ProblemTypeParameterName]; }
|
---|
60 | }
|
---|
61 | public IValueParameter<ConstrainedItemList<IProblem>> ProblemsParameter {
|
---|
62 | get { return (ValueParameter<ConstrainedItemList<IProblem>>)Parameters[ProblemsParameterName]; }
|
---|
63 | }
|
---|
64 | public IValueParameter<ParameterConfigurationTree> ParameterConfigurationTreeParameter {
|
---|
65 | get { return (ValueParameter<ParameterConfigurationTree>)Parameters[ParameterConfigurationTreeParameterName]; }
|
---|
66 | }
|
---|
67 | public IValueParameter<IntValue> RepetitionsParameter {
|
---|
68 | get { return (ValueParameter<IntValue>)Parameters[RepetitionsParameterName]; }
|
---|
69 | }
|
---|
70 |
|
---|
71 | public IValueParameter<IIntValueManipulator> IntValueManipulatorParameter {
|
---|
72 | get { return (ValueParameter<IIntValueManipulator>)Parameters[IntValueManipulatorParameterName]; }
|
---|
73 | }
|
---|
74 |
|
---|
75 | public IValueParameter<IDoubleValueManipulator> DoubleValueManipulatorParameter {
|
---|
76 | get { return (ValueParameter<IDoubleValueManipulator>)Parameters[DoubleValueManipulatorParameterName]; }
|
---|
77 | }
|
---|
78 | #endregion
|
---|
79 |
|
---|
80 | #region Properties
|
---|
81 | public IAlgorithm Algorithm {
|
---|
82 | get { return CreateAlgorithm(AlgorithmType.Value, Problems.FirstOrDefault()); }
|
---|
83 | }
|
---|
84 | public ConstrainedTypeValue<IAlgorithm> AlgorithmType {
|
---|
85 | get { return AlgorithmTypeParameter.Value; }
|
---|
86 | set { AlgorithmTypeParameter.Value = value; }
|
---|
87 | }
|
---|
88 | public ConstrainedTypeValue<IProblem> ProblemType {
|
---|
89 | get { return ProblemTypeParameter.Value; }
|
---|
90 | set { ProblemTypeParameter.Value = value; }
|
---|
91 | }
|
---|
92 | public ConstrainedItemList<IProblem> Problems {
|
---|
93 | get { return ProblemsParameter.Value; }
|
---|
94 | set { ProblemsParameter.Value = value; }
|
---|
95 | }
|
---|
96 | public ParameterConfigurationTree ParameterConfigurationTree {
|
---|
97 | get { return ParameterConfigurationTreeParameter.Value; }
|
---|
98 | set { ParameterConfigurationTreeParameter.Value = value; }
|
---|
99 | }
|
---|
100 | public IntValue Repetitions {
|
---|
101 | get { return RepetitionsParameter.Value; }
|
---|
102 | set { RepetitionsParameter.Value = value; }
|
---|
103 | }
|
---|
104 | private BestParameterConfigurationAnalyzer BestParameterConfigurationAnalyzer {
|
---|
105 | get { return Operators.OfType<BestParameterConfigurationAnalyzer>().FirstOrDefault(); }
|
---|
106 | }
|
---|
107 | private ReferenceQualityAnalyzer ReferenceQualityAnalyzer {
|
---|
108 | get { return Operators.OfType<ReferenceQualityAnalyzer>().FirstOrDefault(); }
|
---|
109 | }
|
---|
110 | private SolutionCacheAnalyzer RunsAnalyzer {
|
---|
111 | get { return Operators.OfType<SolutionCacheAnalyzer>().FirstOrDefault(); }
|
---|
112 | }
|
---|
113 | private PMOPopulationDiversityAnalyzer PMOPopulationDiversityAnalyzer {
|
---|
114 | get { return Operators.OfType<PMOPopulationDiversityAnalyzer>().FirstOrDefault(); }
|
---|
115 | }
|
---|
116 | private PMOProblemQualitiesAnalyzer PMOProblemQualitiesAnalyzer {
|
---|
117 | get { return Operators.OfType<PMOProblemQualitiesAnalyzer>().FirstOrDefault(); }
|
---|
118 | }
|
---|
119 | private PMOBestSolutionHistoryAnalyzer PMOBestSolutionHistoryAnalyzer {
|
---|
120 | get { return Operators.OfType<PMOBestSolutionHistoryAnalyzer>().FirstOrDefault(); }
|
---|
121 | }
|
---|
122 | #endregion
|
---|
123 |
|
---|
124 | public MetaOptimizationProblem()
|
---|
125 | : base() {
|
---|
126 | Parameters.Add(new ValueParameter<ConstrainedTypeValue<IAlgorithm>>(AlgorithmTypeParameterName, "The algorithm which's parameters should be optimized.", new ConstrainedTypeValue<IAlgorithm>(typeof(GeneticAlgorithm))));
|
---|
127 | Parameters.Add(new ValueParameter<ConstrainedTypeValue<IProblem>>(ProblemTypeParameterName, "The problem type.", new ConstrainedTypeValue<IProblem>()));
|
---|
128 | Parameters.Add(new ValueParameter<ConstrainedItemList<IProblem>>(ProblemsParameterName, "The problems that should be evaluated.", new ConstrainedItemList<IProblem>()));
|
---|
129 | Parameters.Add(new ValueParameter<ParameterConfigurationTree>(ParameterConfigurationTreeParameterName, "Tree of algorithm parameters that should be optimized."));
|
---|
130 | Parameters.Add(new ValueParameter<IntValue>(RepetitionsParameterName, "The number of evaluations for each problem.", new IntValue(3)));
|
---|
131 |
|
---|
132 | var validIntManipulators = new ItemSet<IIntValueManipulator>(ApplicationManager.Manager.GetInstances<IIntValueManipulator>());
|
---|
133 | var validDoubleManipulators = new ItemSet<IDoubleValueManipulator>(ApplicationManager.Manager.GetInstances<IDoubleValueManipulator>());
|
---|
134 | var validIntCrossovers = new ItemSet<IIntValueCrossover>(ApplicationManager.Manager.GetInstances<IIntValueCrossover>());
|
---|
135 | var validDoubleCrossovers = new ItemSet<IDoubleValueCrossover>(ApplicationManager.Manager.GetInstances<IDoubleValueCrossover>());
|
---|
136 | Parameters.Add(new ConstrainedValueParameter<IIntValueManipulator>(IntValueManipulatorParameterName, "", validIntManipulators, validIntManipulators.Where(x => x.GetType() == typeof(NormalIntValueManipulator)).SingleOrDefault()));
|
---|
137 | Parameters.Add(new ConstrainedValueParameter<IDoubleValueManipulator>(DoubleValueManipulatorParameterName, "", validDoubleManipulators, validDoubleManipulators.Where(x => x.GetType() == typeof(NormalDoubleValueManipulator)).SingleOrDefault()));
|
---|
138 | Parameters.Add(new ConstrainedValueParameter<IIntValueCrossover>(IntValueCrossoverParameterName, "", validIntCrossovers, validIntCrossovers.Where(x => x.GetType() == typeof(NormalIntValueCrossover)).SingleOrDefault()));
|
---|
139 | Parameters.Add(new ConstrainedValueParameter<IDoubleValueCrossover>(DoubleValueCrossoverParameterName, "", validDoubleCrossovers, validDoubleCrossovers.Where(x => x.GetType() == typeof(NormalDoubleValueCrossover)).SingleOrDefault()));
|
---|
140 |
|
---|
141 | Maximization = new BoolValue(false);
|
---|
142 | SolutionCreator = new RandomParameterConfigurationCreator();
|
---|
143 | Evaluator = new PMOEvaluator();
|
---|
144 |
|
---|
145 | InitializeOperators();
|
---|
146 | RegisterParameterEvents();
|
---|
147 | ParameterizeAnalyzer();
|
---|
148 | ParameterizeSolutionCreator();
|
---|
149 | ParameterizeEvaluator();
|
---|
150 | ParameterizeOperators();
|
---|
151 |
|
---|
152 | AlgorithmTypeParameter_ValueChanged(this, EventArgs.Empty);
|
---|
153 | }
|
---|
154 |
|
---|
155 | [StorableConstructor]
|
---|
156 | private MetaOptimizationProblem(bool deserializing) : base(deserializing) { }
|
---|
157 | private MetaOptimizationProblem(MetaOptimizationProblem original, Cloner cloner)
|
---|
158 | : base(original, cloner) {
|
---|
159 | this.RegisterParameterEvents();
|
---|
160 | }
|
---|
161 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
162 | return new MetaOptimizationProblem(this, cloner);
|
---|
163 | }
|
---|
164 |
|
---|
165 | #region Helpers
|
---|
166 | [StorableHook(HookType.AfterDeserialization)]
|
---|
167 | private void AfterDeserializationHook() {
|
---|
168 | RegisterParameterEvents();
|
---|
169 | }
|
---|
170 | private void RegisterParameterEvents() {
|
---|
171 | SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
|
---|
172 | EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
|
---|
173 | Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
174 | AlgorithmTypeParameter.ValueChanged += new EventHandler(AlgorithmTypeParameter_ValueChanged);
|
---|
175 | AlgorithmType.ValueChanged += new EventHandler(AlgorithmType_ValueChanged);
|
---|
176 | ProblemTypeParameter.ValueChanged += new EventHandler(ProblemTypeParameter_ValueChanged);
|
---|
177 | ProblemType.ValueChanged += new EventHandler(ProblemType_ValueChanged);
|
---|
178 | Problems.ItemsAdded += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<IProblem>>(Problems_ItemsAdded);
|
---|
179 | Problems.ItemsRemoved += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<IProblem>>(Problems_ItemsRemoved);
|
---|
180 | }
|
---|
181 |
|
---|
182 | private void InitializeOperators() {
|
---|
183 | Operators.AddRange(ApplicationManager.Manager.GetInstances<IParameterConfigurationOperator>().Cast<IOperator>());
|
---|
184 | Operators.Add(new BestParameterConfigurationAnalyzer());
|
---|
185 | Operators.Add(new ReferenceQualityAnalyzer());
|
---|
186 | Operators.Add(new SolutionCacheAnalyzer());
|
---|
187 | Operators.Add(new PMOPopulationDiversityAnalyzer());
|
---|
188 | Operators.Add(new PMOProblemQualitiesAnalyzer());
|
---|
189 | Operators.Add(new PMOBestSolutionHistoryAnalyzer());
|
---|
190 | }
|
---|
191 | private void ParameterizeSolutionCreator() {
|
---|
192 | }
|
---|
193 | private void ParameterizeEvaluator() {
|
---|
194 | ((PMOEvaluator)Evaluator).ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
|
---|
195 | }
|
---|
196 | private void ParameterizeAnalyzer() {
|
---|
197 | if (BestParameterConfigurationAnalyzer != null) {
|
---|
198 | BestParameterConfigurationAnalyzer.ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
|
---|
199 | }
|
---|
200 | if (ReferenceQualityAnalyzer != null) {
|
---|
201 | ReferenceQualityAnalyzer.ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
|
---|
202 | }
|
---|
203 | if (RunsAnalyzer != null) {
|
---|
204 | RunsAnalyzer.ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
|
---|
205 | }
|
---|
206 | if (PMOPopulationDiversityAnalyzer != null) {
|
---|
207 | PMOPopulationDiversityAnalyzer.SolutionParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
|
---|
208 | PMOPopulationDiversityAnalyzer.StoreHistoryParameter.Value.Value = true;
|
---|
209 | }
|
---|
210 | if (PMOProblemQualitiesAnalyzer != null) {
|
---|
211 | PMOProblemQualitiesAnalyzer.ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
|
---|
212 | }
|
---|
213 | if (PMOBestSolutionHistoryAnalyzer != null) {
|
---|
214 | PMOBestSolutionHistoryAnalyzer.ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
|
---|
215 | }
|
---|
216 | }
|
---|
217 | private void ParameterizeOperators() {
|
---|
218 | foreach (IParameterConfigurationCrossover op in Operators.OfType<IParameterConfigurationCrossover>()) {
|
---|
219 | op.ParentsParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
|
---|
220 | op.ChildParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
|
---|
221 | }
|
---|
222 | foreach (IParameterConfigurationManipulator op in Operators.OfType<IParameterConfigurationManipulator>()) {
|
---|
223 | op.ParameterConfigurationTreeParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 | #endregion
|
---|
228 |
|
---|
229 | #region Events
|
---|
230 |
|
---|
231 | private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
232 | ParameterizeSolutionCreator();
|
---|
233 | ParameterizeEvaluator();
|
---|
234 | ParameterizeAnalyzer();
|
---|
235 | ParameterizeOperators();
|
---|
236 | OnSolutionCreatorChanged();
|
---|
237 | }
|
---|
238 | private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
239 | Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
240 | ParameterizeEvaluator();
|
---|
241 | ParameterizeAnalyzer();
|
---|
242 | OnEvaluatorChanged();
|
---|
243 | }
|
---|
244 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
245 | ParameterizeAnalyzer();
|
---|
246 | }
|
---|
247 | private void AlgorithmTypeParameter_ValueChanged(object sender, EventArgs e) {
|
---|
248 | AlgorithmType_ValueChanged(sender, e);
|
---|
249 | }
|
---|
250 |
|
---|
251 | private void AlgorithmType_ValueChanged(object sender, EventArgs e) {
|
---|
252 | IAlgorithm instance = (IAlgorithm)Activator.CreateInstance(AlgorithmType.Value);
|
---|
253 | this.ProblemType.ValidTypes = ApplicationManager.Manager.GetTypes(instance.ProblemType, true).ToList();
|
---|
254 | this.ProblemType.Value = this.ProblemType.ValidTypes.SingleOrDefault(t => t == typeof(SingleObjectiveTestFunctionProblem)) ?? this.ProblemType.ValidTypes.Where(t => t != typeof(MetaOptimizationProblem)).FirstOrDefault();
|
---|
255 | // ProblemType_ValueChanged will add one problem and create ParameterConfigurationTree
|
---|
256 | }
|
---|
257 |
|
---|
258 | private void ProblemTypeParameter_ValueChanged(object sender, EventArgs e) {
|
---|
259 | ProblemType_ValueChanged(sender, e);
|
---|
260 | }
|
---|
261 |
|
---|
262 | private void ProblemType_ValueChanged(object sender, EventArgs e) {
|
---|
263 | Problems.Clear();
|
---|
264 | Problems.Type = ProblemType.Value;
|
---|
265 | Problems.Add((IProblem)Activator.CreateInstance(this.ProblemType.Value));
|
---|
266 | }
|
---|
267 |
|
---|
268 | private void Problems_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<IndexedItem<IProblem>> e) {
|
---|
269 | // the first problem in the list is always the instance for the algorithm - this way some basic wiring between problem and algorithm can be sustained
|
---|
270 | if (e.Items.Single().Index == 0) {
|
---|
271 | ParameterConfigurationTreeParameter.ActualValue = new ParameterConfigurationTree(CreateAlgorithm(AlgorithmType.Value, e.Items.Single().Value), e.Items.Single().Value);
|
---|
272 |
|
---|
273 | // special for DataAnalysisProblem: Because of wiring between algorithm and problem, ParameterConfigurationTree needs to be recreated on Reset event
|
---|
274 | var dap = e.Items.Single().Value as IDataAnalysisProblem;
|
---|
275 | if (dap != null) {
|
---|
276 | dap.Reset += new EventHandler(DataAnalysisProblem_Reset);
|
---|
277 | }
|
---|
278 | }
|
---|
279 | }
|
---|
280 |
|
---|
281 | private void Problems_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<IndexedItem<IProblem>> e) {
|
---|
282 | if (e.Items.Single().Index == 0) {
|
---|
283 | ParameterConfigurationTreeParameter.ActualValue = null;
|
---|
284 |
|
---|
285 | var dap = e.Items.Single().Value as IDataAnalysisProblem;
|
---|
286 | if (dap != null) {
|
---|
287 | dap.Reset -= new EventHandler(DataAnalysisProblem_Reset);
|
---|
288 | }
|
---|
289 | }
|
---|
290 | }
|
---|
291 |
|
---|
292 | private void DataAnalysisProblem_Reset(object sender, EventArgs e) {
|
---|
293 | ParameterConfigurationTreeParameter.ActualValue = new ParameterConfigurationTree(CreateAlgorithm(AlgorithmType.Value, Problems.First()), Problems.First());
|
---|
294 | }
|
---|
295 | #endregion
|
---|
296 |
|
---|
297 | private IAlgorithm CreateAlgorithm(Type algorithmType, IProblem problem) {
|
---|
298 | IAlgorithm algorithm = (IAlgorithm)Activator.CreateInstance(algorithmType);
|
---|
299 | algorithm.Problem = problem;
|
---|
300 | return algorithm;
|
---|
301 | }
|
---|
302 |
|
---|
303 | public void ImportAlgorithm(IAlgorithm algorithm) {
|
---|
304 | AlgorithmType.Value = algorithm.GetType();
|
---|
305 | if (algorithm.Problem != null) ProblemType.Value = algorithm.Problem.GetType();
|
---|
306 | if (algorithm.Problem != null) {
|
---|
307 | Problems.Clear();
|
---|
308 | Problems.Add((IProblem)algorithm.Problem);
|
---|
309 | }
|
---|
310 | ParameterConfigurationTreeParameter.ActualValue = new ParameterConfigurationTree(algorithm, Problems.First());
|
---|
311 | }
|
---|
312 | }
|
---|
313 | }
|
---|