[11484] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15584] | 3 | * Copyright (C) 2002-2018 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 |
|
---|
[11553] | 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;
|
---|
[11553] | 29 | using HeuristicLab.Parameters;
|
---|
[11484] | 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[11553] | 31 | using HeuristicLab.PluginInfrastructure;
|
---|
[11484] | 32 |
|
---|
[11949] | 33 | namespace HeuristicLab.Encodings.BinaryVectorEncoding {
|
---|
[11885] | 34 | [Item("BinaryVectorEncoding", "Describes a binary vector encoding.")]
|
---|
[11484] | 35 | [StorableClass]
|
---|
[11885] | 36 | public sealed class BinaryVectorEncoding : Encoding<IBinaryVectorCreator> {
|
---|
[11553] | 37 | #region Encoding Parameters
|
---|
[11598] | 38 | [Storable]
|
---|
[11553] | 39 | private IFixedValueParameter<IntValue> lengthParameter;
|
---|
| 40 | public IFixedValueParameter<IntValue> LengthParameter {
|
---|
| 41 | get { return lengthParameter; }
|
---|
[11484] | 42 | set {
|
---|
[11553] | 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.");
|
---|
[11553] | 45 | if (lengthParameter == value) return;
|
---|
[11588] | 46 |
|
---|
| 47 | if (lengthParameter != null) Parameters.Remove(lengthParameter);
|
---|
[11553] | 48 | lengthParameter = value;
|
---|
[11588] | 49 | Parameters.Add(lengthParameter);
|
---|
[11553] | 50 | OnLengthParameterChanged();
|
---|
[11484] | 51 | }
|
---|
| 52 | }
|
---|
[11553] | 53 | #endregion
|
---|
[11484] | 54 |
|
---|
[11949] | 55 |
|
---|
[11553] | 56 | public int Length {
|
---|
| 57 | get { return LengthParameter.Value.Value; }
|
---|
| 58 | set { LengthParameter.Value.Value = value; }
|
---|
| 59 | }
|
---|
| 60 |
|
---|
[11484] | 61 | [StorableConstructor]
|
---|
[11885] | 62 | private BinaryVectorEncoding(bool deserializing) : base(deserializing) { }
|
---|
[11553] | 63 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 64 | private void AfterDeserialization() {
|
---|
| 65 | RegisterParameterEvents();
|
---|
| 66 | DiscoverOperators();
|
---|
| 67 | }
|
---|
[11885] | 68 | public override IDeepCloneable Clone(Cloner cloner) { return new BinaryVectorEncoding(this, cloner); }
|
---|
| 69 | private BinaryVectorEncoding(BinaryVectorEncoding original, Cloner cloner)
|
---|
[11484] | 70 | : base(original, cloner) {
|
---|
[11553] | 71 | lengthParameter = cloner.Clone(original.lengthParameter);
|
---|
| 72 | RegisterParameterEvents();
|
---|
[11484] | 73 | }
|
---|
[11739] | 74 |
|
---|
[11885] | 75 | public BinaryVectorEncoding() : this("BinaryVector", 10) { }
|
---|
[11892] | 76 | public BinaryVectorEncoding(string name) : this(name, 10) { }
|
---|
[11885] | 77 | public BinaryVectorEncoding(int length) : this("BinaryVector", length) { }
|
---|
| 78 | public BinaryVectorEncoding(string name, int length)
|
---|
[11484] | 79 | : base(name) {
|
---|
[11593] | 80 | lengthParameter = new FixedValueParameter<IntValue>(Name + ".Length", new IntValue(length));
|
---|
| 81 | Parameters.Add(lengthParameter);
|
---|
| 82 |
|
---|
[11587] | 83 | SolutionCreator = new RandomBinaryVectorCreator();
|
---|
[11582] | 84 | RegisterParameterEvents();
|
---|
| 85 | DiscoverOperators();
|
---|
[11484] | 86 | }
|
---|
| 87 |
|
---|
[11553] | 88 | private void OnLengthParameterChanged() {
|
---|
| 89 | RegisterLengthParameterEvents();
|
---|
| 90 | ConfigureOperators(Operators);
|
---|
[11484] | 91 | }
|
---|
[11553] | 92 | private void RegisterParameterEvents() {
|
---|
| 93 | RegisterLengthParameterEvents();
|
---|
| 94 | }
|
---|
| 95 | private void RegisterLengthParameterEvents() {
|
---|
| 96 | LengthParameter.ValueChanged += (o, s) => ConfigureOperators(Operators);
|
---|
| 97 | LengthParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators);
|
---|
| 98 | }
|
---|
[11484] | 99 |
|
---|
[11553] | 100 | #region Operator Discovery
|
---|
| 101 | private static readonly IEnumerable<Type> encodingSpecificOperatorTypes;
|
---|
[11885] | 102 | static BinaryVectorEncoding() {
|
---|
[11553] | 103 | encodingSpecificOperatorTypes = new List<Type>() {
|
---|
| 104 | typeof (IBinaryVectorOperator),
|
---|
| 105 | typeof (IBinaryVectorCreator),
|
---|
| 106 | typeof (IBinaryVectorCrossover),
|
---|
| 107 | typeof (IBinaryVectorManipulator),
|
---|
| 108 | typeof (IBinaryVectorMoveOperator),
|
---|
| 109 | typeof (IBinaryVectorMultiNeighborhoodShakingOperator),
|
---|
| 110 | };
|
---|
| 111 | }
|
---|
| 112 | private void DiscoverOperators() {
|
---|
[11773] | 113 | var assembly = typeof(IBinaryVectorOperator).Assembly;
|
---|
| 114 | var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, assembly, true, false, false);
|
---|
[11553] | 115 | var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t));
|
---|
[11587] | 116 | var newOperators = operators.Except(Operators, new TypeEqualityComparer<IOperator>()).ToList();
|
---|
[11553] | 117 |
|
---|
| 118 | ConfigureOperators(newOperators);
|
---|
[11587] | 119 | foreach (var @operator in newOperators)
|
---|
[11952] | 120 | AddOperator(@operator);
|
---|
[11553] | 121 | }
|
---|
| 122 | #endregion
|
---|
| 123 |
|
---|
[11559] | 124 | public override void ConfigureOperators(IEnumerable<IOperator> operators) {
|
---|
[11553] | 125 | ConfigureCreators(operators.OfType<IBinaryVectorCreator>());
|
---|
| 126 | ConfigureCrossovers(operators.OfType<IBinaryVectorCrossover>());
|
---|
| 127 | ConfigureManipulators(operators.OfType<IBinaryVectorManipulator>());
|
---|
| 128 | ConfigureMoveOperators(operators.OfType<IBinaryVectorMoveOperator>());
|
---|
[11575] | 129 | ConfigureBitFlipMoveOperators(operators.OfType<IOneBitflipMoveOperator>());
|
---|
[11553] | 130 | ConfigureShakingOperators(operators.OfType<IBinaryVectorMultiNeighborhoodShakingOperator>());
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | #region Specific Operator Wiring
|
---|
| 134 | private void ConfigureCreators(IEnumerable<IBinaryVectorCreator> creators) {
|
---|
| 135 | foreach (var creator in creators) {
|
---|
| 136 | creator.BinaryVectorParameter.ActualName = Name;
|
---|
| 137 | creator.LengthParameter.ActualName = LengthParameter.Name;
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 | private void ConfigureCrossovers(IEnumerable<IBinaryVectorCrossover> crossovers) {
|
---|
| 141 | foreach (var crossover in crossovers) {
|
---|
| 142 | crossover.ParentsParameter.ActualName = Name;
|
---|
| 143 | crossover.ChildParameter.ActualName = Name;
|
---|
| 144 | }
|
---|
| 145 | }
|
---|
| 146 | private void ConfigureManipulators(IEnumerable<IBinaryVectorManipulator> manipulators) {
|
---|
| 147 | foreach (var manipulator in manipulators) {
|
---|
| 148 | manipulator.BinaryVectorParameter.ActualName = Name;
|
---|
| 149 | }
|
---|
| 150 | }
|
---|
| 151 | private void ConfigureMoveOperators(IEnumerable<IBinaryVectorMoveOperator> moveOperators) {
|
---|
| 152 | foreach (var moveOperator in moveOperators) {
|
---|
| 153 | moveOperator.BinaryVectorParameter.ActualName = Name;
|
---|
| 154 | }
|
---|
| 155 | }
|
---|
[11575] | 156 | private void ConfigureBitFlipMoveOperators(IEnumerable<IOneBitflipMoveOperator> oneBitflipMoveOperators) {
|
---|
| 157 | foreach (var oneBitFlipMoveOperator in oneBitflipMoveOperators) {
|
---|
| 158 | oneBitFlipMoveOperator.OneBitflipMoveParameter.ActualName = Name + "_OneBitFlipMove";
|
---|
| 159 | }
|
---|
| 160 | }
|
---|
[11553] | 161 | private void ConfigureShakingOperators(IEnumerable<IBinaryVectorMultiNeighborhoodShakingOperator> shakingOperators) {
|
---|
| 162 | foreach (var shakingOperator in shakingOperators) {
|
---|
| 163 | shakingOperator.BinaryVectorParameter.ActualName = Name;
|
---|
| 164 | }
|
---|
| 165 | }
|
---|
| 166 | #endregion
|
---|
[11484] | 167 | }
|
---|
[11949] | 168 |
|
---|
| 169 | public static class IndividualExtensionMethods {
|
---|
| 170 | public static BinaryVector BinaryVector(this Individual individual) {
|
---|
| 171 | var encoding = individual.GetEncoding<BinaryVectorEncoding>();
|
---|
| 172 | return individual.BinaryVector(encoding.Name);
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | public static BinaryVector BinaryVector(this Individual individual, string name) {
|
---|
| 176 | return (BinaryVector)individual[name];
|
---|
| 177 | }
|
---|
| 178 | }
|
---|
[11484] | 179 | }
|
---|