1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 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 HEAL.Attic;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Optimization {
|
---|
32 | [StorableType("D877082E-9E77-4CB1-ABDB-35F63878E116")]
|
---|
33 | public abstract class Problem<TEncoding, TEncodedSolution, TEvaluator> : EncodedProblem,
|
---|
34 | IHeuristicOptimizationProblem, IProblemDefinition<TEncoding, TEncodedSolution>, IStorableContent
|
---|
35 | where TEncoding : class, IEncoding
|
---|
36 | where TEncodedSolution : class, IEncodedSolution
|
---|
37 | where TEvaluator : class, IEvaluator {
|
---|
38 |
|
---|
39 | //TODO remove parameter for encoding?
|
---|
40 | protected IValueParameter<TEncoding> EncodingParameter {
|
---|
41 | get { return (IValueParameter<TEncoding>)Parameters["Encoding"]; }
|
---|
42 | }
|
---|
43 | //mkommend necessary for reuse of operators if the encoding changes
|
---|
44 | private TEncoding oldEncoding;
|
---|
45 | public TEncoding Encoding {
|
---|
46 | get { return EncodingParameter.Value; }
|
---|
47 | protected set {
|
---|
48 | if (value == null) throw new ArgumentNullException("Encoding must not be null.");
|
---|
49 | EncodingParameter.Value = value;
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | //TODO is a parameter for the evaluator really necessary, only single-objective or multi-objective evaluators calling the func are possible
|
---|
54 | public ValueParameter<TEvaluator> EvaluatorParameter {
|
---|
55 | get { return (ValueParameter<TEvaluator>)Parameters["Evaluator"]; }
|
---|
56 | }
|
---|
57 | public TEvaluator Evaluator {
|
---|
58 | get { return EvaluatorParameter.Value; }
|
---|
59 | protected set { EvaluatorParameter.Value = value; }
|
---|
60 | }
|
---|
61 | IEvaluator IHeuristicOptimizationProblem.Evaluator {
|
---|
62 | get { return Evaluator; }
|
---|
63 | }
|
---|
64 | IParameter IHeuristicOptimizationProblem.EvaluatorParameter {
|
---|
65 | get { return EvaluatorParameter; }
|
---|
66 | }
|
---|
67 |
|
---|
68 | public event EventHandler EvaluatorChanged;
|
---|
69 | protected virtual void OnEvaluatorChanged() {
|
---|
70 | EventHandler handler = EvaluatorChanged;
|
---|
71 | if (handler != null)
|
---|
72 | handler(this, EventArgs.Empty);
|
---|
73 | }
|
---|
74 |
|
---|
75 |
|
---|
76 | protected override IEnumerable<IItem> GetOperators() {
|
---|
77 | if (Encoding == null) return base.GetOperators();
|
---|
78 | return base.GetOperators().Concat(Encoding.Operators);
|
---|
79 | }
|
---|
80 | public override IEnumerable<IParameterizedItem> ExecutionContextItems {
|
---|
81 | get {
|
---|
82 | if (Encoding == null) return base.ExecutionContextItems;
|
---|
83 | return base.ExecutionContextItems.Concat(new[] { Encoding });
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | protected Problem(TEncoding encoding) : base() {
|
---|
88 | if (encoding == null) throw new ArgumentNullException("encoding");
|
---|
89 | Parameters.Add(new ValueParameter<TEncoding>("Encoding", "Describes the configuration of the encoding, what the variables are called, what type they are and their bounds if any.", encoding) { Hidden = true });
|
---|
90 | Parameters.Add(new ValueParameter<TEvaluator>("Evaluator", "The operator used to evaluate a solution.") { Hidden = true });
|
---|
91 |
|
---|
92 | oldEncoding = Encoding;
|
---|
93 | Parameterize();
|
---|
94 |
|
---|
95 | RegisterEvents();
|
---|
96 | }
|
---|
97 |
|
---|
98 | protected Problem(Problem<TEncoding, TEncodedSolution, TEvaluator> original, Cloner cloner)
|
---|
99 | : base(original, cloner) {
|
---|
100 | oldEncoding = cloner.Clone(original.oldEncoding);
|
---|
101 | RegisterEvents();
|
---|
102 | }
|
---|
103 |
|
---|
104 | [StorableConstructor]
|
---|
105 | protected Problem(StorableConstructorFlag _) : base(_) { }
|
---|
106 | [StorableHook(HookType.AfterDeserialization)]
|
---|
107 | private void AfterDeserialization() {
|
---|
108 | oldEncoding = Encoding;
|
---|
109 | RegisterEvents();
|
---|
110 | }
|
---|
111 |
|
---|
112 | private void RegisterEvents() {
|
---|
113 | EncodingParameter.ValueChanged += (o, e) => { ParameterizeOperators(); OnEncodingChanged(); };
|
---|
114 | EvaluatorParameter.ValueChanged += (o, e) => { ParameterizeOperators(); OnEvaluatorChanged(); };
|
---|
115 | //var multiEncoding = Encoding as MultiEncoding;
|
---|
116 | //if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged;
|
---|
117 | }
|
---|
118 |
|
---|
119 | protected override void ParameterizeOperators() {
|
---|
120 | base.ParameterizeOperators();
|
---|
121 | Parameterize();
|
---|
122 | }
|
---|
123 |
|
---|
124 | protected virtual void OnEncodingChanged() {
|
---|
125 | OnOperatorsChanged();
|
---|
126 | OnReset();
|
---|
127 | }
|
---|
128 |
|
---|
129 | private void Parameterize() {
|
---|
130 | if (oldEncoding != null) {
|
---|
131 | AdaptEncodingOperators(oldEncoding, Encoding);
|
---|
132 | //var oldMultiEncoding = oldEncoding as MultiEncoding;
|
---|
133 | //if (oldMultiEncoding != null)
|
---|
134 | // oldMultiEncoding.EncodingsChanged -= MultiEncodingOnEncodingsChanged;
|
---|
135 | }
|
---|
136 | oldEncoding = Encoding;
|
---|
137 |
|
---|
138 | foreach (var op in Operators.OfType<IEncodingOperator>())
|
---|
139 | op.EncodingParameter.ActualName = EncodingParameter.Name;
|
---|
140 |
|
---|
141 | Encoding.ConfigureOperators(Operators);
|
---|
142 |
|
---|
143 | //var multiEncoding = Encoding as MultiEncoding;
|
---|
144 | //if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged;
|
---|
145 | }
|
---|
146 |
|
---|
147 | //protected override void OnSolutionCreatorChanged() {
|
---|
148 | // base.OnSolutionCreatorChanged();
|
---|
149 | // Encoding.SolutionCreator = SolutionCreator;
|
---|
150 | //}
|
---|
151 |
|
---|
152 | private static void AdaptEncodingOperators(IEncoding oldEncoding, IEncoding newEncoding) {
|
---|
153 | if (oldEncoding.GetType() != newEncoding.GetType()) return;
|
---|
154 |
|
---|
155 | if (oldEncoding is CombinedEncoding) {
|
---|
156 | var oldMultiEncoding = (CombinedEncoding)oldEncoding;
|
---|
157 | var newMultiEncoding = (CombinedEncoding)newEncoding;
|
---|
158 | if (!oldMultiEncoding.Encodings.SequenceEqual(newMultiEncoding.Encodings, new TypeEqualityComparer<IEncoding>())) return;
|
---|
159 |
|
---|
160 | var nestedEncodings = oldMultiEncoding.Encodings.Zip(newMultiEncoding.Encodings, (o, n) => new { oldEnc = o, newEnc = n });
|
---|
161 | foreach (var multi in nestedEncodings)
|
---|
162 | AdaptEncodingOperators(multi.oldEnc, multi.newEnc);
|
---|
163 | }
|
---|
164 |
|
---|
165 | var comparer = new TypeEqualityComparer<IOperator>();
|
---|
166 | var cloner = new Cloner();
|
---|
167 | var oldOperators = oldEncoding.Operators;
|
---|
168 | var newOperators = newEncoding.Operators;
|
---|
169 |
|
---|
170 | cloner.RegisterClonedObject(oldEncoding, newEncoding);
|
---|
171 | var operators = oldOperators.Intersect(newOperators, comparer)
|
---|
172 | .Select(cloner.Clone)
|
---|
173 | .Union(newOperators, comparer).ToList();
|
---|
174 |
|
---|
175 | newEncoding.ConfigureOperators(operators);
|
---|
176 | newEncoding.Operators = operators;
|
---|
177 | }
|
---|
178 |
|
---|
179 | protected override IEnumerable<KeyValuePair<string, IItem>> GetCollectedValues(IValueParameter param) {
|
---|
180 | if (param.Value == null) yield break;
|
---|
181 | if (param.GetsCollected) {
|
---|
182 | if (param == EncodingParameter) // store only the name of the encoding
|
---|
183 | yield return new KeyValuePair<string, IItem>(String.Empty, new StringValue(EncodingParameter.Value.Name));
|
---|
184 | else yield return new KeyValuePair<string, IItem>(String.Empty, param.Value);
|
---|
185 | }
|
---|
186 | var parameterizedItem = param.Value as IParameterizedItem;
|
---|
187 | if (parameterizedItem != null) {
|
---|
188 | var children = new Dictionary<string, IItem>();
|
---|
189 | parameterizedItem.CollectParameterValues(children);
|
---|
190 | foreach (var child in children) yield return child;
|
---|
191 | }
|
---|
192 | }
|
---|
193 | }
|
---|
194 | }
|
---|