1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2014 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.Data;
|
---|
28 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 | using HeuristicLab.PluginInfrastructure;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Problems.Programmable {
|
---|
33 | [Item("PermutationEncoding", "Describes a permutation encoding.")]
|
---|
34 | [StorableClass]
|
---|
35 | public sealed class PermutationEncoding : Encoding<IPermutationCreator> {
|
---|
36 | #region encoding parameters
|
---|
37 | [Storable]
|
---|
38 | private IFixedValueParameter<IntValue> lengthParameter;
|
---|
39 | public IFixedValueParameter<IntValue> LengthParameter {
|
---|
40 | get { return lengthParameter; }
|
---|
41 | set {
|
---|
42 | if (value == null) throw new ArgumentNullException("Length parameter must not be null.");
|
---|
43 | if (lengthParameter == value) return;
|
---|
44 | lengthParameter = value;
|
---|
45 | OnLengthParameterChanged();
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | [Storable]
|
---|
50 | private IFixedValueParameter<PermutationType> permutationTypeParameter;
|
---|
51 | public IFixedValueParameter<PermutationType> PermutationTypeParameter {
|
---|
52 | get { return permutationTypeParameter; }
|
---|
53 | set {
|
---|
54 | if (value == null) throw new ArgumentNullException("Permutation type parameter must not be null.");
|
---|
55 | if (permutationTypeParameter == value) return;
|
---|
56 | permutationTypeParameter = value;
|
---|
57 | OnPermutationTypeParameterChanged();
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | public override IEnumerable<IValueParameter> Parameters {
|
---|
62 | get { return new IValueParameter[] { LengthParameter, PermutationTypeParameter }; }
|
---|
63 | }
|
---|
64 | #endregion
|
---|
65 |
|
---|
66 | public int Length {
|
---|
67 | get { return LengthParameter.Value.Value; }
|
---|
68 | set { LengthParameter.Value.Value = value; }
|
---|
69 | }
|
---|
70 |
|
---|
71 | public PermutationTypes Type {
|
---|
72 | get { return PermutationTypeParameter.Value.Value; }
|
---|
73 | set { PermutationTypeParameter.Value.Value = value; }
|
---|
74 | }
|
---|
75 |
|
---|
76 | [StorableConstructor]
|
---|
77 | private PermutationEncoding(bool deserializing) : base(deserializing) { }
|
---|
78 | [StorableHook(HookType.AfterDeserialization)]
|
---|
79 | private void AfterDeserialization() {
|
---|
80 | RegisterParameterEvents();
|
---|
81 | DiscoverOperators();
|
---|
82 | }
|
---|
83 |
|
---|
84 | public override IDeepCloneable Clone(Cloner cloner) { return new PermutationEncoding(this, cloner); }
|
---|
85 | private PermutationEncoding(PermutationEncoding original, Cloner cloner)
|
---|
86 | : base(original, cloner) {
|
---|
87 | lengthParameter = cloner.Clone(original.lengthParameter);
|
---|
88 | permutationTypeParameter = cloner.Clone(original.permutationTypeParameter);
|
---|
89 | RegisterParameterEvents();
|
---|
90 | }
|
---|
91 |
|
---|
92 | public PermutationEncoding(string name, int length, PermutationTypes type)
|
---|
93 | : base(name) {
|
---|
94 | Length = length;
|
---|
95 | Type = type;
|
---|
96 |
|
---|
97 | SolutionCreator = new RandomPermutationCreator();
|
---|
98 | RegisterParameterEvents();
|
---|
99 | DiscoverOperators();
|
---|
100 | }
|
---|
101 |
|
---|
102 | private void OnLengthParameterChanged() {
|
---|
103 | RegisterParameterEvents();
|
---|
104 | ConfigureOperators(Operators);
|
---|
105 | }
|
---|
106 |
|
---|
107 | private void OnPermutationTypeParameterChanged() {
|
---|
108 | RegisterParameterEvents();
|
---|
109 | ConfigureOperators(Operators);
|
---|
110 | }
|
---|
111 |
|
---|
112 | private void RegisterParameterEvents() {
|
---|
113 | RegisterLengthParameterEvents();
|
---|
114 | RegisterPermutationTypeParameterEvents();
|
---|
115 | }
|
---|
116 | private void RegisterLengthParameterEvents() {
|
---|
117 | LengthParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators);
|
---|
118 | }
|
---|
119 | private void RegisterPermutationTypeParameterEvents() {
|
---|
120 | PermutationTypeParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators);
|
---|
121 | }
|
---|
122 |
|
---|
123 | #region Operator Discovery
|
---|
124 | private static readonly IEnumerable<Type> encodingSpecificOperatorTypes;
|
---|
125 | static PermutationEncoding() {
|
---|
126 | encodingSpecificOperatorTypes = new List<Type>() {
|
---|
127 | typeof (IPermutationOperator),
|
---|
128 | typeof (IPermutationCreator),
|
---|
129 | typeof (IPermutationCrossover),
|
---|
130 | typeof (IPermutationManipulator),
|
---|
131 | typeof (IPermutationMultiNeighborhoodShakingOperator),
|
---|
132 | typeof (IPermutationMoveOperator),
|
---|
133 | typeof (IPermutationInversionMoveOperator),
|
---|
134 | typeof (IPermutationScrambleMoveOperator),
|
---|
135 | typeof (IPermutationSwap2MoveOperator),
|
---|
136 | typeof (IPermutationTranslocationMoveOperator)
|
---|
137 | };
|
---|
138 | }
|
---|
139 | private void DiscoverOperators() {
|
---|
140 | var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, true, false, false);
|
---|
141 | var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t));
|
---|
142 | var newOperators = operators.Except(Operators, new TypeEqualityComparer<IOperator>()).ToList();
|
---|
143 |
|
---|
144 | ConfigureOperators(newOperators);
|
---|
145 | foreach (var @operator in newOperators)
|
---|
146 | encodingOperators.Add(@operator);
|
---|
147 | }
|
---|
148 | #endregion
|
---|
149 |
|
---|
150 | public override void ConfigureOperators(IEnumerable<IOperator> operators) {
|
---|
151 | ConfigureCreators(Operators.OfType<IPermutationCreator>());
|
---|
152 | ConfigureCrossovers(Operators.OfType<IPermutationCrossover>());
|
---|
153 | ConfigureManipulators(Operators.OfType<IPermutationManipulator>());
|
---|
154 | ConfigureShakingOperators(Operators.OfType<IPermutationMultiNeighborhoodShakingOperator>());
|
---|
155 | ConfigureMoveOperators(Operators.OfType<IPermutationMoveOperator>());
|
---|
156 | ConfigureInversionMoveOperators(Operators.OfType<IPermutationInversionMoveOperator>());
|
---|
157 | ConfigureScrambleMoveOperators(Operators.OfType<IPermutationScrambleMoveOperator>());
|
---|
158 | ConfigureSwap2MoveOperators(Operators.OfType<IPermutationSwap2MoveOperator>());
|
---|
159 | ConfigureTranslocationMoveOperators(Operators.OfType<IPermutationTranslocationMoveOperator>());
|
---|
160 | }
|
---|
161 |
|
---|
162 | #region specific operator wiring
|
---|
163 | private void ConfigureCreators(IEnumerable<IPermutationCreator> creators) {
|
---|
164 | foreach (var creator in creators) {
|
---|
165 | creator.LengthParameter.ActualName = LengthParameter.Name;
|
---|
166 | creator.PermutationParameter.ActualName = Name;
|
---|
167 | creator.PermutationTypeParameter.Value.Value = Type;
|
---|
168 | }
|
---|
169 | }
|
---|
170 | private void ConfigureCrossovers(IEnumerable<IPermutationCrossover> crossovers) {
|
---|
171 | foreach (var crossover in crossovers) {
|
---|
172 | crossover.ChildParameter.ActualName = Name;
|
---|
173 | crossover.ParentsParameter.ActualName = Name;
|
---|
174 | }
|
---|
175 | }
|
---|
176 | private void ConfigureManipulators(IEnumerable<IPermutationManipulator> manipulators) {
|
---|
177 | foreach (var manipulator in manipulators) {
|
---|
178 | manipulator.PermutationParameter.ActualName = Name;
|
---|
179 | }
|
---|
180 | }
|
---|
181 | private void ConfigureShakingOperators(IEnumerable<IPermutationMultiNeighborhoodShakingOperator> shakingOperators) {
|
---|
182 | foreach (var shakingOperator in shakingOperators) {
|
---|
183 | shakingOperator.PermutationParameter.ActualName = Name;
|
---|
184 | }
|
---|
185 | }
|
---|
186 | private void ConfigureMoveOperators(IEnumerable<IPermutationMoveOperator> moveOperators) {
|
---|
187 | foreach (var moveOperator in moveOperators) {
|
---|
188 | moveOperator.PermutationParameter.ActualName = Name;
|
---|
189 | }
|
---|
190 | }
|
---|
191 | private void ConfigureInversionMoveOperators(IEnumerable<IPermutationInversionMoveOperator> inversionMoveOperators) {
|
---|
192 | foreach (var inversionMoveOperator in inversionMoveOperators) {
|
---|
193 | inversionMoveOperator.InversionMoveParameter.ActualName = Name + "_InversionMove";
|
---|
194 | }
|
---|
195 | }
|
---|
196 | private void ConfigureScrambleMoveOperators(IEnumerable<IPermutationScrambleMoveOperator> scrambleMoveOperators) {
|
---|
197 | foreach (var scrambleMoveOperator in scrambleMoveOperators) {
|
---|
198 | scrambleMoveOperator.ScrambleMoveParameter.ActualName = Name + "_ScambleMove";
|
---|
199 | }
|
---|
200 | }
|
---|
201 | private void ConfigureSwap2MoveOperators(IEnumerable<IPermutationSwap2MoveOperator> swap2MoveOperators) {
|
---|
202 | foreach (var swap2MoveOperator in swap2MoveOperators) {
|
---|
203 | swap2MoveOperator.Swap2MoveParameter.ActualName = Name + "_Swap2Move";
|
---|
204 | }
|
---|
205 | }
|
---|
206 | private void ConfigureTranslocationMoveOperators(IEnumerable<IPermutationTranslocationMoveOperator> translocationMoveOperators) {
|
---|
207 | foreach (var translocationMoveOperator in translocationMoveOperators) {
|
---|
208 | translocationMoveOperator.TranslocationMoveParameter.ActualName = Name + "_TranslocationMove";
|
---|
209 | }
|
---|
210 | }
|
---|
211 |
|
---|
212 | #endregion
|
---|
213 | }
|
---|
214 | }
|
---|