[12285] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2015 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 |
|
---|
| 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.Encodings.LinearLinkageEncoding.Creators;
|
---|
| 29 | using HeuristicLab.Optimization;
|
---|
| 30 | using HeuristicLab.Parameters;
|
---|
| 31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 32 | using HeuristicLab.PluginInfrastructure;
|
---|
| 33 |
|
---|
| 34 | namespace HeuristicLab.Encodings.LinearLinkageEncoding {
|
---|
[12288] | 35 | [Item("Linear Linkage Encoding", "Describes a linear linkage (LLE) encoding.")]
|
---|
[12285] | 36 | [StorableClass]
|
---|
| 37 | public sealed class LinearLinkageEncoding : Encoding<ILinearLinkageCreator> {
|
---|
| 38 | #region encoding parameters
|
---|
| 39 | [Storable]
|
---|
| 40 | private IFixedValueParameter<IntValue> lengthParameter;
|
---|
| 41 | public IFixedValueParameter<IntValue> LengthParameter {
|
---|
| 42 | get { return lengthParameter; }
|
---|
| 43 | set {
|
---|
| 44 | if (value == null) throw new ArgumentNullException("Length parameter must not be null.");
|
---|
| 45 | if (value.Value == null) throw new ArgumentNullException("Length parameter value must not be null.");
|
---|
| 46 | if (lengthParameter == value) return;
|
---|
| 47 |
|
---|
| 48 | if (lengthParameter != null) Parameters.Remove(lengthParameter);
|
---|
| 49 | lengthParameter = value;
|
---|
| 50 | Parameters.Add(lengthParameter);
|
---|
| 51 | OnLengthParameterChanged();
|
---|
| 52 | }
|
---|
| 53 | }
|
---|
| 54 | #endregion
|
---|
| 55 |
|
---|
| 56 | public int Length {
|
---|
| 57 | get { return LengthParameter.Value.Value; }
|
---|
| 58 | set { LengthParameter.Value.Value = value; }
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | [StorableConstructor]
|
---|
| 62 | private LinearLinkageEncoding(bool deserializing) : base(deserializing) { }
|
---|
| 63 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 64 | private void AfterDeserialization() {
|
---|
| 65 | RegisterParameterEvents();
|
---|
| 66 | DiscoverOperators();
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | public override IDeepCloneable Clone(Cloner cloner) { return new LinearLinkageEncoding(this, cloner); }
|
---|
| 70 | private LinearLinkageEncoding(LinearLinkageEncoding original, Cloner cloner)
|
---|
| 71 | : base(original, cloner) {
|
---|
| 72 | lengthParameter = cloner.Clone(original.lengthParameter);
|
---|
| 73 | RegisterParameterEvents();
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 |
|
---|
| 77 | public LinearLinkageEncoding() : this("LLE", 10) { }
|
---|
| 78 | public LinearLinkageEncoding(string name) : this(name, 10) { }
|
---|
| 79 | public LinearLinkageEncoding(int length) : this("LLE", length) { }
|
---|
| 80 | public LinearLinkageEncoding(string name, int length)
|
---|
| 81 | : base(name) {
|
---|
| 82 | lengthParameter = new FixedValueParameter<IntValue>(Name + ".Length", new IntValue(length));
|
---|
| 83 | Parameters.Add(lengthParameter);
|
---|
| 84 |
|
---|
| 85 | SolutionCreator = new RandomLinearLinkageCreator();
|
---|
| 86 | RegisterParameterEvents();
|
---|
| 87 | DiscoverOperators();
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | private void OnLengthParameterChanged() {
|
---|
| 91 | RegisterLengthParameterEvents();
|
---|
| 92 | ConfigureOperators(Operators);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | private void RegisterParameterEvents() {
|
---|
| 96 | RegisterLengthParameterEvents();
|
---|
| 97 | }
|
---|
| 98 | private void RegisterLengthParameterEvents() {
|
---|
| 99 | LengthParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators);
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | #region Operator Discovery
|
---|
| 103 | private static readonly IEnumerable<Type> encodingSpecificOperatorTypes;
|
---|
| 104 | static LinearLinkageEncoding() {
|
---|
| 105 | encodingSpecificOperatorTypes = new List<Type>() {
|
---|
| 106 | typeof (ILinearLinkageOperator),
|
---|
| 107 | typeof (ILinearLinkageCreator),
|
---|
| 108 | typeof (ILinearLinkageCrossover),
|
---|
| 109 | typeof (ILinearLinkageManipulator),
|
---|
| 110 | typeof (ILinearLinkageShakingOperator)
|
---|
| 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>());
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | #region specific operator wiring
|
---|
| 133 | private void ConfigureCreators(IEnumerable<ILinearLinkageCreator> creators) {
|
---|
| 134 | foreach (var creator in creators) {
|
---|
| 135 | creator.LengthParameter.ActualName = LengthParameter.Name;
|
---|
| 136 | creator.LLEParameter.ActualName = Name;
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 | private void ConfigureCrossovers(IEnumerable<ILinearLinkageCrossover> crossovers) {
|
---|
| 140 | foreach (var crossover in crossovers) {
|
---|
| 141 | crossover.ChildParameter.ActualName = Name;
|
---|
| 142 | crossover.ParentsParameter.ActualName = Name;
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 | private void ConfigureManipulators(IEnumerable<ILinearLinkageManipulator> manipulators) {
|
---|
| 146 | foreach (var manipulator in manipulators) {
|
---|
| 147 | manipulator.LLEParameter.ActualName = Name;
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
| 150 | private void ConfigureShakingOperators(IEnumerable<ILinearLinkageShakingOperator> shakingOperators) {
|
---|
| 151 | foreach (var shakingOperator in shakingOperators) {
|
---|
| 152 | shakingOperator.LLEParameter.ActualName = Name;
|
---|
| 153 | }
|
---|
| 154 | }
|
---|
| 155 | #endregion
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | public static class IndividualExtensionMethods {
|
---|
| 159 | public static LinearLinkage LinearLinkage(this Individual individual) {
|
---|
| 160 | var encoding = individual.GetEncoding<LinearLinkageEncoding>();
|
---|
| 161 | return individual.LinearLinkage(encoding.Name);
|
---|
| 162 | }
|
---|
| 163 | public static LinearLinkage LinearLinkage(this Individual individual, string name) {
|
---|
| 164 | return (LinearLinkage)individual[name];
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 167 | }
|
---|