Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/13/14 16:46:40 (10 years ago)
Author:
mkommend
Message:

#2174: Updated IEncoding interface, adapted problems and refactored operator discovery in realencoding

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

Legend:

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

    r11546 r11550  
    4141    protected Encoding(string name) : base(name) { }
    4242
    43     public virtual IEnumerable<IValueParameter> EncodingParameters {
     43    public virtual IEnumerable<IValueParameter> Parameters {
    4444      get { return Enumerable.Empty<IValueParameter>(); }
     45    }
     46
     47    public virtual IEnumerable<IOperator> Operators {
     48      get { return Enumerable.Empty<IOperator>(); }
     49      protected set { }
    4550    }
    4651
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/RealEncoding.cs

    r11546 r11550  
    2424using System.Collections.Generic;
    2525using System.Linq;
     26using System.Runtime.CompilerServices;
    2627using System.Text;
    2728using HeuristicLab.Common;
     
    3839  [StorableClass]
    3940  public sealed class RealEncoding : Encoding {
    40 
     41    #region Encoding Parameters
    4142    [Storable]
    4243    private IFixedValueParameter<IntValue> lengthParameter;
     
    6364    }
    6465
    65     public override IEnumerable<IValueParameter> EncodingParameters {
    66       get { return base.EncodingParameters.Concat(new IValueParameter[] { LengthParameter, BoundsParameter }); }
     66    public override IEnumerable<IValueParameter> Parameters {
     67      get { return base.Parameters.Concat(new IValueParameter[] { LengthParameter, BoundsParameter }); }
     68    }
     69    #endregion
     70
     71    private List<IOperator> encodingOperators = new List<IOperator>();
     72    [Storable]
     73    public override IEnumerable<IOperator> Operators {
     74      get { return encodingOperators; }
     75      protected set { encodingOperators = new List<IOperator>(value); }
     76    }
     77
     78    public IRealVectorCreator DefaultSolutionCreator {
     79      get { return Operators.OfType<UniformRandomRealVectorCreator>().First(); }
    6780    }
    6881
     
    7689    }
    7790
    78     public IRealVectorCreator DefaultSolutionCreator {
    79       get {
    80         var creator = new UniformRandomRealVectorCreator();
    81         creator.RealVectorParameter.ActualName = Name;
    82         creator.LengthParameter.ActualName = LengthParameter.Name;
    83         creator.BoundsParameter.ActualName = BoundsParameter.Name;
    84         return creator;
    85       }
    86     }
     91
    8792
    8893    [StorableConstructor]
     
    9196    private void AfterDeserialization() {
    9297      RegisterParameterEvents();
     98      DiscoverOperators();
    9399    }
    94100
     
    98104      lengthParameter = cloner.Clone(original.lengthParameter);
    99105      boundsParameter = cloner.Clone(original.boundsParameter);
     106      encodingOperators = original.Operators.Select(cloner.Clone).ToList();
    100107      RegisterParameterEvents();
    101108    }
     109
     110
    102111
    103112    public RealEncoding(string name, int length, double min, double max)
     
    112121      boundsParameter = new ValueParameter<DoubleMatrix>(Name + "Bounds", bounds);
    113122      RegisterParameterEvents();
     123      DiscoverOperators();
    114124    }
    115125
     
    128138      boundsParameter = new ValueParameter<DoubleMatrix>(Name + "Bounds", bounds);
    129139      RegisterParameterEvents();
     140      DiscoverOperators();
    130141    }
    131142
    132143    private void OnLengthParameterChanged() {
    133144      RegisterLengthParameterEvents();
     145      ConfigureOperators(Operators);
    134146    }
    135147    private void OnBoundsParameterChanged() {
    136148      RegisterBoundsParameterEvents();
    137     }
    138     private void RegisterParameterEvents()
    139     {
     149      ConfigureOperators(Operators);
     150    }
     151    private void RegisterParameterEvents() {
    140152      RegisterLengthParameterEvents();
    141153      RegisterBoundsParameterEvents();
    142154    }
    143155    private void RegisterLengthParameterEvents() {
    144       //TODO fire correct event
    145       LengthParameter.ValueChanged += (o, s) => ConfigureOperators(Enumerable.Empty<IOperator>());
    146       LengthParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Enumerable.Empty<IOperator>());
     156      LengthParameter.ValueChanged += (o, s) => ConfigureOperators(Operators);
     157      LengthParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators);
    147158    }
    148159    private void RegisterBoundsParameterEvents() {
    149       //TODO fire correct event
    150       BoundsParameter.ValueChanged += (o, s) => ConfigureOperators(Enumerable.Empty<IOperator>());
    151       boundsParameter.Value.ToStringChanged += (o, s) => ConfigureOperators(Enumerable.Empty<IOperator>());
    152     }
    153 
    154     public void DiscoverOperators() {
    155       var types = new List<Type>()
    156       {
     160      BoundsParameter.ValueChanged += (o, s) => ConfigureOperators(Operators);
     161      boundsParameter.Value.ToStringChanged += (o, s) => ConfigureOperators(Operators);
     162    }
     163
     164    #region Operator Discovery
     165    private static readonly IEnumerable<Type> encodingSpecificOperatorTypes;
     166    static RealEncoding() {
     167      encodingSpecificOperatorTypes = new List<Type>() {
    157168        typeof (IRealVectorCreator),
    158169        typeof (IRealVectorCrossover),
    159170        typeof (IRealVectorManipulator),
    160171        typeof (IRealVectorStdDevStrategyParameterOperator),
     172        typeof (IRealVectorSwarmUpdater),
    161173        typeof (IRealVectorParticleCreator),
    162174        typeof (IRealVectorParticleUpdater),
    163         typeof (IRealVectorParticleUpdater),
     175        typeof (IRealVectorMultiNeighborhoodShakingOperator),
     176        typeof (IRealVectorBoundsChecker),
     177        typeof (IRealVectorMoveOperator),
     178        typeof (IRealVectorMoveGenerator)
    164179      };
    165       var discoveredTypes = ApplicationManager.Manager.GetTypes(types, true, false, false);
    166       var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t)).ToList();
    167     }
     180    }
     181    private void DiscoverOperators() {
     182      var pluginDescription = ApplicationManager.Manager.GetDeclaringPlugin(typeof(IRealVectorOperator));
     183      var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, pluginDescription, true, false, false);
     184      var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t));
     185      var newOperators = operators.Except(encodingOperators, new TypeEqualityComparer<IOperator>()).ToList();
     186
     187      ConfigureOperators(newOperators);
     188      encodingOperators.AddRange(newOperators);
     189    }
     190    #endregion
    168191
    169192    public void ConfigureOperators(IEnumerable<IOperator> operators) {
     
    176199      ConfigureParticleUpdaters(operators.OfType<IRealVectorParticleUpdater>());
    177200      ConfigureShakingOperators(operators.OfType<IRealVectorMultiNeighborhoodShakingOperator>());
    178     }
    179 
     201      ConfigureBoundsCheckers(operators.OfType<IRealVectorBoundsChecker>());
     202      ConfigureMoveOperators(operators.OfType<IRealVectorMoveOperator>());
     203      ConfigureMoveGenerators(operators.OfType<IRealVectorMoveGenerator>());
     204    }
     205
     206    #region Specific Operator Wiring
    180207    private void ConfigureCreators(IEnumerable<IRealVectorCreator> creators) {
    181208      foreach (var creator in creators) {
     
    260287      }
    261288    }
     289    private void ConfigureBoundsCheckers(IEnumerable<IRealVectorBoundsChecker> boundsCheckers) {
     290      foreach (var boundsChecker in boundsCheckers) {
     291        boundsChecker.RealVectorParameter.ActualName = Name;
     292        boundsChecker.BoundsParameter.ActualName = BoundsParameter.Name;
     293      }
     294    }
     295    private void ConfigureMoveOperators(IEnumerable<IRealVectorMoveOperator> moveOperators) {
     296      foreach (var moveOperator in moveOperators)
     297        moveOperator.RealVectorParameter.ActualName = Name;
     298    }
     299
     300    private void ConfigureMoveGenerators(IEnumerable<IRealVectorMoveGenerator> moveGenerators) {
     301      foreach (var moveGenerator in moveGenerators)
     302        moveGenerator.BoundsParameter.ActualName = BoundsParameter.Name;
     303    }
     304    #endregion
    262305  }
    263306}
Note: See TracChangeset for help on using the changeset viewer.