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 base.Parameters.Concat(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 | public PermutationEncoding(string name, int length, PermutationTypes type)
|
---|
92 | : base(name) {
|
---|
93 | Length = length;
|
---|
94 | Type = type;
|
---|
95 | RegisterParameterEvents();
|
---|
96 | DiscoverOperators();
|
---|
97 | SolutionCreator = Operators.OfType<RandomPermutationCreator>().Single();
|
---|
98 | }
|
---|
99 |
|
---|
100 | private void OnLengthParameterChanged() {
|
---|
101 | RegisterParameterEvents();
|
---|
102 | ConfigureOperators(Operators);
|
---|
103 | }
|
---|
104 |
|
---|
105 | private void OnPermutationTypeParameterChanged() {
|
---|
106 | RegisterParameterEvents();
|
---|
107 | ConfigureOperators(Operators);
|
---|
108 | }
|
---|
109 |
|
---|
110 | private void RegisterParameterEvents() {
|
---|
111 | RegisterLengthParameterEvents();
|
---|
112 | RegisterPermutationTypeParameterEvents();
|
---|
113 | }
|
---|
114 | private void RegisterLengthParameterEvents() {
|
---|
115 | LengthParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators);
|
---|
116 | }
|
---|
117 | private void RegisterPermutationTypeParameterEvents() {
|
---|
118 | PermutationTypeParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators);
|
---|
119 | }
|
---|
120 |
|
---|
121 | #region Operator Discovery
|
---|
122 | private static readonly IEnumerable<Type> encodingSpecificOperatorTypes;
|
---|
123 | static PermutationEncoding() {
|
---|
124 | encodingSpecificOperatorTypes = new List<Type>() {
|
---|
125 | typeof (IPermutationOperator),
|
---|
126 | typeof (IPermutationCreator),
|
---|
127 | typeof (IPermutationCrossover),
|
---|
128 | typeof (IPermutationManipulator),
|
---|
129 | typeof (IPermutationMultiNeighborhoodShakingOperator),
|
---|
130 | typeof (IPermutationMoveOperator),
|
---|
131 | typeof (IPermutationInversionMoveOperator),
|
---|
132 | typeof (IPermutationScrambleMoveOperator),
|
---|
133 | typeof (IPermutationSwap2MoveOperator),
|
---|
134 | typeof (IPermutationTranslocationMoveOperator)
|
---|
135 | };
|
---|
136 | }
|
---|
137 | private void DiscoverOperators() {
|
---|
138 | var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, true, false, false);
|
---|
139 | var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t));
|
---|
140 | var newOperators = operators.Except(encodingOperators, new TypeEqualityComparer<IOperator>()).ToList();
|
---|
141 |
|
---|
142 | ConfigureOperators(newOperators);
|
---|
143 | encodingOperators.AddRange(newOperators);
|
---|
144 | }
|
---|
145 | #endregion
|
---|
146 |
|
---|
147 | public override void ConfigureOperators(IEnumerable<IOperator> operators) {
|
---|
148 | base.ConfigureOperators(operators);
|
---|
149 | ConfigureCreators(Operators.OfType<IPermutationCreator>());
|
---|
150 | ConfigureCrossovers(Operators.OfType<IPermutationCrossover>());
|
---|
151 | ConfigureManipulators(Operators.OfType<IPermutationManipulator>());
|
---|
152 | ConfigureShakingOperators(Operators.OfType<IPermutationMultiNeighborhoodShakingOperator>());
|
---|
153 | ConfigureMoveOperators(Operators.OfType<IPermutationMoveOperator>());
|
---|
154 | ConfigureInversionMoveOperators(Operators.OfType<IPermutationInversionMoveOperator>());
|
---|
155 | ConfigureScrambleMoveOperators(Operators.OfType<IPermutationScrambleMoveOperator>());
|
---|
156 | ConfigureSwap2MoveOperators(Operators.OfType<IPermutationSwap2MoveOperator>());
|
---|
157 | ConfigureTranslocationMoveOperators(Operators.OfType<IPermutationTranslocationMoveOperator>());
|
---|
158 | }
|
---|
159 |
|
---|
160 | #region specific operator wiring
|
---|
161 | private void ConfigureCreators(IEnumerable<IPermutationCreator> creators) {
|
---|
162 | foreach (var creator in creators) {
|
---|
163 | creator.LengthParameter.ActualName = LengthParameter.Name;
|
---|
164 | creator.PermutationParameter.ActualName = Name;
|
---|
165 | creator.PermutationTypeParameter.Value.Value = Type;
|
---|
166 | }
|
---|
167 | }
|
---|
168 | private void ConfigureCrossovers(IEnumerable<IPermutationCrossover> crossovers) {
|
---|
169 | foreach (var crossover in crossovers) {
|
---|
170 | crossover.ChildParameter.ActualName = Name;
|
---|
171 | crossover.ParentsParameter.ActualName = Name;
|
---|
172 | }
|
---|
173 | }
|
---|
174 | private void ConfigureManipulators(IEnumerable<IPermutationManipulator> manipulators) {
|
---|
175 | foreach (var manipulator in manipulators) {
|
---|
176 | manipulator.PermutationParameter.ActualName = Name;
|
---|
177 | }
|
---|
178 | }
|
---|
179 | private void ConfigureShakingOperators(IEnumerable<IPermutationMultiNeighborhoodShakingOperator> shakingOperators) {
|
---|
180 | foreach (var shakingOperator in shakingOperators) {
|
---|
181 | shakingOperator.PermutationParameter.ActualName = Name;
|
---|
182 | }
|
---|
183 | }
|
---|
184 | private void ConfigureMoveOperators(IEnumerable<IPermutationMoveOperator> moveOperators) {
|
---|
185 | foreach (var moveOperator in moveOperators) {
|
---|
186 | moveOperator.PermutationParameter.ActualName = Name;
|
---|
187 | }
|
---|
188 | }
|
---|
189 | private void ConfigureInversionMoveOperators(IEnumerable<IPermutationInversionMoveOperator> inversionMoveOperators) {
|
---|
190 | foreach (var inversionMoveOperator in inversionMoveOperators) {
|
---|
191 | inversionMoveOperator.InversionMoveParameter.ActualName = Name + "_InversionMove";
|
---|
192 | }
|
---|
193 | }
|
---|
194 | private void ConfigureScrambleMoveOperators(IEnumerable<IPermutationScrambleMoveOperator> scrambleMoveOperators) {
|
---|
195 | foreach (var scrambleMoveOperator in scrambleMoveOperators) {
|
---|
196 | scrambleMoveOperator.ScrambleMoveParameter.ActualName = Name + "_ScambleMove";
|
---|
197 | }
|
---|
198 | }
|
---|
199 | private void ConfigureSwap2MoveOperators(IEnumerable<IPermutationSwap2MoveOperator> swap2MoveOperators) {
|
---|
200 | foreach (var swap2MoveOperator in swap2MoveOperators) {
|
---|
201 | swap2MoveOperator.Swap2MoveParameter.ActualName = Name + "_Swap2Move";
|
---|
202 | }
|
---|
203 | }
|
---|
204 | private void ConfigureTranslocationMoveOperators(IEnumerable<IPermutationTranslocationMoveOperator> translocationMoveOperators) {
|
---|
205 | foreach (var translocationMoveOperator in translocationMoveOperators) {
|
---|
206 | translocationMoveOperator.TranslocationMoveParameter.ActualName = Name + "_TranslocationMove";
|
---|
207 | }
|
---|
208 | }
|
---|
209 |
|
---|
210 | #endregion
|
---|
211 | }
|
---|
212 | }
|
---|