Free cookie consent management tool by TermsFeed Policy Generator

Changeset 11575


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

#2174: Adapted permutation encoding.

Location:
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3
Files:
4 edited

Legend:

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

    r11559 r11575  
    116116      ConfigureManipulators(operators.OfType<IBinaryVectorManipulator>());
    117117      ConfigureMoveOperators(operators.OfType<IBinaryVectorMoveOperator>());
     118      ConfigureBitFlipMoveOperators(operators.OfType<IOneBitflipMoveOperator>());
    118119      ConfigureShakingOperators(operators.OfType<IBinaryVectorMultiNeighborhoodShakingOperator>());
    119120    }
     
    142143      }
    143144    }
     145    private void ConfigureBitFlipMoveOperators(IEnumerable<IOneBitflipMoveOperator> oneBitflipMoveOperators) {
     146      foreach (var oneBitFlipMoveOperator in oneBitflipMoveOperators) {
     147        oneBitFlipMoveOperator.OneBitflipMoveParameter.ActualName = Name + "_OneBitFlipMove";
     148      }
     149    }
    144150    private void ConfigureShakingOperators(IEnumerable<IBinaryVectorMultiNeighborhoodShakingOperator> shakingOperators) {
    145151      foreach (var shakingOperator in shakingOperators) {
  • 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}
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/MultiObjectiveProgrammableProblem.cs

    r11561 r11575  
    211211      var permEnc = encoding as PermutationEncoding;
    212212      if (permEnc != null) {
    213         var l = new ValueParameter<IntValue>(permEnc.Name + "Length", permEnc.Length);
     213        var l = new ValueParameter<IntValue>(permEnc.Name + "Length", new IntValue(permEnc.Length));
    214214        DynamicEncodingParameters.Add(l);
    215215
     
    217217        creator.PermutationParameter.ActualName = permEnc.Name;
    218218        creator.LengthParameter.ActualName = l.Name;
    219         creator.PermutationTypeParameter.Value = permEnc.Type;
     219        creator.PermutationTypeParameter.Value = new PermutationType(permEnc.Type);
    220220        return creator;
    221221      }
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveProgrammableProblem.cs

    r11561 r11575  
    205205      if (intEnc != null) {
    206206        DynamicEncodingParameters.AddRange(intEnc.Parameters);
    207         return intEnc.DefaultSolutionCreator;
     207        return intEnc.SolutionCreator;
     208      }
     209      #endregion
     210      #region Configure Permutation Creator
     211      var permEnc = encoding as PermutationEncoding;
     212      if (permEnc != null) {
     213        DynamicEncodingParameters.AddRange(permEnc.Parameters);
     214        return permEnc.SolutionCreator;
    208215      }
    209216      #endregion
     
    219226      }
    220227      #endregion
    221       #region Configure Permutation Creator
    222       var permEnc = encoding as PermutationEncoding;
    223       if (permEnc != null) {
    224         var l = new ValueParameter<IntValue>(permEnc.Name + "Length", permEnc.Length);
    225         DynamicEncodingParameters.Add(l);
    226 
    227         var creator = new RandomPermutationCreator();
    228         creator.PermutationParameter.ActualName = permEnc.Name;
    229         creator.LengthParameter.ActualName = l.Name;
    230         creator.PermutationTypeParameter.Value = permEnc.Type;
    231         return creator;
    232       }
    233       #endregion
    234228      throw new ArgumentException(string.Format("Encoding {0} is unknown.", encoding != null ? encoding.GetType().FullName : "(null)"));
    235229    }
    236230
    237 
    238     private IEnumerable<IOperator> GetDiscoveredOperators<T>() where T : class,IOperator {
    239       return ApplicationManager.Manager.GetInstances<T>()
    240           .Except(Operators.OfType<T>(), new TypeEqualityComparer<T>());
    241     }
    242231    protected virtual void ConfigureSingleEncodingOperators(ISolutionCreator newCreator, IEncoding encoding) {
    243232      // remove all multiencoding operators
     
    283272      #endregion
    284273      #region Configure Operators for PermutationEncoding
    285       var permCreator = newCreator as IPermutationCreator;
    286       if (permCreator != null) {
    287         var paramName = permCreator.PermutationParameter.ActualName;
     274      var permEncoding = encoding as PermutationEncoding;
     275      if (permEncoding != null) {
    288276        // do not replace a permutation creator that was manually set
    289277        if (!(SolutionCreator is IPermutationCreator)
    290             || ((IPermutationCreator)SolutionCreator).PermutationParameter.ActualName != permCreator.PermutationParameter.ActualName) {
     278            || ((IPermutationCreator)SolutionCreator).PermutationParameter.ActualName != permEncoding.Name) {
    291279          Operators.Remove(SolutionCreator);
    292280          SolutionCreator = newCreator;
    293           Operators.Add(SolutionCreator);
    294         }
    295 
    296         #region Wire Permutation Crossovers
    297         var crossovers = Operators.OfType<IPermutationCrossover>()
    298           .Union(ApplicationManager.Manager.GetInstances<IPermutationCrossover>(), new TypeEqualityComparer<IPermutationCrossover>())
    299           .ToList();
    300         foreach (var xo in crossovers) {
    301           xo.ChildParameter.ActualName = permCreator.PermutationParameter.ActualName;
    302           xo.ChildParameter.Hidden = true;
    303           xo.ParentsParameter.ActualName = permCreator.PermutationParameter.ActualName;
    304           xo.ParentsParameter.Hidden = true;
    305         }
    306         Operators.AddRange(crossovers.Except(Operators.OfType<IPermutationCrossover>()));
    307         #endregion
    308         #region Wire Permutation Manipulators
    309         var manipulators = Operators.OfType<IPermutationManipulator>()
    310           .Union(ApplicationManager.Manager.GetInstances<IPermutationManipulator>(), new TypeEqualityComparer<IPermutationManipulator>())
    311           .ToList();
    312         foreach (var m in manipulators) {
    313           m.PermutationParameter.ActualName = permCreator.PermutationParameter.ActualName;
    314           m.PermutationParameter.Hidden = true;
    315         }
    316         Operators.AddRange(manipulators.Except(Operators.OfType<IPermutationManipulator>()));
    317         #endregion
    318         #region Wire Permutation ShakingOperators
    319         var shakingOperators = Operators.OfType<IPermutationMultiNeighborhoodShakingOperator>()
    320           .Union(ApplicationManager.Manager.GetInstances<IPermutationMultiNeighborhoodShakingOperator>(), new TypeEqualityComparer<IPermutationMultiNeighborhoodShakingOperator>())
    321           .ToList();
    322         foreach (var op in shakingOperators) {
    323           op.PermutationParameter.ActualName = paramName;
    324           op.PermutationParameter.Hidden = true;
    325         }
    326         Operators.AddRange(shakingOperators.Except(Operators.OfType<IPermutationMultiNeighborhoodShakingOperator>()));
    327         #endregion
     281        }
    328282      } else {
    329283        Operators.RemoveAll(x => x is IPermutationCrossover
Note: See TracChangeset for help on using the changeset viewer.