[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
|
---|
| 38 | private IFixedValueParameter<IntValue> lengthParameter;
|
---|
| 39 | public IFixedValueParameter<IntValue> LengthParameter {
|
---|
| 40 | get { return lengthParameter; }
|
---|
[11484] | 41 | set {
|
---|
[11553] | 42 | if (value == null) throw new ArgumentNullException("Length parameter must not be null.");
|
---|
[11588] | 43 | if (value.Value == null) throw new ArgumentNullException("Length parameter value must not be null.");
|
---|
[11553] | 44 | if (lengthParameter == value) return;
|
---|
[11588] | 45 |
|
---|
| 46 | if (lengthParameter != null) Parameters.Remove(lengthParameter);
|
---|
[11553] | 47 | lengthParameter = value;
|
---|
[11588] | 48 | Parameters.Add(lengthParameter);
|
---|
[11553] | 49 | OnLengthParameterChanged();
|
---|
[11484] | 50 | }
|
---|
| 51 | }
|
---|
[11553] | 52 | #endregion
|
---|
[11484] | 53 |
|
---|
[11553] | 54 | public int Length {
|
---|
| 55 | get { return LengthParameter.Value.Value; }
|
---|
| 56 | set { LengthParameter.Value.Value = value; }
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[11484] | 59 | [StorableConstructor]
|
---|
[11553] | 60 | private BinaryEncoding(bool deserializing) : base(deserializing) { }
|
---|
| 61 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 62 | private void AfterDeserialization() {
|
---|
| 63 | RegisterParameterEvents();
|
---|
| 64 | DiscoverOperators();
|
---|
| 65 | }
|
---|
| 66 | public override IDeepCloneable Clone(Cloner cloner) { return new BinaryEncoding(this, cloner); }
|
---|
| 67 | private BinaryEncoding(BinaryEncoding original, Cloner cloner)
|
---|
[11484] | 68 | : base(original, cloner) {
|
---|
[11553] | 69 | lengthParameter = cloner.Clone(original.lengthParameter);
|
---|
| 70 | RegisterParameterEvents();
|
---|
[11484] | 71 | }
|
---|
| 72 | public BinaryEncoding(string name, int length)
|
---|
| 73 | : base(name) {
|
---|
[11588] | 74 | LengthParameter = new FixedValueParameter<IntValue>(Name + "Length", new IntValue(length));
|
---|
[11587] | 75 | SolutionCreator = new RandomBinaryVectorCreator();
|
---|
[11582] | 76 | RegisterParameterEvents();
|
---|
| 77 | DiscoverOperators();
|
---|
[11484] | 78 | }
|
---|
| 79 |
|
---|
[11553] | 80 | private void OnLengthParameterChanged() {
|
---|
| 81 | RegisterLengthParameterEvents();
|
---|
| 82 | ConfigureOperators(Operators);
|
---|
[11484] | 83 | }
|
---|
[11553] | 84 | private void RegisterParameterEvents() {
|
---|
| 85 | RegisterLengthParameterEvents();
|
---|
| 86 | }
|
---|
| 87 | private void RegisterLengthParameterEvents() {
|
---|
| 88 | LengthParameter.ValueChanged += (o, s) => ConfigureOperators(Operators);
|
---|
| 89 | LengthParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators);
|
---|
| 90 | }
|
---|
[11484] | 91 |
|
---|
[11553] | 92 | #region Operator Discovery
|
---|
| 93 | private static readonly IEnumerable<Type> encodingSpecificOperatorTypes;
|
---|
| 94 | static BinaryEncoding() {
|
---|
| 95 | encodingSpecificOperatorTypes = new List<Type>() {
|
---|
| 96 | typeof (IBinaryVectorOperator),
|
---|
| 97 | typeof (IBinaryVectorCreator),
|
---|
| 98 | typeof (IBinaryVectorCrossover),
|
---|
| 99 | typeof (IBinaryVectorManipulator),
|
---|
| 100 | typeof (IBinaryVectorMoveOperator),
|
---|
| 101 | typeof (IBinaryVectorMultiNeighborhoodShakingOperator),
|
---|
| 102 | };
|
---|
| 103 | }
|
---|
| 104 | private void DiscoverOperators() {
|
---|
| 105 | var pluginDescription = ApplicationManager.Manager.GetDeclaringPlugin(typeof(IBinaryVectorOperator));
|
---|
| 106 | var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, pluginDescription, true, false, false);
|
---|
| 107 | var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t));
|
---|
[11587] | 108 | var newOperators = operators.Except(Operators, new TypeEqualityComparer<IOperator>()).ToList();
|
---|
[11553] | 109 |
|
---|
| 110 | ConfigureOperators(newOperators);
|
---|
[11587] | 111 | foreach (var @operator in newOperators)
|
---|
| 112 | encodingOperators.Add(@operator);
|
---|
[11553] | 113 | }
|
---|
| 114 | #endregion
|
---|
| 115 |
|
---|
[11559] | 116 | public override void ConfigureOperators(IEnumerable<IOperator> operators) {
|
---|
[11553] | 117 | ConfigureCreators(operators.OfType<IBinaryVectorCreator>());
|
---|
| 118 | ConfigureCrossovers(operators.OfType<IBinaryVectorCrossover>());
|
---|
| 119 | ConfigureManipulators(operators.OfType<IBinaryVectorManipulator>());
|
---|
| 120 | ConfigureMoveOperators(operators.OfType<IBinaryVectorMoveOperator>());
|
---|
[11575] | 121 | ConfigureBitFlipMoveOperators(operators.OfType<IOneBitflipMoveOperator>());
|
---|
[11553] | 122 | ConfigureShakingOperators(operators.OfType<IBinaryVectorMultiNeighborhoodShakingOperator>());
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | #region Specific Operator Wiring
|
---|
| 126 | private void ConfigureCreators(IEnumerable<IBinaryVectorCreator> creators) {
|
---|
| 127 | foreach (var creator in creators) {
|
---|
| 128 | creator.BinaryVectorParameter.ActualName = Name;
|
---|
| 129 | creator.LengthParameter.ActualName = LengthParameter.Name;
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 | private void ConfigureCrossovers(IEnumerable<IBinaryVectorCrossover> crossovers) {
|
---|
| 133 | foreach (var crossover in crossovers) {
|
---|
| 134 | crossover.ParentsParameter.ActualName = Name;
|
---|
| 135 | crossover.ChildParameter.ActualName = Name;
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 | private void ConfigureManipulators(IEnumerable<IBinaryVectorManipulator> manipulators) {
|
---|
| 139 | foreach (var manipulator in manipulators) {
|
---|
| 140 | manipulator.BinaryVectorParameter.ActualName = Name;
|
---|
| 141 | }
|
---|
| 142 | }
|
---|
| 143 | private void ConfigureMoveOperators(IEnumerable<IBinaryVectorMoveOperator> moveOperators) {
|
---|
| 144 | foreach (var moveOperator in moveOperators) {
|
---|
| 145 | moveOperator.BinaryVectorParameter.ActualName = Name;
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
[11575] | 148 | private void ConfigureBitFlipMoveOperators(IEnumerable<IOneBitflipMoveOperator> oneBitflipMoveOperators) {
|
---|
| 149 | foreach (var oneBitFlipMoveOperator in oneBitflipMoveOperators) {
|
---|
| 150 | oneBitFlipMoveOperator.OneBitflipMoveParameter.ActualName = Name + "_OneBitFlipMove";
|
---|
| 151 | }
|
---|
| 152 | }
|
---|
[11553] | 153 | private void ConfigureShakingOperators(IEnumerable<IBinaryVectorMultiNeighborhoodShakingOperator> shakingOperators) {
|
---|
| 154 | foreach (var shakingOperator in shakingOperators) {
|
---|
| 155 | shakingOperator.BinaryVectorParameter.ActualName = Name;
|
---|
| 156 | }
|
---|
| 157 | }
|
---|
| 158 | #endregion
|
---|
[11484] | 159 | }
|
---|
| 160 | }
|
---|