[12285] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16956] | 3 | * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[12285] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
| 28 | using HeuristicLab.Optimization;
|
---|
| 29 | using HeuristicLab.Parameters;
|
---|
[16956] | 30 | using HEAL.Attic;
|
---|
[12285] | 31 | using HeuristicLab.PluginInfrastructure;
|
---|
| 32 |
|
---|
| 33 | namespace HeuristicLab.Encodings.LinearLinkageEncoding {
|
---|
[12288] | 34 | [Item("Linear Linkage Encoding", "Describes a linear linkage (LLE) encoding.")]
|
---|
[16956] | 35 | [StorableType("7AE11F39-E6BD-4FC7-8112-0A5EDCBFBDB6")]
|
---|
[12285] | 36 | public sealed class LinearLinkageEncoding : Encoding<ILinearLinkageCreator> {
|
---|
| 37 | #region encoding parameters
|
---|
| 38 | [Storable]
|
---|
| 39 | private IFixedValueParameter<IntValue> lengthParameter;
|
---|
| 40 | public IFixedValueParameter<IntValue> LengthParameter {
|
---|
| 41 | get { return lengthParameter; }
|
---|
| 42 | set {
|
---|
| 43 | if (value == null) throw new ArgumentNullException("Length parameter must not be null.");
|
---|
| 44 | if (value.Value == null) throw new ArgumentNullException("Length parameter value must not be null.");
|
---|
| 45 | if (lengthParameter == value) return;
|
---|
| 46 |
|
---|
| 47 | if (lengthParameter != null) Parameters.Remove(lengthParameter);
|
---|
| 48 | lengthParameter = value;
|
---|
| 49 | Parameters.Add(lengthParameter);
|
---|
| 50 | OnLengthParameterChanged();
|
---|
| 51 | }
|
---|
| 52 | }
|
---|
| 53 | #endregion
|
---|
| 54 |
|
---|
| 55 | public int Length {
|
---|
| 56 | get { return LengthParameter.Value.Value; }
|
---|
| 57 | set { LengthParameter.Value.Value = value; }
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | [StorableConstructor]
|
---|
[16956] | 61 | private LinearLinkageEncoding(StorableConstructorFlag _) : base(_) { }
|
---|
[12285] | 62 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 63 | private void AfterDeserialization() {
|
---|
| 64 | RegisterParameterEvents();
|
---|
| 65 | DiscoverOperators();
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | public override IDeepCloneable Clone(Cloner cloner) { return new LinearLinkageEncoding(this, cloner); }
|
---|
| 69 | private LinearLinkageEncoding(LinearLinkageEncoding original, Cloner cloner)
|
---|
| 70 | : base(original, cloner) {
|
---|
| 71 | lengthParameter = cloner.Clone(original.lengthParameter);
|
---|
| 72 | RegisterParameterEvents();
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 |
|
---|
| 76 | public LinearLinkageEncoding() : this("LLE", 10) { }
|
---|
| 77 | public LinearLinkageEncoding(string name) : this(name, 10) { }
|
---|
| 78 | public LinearLinkageEncoding(int length) : this("LLE", length) { }
|
---|
| 79 | public LinearLinkageEncoding(string name, int length)
|
---|
| 80 | : base(name) {
|
---|
| 81 | lengthParameter = new FixedValueParameter<IntValue>(Name + ".Length", new IntValue(length));
|
---|
| 82 | Parameters.Add(lengthParameter);
|
---|
| 83 |
|
---|
| 84 | SolutionCreator = new RandomLinearLinkageCreator();
|
---|
| 85 | RegisterParameterEvents();
|
---|
| 86 | DiscoverOperators();
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | private void OnLengthParameterChanged() {
|
---|
| 90 | RegisterLengthParameterEvents();
|
---|
| 91 | ConfigureOperators(Operators);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | private void RegisterParameterEvents() {
|
---|
| 95 | RegisterLengthParameterEvents();
|
---|
| 96 | }
|
---|
| 97 | private void RegisterLengthParameterEvents() {
|
---|
| 98 | LengthParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators);
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | #region Operator Discovery
|
---|
| 102 | private static readonly IEnumerable<Type> encodingSpecificOperatorTypes;
|
---|
| 103 | static LinearLinkageEncoding() {
|
---|
| 104 | encodingSpecificOperatorTypes = new List<Type>() {
|
---|
| 105 | typeof (ILinearLinkageOperator),
|
---|
| 106 | typeof (ILinearLinkageCreator),
|
---|
| 107 | typeof (ILinearLinkageCrossover),
|
---|
| 108 | typeof (ILinearLinkageManipulator),
|
---|
[12643] | 109 | typeof (ILinearLinkageShakingOperator),
|
---|
| 110 | typeof (ILinearLinkageMoveOperator)
|
---|
[12285] | 111 | };
|
---|
| 112 | }
|
---|
| 113 | private void DiscoverOperators() {
|
---|
| 114 | var assembly = typeof(ILinearLinkageOperator).Assembly;
|
---|
| 115 | var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, assembly, true, false, false);
|
---|
| 116 | var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t));
|
---|
| 117 | var newOperators = operators.Except(Operators, new TypeEqualityComparer<IOperator>()).ToList();
|
---|
| 118 |
|
---|
| 119 | ConfigureOperators(newOperators);
|
---|
| 120 | foreach (var @operator in newOperators)
|
---|
| 121 | AddOperator(@operator);
|
---|
| 122 | }
|
---|
| 123 | #endregion
|
---|
| 124 |
|
---|
| 125 | public override void ConfigureOperators(IEnumerable<IOperator> operators) {
|
---|
| 126 | ConfigureCreators(operators.OfType<ILinearLinkageCreator>());
|
---|
| 127 | ConfigureCrossovers(operators.OfType<ILinearLinkageCrossover>());
|
---|
| 128 | ConfigureManipulators(operators.OfType<ILinearLinkageManipulator>());
|
---|
| 129 | ConfigureShakingOperators(operators.OfType<ILinearLinkageShakingOperator>());
|
---|
[12643] | 130 | ConfigureMoveOperators(operators.OfType<ILinearLinkageMoveOperator>());
|
---|
| 131 | ConfigureSwap2MoveOperators(operators.OfType<ILinearLinkageSwap2MoveOperator>());
|
---|
[12285] | 132 | }
|
---|
| 133 |
|
---|
| 134 | #region specific operator wiring
|
---|
| 135 | private void ConfigureCreators(IEnumerable<ILinearLinkageCreator> creators) {
|
---|
| 136 | foreach (var creator in creators) {
|
---|
| 137 | creator.LengthParameter.ActualName = LengthParameter.Name;
|
---|
| 138 | creator.LLEParameter.ActualName = Name;
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
| 141 | private void ConfigureCrossovers(IEnumerable<ILinearLinkageCrossover> crossovers) {
|
---|
| 142 | foreach (var crossover in crossovers) {
|
---|
| 143 | crossover.ChildParameter.ActualName = Name;
|
---|
| 144 | crossover.ParentsParameter.ActualName = Name;
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
| 147 | private void ConfigureManipulators(IEnumerable<ILinearLinkageManipulator> manipulators) {
|
---|
| 148 | foreach (var manipulator in manipulators) {
|
---|
| 149 | manipulator.LLEParameter.ActualName = Name;
|
---|
| 150 | }
|
---|
| 151 | }
|
---|
| 152 | private void ConfigureShakingOperators(IEnumerable<ILinearLinkageShakingOperator> shakingOperators) {
|
---|
| 153 | foreach (var shakingOperator in shakingOperators) {
|
---|
| 154 | shakingOperator.LLEParameter.ActualName = Name;
|
---|
| 155 | }
|
---|
| 156 | }
|
---|
[12643] | 157 | private void ConfigureMoveOperators(IEnumerable<ILinearLinkageMoveOperator> moveOperators) {
|
---|
| 158 | foreach (var moveOperator in moveOperators) {
|
---|
| 159 | moveOperator.LLEParameter.ActualName = Name;
|
---|
| 160 | }
|
---|
| 161 | }
|
---|
| 162 | private void ConfigureSwap2MoveOperators(IEnumerable<ILinearLinkageSwap2MoveOperator> swap2MoveOperators) {
|
---|
| 163 | foreach (var swap2MoveOperator in swap2MoveOperators) {
|
---|
| 164 | swap2MoveOperator.Swap2MoveParameter.ActualName = Name + ".Swap2Move";
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
[12285] | 167 | #endregion
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | public static class IndividualExtensionMethods {
|
---|
| 171 | public static LinearLinkage LinearLinkage(this Individual individual) {
|
---|
| 172 | var encoding = individual.GetEncoding<LinearLinkageEncoding>();
|
---|
| 173 | return individual.LinearLinkage(encoding.Name);
|
---|
| 174 | }
|
---|
| 175 | public static LinearLinkage LinearLinkage(this Individual individual, string name) {
|
---|
| 176 | return (LinearLinkage)individual[name];
|
---|
| 177 | }
|
---|
| 178 | }
|
---|
| 179 | }
|
---|