Changeset 11550


Ignore:
Timestamp:
11/13/14 16:46:40 (8 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
Files:
9 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}
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Interfaces/IEncoding.cs

    r11543 r11550  
    2727
    2828namespace HeuristicLab.Problems.Programmable {
    29   interface IEncoding : INamedItem {
     29  public interface IEncoding : INamedItem {
    3030
     31    IEnumerable<IValueParameter> Parameters { get; }
     32    IEnumerable<IOperator> Operators { get; }
    3133    //event EventHandler ParameterConfigurationChanged;
    3234  }
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Interfaces/IProblemDefinition.cs

    r11484 r11550  
    2626namespace HeuristicLab.Problems.Programmable {
    2727  public interface IProblemDefinition : INamedItem {
    28     Encoding Encoding { get; }
     28    IEncoding Encoding { get; }
    2929    IEnumerable<Individual> GetNeighbors(IRandom random, Individual individual);
    3030
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/MultiObjectiveProblemScript.cs

    r11485 r11550  
    124124    }
    125125
    126     Encoding IProblemDefinition.Encoding {
     126    IEncoding IProblemDefinition.Encoding {
    127127      get { return Instance != null ? Instance.Encoding : null; }
    128128    }
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/MultiObjectiveProgrammableProblem.cs

    r11546 r11550  
    5858    }
    5959
    60     protected IValueParameter<Encoding> EncodingParameter {
    61       get { return (IValueParameter<Encoding>)Parameters["Encoding"]; }
     60    protected IValueParameter<IEncoding> EncodingParameter {
     61      get { return (IValueParameter<IEncoding>)Parameters["Encoding"]; }
    6262    }
    6363
     
    8080      : base(new MultiObjectiveEvaluator(), new MultiEncodingCreator()) {
    8181      Parameters.Add(new ValueParameter<IMultiObjectiveProblemDefinition>("ProblemDefinition", "Defines the problem.", new MultiObjectiveProblemScript() { Name = Name }));
    82       Parameters.Add(new ValueParameter<Encoding>("Encoding", "Describes which parameters exist, what they're called, what type they are and their bounds if any."));
     82      Parameters.Add(new ValueParameter<IEncoding>("Encoding", "Describes which parameters exist, what they're called, what type they are and their bounds if any."));
    8383
    8484      DynamicEncodingParameters = new List<IParameter>();
     
    140140      if (definition == null) return;
    141141
    142       Encoding encoding = definition.Encoding;
     142      IEncoding encoding = definition.Encoding;
    143143      EncodingParameter.Value = encoding;
    144144      Maximization = new BoolArray(definition.Maximization);
     
    175175    }
    176176
    177     protected virtual ISolutionCreator ConfigureCreator(Encoding encoding) {
     177    protected virtual ISolutionCreator ConfigureCreator(IEncoding encoding) {
    178178      #region Configure MultiEncoding Creator
    179179      var multiEncoding = encoding as MultiEncoding;
     
    217217      var realEnc = encoding as RealEncoding;
    218218      if (realEnc != null) {
    219         DynamicEncodingParameters.AddRange(realEnc.EncodingParameters);
     219        DynamicEncodingParameters.AddRange(realEnc.Parameters);
    220220        return realEnc.DefaultSolutionCreator;
    221221      }
     
    237237    }
    238238
    239     protected virtual void ConfigureSingleEncodingOperators(ISolutionCreator newCreator, Encoding encoding) {
     239    protected virtual void ConfigureSingleEncodingOperators(ISolutionCreator newCreator, IEncoding encoding) {
    240240      // remove all multiencoding operators
    241241      Operators.RemoveAll(x => x is MultiEncodingCrossover
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/ProblemDefinition.cs

    r11543 r11550  
    3131  public abstract class ProblemDefinition : NamedItem, IProblemDefinition {
    3232    [Storable(Name = "Encoding")]
    33     private Encoding encoding;
    34     public Encoding Encoding {
     33    private IEncoding encoding;
     34    public IEncoding Encoding {
    3535      get { return encoding; }
    3636      protected set {
     
    4848      this.encoding = cloner.Clone(original.encoding);
    4949    }
    50     protected ProblemDefinition(Encoding encoding) : this(encoding, "ProblemDefinition") { }
    51     protected ProblemDefinition(Encoding encoding, string name) : this(encoding, name, string.Empty) { }
    52     protected ProblemDefinition(Encoding encoding, string name, string description)
     50    protected ProblemDefinition(IEncoding encoding) : this(encoding, "ProblemDefinition") { }
     51    protected ProblemDefinition(IEncoding encoding, string name) : this(encoding, name, string.Empty) { }
     52    protected ProblemDefinition(IEncoding encoding, string name, string description)
    5353      : base(name, description) {
    5454      Encoding = encoding;
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveProblemScript.cs

    r11485 r11550  
    122122    }
    123123
    124     Encoding IProblemDefinition.Encoding {
     124    IEncoding IProblemDefinition.Encoding {
    125125      get { return Instance != null ? Instance.Encoding : null; }
    126126    }
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveProgrammableProblem.cs

    r11546 r11550  
    5959    }
    6060
    61     protected IValueParameter<Encoding> EncodingParameter {
    62       get { return (IValueParameter<Encoding>)Parameters["Encoding"]; }
    63     }
     61    protected IValueParameter<IEncoding> EncodingParameter {
     62      get { return (IValueParameter<IEncoding>)Parameters["Encoding"]; }
     63    }
     64
    6465
    6566    public ISingleObjectiveProblemDefinition ProblemDefinition {
     
    7071    [Storable]
    7172    protected List<IParameter> DynamicEncodingParameters;
     73
     74    protected override IEnumerable<IItem> GetOperators() {
     75      return base.GetOperators().Concat(ProblemDefinition.Encoding.Operators);
     76    }
     77
    7278
    7379    [StorableConstructor]
     
    8288      : base(new SingleObjectiveEvaluator(), new MultiEncodingCreator()) {
    8389      Parameters.Add(new ValueParameter<ISingleObjectiveProblemDefinition>("ProblemDefinition", "Defines the problem.", new SingleObjectiveProblemScript() { Name = Name }));
    84       Parameters.Add(new ValueParameter<Encoding>("Encoding", "Describes the configuration of the encoding, what the variables are called, what type they are and their bounds if any."));
     90      Parameters.Add(new ValueParameter<IEncoding>("Encoding", "Describes the configuration of the encoding, what the variables are called, what type they are and their bounds if any."));
    8591
    8692      DynamicEncodingParameters = new List<IParameter>();
     
    143149      if (definition == null) return;
    144150
    145       Encoding encoding = definition.Encoding;
     151      IEncoding encoding = definition.Encoding;
    146152
    147153      EncodingParameter.Value = encoding;
     
    181187    }
    182188
    183     protected virtual ISolutionCreator ConfigureCreator(Encoding encoding) {
     189    protected virtual ISolutionCreator ConfigureCreator(IEncoding encoding) {
    184190      #region Configure RealVector Creator
    185191      var realEnc = encoding as RealEncoding;
    186192      if (realEnc != null) {
    187         DynamicEncodingParameters.AddRange(realEnc.EncodingParameters);
     193        DynamicEncodingParameters.AddRange(realEnc.Parameters);
    188194        return realEnc.DefaultSolutionCreator;
    189195      }
     
    244250
    245251
    246     private IEnumerable<IOperator> GetDiscoveredOperators<T>() where T : class,IOperator
    247     {
    248         return ApplicationManager.Manager.GetInstances<T>()
    249             .Except(Operators.OfType<T>(), new TypeEqualityComparer<T>());
    250     }
    251     protected virtual void ConfigureSingleEncodingOperators(ISolutionCreator newCreator, Encoding encoding) {
     252    private IEnumerable<IOperator> GetDiscoveredOperators<T>() where T : class,IOperator {
     253      return ApplicationManager.Manager.GetInstances<T>()
     254          .Except(Operators.OfType<T>(), new TypeEqualityComparer<T>());
     255    }
     256    protected virtual void ConfigureSingleEncodingOperators(ISolutionCreator newCreator, IEncoding encoding) {
    252257      // remove all multiencoding operators
    253258      Operators.RemoveAll(x => x is MultiEncodingCrossover
     
    266271          Operators.Remove(SolutionCreator);
    267272          SolutionCreator = newCreator;
    268           Operators.Add(SolutionCreator);
    269         }
    270 
    271         Operators.AddRange(GetDiscoveredOperators<IRealVectorCrossover>());
    272         Operators.AddRange(GetDiscoveredOperators<IRealVectorManipulator>());
    273         Operators.AddRange(GetDiscoveredOperators<IRealVectorStdDevStrategyParameterOperator>());
    274         Operators.AddRange(GetDiscoveredOperators<IRealVectorParticleCreator>());
    275         Operators.AddRange(GetDiscoveredOperators<IRealVectorParticleUpdater>());
    276         Operators.AddRange(GetDiscoveredOperators<IRealVectorSwarmUpdater>());
    277         Operators.AddRange(GetDiscoveredOperators<IRealVectorMultiNeighborhoodShakingOperator>());
    278 
    279         foreach (var su in Operators.OfType<IRealVectorSwarmUpdater>()) {
     273        }
     274
     275        foreach (var su in realEncoding.Operators.OfType<IRealVectorSwarmUpdater>()) {
    280276          su.MaximizationParameter.ActualName = MaximizationParameter.Name;
    281277        }
    282         realEncoding.ConfigureOperators(Operators.OfType<IOperator>());
    283      
    284       } else {
    285         Operators.RemoveAll(x => x is IRealVectorCrossover
    286           || x is IRealVectorManipulator
    287           || x is IRealVectorStdDevStrategyParameterOperator
    288           || x is IRealVectorParticleCreator
    289           || x is IRealVectorParticleUpdater
    290           || x is IRealVectorSwarmUpdater
    291           || x is IRealVectorMultiNeighborhoodShakingOperator);
    292       }
     278      }
    293279      #endregion
    294280      #region Configure Operators for BinaryVectorEncoding
Note: See TracChangeset for help on using the changeset viewer.