[11484] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17226] | 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 |
|
---|
[11553] | 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
[17544] | 24 | using System.ComponentModel;
|
---|
[11553] | 25 | using System.Linq;
|
---|
[17544] | 26 | using HEAL.Attic;
|
---|
[11484] | 27 | using HeuristicLab.Common;
|
---|
| 28 | using HeuristicLab.Core;
|
---|
| 29 | using HeuristicLab.Data;
|
---|
[11949] | 30 | using HeuristicLab.Optimization;
|
---|
[11553] | 31 | using HeuristicLab.Parameters;
|
---|
| 32 | using HeuristicLab.PluginInfrastructure;
|
---|
[11484] | 33 |
|
---|
[11949] | 34 | namespace HeuristicLab.Encodings.BinaryVectorEncoding {
|
---|
[11885] | 35 | [Item("BinaryVectorEncoding", "Describes a binary vector encoding.")]
|
---|
[16723] | 36 | [StorableType("889C5E1A-3FBF-4AB3-AB2E-199A781503B5")]
|
---|
[17544] | 37 | public sealed class BinaryVectorEncoding : Encoding<BinaryVector>, INotifyPropertyChanged {
|
---|
[11553] | 38 | #region Encoding Parameters
|
---|
[17544] | 39 | [Storable] public IValueParameter<IntValue> LengthParameter { get; private set; }
|
---|
[11553] | 40 | #endregion
|
---|
[11484] | 41 |
|
---|
[11553] | 42 | public int Length {
|
---|
| 43 | get { return LengthParameter.Value.Value; }
|
---|
[17544] | 44 | set {
|
---|
| 45 | if (Length == value) return;
|
---|
| 46 | LengthParameter.ForceValue(new IntValue(value, @readonly: LengthParameter.Value.ReadOnly));
|
---|
| 47 | }
|
---|
[11553] | 48 | }
|
---|
| 49 |
|
---|
[11484] | 50 | [StorableConstructor]
|
---|
[16723] | 51 | private BinaryVectorEncoding(StorableConstructorFlag _) : base(_) { }
|
---|
[11553] | 52 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 53 | private void AfterDeserialization() {
|
---|
| 54 | DiscoverOperators();
|
---|
[17544] | 55 | RegisterEventHandlers();
|
---|
[11553] | 56 | }
|
---|
[11885] | 57 | public override IDeepCloneable Clone(Cloner cloner) { return new BinaryVectorEncoding(this, cloner); }
|
---|
| 58 | private BinaryVectorEncoding(BinaryVectorEncoding original, Cloner cloner)
|
---|
[11484] | 59 | : base(original, cloner) {
|
---|
[17544] | 60 | LengthParameter = cloner.Clone(original.LengthParameter);
|
---|
| 61 | RegisterEventHandlers();
|
---|
[11484] | 62 | }
|
---|
[11739] | 63 |
|
---|
[11885] | 64 | public BinaryVectorEncoding() : this("BinaryVector", 10) { }
|
---|
[11892] | 65 | public BinaryVectorEncoding(string name) : this(name, 10) { }
|
---|
[11885] | 66 | public BinaryVectorEncoding(int length) : this("BinaryVector", length) { }
|
---|
| 67 | public BinaryVectorEncoding(string name, int length)
|
---|
[11484] | 68 | : base(name) {
|
---|
[17545] | 69 | Parameters.Add(LengthParameter = new ValueParameter<IntValue>(Name + ".Length", new IntValue(length)) { ReadOnly = true });
|
---|
[17544] | 70 |
|
---|
[11587] | 71 | SolutionCreator = new RandomBinaryVectorCreator();
|
---|
[11582] | 72 | DiscoverOperators();
|
---|
[17544] | 73 |
|
---|
| 74 | RegisterEventHandlers();
|
---|
[11484] | 75 | }
|
---|
| 76 |
|
---|
[17544] | 77 | private void RegisterEventHandlers() {
|
---|
| 78 | LengthParameter.ValueChanged += (_, __) => {
|
---|
| 79 | if (!LengthParameter.Value.ReadOnly) LengthParameter.Value.ValueChanged += (___, ____) => OnPropertyChanged(nameof(Length));
|
---|
| 80 | OnPropertyChanged(nameof(Length));
|
---|
| 81 | };
|
---|
[11484] | 82 | }
|
---|
| 83 |
|
---|
[11553] | 84 | #region Operator Discovery
|
---|
| 85 | private static readonly IEnumerable<Type> encodingSpecificOperatorTypes;
|
---|
[17544] | 86 |
|
---|
[11885] | 87 | static BinaryVectorEncoding() {
|
---|
[11553] | 88 | encodingSpecificOperatorTypes = new List<Type>() {
|
---|
| 89 | typeof (IBinaryVectorOperator),
|
---|
| 90 | typeof (IBinaryVectorCreator),
|
---|
| 91 | typeof (IBinaryVectorCrossover),
|
---|
| 92 | typeof (IBinaryVectorManipulator),
|
---|
| 93 | typeof (IBinaryVectorMoveOperator),
|
---|
| 94 | typeof (IBinaryVectorMultiNeighborhoodShakingOperator),
|
---|
[13404] | 95 | typeof (IBinaryVectorSolutionOperator),
|
---|
| 96 | typeof (IBinaryVectorSolutionsOperator)
|
---|
[11553] | 97 | };
|
---|
| 98 | }
|
---|
| 99 | private void DiscoverOperators() {
|
---|
[11773] | 100 | var assembly = typeof(IBinaryVectorOperator).Assembly;
|
---|
| 101 | var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, assembly, true, false, false);
|
---|
[11553] | 102 | var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t));
|
---|
[11587] | 103 | var newOperators = operators.Except(Operators, new TypeEqualityComparer<IOperator>()).ToList();
|
---|
[11553] | 104 |
|
---|
| 105 | ConfigureOperators(newOperators);
|
---|
[11587] | 106 | foreach (var @operator in newOperators)
|
---|
[11952] | 107 | AddOperator(@operator);
|
---|
[11553] | 108 | }
|
---|
| 109 | #endregion
|
---|
| 110 |
|
---|
[13396] | 111 | public override void ConfigureOperators(IEnumerable<IItem> operators) {
|
---|
[11553] | 112 | ConfigureCreators(operators.OfType<IBinaryVectorCreator>());
|
---|
| 113 | ConfigureCrossovers(operators.OfType<IBinaryVectorCrossover>());
|
---|
| 114 | ConfigureManipulators(operators.OfType<IBinaryVectorManipulator>());
|
---|
| 115 | ConfigureMoveOperators(operators.OfType<IBinaryVectorMoveOperator>());
|
---|
[11575] | 116 | ConfigureBitFlipMoveOperators(operators.OfType<IOneBitflipMoveOperator>());
|
---|
[11553] | 117 | ConfigureShakingOperators(operators.OfType<IBinaryVectorMultiNeighborhoodShakingOperator>());
|
---|
[13404] | 118 | ConfigureSolutionOperators(operators.OfType<IBinaryVectorSolutionOperator>());
|
---|
| 119 | ConfigureSolutionsOperators(operators.OfType<IBinaryVectorSolutionsOperator>());
|
---|
[11553] | 120 | }
|
---|
| 121 |
|
---|
| 122 | #region Specific Operator Wiring
|
---|
| 123 | private void ConfigureCreators(IEnumerable<IBinaryVectorCreator> creators) {
|
---|
| 124 | foreach (var creator in creators) {
|
---|
| 125 | creator.LengthParameter.ActualName = LengthParameter.Name;
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
| 128 | private void ConfigureCrossovers(IEnumerable<IBinaryVectorCrossover> crossovers) {
|
---|
| 129 | foreach (var crossover in crossovers) {
|
---|
| 130 | crossover.ParentsParameter.ActualName = Name;
|
---|
| 131 | crossover.ChildParameter.ActualName = Name;
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 | private void ConfigureManipulators(IEnumerable<IBinaryVectorManipulator> manipulators) {
|
---|
[13404] | 135 | // binary vector manipulators don't contain additional parameters besides the solution parameter
|
---|
[11553] | 136 | }
|
---|
| 137 | private void ConfigureMoveOperators(IEnumerable<IBinaryVectorMoveOperator> moveOperators) {
|
---|
[13404] | 138 | // binary vector move operators don't contain additional parameters besides the solution parameter
|
---|
[11553] | 139 | }
|
---|
[11575] | 140 | private void ConfigureBitFlipMoveOperators(IEnumerable<IOneBitflipMoveOperator> oneBitflipMoveOperators) {
|
---|
| 141 | foreach (var oneBitFlipMoveOperator in oneBitflipMoveOperators) {
|
---|
| 142 | oneBitFlipMoveOperator.OneBitflipMoveParameter.ActualName = Name + "_OneBitFlipMove";
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
[11553] | 145 | private void ConfigureShakingOperators(IEnumerable<IBinaryVectorMultiNeighborhoodShakingOperator> shakingOperators) {
|
---|
[13404] | 146 | // binary vector shaking operators don't contain additional parameters besides the solution parameter
|
---|
[11553] | 147 | }
|
---|
[13404] | 148 | private void ConfigureSolutionOperators(IEnumerable<IBinaryVectorSolutionOperator> solutionOperators) {
|
---|
| 149 | foreach (var solutionOperator in solutionOperators)
|
---|
| 150 | solutionOperator.BinaryVectorParameter.ActualName = Name;
|
---|
| 151 | }
|
---|
| 152 | private void ConfigureSolutionsOperators(IEnumerable<IBinaryVectorSolutionsOperator> solutionsOperators) {
|
---|
| 153 | foreach (var solutionsOperator in solutionsOperators)
|
---|
| 154 | solutionsOperator.BinaryVectorsParameter.ActualName = Name;
|
---|
| 155 | }
|
---|
[11553] | 156 | #endregion
|
---|
[17544] | 157 |
|
---|
| 158 | public event PropertyChangedEventHandler PropertyChanged;
|
---|
| 159 | private void OnPropertyChanged(string property) {
|
---|
| 160 | PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
|
---|
| 161 | }
|
---|
[11484] | 162 | }
|
---|
| 163 | }
|
---|