1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2018 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 HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Optimization {
|
---|
31 | [StorableClass]
|
---|
32 | public abstract class BasicProblem<TEncoding, TEvaluator> : HeuristicOptimizationProblem<TEvaluator, ISolutionCreator>, IProblemDefinition, IStorableContent
|
---|
33 | where TEncoding : class, IEncoding
|
---|
34 | where TEvaluator : class, IEvaluator {
|
---|
35 |
|
---|
36 | public string Filename { get; set; }
|
---|
37 |
|
---|
38 | protected IValueParameter<TEncoding> EncodingParameter {
|
---|
39 | get { return (IValueParameter<TEncoding>)Parameters["Encoding"]; }
|
---|
40 | }
|
---|
41 |
|
---|
42 | //mkommend necessary for reuse of operators if the encoding changes
|
---|
43 | private TEncoding oldEncoding;
|
---|
44 |
|
---|
45 | IEncoding IProblemDefinition.Encoding { get { return Encoding; } }
|
---|
46 | public TEncoding Encoding {
|
---|
47 | get { return EncodingParameter.Value; }
|
---|
48 | protected set {
|
---|
49 | if (value == null) throw new ArgumentNullException("Encoding must not be null.");
|
---|
50 | EncodingParameter.Value = value;
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | protected override IEnumerable<IItem> GetOperators() {
|
---|
55 | if (Encoding == null) return base.GetOperators();
|
---|
56 | return base.GetOperators().Concat(Encoding.Operators);
|
---|
57 | }
|
---|
58 | public override IEnumerable<IParameterizedItem> ExecutionContextItems {
|
---|
59 | get {
|
---|
60 | if (Encoding == null) return base.ExecutionContextItems;
|
---|
61 | return base.ExecutionContextItems.Concat(new[] { Encoding });
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | protected BasicProblem()
|
---|
66 | : base() {
|
---|
67 | var encodingParameter = new ValueParameter<TEncoding>("Encoding", "Describes the configuration of the encoding, what the variables are called, what type they are and their bounds if any.");
|
---|
68 | // ABE: disable showing the encoding itself in the run by default as it wastes a lot of space
|
---|
69 | // note that parameters of the encoding might still show up in the run
|
---|
70 | encodingParameter.GetsCollected = false;
|
---|
71 | Parameters.Add(encodingParameter);
|
---|
72 | oldEncoding = Encoding;
|
---|
73 | if(Encoding != null) Parameterize();
|
---|
74 | RegisterEvents();
|
---|
75 | }
|
---|
76 |
|
---|
77 | protected BasicProblem(BasicProblem<TEncoding, TEvaluator> original, Cloner cloner)
|
---|
78 | : base(original, cloner) {
|
---|
79 | oldEncoding = cloner.Clone(original.oldEncoding);
|
---|
80 | RegisterEvents();
|
---|
81 | }
|
---|
82 |
|
---|
83 | [StorableConstructor]
|
---|
84 | protected BasicProblem(bool deserializing) : base(deserializing) { }
|
---|
85 | [StorableHook(HookType.AfterDeserialization)]
|
---|
86 | private void AfterDeserialization() {
|
---|
87 | oldEncoding = Encoding;
|
---|
88 | RegisterEvents();
|
---|
89 | }
|
---|
90 |
|
---|
91 | private void RegisterEvents() {
|
---|
92 | EncodingParameter.ValueChanged += (o, e) => OnEncodingChanged();
|
---|
93 | var multiEncoding = Encoding as MultiEncoding;
|
---|
94 | if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged;
|
---|
95 | }
|
---|
96 |
|
---|
97 | protected virtual void OnEncodingChanged() {
|
---|
98 | Parameterize();
|
---|
99 |
|
---|
100 | OnOperatorsChanged();
|
---|
101 | OnReset();
|
---|
102 | }
|
---|
103 |
|
---|
104 | private void Parameterize() {
|
---|
105 | if (oldEncoding != null) {
|
---|
106 | AdaptEncodingOperators(oldEncoding, Encoding);
|
---|
107 | var oldMultiEncoding = oldEncoding as MultiEncoding;
|
---|
108 | if (oldMultiEncoding != null)
|
---|
109 | oldMultiEncoding.EncodingsChanged -= MultiEncodingOnEncodingsChanged;
|
---|
110 | }
|
---|
111 | oldEncoding = Encoding;
|
---|
112 |
|
---|
113 | foreach (var op in Operators.OfType<IEncodingOperator>())
|
---|
114 | op.EncodingParameter.ActualName = EncodingParameter.Name;
|
---|
115 |
|
---|
116 | var solutionCreatorType = Encoding.GetType().BaseType.GetGenericArguments();
|
---|
117 | var paramType = typeof(ValueParameter<>).MakeGenericType(solutionCreatorType);
|
---|
118 | var solutionCreatorParam = (IParameter)Activator.CreateInstance(paramType, SolutionCreatorParameter.Name, SolutionCreatorParameter.Description,
|
---|
119 | Encoding.SolutionCreator);
|
---|
120 | Parameters.Remove(SolutionCreatorParameter);
|
---|
121 | Parameters.Add(solutionCreatorParam);
|
---|
122 | ((IValueParameter)solutionCreatorParam).ValueChanged += SolutionCreatorParameter_ValueChanged;
|
---|
123 |
|
---|
124 | var multiEncoding = Encoding as MultiEncoding;
|
---|
125 | if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged;
|
---|
126 | }
|
---|
127 |
|
---|
128 | protected override void OnSolutionCreatorChanged() {
|
---|
129 | base.OnSolutionCreatorChanged();
|
---|
130 | Encoding.SolutionCreator = SolutionCreator;
|
---|
131 | }
|
---|
132 |
|
---|
133 | private static void AdaptEncodingOperators(IEncoding oldEncoding, IEncoding newEncoding) {
|
---|
134 | if (oldEncoding.GetType() != newEncoding.GetType()) return;
|
---|
135 |
|
---|
136 | if (oldEncoding.GetType() == typeof(MultiEncoding)) {
|
---|
137 | var oldMultiEncoding = (MultiEncoding)oldEncoding;
|
---|
138 | var newMultiEncoding = (MultiEncoding)newEncoding;
|
---|
139 | if (!oldMultiEncoding.Encodings.SequenceEqual(newMultiEncoding.Encodings, new TypeEqualityComparer<IEncoding>())) return;
|
---|
140 |
|
---|
141 | var nestedEncodings = oldMultiEncoding.Encodings.Zip(newMultiEncoding.Encodings, (o, n) => new { oldEnc = o, newEnc = n });
|
---|
142 | foreach (var multi in nestedEncodings)
|
---|
143 | AdaptEncodingOperators(multi.oldEnc, multi.newEnc);
|
---|
144 | }
|
---|
145 |
|
---|
146 | var comparer = new TypeEqualityComparer<IOperator>();
|
---|
147 | var cloner = new Cloner();
|
---|
148 | var oldOperators = oldEncoding.Operators;
|
---|
149 | var newOperators = newEncoding.Operators;
|
---|
150 |
|
---|
151 | cloner.RegisterClonedObject(oldEncoding, newEncoding);
|
---|
152 | var operators = oldOperators.Intersect(newOperators, comparer)
|
---|
153 | .Select(cloner.Clone)
|
---|
154 | .Union(newOperators, comparer).ToList();
|
---|
155 |
|
---|
156 | newEncoding.ConfigureOperators(operators);
|
---|
157 | newEncoding.Operators = operators;
|
---|
158 | }
|
---|
159 |
|
---|
160 | protected virtual void MultiEncodingOnEncodingsChanged(object sender, EventArgs e) {
|
---|
161 | OnOperatorsChanged();
|
---|
162 | }
|
---|
163 | }
|
---|
164 | }
|
---|