[11484] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[11484] | 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 |
|
---|
[11575] | 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
[11484] | 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
[11949] | 28 | using HeuristicLab.Optimization;
|
---|
[11588] | 29 | using HeuristicLab.Parameters;
|
---|
[16565] | 30 | using HEAL.Attic;
|
---|
[11575] | 31 | using HeuristicLab.PluginInfrastructure;
|
---|
[11484] | 32 |
|
---|
[11949] | 33 | namespace HeuristicLab.Encodings.PermutationEncoding {
|
---|
[11484] | 34 | [Item("PermutationEncoding", "Describes a permutation encoding.")]
|
---|
[16565] | 35 | [StorableType("E30E7507-44BA-4021-8F56-C3FC5569A6FE")]
|
---|
[11575] | 36 | public sealed class PermutationEncoding : Encoding<IPermutationCreator> {
|
---|
| 37 | #region encoding parameters
|
---|
[11598] | 38 | [Storable]
|
---|
[11575] | 39 | private IFixedValueParameter<IntValue> lengthParameter;
|
---|
| 40 | public IFixedValueParameter<IntValue> LengthParameter {
|
---|
| 41 | get { return lengthParameter; }
|
---|
[11484] | 42 | set {
|
---|
[11575] | 43 | if (value == null) throw new ArgumentNullException("Length parameter must not be null.");
|
---|
[11588] | 44 | if (value.Value == null) throw new ArgumentNullException("Length parameter value must not be null.");
|
---|
[11575] | 45 | if (lengthParameter == value) return;
|
---|
[11588] | 46 |
|
---|
| 47 | if (lengthParameter != null) Parameters.Remove(lengthParameter);
|
---|
[11575] | 48 | lengthParameter = value;
|
---|
[11588] | 49 | Parameters.Add(lengthParameter);
|
---|
[11575] | 50 | OnLengthParameterChanged();
|
---|
[11484] | 51 | }
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[11598] | 54 | [Storable]
|
---|
[11575] | 55 | private IFixedValueParameter<PermutationType> permutationTypeParameter;
|
---|
| 56 | public IFixedValueParameter<PermutationType> PermutationTypeParameter {
|
---|
| 57 | get { return permutationTypeParameter; }
|
---|
[11484] | 58 | set {
|
---|
[11575] | 59 | if (value == null) throw new ArgumentNullException("Permutation type parameter must not be null.");
|
---|
[11588] | 60 | if (value.Value == null) throw new ArgumentNullException("Permutation type parameter value must not be null.");
|
---|
[11575] | 61 | if (permutationTypeParameter == value) return;
|
---|
[11588] | 62 |
|
---|
| 63 | if (permutationTypeParameter != null) Parameters.Remove(permutationTypeParameter);
|
---|
[11575] | 64 | permutationTypeParameter = value;
|
---|
[11588] | 65 | Parameters.Add(permutationTypeParameter);
|
---|
[11575] | 66 | OnPermutationTypeParameterChanged();
|
---|
[11484] | 67 | }
|
---|
| 68 | }
|
---|
[11575] | 69 | #endregion
|
---|
| 70 |
|
---|
| 71 | public int Length {
|
---|
| 72 | get { return LengthParameter.Value.Value; }
|
---|
| 73 | set { LengthParameter.Value.Value = value; }
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | public PermutationTypes Type {
|
---|
| 77 | get { return PermutationTypeParameter.Value.Value; }
|
---|
| 78 | set { PermutationTypeParameter.Value.Value = value; }
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[11484] | 81 | [StorableConstructor]
|
---|
[16565] | 82 | private PermutationEncoding(StorableConstructorFlag _) : base(_) { }
|
---|
[11575] | 83 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 84 | private void AfterDeserialization() {
|
---|
| 85 | RegisterParameterEvents();
|
---|
| 86 | DiscoverOperators();
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | public override IDeepCloneable Clone(Cloner cloner) { return new PermutationEncoding(this, cloner); }
|
---|
| 90 | private PermutationEncoding(PermutationEncoding original, Cloner cloner)
|
---|
[11484] | 91 | : base(original, cloner) {
|
---|
[11575] | 92 | lengthParameter = cloner.Clone(original.lengthParameter);
|
---|
| 93 | permutationTypeParameter = cloner.Clone(original.permutationTypeParameter);
|
---|
| 94 | RegisterParameterEvents();
|
---|
[11484] | 95 | }
|
---|
[11587] | 96 |
|
---|
[11739] | 97 |
|
---|
| 98 | public PermutationEncoding() : this("Permutation", 10, PermutationTypes.Absolute) { }
|
---|
[11892] | 99 | public PermutationEncoding(string name) : this(name, 10, PermutationTypes.Absolute) { }
|
---|
[12015] | 100 | public PermutationEncoding(int length) : this("Permutation", length, PermutationTypes.Absolute) { }
|
---|
[11484] | 101 | public PermutationEncoding(string name, int length, PermutationTypes type)
|
---|
| 102 | : base(name) {
|
---|
[11593] | 103 | lengthParameter = new FixedValueParameter<IntValue>(Name + ".Length", new IntValue(length));
|
---|
| 104 | permutationTypeParameter = new FixedValueParameter<PermutationType>(Name + ".Type", new PermutationType(type));
|
---|
| 105 | Parameters.Add(lengthParameter);
|
---|
| 106 | Parameters.Add(permutationTypeParameter);
|
---|
[11587] | 107 |
|
---|
| 108 | SolutionCreator = new RandomPermutationCreator();
|
---|
[11575] | 109 | RegisterParameterEvents();
|
---|
| 110 | DiscoverOperators();
|
---|
[11484] | 111 | }
|
---|
| 112 |
|
---|
[11575] | 113 | private void OnLengthParameterChanged() {
|
---|
[11588] | 114 | RegisterLengthParameterEvents();
|
---|
[11575] | 115 | ConfigureOperators(Operators);
|
---|
[11484] | 116 | }
|
---|
| 117 |
|
---|
[11575] | 118 | private void OnPermutationTypeParameterChanged() {
|
---|
[11588] | 119 | RegisterPermutationTypeParameterEvents();
|
---|
[11575] | 120 | ConfigureOperators(Operators);
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | private void RegisterParameterEvents() {
|
---|
| 124 | RegisterLengthParameterEvents();
|
---|
| 125 | RegisterPermutationTypeParameterEvents();
|
---|
| 126 | }
|
---|
| 127 | private void RegisterLengthParameterEvents() {
|
---|
| 128 | LengthParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators);
|
---|
| 129 | }
|
---|
| 130 | private void RegisterPermutationTypeParameterEvents() {
|
---|
| 131 | PermutationTypeParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators);
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | #region Operator Discovery
|
---|
| 135 | private static readonly IEnumerable<Type> encodingSpecificOperatorTypes;
|
---|
| 136 | static PermutationEncoding() {
|
---|
| 137 | encodingSpecificOperatorTypes = new List<Type>() {
|
---|
| 138 | typeof (IPermutationOperator),
|
---|
| 139 | typeof (IPermutationCreator),
|
---|
| 140 | typeof (IPermutationCrossover),
|
---|
| 141 | typeof (IPermutationManipulator),
|
---|
| 142 | typeof (IPermutationMultiNeighborhoodShakingOperator),
|
---|
| 143 | typeof (IPermutationMoveOperator),
|
---|
| 144 | typeof (IPermutationInversionMoveOperator),
|
---|
| 145 | typeof (IPermutationScrambleMoveOperator),
|
---|
| 146 | typeof (IPermutationSwap2MoveOperator),
|
---|
| 147 | typeof (IPermutationTranslocationMoveOperator)
|
---|
| 148 | };
|
---|
| 149 | }
|
---|
| 150 | private void DiscoverOperators() {
|
---|
[11773] | 151 | var assembly = typeof(IPermutationOperator).Assembly;
|
---|
| 152 | var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, assembly, true, false, false);
|
---|
[11575] | 153 | var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t));
|
---|
[11587] | 154 | var newOperators = operators.Except(Operators, new TypeEqualityComparer<IOperator>()).ToList();
|
---|
[11575] | 155 |
|
---|
| 156 | ConfigureOperators(newOperators);
|
---|
[11587] | 157 | foreach (var @operator in newOperators)
|
---|
[11952] | 158 | AddOperator(@operator);
|
---|
[11575] | 159 | }
|
---|
| 160 | #endregion
|
---|
| 161 |
|
---|
| 162 | public override void ConfigureOperators(IEnumerable<IOperator> operators) {
|
---|
[11746] | 163 | ConfigureCreators(operators.OfType<IPermutationCreator>());
|
---|
| 164 | ConfigureCrossovers(operators.OfType<IPermutationCrossover>());
|
---|
| 165 | ConfigureManipulators(operators.OfType<IPermutationManipulator>());
|
---|
| 166 | ConfigureShakingOperators(operators.OfType<IPermutationMultiNeighborhoodShakingOperator>());
|
---|
| 167 | ConfigureMoveOperators(operators.OfType<IPermutationMoveOperator>());
|
---|
| 168 | ConfigureInversionMoveOperators(operators.OfType<IPermutationInversionMoveOperator>());
|
---|
| 169 | ConfigureScrambleMoveOperators(operators.OfType<IPermutationScrambleMoveOperator>());
|
---|
| 170 | ConfigureSwap2MoveOperators(operators.OfType<IPermutationSwap2MoveOperator>());
|
---|
| 171 | ConfigureTranslocationMoveOperators(operators.OfType<IPermutationTranslocationMoveOperator>());
|
---|
[11575] | 172 | }
|
---|
| 173 |
|
---|
| 174 | #region specific operator wiring
|
---|
| 175 | private void ConfigureCreators(IEnumerable<IPermutationCreator> creators) {
|
---|
| 176 | foreach (var creator in creators) {
|
---|
| 177 | creator.LengthParameter.ActualName = LengthParameter.Name;
|
---|
| 178 | creator.PermutationParameter.ActualName = Name;
|
---|
| 179 | creator.PermutationTypeParameter.Value.Value = Type;
|
---|
| 180 | }
|
---|
| 181 | }
|
---|
| 182 | private void ConfigureCrossovers(IEnumerable<IPermutationCrossover> crossovers) {
|
---|
| 183 | foreach (var crossover in crossovers) {
|
---|
| 184 | crossover.ChildParameter.ActualName = Name;
|
---|
| 185 | crossover.ParentsParameter.ActualName = Name;
|
---|
| 186 | }
|
---|
| 187 | }
|
---|
| 188 | private void ConfigureManipulators(IEnumerable<IPermutationManipulator> manipulators) {
|
---|
| 189 | foreach (var manipulator in manipulators) {
|
---|
| 190 | manipulator.PermutationParameter.ActualName = Name;
|
---|
| 191 | }
|
---|
| 192 | }
|
---|
| 193 | private void ConfigureShakingOperators(IEnumerable<IPermutationMultiNeighborhoodShakingOperator> shakingOperators) {
|
---|
| 194 | foreach (var shakingOperator in shakingOperators) {
|
---|
| 195 | shakingOperator.PermutationParameter.ActualName = Name;
|
---|
| 196 | }
|
---|
| 197 | }
|
---|
| 198 | private void ConfigureMoveOperators(IEnumerable<IPermutationMoveOperator> moveOperators) {
|
---|
| 199 | foreach (var moveOperator in moveOperators) {
|
---|
| 200 | moveOperator.PermutationParameter.ActualName = Name;
|
---|
| 201 | }
|
---|
| 202 | }
|
---|
| 203 | private void ConfigureInversionMoveOperators(IEnumerable<IPermutationInversionMoveOperator> inversionMoveOperators) {
|
---|
| 204 | foreach (var inversionMoveOperator in inversionMoveOperators) {
|
---|
[11593] | 205 | inversionMoveOperator.InversionMoveParameter.ActualName = Name + ".InversionMove";
|
---|
[11575] | 206 | }
|
---|
| 207 | }
|
---|
| 208 | private void ConfigureScrambleMoveOperators(IEnumerable<IPermutationScrambleMoveOperator> scrambleMoveOperators) {
|
---|
| 209 | foreach (var scrambleMoveOperator in scrambleMoveOperators) {
|
---|
[12015] | 210 | scrambleMoveOperator.ScrambleMoveParameter.ActualName = Name + ".ScrambleMove";
|
---|
[11575] | 211 | }
|
---|
| 212 | }
|
---|
| 213 | private void ConfigureSwap2MoveOperators(IEnumerable<IPermutationSwap2MoveOperator> swap2MoveOperators) {
|
---|
| 214 | foreach (var swap2MoveOperator in swap2MoveOperators) {
|
---|
[11593] | 215 | swap2MoveOperator.Swap2MoveParameter.ActualName = Name + ".Swap2Move";
|
---|
[11575] | 216 | }
|
---|
| 217 | }
|
---|
| 218 | private void ConfigureTranslocationMoveOperators(IEnumerable<IPermutationTranslocationMoveOperator> translocationMoveOperators) {
|
---|
| 219 | foreach (var translocationMoveOperator in translocationMoveOperators) {
|
---|
[11593] | 220 | translocationMoveOperator.TranslocationMoveParameter.ActualName = Name + ".TranslocationMove";
|
---|
[11575] | 221 | }
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | #endregion
|
---|
[11484] | 225 | }
|
---|
[11949] | 226 |
|
---|
| 227 | public static class IndividualExtensionMethods {
|
---|
| 228 | public static Permutation Permutation(this Individual individual) {
|
---|
| 229 | var encoding = individual.GetEncoding<PermutationEncoding>();
|
---|
| 230 | return individual.Permutation(encoding.Name);
|
---|
| 231 | }
|
---|
| 232 | public static Permutation Permutation(this Individual individual, string name) {
|
---|
| 233 | return (Permutation)individual[name];
|
---|
| 234 | }
|
---|
| 235 | }
|
---|
[11484] | 236 | }
|
---|