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.Optimization;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.PluginInfrastructure;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Encodings.PermutationEncoding {
|
---|
34 | [Item("PermutationEncoding", "Describes a permutation encoding.")]
|
---|
35 | [StorableType("E30E7507-44BA-4021-8F56-C3FC5569A6FE")]
|
---|
36 | public sealed class PermutationEncoding : VectorEncoding<Permutation> {
|
---|
37 | #region encoding parameters
|
---|
38 | [Storable] public IValueParameter<EnumValue<PermutationTypes>> PermutationTypeParameter { get; private set; }
|
---|
39 | public PermutationTypes Type {
|
---|
40 | get { return PermutationTypeParameter.Value.Value; }
|
---|
41 | set {
|
---|
42 | if (Type == value) return;
|
---|
43 | PermutationTypeParameter.Value.Value = value;
|
---|
44 | }
|
---|
45 | }
|
---|
46 | #endregion
|
---|
47 |
|
---|
48 | [StorableConstructor]
|
---|
49 | private PermutationEncoding(StorableConstructorFlag _) : base(_) { }
|
---|
50 | [StorableHook(HookType.AfterDeserialization)]
|
---|
51 | private void AfterDeserialization() {
|
---|
52 | DiscoverOperators();
|
---|
53 | RegisterParameterEvents();
|
---|
54 | }
|
---|
55 |
|
---|
56 | public override IDeepCloneable Clone(Cloner cloner) { return new PermutationEncoding(this, cloner); }
|
---|
57 | private PermutationEncoding(PermutationEncoding original, Cloner cloner)
|
---|
58 | : base(original, cloner) {
|
---|
59 | PermutationTypeParameter = cloner.Clone(original.PermutationTypeParameter);
|
---|
60 | RegisterParameterEvents();
|
---|
61 | }
|
---|
62 |
|
---|
63 |
|
---|
64 | public PermutationEncoding() : this("Permutation", 10, PermutationTypes.Absolute) { }
|
---|
65 | public PermutationEncoding(string name) : this(name, 10, PermutationTypes.Absolute) { }
|
---|
66 | public PermutationEncoding(int length) : this("Permutation", length, PermutationTypes.Absolute) { }
|
---|
67 | public PermutationEncoding(string name, int length, PermutationTypes type)
|
---|
68 | : base(name, length) {
|
---|
69 | PermutationTypeParameter = new ValueParameter<EnumValue<PermutationTypes>>(Name + ".Type", new EnumValue<PermutationTypes>(type));
|
---|
70 | Parameters.Add(PermutationTypeParameter);
|
---|
71 |
|
---|
72 | DiscoverOperators();
|
---|
73 | RegisterParameterEvents();
|
---|
74 | }
|
---|
75 |
|
---|
76 | private void RegisterParameterEvents() {
|
---|
77 | EnumValueParameterChangeHandler<PermutationTypes>.Create(PermutationTypeParameter, () => {
|
---|
78 | ConfigureOperators(Operators);
|
---|
79 | OnTypeChanged();
|
---|
80 | });
|
---|
81 | }
|
---|
82 |
|
---|
83 | #region Operator Discovery
|
---|
84 | private static readonly IEnumerable<Type> encodingSpecificOperatorTypes;
|
---|
85 | static PermutationEncoding() {
|
---|
86 | encodingSpecificOperatorTypes = new List<Type>() {
|
---|
87 | typeof (IPermutationOperator),
|
---|
88 | typeof (IPermutationCreator),
|
---|
89 | typeof (IPermutationCrossover),
|
---|
90 | typeof (IPermutationManipulator),
|
---|
91 | typeof (IPermutationMultiNeighborhoodShakingOperator),
|
---|
92 | typeof (IPermutationMoveOperator),
|
---|
93 | typeof (IPermutationInversionMoveOperator),
|
---|
94 | typeof (IPermutationScrambleMoveOperator),
|
---|
95 | typeof (IPermutationSwap2MoveOperator),
|
---|
96 | typeof (IPermutationTranslocationMoveOperator),
|
---|
97 | typeof (IPermutationLocalImprovementOperator),
|
---|
98 | typeof (IPermutationSolutionOperator),
|
---|
99 | typeof (IPermutationSolutionsOperator),
|
---|
100 | };
|
---|
101 | }
|
---|
102 | private void DiscoverOperators() {
|
---|
103 | var assembly = typeof(IPermutationOperator).Assembly;
|
---|
104 | var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, assembly, true, false, false);
|
---|
105 | var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t));
|
---|
106 | var newOperators = operators.Except(Operators, new TypeEqualityComparer<IOperator>()).ToList();
|
---|
107 |
|
---|
108 | ConfigureOperators(newOperators);
|
---|
109 | foreach (var @operator in newOperators)
|
---|
110 | AddOperator(@operator);
|
---|
111 | }
|
---|
112 | #endregion
|
---|
113 |
|
---|
114 | public override void ConfigureOperators(IEnumerable<IItem> operators) {
|
---|
115 | base.ConfigureOperators(operators);
|
---|
116 | ConfigureCreators(operators.OfType<IPermutationCreator>());
|
---|
117 | ConfigureCrossovers(operators.OfType<IPermutationCrossover>());
|
---|
118 | ConfigureManipulators(operators.OfType<IPermutationManipulator>());
|
---|
119 | ConfigureShakingOperators(operators.OfType<IPermutationMultiNeighborhoodShakingOperator>());
|
---|
120 | ConfigureMoveOperators(operators.OfType<IPermutationMoveOperator>());
|
---|
121 | ConfigureInversionMoveOperators(operators.OfType<IPermutationInversionMoveOperator>());
|
---|
122 | ConfigureScrambleMoveOperators(operators.OfType<IPermutationScrambleMoveOperator>());
|
---|
123 | ConfigureSwap2MoveOperators(operators.OfType<IPermutationSwap2MoveOperator>());
|
---|
124 | ConfigureTranslocationMoveOperators(operators.OfType<IPermutationTranslocationMoveOperator>());
|
---|
125 | ConfigureLocalImprovementOperators(operators.OfType<IPermutationLocalImprovementOperator>());
|
---|
126 | ConfigureSolutionOperators(operators.OfType<IPermutationSolutionOperator>());
|
---|
127 | ConfigureSolutionsOperators(operators.OfType<IPermutationSolutionsOperator>());
|
---|
128 | }
|
---|
129 |
|
---|
130 | #region specific operator wiring
|
---|
131 | private void ConfigureCreators(IEnumerable<IPermutationCreator> creators) {
|
---|
132 | foreach (var creator in creators) {
|
---|
133 | creator.LengthParameter.ActualName = LengthParameter.Name;
|
---|
134 | creator.PermutationTypeParameter.Value.Value = Type;
|
---|
135 | }
|
---|
136 | }
|
---|
137 | private void ConfigureCrossovers(IEnumerable<IPermutationCrossover> crossovers) {
|
---|
138 | foreach (var crossover in crossovers) {
|
---|
139 | crossover.ChildParameter.ActualName = Name;
|
---|
140 | crossover.ParentsParameter.ActualName = Name;
|
---|
141 | }
|
---|
142 | }
|
---|
143 | private void ConfigureManipulators(IEnumerable<IPermutationManipulator> manipulators) {
|
---|
144 | // IPermutationManipulator does not contain additional parameters (already contained in IPermutationSolutionOperator)
|
---|
145 | }
|
---|
146 | private void ConfigureShakingOperators(IEnumerable<IPermutationMultiNeighborhoodShakingOperator> shakingOperators) {
|
---|
147 | foreach (var shakingOperator in shakingOperators) {
|
---|
148 | shakingOperator.PermutationParameter.ActualName = Name;
|
---|
149 | }
|
---|
150 | }
|
---|
151 | private void ConfigureMoveOperators(IEnumerable<IPermutationMoveOperator> moveOperators) {
|
---|
152 | foreach (var moveOperator in moveOperators) {
|
---|
153 | moveOperator.PermutationParameter.ActualName = Name;
|
---|
154 | }
|
---|
155 | }
|
---|
156 | private void ConfigureInversionMoveOperators(IEnumerable<IPermutationInversionMoveOperator> inversionMoveOperators) {
|
---|
157 | foreach (var inversionMoveOperator in inversionMoveOperators) {
|
---|
158 | inversionMoveOperator.InversionMoveParameter.ActualName = Name + ".InversionMove";
|
---|
159 | }
|
---|
160 | }
|
---|
161 | private void ConfigureScrambleMoveOperators(IEnumerable<IPermutationScrambleMoveOperator> scrambleMoveOperators) {
|
---|
162 | foreach (var scrambleMoveOperator in scrambleMoveOperators) {
|
---|
163 | scrambleMoveOperator.ScrambleMoveParameter.ActualName = Name + ".ScrambleMove";
|
---|
164 | }
|
---|
165 | }
|
---|
166 | private void ConfigureSwap2MoveOperators(IEnumerable<IPermutationSwap2MoveOperator> swap2MoveOperators) {
|
---|
167 | foreach (var swap2MoveOperator in swap2MoveOperators) {
|
---|
168 | swap2MoveOperator.Swap2MoveParameter.ActualName = Name + ".Swap2Move";
|
---|
169 | }
|
---|
170 | }
|
---|
171 | private void ConfigureTranslocationMoveOperators(IEnumerable<IPermutationTranslocationMoveOperator> translocationMoveOperators) {
|
---|
172 | foreach (var translocationMoveOperator in translocationMoveOperators) {
|
---|
173 | translocationMoveOperator.TranslocationMoveParameter.ActualName = Name + ".TranslocationMove";
|
---|
174 | }
|
---|
175 | }
|
---|
176 | private void ConfigureLocalImprovementOperators(IEnumerable<IPermutationLocalImprovementOperator> localImprovementOperators) {
|
---|
177 | // IPermutationLocalImprovementOperator does not contain additional parameters (already contained in IPermutationSolutionOperator)
|
---|
178 | }
|
---|
179 | private void ConfigureSolutionOperators(IEnumerable<IPermutationSolutionOperator> solutionOperators) {
|
---|
180 | foreach (var solutionOperator in solutionOperators) {
|
---|
181 | solutionOperator.PermutationParameter.ActualName = Name;
|
---|
182 | }
|
---|
183 | }
|
---|
184 | private void ConfigureSolutionsOperators(IEnumerable<IPermutationSolutionsOperator> solutionsOperators) {
|
---|
185 | foreach (var solutionsOperator in solutionsOperators) {
|
---|
186 | solutionsOperator.PermutationsParameter.ActualName = Name;
|
---|
187 | }
|
---|
188 | }
|
---|
189 | #endregion
|
---|
190 |
|
---|
191 | public event EventHandler TypeChanged;
|
---|
192 | private void OnTypeChanged() {
|
---|
193 | TypeChanged?.Invoke(this, EventArgs.Empty);
|
---|
194 | }
|
---|
195 | }
|
---|
196 | }
|
---|