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