Changeset 11553


Ignore:
Timestamp:
11/15/14 17:30:02 (8 years ago)
Author:
mkommend
Message:

#2174: Adapted binary encoding to new wiring method.

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

Legend:

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

    r11484 r11553  
    2020#endregion
    2121
     22using System;
     23using System.Collections.Generic;
     24using System.Linq;
    2225using HeuristicLab.Common;
    2326using HeuristicLab.Core;
    2427using HeuristicLab.Data;
     28using HeuristicLab.Encodings.BinaryVectorEncoding;
     29using HeuristicLab.Optimization;
     30using HeuristicLab.Parameters;
    2531using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     32using HeuristicLab.PluginInfrastructure;
    2633
    2734namespace HeuristicLab.Problems.Programmable {
    2835  [Item("BinaryEncoding", "Describes a binary vector encoding.")]
    2936  [StorableClass]
    30   public class BinaryEncoding : Encoding {
     37  public sealed class BinaryEncoding : Encoding {
     38    #region Encoding Parameters
    3139    [Storable]
    32     private IntValue length;
    33     public IntValue Length {
    34       get { return length; }
     40    private IFixedValueParameter<IntValue> lengthParameter;
     41    public IFixedValueParameter<IntValue> LengthParameter {
     42      get { return lengthParameter; }
    3543      set {
    36         if (length == value) return;
    37         length = value;
    38         OnParameterConfigurationChanged();
     44        if (value == null) throw new ArgumentNullException("Length parameter must not be null.");
     45        if (lengthParameter == value) return;
     46        lengthParameter = value;
     47        OnLengthParameterChanged();
    3948      }
     49    }
     50    public override IEnumerable<IValueParameter> Parameters {
     51      get { return base.Parameters.Concat(new IValueParameter[] { LengthParameter }); }
     52    }
     53    #endregion
     54
     55    private List<IOperator> encodingOperators = new List<IOperator>();
     56    [Storable]
     57    public override IEnumerable<IOperator> Operators {
     58      get { return encodingOperators; }
     59      protected set { encodingOperators = new List<IOperator>(value); }
     60    }
     61
     62    public override ISolutionCreator DefaultSolutionCreator {
     63      get { return Operators.OfType<RandomBinaryVectorCreator>().First(); }
     64    }
     65
     66    public int Length {
     67      get { return LengthParameter.Value.Value; }
     68      set { LengthParameter.Value.Value = value; }
    4069    }
    4170
    4271    [StorableConstructor]
    43     protected BinaryEncoding(bool deserializing) : base(deserializing) { }
    44     protected BinaryEncoding(BinaryEncoding original, Cloner cloner)
     72    private BinaryEncoding(bool deserializing) : base(deserializing) { }
     73    [StorableHook(HookType.AfterDeserialization)]
     74    private void AfterDeserialization() {
     75      RegisterParameterEvents();
     76      DiscoverOperators();
     77    }
     78    public override IDeepCloneable Clone(Cloner cloner) { return new BinaryEncoding(this, cloner); }
     79    private BinaryEncoding(BinaryEncoding original, Cloner cloner)
    4580      : base(original, cloner) {
    46       length = cloner.Clone(original.length);
     81      lengthParameter = cloner.Clone(original.lengthParameter);
     82      encodingOperators = original.Operators.Select(cloner.Clone).ToList();
     83      RegisterParameterEvents();
    4784    }
    4885    public BinaryEncoding(string name, int length)
    4986      : base(name) {
    50       this.length = new IntValue(length);
     87      lengthParameter = new FixedValueParameter<IntValue>(Name + "Length", new IntValue(length));
    5188    }
    5289
    53     public override IDeepCloneable Clone(Cloner cloner) {
    54       return new BinaryEncoding(this, cloner);
     90    private void OnLengthParameterChanged() {
     91      RegisterLengthParameterEvents();
     92      ConfigureOperators(Operators);
     93    }
     94    private void RegisterParameterEvents() {
     95      RegisterLengthParameterEvents();
     96    }
     97    private void RegisterLengthParameterEvents() {
     98      LengthParameter.ValueChanged += (o, s) => ConfigureOperators(Operators);
     99      LengthParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators);
    55100    }
    56101
     102    #region Operator Discovery
     103    private static readonly IEnumerable<Type> encodingSpecificOperatorTypes;
     104    static BinaryEncoding() {
     105      encodingSpecificOperatorTypes = new List<Type>() {
     106        typeof (IBinaryVectorOperator),
     107        typeof (IBinaryVectorCreator),
     108        typeof (IBinaryVectorCrossover),
     109        typeof (IBinaryVectorManipulator),
     110        typeof (IBinaryVectorMoveOperator),
     111        typeof (IBinaryVectorMultiNeighborhoodShakingOperator),
     112      };
     113    }
     114    private void DiscoverOperators() {
     115      var pluginDescription = ApplicationManager.Manager.GetDeclaringPlugin(typeof(IBinaryVectorOperator));
     116      var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, pluginDescription, true, false, false);
     117      var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t));
     118      var newOperators = operators.Except(encodingOperators, new TypeEqualityComparer<IOperator>()).ToList();
     119
     120      ConfigureOperators(newOperators);
     121      encodingOperators.AddRange(newOperators);
     122    }
     123    #endregion
     124
     125    public void ConfigureOperators(IEnumerable<IOperator> operators) {
     126      ConfigureCreators(operators.OfType<IBinaryVectorCreator>());
     127      ConfigureCrossovers(operators.OfType<IBinaryVectorCrossover>());
     128      ConfigureManipulators(operators.OfType<IBinaryVectorManipulator>());
     129      ConfigureMoveOperators(operators.OfType<IBinaryVectorMoveOperator>());
     130      ConfigureShakingOperators(operators.OfType<IBinaryVectorMultiNeighborhoodShakingOperator>());
     131    }
     132
     133    #region Specific Operator Wiring
     134    private void ConfigureCreators(IEnumerable<IBinaryVectorCreator> creators) {
     135      foreach (var creator in creators) {
     136        creator.BinaryVectorParameter.ActualName = Name;
     137        creator.LengthParameter.ActualName = LengthParameter.Name;
     138      }
     139    }
     140    private void ConfigureCrossovers(IEnumerable<IBinaryVectorCrossover> crossovers) {
     141      foreach (var crossover in crossovers) {
     142        crossover.ParentsParameter.ActualName = Name;
     143        crossover.ChildParameter.ActualName = Name;
     144      }
     145    }
     146    private void ConfigureManipulators(IEnumerable<IBinaryVectorManipulator> manipulators) {
     147      foreach (var manipulator in manipulators) {
     148        manipulator.BinaryVectorParameter.ActualName = Name;
     149      }
     150    }
     151    private void ConfigureMoveOperators(IEnumerable<IBinaryVectorMoveOperator> moveOperators) {
     152      foreach (var moveOperator in moveOperators) {
     153        moveOperator.BinaryVectorParameter.ActualName = Name;
     154      }
     155    }
     156    private void ConfigureShakingOperators(IEnumerable<IBinaryVectorMultiNeighborhoodShakingOperator> shakingOperators) {
     157      foreach (var shakingOperator in shakingOperators) {
     158        shakingOperator.BinaryVectorParameter.ActualName = Name;
     159      }
     160    }
     161    #endregion
    57162  }
    58163}
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/Encoding.cs

    r11550 r11553  
    2525using HeuristicLab.Common;
    2626using HeuristicLab.Core;
     27using HeuristicLab.Optimization;
    2728using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2829
     
    3536    }
    3637
    37     [StorableConstructor]
    38     protected Encoding(bool deserializing) : base(deserializing) { }
    39     protected Encoding(Encoding original, Cloner cloner)
    40       : base(original, cloner) { }
    41     protected Encoding(string name) : base(name) { }
    42 
    4338    public virtual IEnumerable<IValueParameter> Parameters {
    4439      get { return Enumerable.Empty<IValueParameter>(); }
     
    5045    }
    5146
     47    public virtual ISolutionCreator DefaultSolutionCreator {
     48      get { return null; }
     49    }
     50
     51    [StorableConstructor]
     52    protected Encoding(bool deserializing) : base(deserializing) { }
     53    protected Encoding(Encoding original, Cloner cloner)
     54      : base(original, cloner) { }
     55    protected Encoding(string name) : base(name) { }
     56
    5257    public event EventHandler ParameterConfigurationChanged;
    5358    protected virtual void OnParameterConfigurationChanged() {
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/RealEncoding.cs

    r11550 r11553  
    7676    }
    7777
    78     public IRealVectorCreator DefaultSolutionCreator {
     78    public override ISolutionCreator DefaultSolutionCreator {
    7979      get { return Operators.OfType<UniformRandomRealVectorCreator>().First(); }
    8080    }
     
    166166    static RealEncoding() {
    167167      encodingSpecificOperatorTypes = new List<Type>() {
     168        typeof (IRealVectorOperator),
    168169        typeof (IRealVectorCreator),
    169170        typeof (IRealVectorCrossover),
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Interfaces/IEncoding.cs

    r11550 r11553  
    2525using System.Text;
    2626using HeuristicLab.Core;
     27using HeuristicLab.Optimization;
    2728
    2829namespace HeuristicLab.Problems.Programmable {
    29   public interface IEncoding : INamedItem {
    30 
     30  public interface IEncoding : INamedItem
     31  {
     32    ISolutionCreator DefaultSolutionCreator { get; }
    3133    IEnumerable<IValueParameter> Parameters { get; }
    3234    IEnumerable<IOperator> Operators { get; }
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/MultiObjectiveProgrammableProblem.cs

    r11550 r11553  
    176176
    177177    protected virtual ISolutionCreator ConfigureCreator(IEncoding encoding) {
     178      #region Configure RealVector Creator
     179      var realEnc = encoding as RealEncoding;
     180      if (realEnc != null) {
     181        DynamicEncodingParameters.AddRange(realEnc.Parameters);
     182        return realEnc.DefaultSolutionCreator;
     183      }
     184      #endregion
     185      #region Configure BinaryVector Creator
     186      var binEnc = encoding as BinaryEncoding;
     187      if (binEnc != null) {
     188        DynamicEncodingParameters.AddRange(binEnc.Parameters);
     189        return binEnc.DefaultSolutionCreator;
     190      }
     191      #endregion
    178192      #region Configure MultiEncoding Creator
    179193      var multiEncoding = encoding as MultiEncoding;
     
    184198          creator.Operators.Add(ConfigureCreator(enc));
    185199        }
    186         return creator;
    187       }
    188       #endregion
    189       #region Configure BinaryVector Creator
    190       var binEnc = encoding as BinaryEncoding;
    191       if (binEnc != null) {
    192         var p = new ValueParameter<IntValue>(binEnc.Name + "Length", binEnc.Length);
    193         DynamicEncodingParameters.Add(p);
    194 
    195         var creator = new RandomBinaryVectorCreator();
    196         creator.BinaryVectorParameter.ActualName = binEnc.Name;
    197         creator.LengthParameter.ActualName = p.Name;
    198200        return creator;
    199201      }
     
    212214        creator.BoundsParameter.ActualName = b.Name;
    213215        return creator;
    214       }
    215       #endregion
    216       #region Configure RealVector Creator
    217       var realEnc = encoding as RealEncoding;
    218       if (realEnc != null) {
    219         DynamicEncodingParameters.AddRange(realEnc.Parameters);
    220         return realEnc.DefaultSolutionCreator;
    221216      }
    222217      #endregion
     
    784779      var binConfig = (BinaryEncoding)config.Encodings[paramName];
    785780      IBinaryVectorCrossover binXo;
    786       if (binConfig.Length.Value > 3) binXo = new Encodings.BinaryVectorEncoding.SinglePointCrossover();
     781      if (binConfig.Length > 3) binXo = new Encodings.BinaryVectorEncoding.SinglePointCrossover();
    787782      else binXo = new Encodings.BinaryVectorEncoding.UniformCrossover();
    788783      binXo.ChildParameter.ActualName = paramName;
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveProgrammableProblem.cs

    r11550 r11553  
    7676    }
    7777
    78 
    7978    [StorableConstructor]
    8079    protected SingleObjectiveProgrammableProblem(bool deserializing) : base(deserializing) { }
     
    193192        DynamicEncodingParameters.AddRange(realEnc.Parameters);
    194193        return realEnc.DefaultSolutionCreator;
     194      }
     195      #endregion
     196      #region Configure BinaryVector Creator
     197      var binEnc = encoding as BinaryEncoding;
     198      if (binEnc != null) {
     199        DynamicEncodingParameters.AddRange(binEnc.Parameters);
     200        return binEnc.DefaultSolutionCreator;
    195201      }
    196202      #endregion
     
    206212      }
    207213      #endregion
    208       #region Configure BinaryVector Creator
    209       var binEnc = encoding as BinaryEncoding;
    210       if (binEnc != null) {
    211         var p = new ValueParameter<IntValue>(binEnc.Name + "Length", binEnc.Length);
    212         DynamicEncodingParameters.Add(p);
    213 
    214         var creator = new RandomBinaryVectorCreator();
    215         creator.BinaryVectorParameter.ActualName = binEnc.Name;
    216         creator.LengthParameter.ActualName = p.Name;
    217         return creator;
    218       }
    219       #endregion
    220214      #region Configure IntegerVector Creator
    221215      var intEnc = encoding as IntegerEncoding;
     
    261255
    262256      #region Configure Operators for RealVectorEncoding
    263       var realCreator = newCreator as IRealVectorCreator;
    264257      var realEncoding = encoding as RealEncoding;
    265258      if (realEncoding != null) {
    266259        // do not replace a real vector creator that was manually set
    267260        if (!(SolutionCreator is IRealVectorCreator)
    268             ||
    269             ((IRealVectorCreator)SolutionCreator).RealVectorParameter.ActualName !=
    270             realCreator.RealVectorParameter.ActualName) {
     261            || ((IRealVectorCreator)SolutionCreator).RealVectorParameter.ActualName != realEncoding.Name) {
    271262          Operators.Remove(SolutionCreator);
    272263          SolutionCreator = newCreator;
     
    276267          su.MaximizationParameter.ActualName = MaximizationParameter.Name;
    277268        }
    278       } 
     269      }
    279270      #endregion
    280271      #region Configure Operators for BinaryVectorEncoding
    281       var binCreator = newCreator as IBinaryVectorCreator;
    282       if (binCreator != null) {
    283         var paramName = binCreator.BinaryVectorParameter.ActualName;
     272      var binEncoding = encoding as BinaryEncoding;
     273      if (binEncoding != null) {
    284274        // do not replace a binary vector creator that was manually set
    285         if (!(SolutionCreator is IBinaryVectorCreator) || ((IBinaryVectorCreator)SolutionCreator).BinaryVectorParameter.ActualName != paramName) {
     275        if (!(SolutionCreator is IBinaryVectorCreator) || ((IBinaryVectorCreator)SolutionCreator).BinaryVectorParameter.ActualName != binEncoding.Name) {
    286276          Operators.Remove(SolutionCreator);
    287277          SolutionCreator = newCreator;
    288           Operators.Add(SolutionCreator);
    289         }
    290 
    291         #region Wire BinaryVector Crossovers
    292         var crossovers = Operators.OfType<IBinaryVectorCrossover>()
    293           .Union(ApplicationManager.Manager.GetInstances<IBinaryVectorCrossover>(), new TypeEqualityComparer<IBinaryVectorCrossover>())
    294           .ToList();
    295         foreach (var xo in crossovers) {
    296           xo.ChildParameter.ActualName = binCreator.BinaryVectorParameter.ActualName;
    297           xo.ChildParameter.Hidden = true;
    298           xo.ParentsParameter.ActualName = binCreator.BinaryVectorParameter.ActualName;
    299           xo.ParentsParameter.Hidden = true;
    300         }
    301         Operators.AddRange(crossovers.Except(Operators.OfType<IBinaryVectorCrossover>()));
    302         #endregion
    303         #region Wire BinaryVector Manipulators
    304         var manipulators = Operators.OfType<IBinaryVectorManipulator>()
    305           .Union(ApplicationManager.Manager.GetInstances<IBinaryVectorManipulator>(), new TypeEqualityComparer<IBinaryVectorManipulator>())
    306           .ToList();
    307         foreach (var m in manipulators) {
    308           m.BinaryVectorParameter.ActualName = binCreator.BinaryVectorParameter.ActualName;
    309           m.BinaryVectorParameter.Hidden = true;
    310         }
    311         Operators.AddRange(manipulators.Except(Operators.OfType<IBinaryVectorManipulator>()));
    312         #endregion
    313         #region Wire BinaryVector ShakingOperators
    314         var shakingOperators = Operators.OfType<IBinaryVectorMultiNeighborhoodShakingOperator>()
    315           .Union(ApplicationManager.Manager.GetInstances<IBinaryVectorMultiNeighborhoodShakingOperator>(), new TypeEqualityComparer<IBinaryVectorMultiNeighborhoodShakingOperator>())
    316           .ToList();
    317         foreach (var so in shakingOperators) {
    318           so.BinaryVectorParameter.ActualName = paramName;
    319           so.BinaryVectorParameter.Hidden = true;
    320         }
    321         Operators.AddRange(shakingOperators.Except(Operators.OfType<IBinaryVectorMultiNeighborhoodShakingOperator>()));
    322         #endregion
    323       } else {
    324         Operators.RemoveAll(x => x is IBinaryVectorCrossover
    325           || x is IBinaryVectorManipulator
    326           || x is IBinaryVectorMultiNeighborhoodShakingOperator);
     278        }
    327279      }
    328280      #endregion
     
    714666      var binConfig = (BinaryEncoding)config.Encodings[paramName];
    715667      IBinaryVectorCrossover binXo;
    716       if (binConfig.Length.Value > 3) binXo = new Encodings.BinaryVectorEncoding.SinglePointCrossover();
     668      if (binConfig.Length > 3) binXo = new Encodings.BinaryVectorEncoding.SinglePointCrossover();
    717669      else binXo = new Encodings.BinaryVectorEncoding.UniformCrossover();
    718670      binXo.ChildParameter.ActualName = paramName;
Note: See TracChangeset for help on using the changeset viewer.