Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/24/14 17:08:24 (9 years ago)
Author:
mkommend
Message:

#2174: Adapted permutation encoding.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/PermutationEncoding.cs

    r11559 r11575  
    2020#endregion
    2121
     22using System;
     23using System.Collections.Generic;
     24using System.Linq;
    2225using HeuristicLab.Common;
    2326using HeuristicLab.Core;
    2427using HeuristicLab.Data;
    2528using HeuristicLab.Encodings.PermutationEncoding;
     29using HeuristicLab.Encodings.RealVectorEncoding;
    2630using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     31using HeuristicLab.PluginInfrastructure;
    2732
    2833namespace HeuristicLab.Problems.Programmable {
    2934  [Item("PermutationEncoding", "Describes a permutation encoding.")]
    3035  [StorableClass]
    31   public class PermutationEncoding : Encoding<IPermutationCreator> {
     36  public sealed class PermutationEncoding : Encoding<IPermutationCreator> {
     37    #region encoding parameters
    3238    [Storable]
    33     private IntValue length;
    34     public IntValue Length {
    35       get { return length; }
     39    private IFixedValueParameter<IntValue> lengthParameter;
     40    public IFixedValueParameter<IntValue> LengthParameter {
     41      get { return lengthParameter; }
    3642      set {
    37         if (length == value) return;
    38         length = value;
    39         OnParameterConfigurationChanged();
     43        if (value == null) throw new ArgumentNullException("Length parameter must not be null.");
     44        if (lengthParameter == value) return;
     45        lengthParameter = value;
     46        OnLengthParameterChanged();
    4047      }
    4148    }
    4249
    4350    [Storable]
    44     private PermutationType type;
    45     public PermutationType Type {
    46       get { return type; }
     51    private IFixedValueParameter<PermutationType> permutationTypeParameter;
     52    public IFixedValueParameter<PermutationType> PermutationTypeParameter {
     53      get { return permutationTypeParameter; }
    4754      set {
    48         if (type == value) return;
    49         type = value;
    50         OnParameterConfigurationChanged();
    51       }
     55        if (value == null) throw new ArgumentNullException("Permutation type parameter must not be null.");
     56        if (permutationTypeParameter == value) return;
     57        permutationTypeParameter = value;
     58        OnPermutationTypeParameterChanged();
     59      }
     60    }
     61
     62    public override IEnumerable<IValueParameter> Parameters {
     63      get { return base.Parameters.Concat(new IValueParameter[] { LengthParameter, PermutationTypeParameter }); }
     64    }
     65    #endregion
     66
     67    public int Length {
     68      get { return LengthParameter.Value.Value; }
     69      set { LengthParameter.Value.Value = value; }
     70    }
     71
     72    public PermutationTypes Type {
     73      get { return PermutationTypeParameter.Value.Value; }
     74      set { PermutationTypeParameter.Value.Value = value; }
    5275    }
    5376
    5477    [StorableConstructor]
    55     protected PermutationEncoding(bool deserializing) : base(deserializing) { }
    56     protected PermutationEncoding(PermutationEncoding original, Cloner cloner)
     78    private PermutationEncoding(bool deserializing) : base(deserializing) { }
     79    [StorableHook(HookType.AfterDeserialization)]
     80    private void AfterDeserialization() {
     81      RegisterParameterEvents();
     82      DiscoverOperators();
     83    }
     84
     85    public override IDeepCloneable Clone(Cloner cloner) { return new PermutationEncoding(this, cloner); }
     86    private PermutationEncoding(PermutationEncoding original, Cloner cloner)
    5787      : base(original, cloner) {
    58       length = cloner.Clone(original.length);
    59       type = cloner.Clone(original.type);
     88      lengthParameter = cloner.Clone(original.lengthParameter);
     89      permutationTypeParameter = cloner.Clone(original.permutationTypeParameter);
     90      RegisterParameterEvents();
    6091    }
    6192    public PermutationEncoding(string name, int length, PermutationTypes type)
    6293      : base(name) {
    63       this.length = new IntValue(length);
    64       this.type = new PermutationType(type);
    65     }
    66 
    67     public override IDeepCloneable Clone(Cloner cloner) {
    68       return new PermutationEncoding(this, cloner);
    69     }
    70 
     94      Length = length;
     95      Type = type;
     96      RegisterParameterEvents();
     97      DiscoverOperators();
     98    }
     99
     100    private void OnLengthParameterChanged() {
     101      RegisterParameterEvents();
     102      ConfigureOperators(Operators);
     103    }
     104
     105    private void OnPermutationTypeParameterChanged() {
     106      RegisterParameterEvents();
     107      ConfigureOperators(Operators);
     108    }
     109
     110    private void RegisterParameterEvents() {
     111      RegisterLengthParameterEvents();
     112      RegisterPermutationTypeParameterEvents();
     113    }
     114    private void RegisterLengthParameterEvents() {
     115      LengthParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators);
     116    }
     117    private void RegisterPermutationTypeParameterEvents() {
     118      PermutationTypeParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators);
     119    }
     120
     121    #region Operator Discovery
     122    private static readonly IEnumerable<Type> encodingSpecificOperatorTypes;
     123    static PermutationEncoding() {
     124      encodingSpecificOperatorTypes = new List<Type>() {
     125          typeof (IPermutationOperator),
     126          typeof (IPermutationCreator),
     127          typeof (IPermutationCrossover),
     128          typeof (IPermutationManipulator),
     129          typeof (IPermutationMultiNeighborhoodShakingOperator),
     130          typeof (IPermutationMoveOperator),
     131          typeof (IPermutationInversionMoveOperator),
     132          typeof (IPermutationScrambleMoveOperator),
     133          typeof (IPermutationSwap2MoveOperator),                   
     134          typeof (IPermutationTranslocationMoveOperator)
     135      };
     136    }
     137    private void DiscoverOperators() {
     138      var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, true, false, false);
     139      var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t));
     140      var newOperators = operators.Except(encodingOperators, new TypeEqualityComparer<IOperator>()).ToList();
     141
     142      ConfigureOperators(newOperators);
     143      encodingOperators.AddRange(newOperators);
     144    }
     145    #endregion
     146
     147    public override void ConfigureOperators(IEnumerable<IOperator> operators) {
     148      base.ConfigureOperators(operators);
     149      ConfigureCreators(Operators.OfType<IPermutationCreator>());
     150      ConfigureCrossovers(Operators.OfType<IPermutationCrossover>());
     151      ConfigureManipulators(Operators.OfType<IPermutationManipulator>());
     152      ConfigureShakingOperators(Operators.OfType<IPermutationMultiNeighborhoodShakingOperator>());
     153      ConfigureMoveOperators(Operators.OfType<IPermutationMoveOperator>());
     154      ConfigureInversionMoveOperators(Operators.OfType<IPermutationInversionMoveOperator>());
     155      ConfigureScrambleMoveOperators(Operators.OfType<IPermutationScrambleMoveOperator>());
     156      ConfigureSwap2MoveOperators(Operators.OfType<IPermutationSwap2MoveOperator>());
     157      ConfigureTranslocationMoveOperators(Operators.OfType<IPermutationTranslocationMoveOperator>());
     158    }
     159
     160    #region specific operator wiring
     161    private void ConfigureCreators(IEnumerable<IPermutationCreator> creators) {
     162      foreach (var creator in creators) {
     163        creator.LengthParameter.ActualName = LengthParameter.Name;
     164        creator.PermutationParameter.ActualName = Name;
     165        creator.PermutationTypeParameter.Value.Value = Type;
     166      }
     167    }
     168    private void ConfigureCrossovers(IEnumerable<IPermutationCrossover> crossovers) {
     169      foreach (var crossover in crossovers) {
     170        crossover.ChildParameter.ActualName = Name;
     171        crossover.ParentsParameter.ActualName = Name;
     172      }
     173    }
     174    private void ConfigureManipulators(IEnumerable<IPermutationManipulator> manipulators) {
     175      foreach (var manipulator in manipulators) {
     176        manipulator.PermutationParameter.ActualName = Name;
     177      }
     178    }
     179    private void ConfigureShakingOperators(IEnumerable<IPermutationMultiNeighborhoodShakingOperator> shakingOperators) {
     180      foreach (var shakingOperator in shakingOperators) {
     181        shakingOperator.PermutationParameter.ActualName = Name;
     182      }
     183    }
     184    private void ConfigureMoveOperators(IEnumerable<IPermutationMoveOperator> moveOperators) {
     185      foreach (var moveOperator in moveOperators) {
     186        moveOperator.PermutationParameter.ActualName = Name;
     187      }
     188    }
     189    private void ConfigureInversionMoveOperators(IEnumerable<IPermutationInversionMoveOperator> inversionMoveOperators) {
     190      foreach (var inversionMoveOperator in inversionMoveOperators) {
     191        inversionMoveOperator.InversionMoveParameter.ActualName = Name + "_InversionMove";
     192      }
     193    }
     194    private void ConfigureScrambleMoveOperators(IEnumerable<IPermutationScrambleMoveOperator> scrambleMoveOperators) {
     195      foreach (var scrambleMoveOperator in scrambleMoveOperators) {
     196        scrambleMoveOperator.ScrambleMoveParameter.ActualName = Name + "_ScambleMove";
     197      }
     198    }
     199    private void ConfigureSwap2MoveOperators(IEnumerable<IPermutationSwap2MoveOperator> swap2MoveOperators) {
     200      foreach (var swap2MoveOperator in swap2MoveOperators) {
     201        swap2MoveOperator.Swap2MoveParameter.ActualName = Name + "_Swap2Move";
     202      }
     203    }
     204    private void ConfigureTranslocationMoveOperators(IEnumerable<IPermutationTranslocationMoveOperator> translocationMoveOperators) {
     205      foreach (var translocationMoveOperator in translocationMoveOperators) {
     206        translocationMoveOperator.TranslocationMoveParameter.ActualName = Name + "_TranslocationMove";
     207      }
     208    }
     209
     210    #endregion
    71211  }
    72212}
Note: See TracChangeset for help on using the changeset viewer.