1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 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.Collections;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Operators;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Optimization {
|
---|
33 | [StorableClass]
|
---|
34 | public abstract class MultiEncodingOperator<T> : InstrumentedOperator, IMultiEncodingOperator where T : class, IOperator {
|
---|
35 | [Storable]
|
---|
36 | private MultiEncoding encoding;
|
---|
37 | public MultiEncoding Encoding {
|
---|
38 | get { return encoding; }
|
---|
39 | set {
|
---|
40 | if (value == null) throw new ArgumentNullException("Encoding must not be null.");
|
---|
41 | if (value == encoding) return;
|
---|
42 | if (encoding != null) DeregisterEventHandlers();
|
---|
43 | encoding = value;
|
---|
44 | CombinedSolutionParameter.ActualName = encoding.Name;
|
---|
45 | RegisterEventHandlers();
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | public ILookupParameter<CombinedSolution> CombinedSolutionParameter {
|
---|
50 | get { return (ILookupParameter<CombinedSolution>)Parameters["CombinedSolution"]; }
|
---|
51 | }
|
---|
52 |
|
---|
53 | [StorableConstructor]
|
---|
54 | protected MultiEncodingOperator(bool deserializing) : base(deserializing) { }
|
---|
55 | protected MultiEncodingOperator(MultiEncodingOperator<T> original, Cloner cloner)
|
---|
56 | : base(original, cloner) {
|
---|
57 | encoding = cloner.Clone(original.encoding);
|
---|
58 | RegisterEventHandlers();
|
---|
59 | }
|
---|
60 | protected MultiEncodingOperator()
|
---|
61 | : base() {
|
---|
62 | Parameters.Add(new LookupParameter<CombinedSolution>("CombinedSolution", "The combined solution that gets created."));
|
---|
63 | }
|
---|
64 |
|
---|
65 | [StorableHook(HookType.AfterDeserialization)]
|
---|
66 | private void AfterDeserialization() {
|
---|
67 | RegisterEventHandlers();
|
---|
68 | }
|
---|
69 |
|
---|
70 | private void RegisterEventHandlers() {
|
---|
71 | encoding.Encodings.ItemsAdded += EncodingsOnItemsChanged;
|
---|
72 | encoding.Encodings.CollectionReset += EncodingsOnItemsChanged;
|
---|
73 | encoding.Encodings.ItemsRemoved += EncodingsOnItemsRemoved;
|
---|
74 | foreach (var enc in encoding.Encodings)
|
---|
75 | enc.OperatorsChanged += Encoding_OperatorsChanged;
|
---|
76 | }
|
---|
77 |
|
---|
78 | private void DeregisterEventHandlers() {
|
---|
79 | encoding.Encodings.ItemsAdded -= EncodingsOnItemsChanged;
|
---|
80 | encoding.Encodings.CollectionReset -= EncodingsOnItemsChanged;
|
---|
81 | encoding.Encodings.ItemsRemoved -= EncodingsOnItemsRemoved;
|
---|
82 | foreach (var enc in encoding.Encodings)
|
---|
83 | enc.OperatorsChanged -= Encoding_OperatorsChanged;
|
---|
84 | }
|
---|
85 |
|
---|
86 | private void EncodingsOnItemsChanged(object sender, CollectionItemsChangedEventArgs<IEncoding> e) {
|
---|
87 | foreach (var enc in e.Items)
|
---|
88 | AddEncoding(enc);
|
---|
89 | foreach (var enc in e.OldItems)
|
---|
90 | RemoveEncoding(enc);
|
---|
91 | }
|
---|
92 |
|
---|
93 | private void EncodingsOnItemsRemoved(object sender, CollectionItemsChangedEventArgs<IEncoding> e) {
|
---|
94 | foreach (var enc in e.Items)
|
---|
95 | RemoveEncoding(enc);
|
---|
96 | }
|
---|
97 |
|
---|
98 | public override IOperation InstrumentedApply() {
|
---|
99 | var operations = Parameters.Select(p => p.ActualValue).OfType<IOperator>().Select(op => ExecutionContext.CreateOperation(op));
|
---|
100 | return new OperationCollection(operations);
|
---|
101 | }
|
---|
102 |
|
---|
103 | protected virtual void AddEncoding(IEncoding encoding) {
|
---|
104 | if (Parameters.ContainsKey(encoding.Name)) throw new ArgumentException(string.Format("Encoding {0} was already added.", encoding.Name));
|
---|
105 |
|
---|
106 | encoding.OperatorsChanged += Encoding_OperatorsChanged;
|
---|
107 |
|
---|
108 | var param = new ConstrainedValueParameter<T>(encoding.Name, new ItemSet<T>(encoding.Operators.OfType<T>()));
|
---|
109 | param.Value = param.ValidValues.First();
|
---|
110 | Parameters.Add(param);
|
---|
111 | }
|
---|
112 |
|
---|
113 | protected virtual bool RemoveEncoding(IEncoding encoding) {
|
---|
114 | if (!Parameters.ContainsKey(encoding.Name)) throw new ArgumentException(string.Format("Encoding {0} was not added to the MultiEncoding.", encoding.Name));
|
---|
115 | encoding.OperatorsChanged -= Encoding_OperatorsChanged;
|
---|
116 | return Parameters.Remove(encoding.Name);
|
---|
117 | }
|
---|
118 |
|
---|
119 | protected IConstrainedValueParameter<T> GetParameter(IEncoding encoding) {
|
---|
120 | if (!Parameters.ContainsKey(encoding.Name)) throw new ArgumentException(string.Format("Encoding {0} was not added to the MultiEncoding.", encoding.Name));
|
---|
121 |
|
---|
122 | return (IConstrainedValueParameter<T>)Parameters[encoding.Name];
|
---|
123 | }
|
---|
124 |
|
---|
125 | private void Encoding_OperatorsChanged(object sender, EventArgs e) {
|
---|
126 | var enc = (IEncoding)sender;
|
---|
127 | var param = GetParameter(enc);
|
---|
128 |
|
---|
129 | var oldParameterValue = param.Value;
|
---|
130 | param.ValidValues.Clear();
|
---|
131 | foreach (var op in enc.Operators.OfType<T>())
|
---|
132 | param.ValidValues.Add(op);
|
---|
133 |
|
---|
134 | var newValue = param.ValidValues.FirstOrDefault(op => op.GetType() == oldParameterValue.GetType());
|
---|
135 | if (newValue == null) newValue = param.ValidValues.First();
|
---|
136 | param.Value = newValue;
|
---|
137 | }
|
---|
138 | }
|
---|
139 | }
|
---|