Changeset 13356


Ignore:
Timestamp:
11/24/15 12:56:04 (3 years ago)
Author:
abeham
Message:

#2521: working on multi-encoding

Location:
branches/ProblemRefactoring
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Individuals/MultiSolution.cs

    r13351 r13356  
    2828
    2929namespace HeuristicLab.Optimization {
    30   [Item("MultiSolution", "A solution that consists of other solutions.")]
     30  [Item("CombinedSolution", "A solution that consists of other solutions.")]
    3131  [StorableClass]
    32   public sealed class MultiSolution : Item, ISolution {
     32  public sealed class CombinedSolution : Item, ISolution {
    3333
    3434    private MultiEncoding Encoding { get; set; }
     
    3838
    3939    [StorableConstructor]
    40     private MultiSolution(bool deserializing) : base(deserializing) { }
     40    private CombinedSolution(bool deserializing) : base(deserializing) { }
    4141
    42     private MultiSolution(MultiSolution original, Cloner cloner)
     42    private CombinedSolution(CombinedSolution original, Cloner cloner)
    4343      : base(original, cloner) {
    4444      Encoding = cloner.Clone(original.Encoding);
     
    4646      solutions = original.solutions.ToDictionary(x => x.Key, x => cloner.Clone(x.Value));
    4747    }
    48     public MultiSolution(MultiEncoding encoding, IScope scope) {
     48    public CombinedSolution(IScope scope, MultiEncoding encoding) {
    4949      Encoding = encoding;
    5050      Scope = scope;
     
    5454
    5555    public override IDeepCloneable Clone(Cloner cloner) {
    56       return new MultiSolution(this, cloner);
     56      return new CombinedSolution(this, cloner);
    5757    }
    5858
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/IMultiEncodingOperator.cs

    r13351 r13356  
    2424namespace HeuristicLab.Optimization {
    2525  public interface IMultiEncodingOperator : IOperator {
    26     void AddEncoding(IEncoding encoding);
    27     bool RemoveEncoding(IEncoding encoding);
     26    MultiEncoding Encoding { get; set; }
    2827  }
    2928}
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/MultiEncoding.cs

    r13351 r13356  
    3131  [Item("MultiEncoding", "Describes a combined encoding consisting of multiple simpler encodings.")]
    3232  [StorableClass]
    33   public sealed class MultiEncoding : Encoding<MultiSolution> {
     33  public sealed class MultiEncoding : Encoding<CombinedSolution> {
    3434
    35     private readonly List<IEncoding> encodings;
     35    private readonly ItemCollection<IEncoding> encodings;
     36
    3637    [Storable]
    37     public IEnumerable<IEncoding> Encodings {
     38    private IEnumerable<IEncoding> StorableEncodings {
    3839      get { return encodings; }
    39       private set { encodings.AddRange(value); }
     40      set { encodings.AddRange(value); }
     41    }
     42
     43    public ReadOnlyItemCollection<IEncoding> Encodings {
     44      get { return encodings.AsReadOnly(); }
    4045    }
    4146
     
    4348    private MultiEncoding(bool deserializing)
    4449      : base(deserializing) {
    45       encodings = new List<IEncoding>();
     50      encodings = new ItemCollection<IEncoding>();
    4651    }
    4752    public override IDeepCloneable Clone(Cloner cloner) { return new MultiEncoding(this, cloner); }
    4853    private MultiEncoding(MultiEncoding original, Cloner cloner)
    4954      : base(original, cloner) {
    50       encodings = new List<IEncoding>(original.Encodings.Select(cloner.Clone));
     55      encodings = new ItemCollection<IEncoding>(original.Encodings.Select(cloner.Clone));
    5156    }
    5257    public MultiEncoding()
    5358      : base("MultiEncoding") {
    54       encodings = new List<IEncoding>();
    55       SolutionCreator = new MultiEncodingCreator();
    56 
    57       foreach (var @operator in ApplicationManager.Manager.GetInstances<IMultiEncodingOperator>())
     59      encodings = new ItemCollection<IEncoding>();
     60      SolutionCreator = new MultiEncodingCreator() { Encoding = this };
     61      foreach (var @operator in ApplicationManager.Manager.GetInstances<IMultiEncodingOperator>()) {
     62        @operator.Encoding = this;
    5863        AddOperator(@operator);
     64      }
    5965    }
    6066
     
    6369      if (Encodings.Any(e => e.Name == encoding.Name)) throw new ArgumentException("Encoding name must be unique", "encoding.Name");
    6470      encodings.Add(encoding);
    65 
    6671      Parameters.AddRange(encoding.Parameters);
    67 
    68       foreach (var @operator in Operators.OfType<IMultiEncodingOperator>()) {
    69         @operator.AddEncoding(encoding);
    70       }
    71       OnEncodingsChanged();
    7272      return this;
    7373    }
     
    7676      var success = encodings.Remove(encoding);
    7777      Parameters.RemoveRange(encoding.Parameters);
    78       foreach (var @operator in Operators.OfType<IMultiEncodingOperator>()) {
    79         @operator.RemoveEncoding(encoding);
    80       }
    81       OnEncodingsChanged();
    8278      return success;
    8379    }
    8480
    85     public override void ConfigureOperators(IEnumerable<IOperator> operators) {
    86     }
    87 
    88     public event EventHandler EncodingsChanged;
    89     private void OnEncodingsChanged() {
    90       var handler = EncodingsChanged;
    91       if (handler != null) handler(this, EventArgs.Empty);
    92     }
     81    public override void ConfigureOperators(IEnumerable<IOperator> operators) { }
    9382  }
    9483}
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/MultiEncodingCreator.cs

    r13351 r13356  
    2424using HeuristicLab.Common;
    2525using HeuristicLab.Core;
     26using HeuristicLab.Parameters;
    2627using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2728
     
    2930  [Item("MultiEncodingCreator", "Contains solution creators that together create a multi-encoding.")]
    3031  [StorableClass]
    31   public sealed class MultiEncodingCreator : MultiEncodingOperator<ISolutionCreator>, ISolutionCreator<MultiSolution> {
     32  public sealed class MultiEncodingCreator : MultiEncodingOperator<ISolutionCreator>, ISolutionCreator<CombinedSolution> {
     33
    3234    [StorableConstructor]
    3335    private MultiEncodingCreator(bool deserializing) : base(deserializing) { }
    3436    private MultiEncodingCreator(MultiEncodingCreator original, Cloner cloner) : base(original, cloner) { }
    35     public MultiEncodingCreator() { }
     37    public MultiEncodingCreator() : base() { }
    3638
    3739    public override IDeepCloneable Clone(Cloner cloner) {
     
    3941    }
    4042
    41     public override void AddEncoding(IEncoding encoding) {
     43    protected override void AddEncoding(IEncoding encoding) {
    4244      base.AddEncoding(encoding);
    4345      var parameter = GetParameter(encoding);
     
    4648    }
    4749
    48     public override bool RemoveEncoding(IEncoding encoding) {
     50    protected override bool RemoveEncoding(IEncoding encoding) {
    4951      var success = base.RemoveEncoding(encoding);
    5052      encoding.SolutionCreatorChanged -= Encoding_SolutionCreatorChanged;
     
    6163      parameter.Value = encoding.SolutionCreator;
    6264    }
     65
     66
     67    public override IOperation InstrumentedApply() {
     68      CombinedSolutionParameter.ActualValue = new CombinedSolution(ExecutionContext.Scope, Encoding);
     69      return base.Apply();
     70    }
    6371  }
    6472}
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/MultiEncodingCrossover.cs

    r12012 r13356  
    2222using HeuristicLab.Common;
    2323using HeuristicLab.Core;
     24using HeuristicLab.Parameters;
    2425using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2526
     
    2829  [StorableClass]
    2930  public sealed class MultiEncodingCrossover : MultiEncodingOperator<ICrossover>, ICrossover {
     31
    3032    [StorableConstructor]
    3133    private MultiEncodingCrossover(bool deserializing) : base(deserializing) { }
    3234    private MultiEncodingCrossover(MultiEncodingCrossover original, Cloner cloner) : base(original, cloner) { }
    33     public MultiEncodingCrossover() { }
     35    public MultiEncodingCrossover() : base() { }
    3436
    3537    public override IDeepCloneable Clone(Cloner cloner) { return new MultiEncodingCrossover(this, cloner); }
     38
     39
     40    public override IOperation InstrumentedApply() {
     41      CombinedSolutionParameter.ActualValue = new CombinedSolution(ExecutionContext.Scope, Encoding);
     42      return base.Apply();
     43    }
    3644  }
    3745}
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/MultiEncodingManipulator.cs

    r12012 r13356  
    3030    [StorableConstructor]
    3131    private MultiEncodingManipulator(bool deserializing) : base(deserializing) { }
    32     private MultiEncodingManipulator(MultiEncodingManipulator original, Cloner cloner)
    33       : base(original, cloner) { }
    34     public MultiEncodingManipulator() { }
     32    private MultiEncodingManipulator(MultiEncodingManipulator original, Cloner cloner) : base(original, cloner) { }
     33    public MultiEncodingManipulator() : base() { }
    3534
    3635    public override IDeepCloneable Clone(Cloner cloner) { return new MultiEncodingManipulator(this, cloner); }
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/MultiEncodingOperator.cs

    r13351 r13356  
    2323using System.Collections.Generic;
    2424using System.Linq;
     25using HeuristicLab.Collections;
    2526using HeuristicLab.Common;
    2627using HeuristicLab.Core;
     
    3132namespace HeuristicLab.Optimization {
    3233  [StorableClass]
    33   public abstract class MultiEncodingOperator<T> : Operator, IMultiEncodingOperator where T : class, IOperator {
    34     private List<IEncoding> encodings = new List<IEncoding>();
    35     [Storable(Name = "Encodings")]
    36     private IEnumerable<IEncoding> StorableEncodings {
    37       get { return encodings; }
    38       set { encodings = new List<IEncoding>(value); }
     34  public abstract class MultiEncodingOperator<T> : InstrumentedOperator, IMultiEncodingOperator where T : class, IOperator {
     35    [Storable]
     36    private MultiEncoding encoding;
     37    public MultiEncoding Encoding {
     38      get { return encoding; }
     39      set {
     40        if (value == null) throw new ArgumentNullException("Encoding must not be null.");
     41        if (value == encoding) return;
     42        if (encoding != null) DeregisterEventHandlers();
     43        encoding = value;
     44        CombinedSolutionParameter.ActualName = encoding.Name;
     45        RegisterEventHandlers();
     46      }
     47    }
     48
     49    public ILookupParameter<CombinedSolution> CombinedSolutionParameter {
     50      get { return (ILookupParameter<CombinedSolution>)Parameters["CombinedSolution"]; }
    3951    }
    4052
    4153    [StorableConstructor]
    42     protected MultiEncodingOperator(bool deserializing)
    43       : base(deserializing) {
    44     }
    45 
     54    protected MultiEncodingOperator(bool deserializing) : base(deserializing) { }
    4655    protected MultiEncodingOperator(MultiEncodingOperator<T> original, Cloner cloner)
    4756      : base(original, cloner) {
    48       encodings = new List<IEncoding>(original.encodings.Select(cloner.Clone));
    49       foreach (var encoding in encodings)
    50         encoding.OperatorsChanged += Encoding_OperatorsChanged;
     57      encoding = cloner.Clone(original.encoding);
     58      RegisterEventHandlers();
    5159    }
    52 
    53     protected MultiEncodingOperator() : base() { }
     60    protected MultiEncodingOperator()
     61      : base() {
     62      Parameters.Add(new LookupParameter<CombinedSolution>("CombinedSolution", "The combined solution that gets created."));
     63    }
    5464
    5565    [StorableHook(HookType.AfterDeserialization)]
    5666    private void AfterDeserialization() {
    57       foreach (var encoding in encodings)
    58         encoding.OperatorsChanged += Encoding_OperatorsChanged;
     67      RegisterEventHandlers();
    5968    }
    6069
     70    private void RegisterEventHandlers() {
     71      encoding.Encodings.ItemsAdded += EncodingsOnItemsChanged;
     72      encoding.Encodings.CollectionReset += EncodingsOnItemsChanged;
     73      encoding.Encodings.ItemsRemoved += EncodingsOnItemsRemoved;
     74      foreach (var enc in encoding.Encodings)
     75        enc.OperatorsChanged += Encoding_OperatorsChanged;
     76    }
    6177
    62     public override IOperation Apply() {
     78    private void DeregisterEventHandlers() {
     79      encoding.Encodings.ItemsAdded -= EncodingsOnItemsChanged;
     80      encoding.Encodings.CollectionReset -= EncodingsOnItemsChanged;
     81      encoding.Encodings.ItemsRemoved -= EncodingsOnItemsRemoved;
     82      foreach (var enc in encoding.Encodings)
     83        enc.OperatorsChanged -= Encoding_OperatorsChanged;
     84    }
     85
     86    private void EncodingsOnItemsChanged(object sender, CollectionItemsChangedEventArgs<IEncoding> e) {
     87      foreach (var enc in e.Items)
     88        AddEncoding(enc);
     89      foreach (var enc in e.OldItems)
     90        RemoveEncoding(enc);
     91    }
     92
     93    private void EncodingsOnItemsRemoved(object sender, CollectionItemsChangedEventArgs<IEncoding> e) {
     94      foreach (var enc in e.Items)
     95        RemoveEncoding(enc);
     96    }
     97
     98    public override IOperation InstrumentedApply() {
    6399      var operations = Parameters.Select(p => p.ActualValue).OfType<IOperator>().Select(op => ExecutionContext.CreateOperation(op));
    64100      return new OperationCollection(operations);
    65101    }
    66102
    67     public virtual void AddEncoding(IEncoding encoding) {
     103    protected virtual void AddEncoding(IEncoding encoding) {
    68104      if (Parameters.ContainsKey(encoding.Name)) throw new ArgumentException(string.Format("Encoding {0} was already added.", encoding.Name));
    69105
    70       encodings.Add(encoding);
    71106      encoding.OperatorsChanged += Encoding_OperatorsChanged;
    72107
     
    76111    }
    77112
    78     public virtual bool RemoveEncoding(IEncoding encoding) {
    79       if (!encodings.Remove(encoding)) throw new ArgumentException(string.Format("Encoding {0} was not added to the MultiEncoding.", encoding.Name));
     113    protected virtual bool RemoveEncoding(IEncoding encoding) {
     114      if (!Parameters.ContainsKey(encoding.Name)) throw new ArgumentException(string.Format("Encoding {0} was not added to the MultiEncoding.", encoding.Name));
    80115      encoding.OperatorsChanged -= Encoding_OperatorsChanged;
    81116      return Parameters.Remove(encoding.Name);
     
    89124
    90125    private void Encoding_OperatorsChanged(object sender, EventArgs e) {
    91       var encoding = (IEncoding)sender;
    92       var param = GetParameter(encoding);
     126      var enc = (IEncoding)sender;
     127      var param = GetParameter(enc);
    93128
    94129      var oldParameterValue = param.Value;
    95130      param.ValidValues.Clear();
    96       foreach (var op in encoding.Operators.OfType<T>())
     131      foreach (var op in enc.Operators.OfType<T>())
    97132        param.ValidValues.Add(op);
    98133
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/HeuristicLab.Optimization-3.3.csproj

    r13351 r13356  
    122122    <Compile Include="Algorithms\BasicAlgorithm.cs" />
    123123    <Compile Include="BasicProblems\Individuals\MultiSolution.cs" />
     124    <Compile Include="BasicProblems\Interfaces\IMultiEncodingOperator.cs" />
    124125    <Compile Include="BasicProblems\Interfaces\ISingleObjectiveProblem.cs" />
    125126    <Compile Include="BasicProblems\MultiEncoding.cs" />
     
    132133    <Compile Include="BasicProblems\Interfaces\IEncoding.cs" />
    133134    <Compile Include="BasicProblems\Interfaces\IEncodingOperator.cs" />
    134     <Compile Include="BasicProblems\Interfaces\IMultiEncodingOperator.cs" />
    135135    <Compile Include="BasicProblems\Interfaces\IMultiObjectiveProblemDefinition.cs" />
    136136    <Compile Include="BasicProblems\Interfaces\internal\IMultiObjectiveAnalysisOperator.cs" />
  • branches/ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3/MultiObjectiveProgrammableProblems.cs

    r13351 r13356  
    5656  [Creatable(CreatableAttribute.Categories.Problems, Priority = 100)]
    5757  [StorableClass]
    58   public sealed class MultiObjectiveMultiSolutionProgrammableProblem : MultiObjectiveProgrammableProblem<MultiEncoding, MultiSolution> {
     58  public sealed class MultiObjectiveMultiSolutionProgrammableProblem : MultiObjectiveProgrammableProblem<MultiEncoding, CombinedSolution> {
    5959
    6060    [StorableConstructor]
  • branches/ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveProgrammableProblems.cs

    r13351 r13356  
    5656  [Creatable(CreatableAttribute.Categories.Problems, Priority = 100)]
    5757  [StorableClass]
    58   public sealed class SingleObjectiveMultiSolutionProgrammableProblem : SingleObjectiveProgrammableProblem<MultiEncoding, MultiSolution> {
     58  public sealed class SingleObjectiveMultiSolutionProgrammableProblem : SingleObjectiveProgrammableProblem<MultiEncoding, CombinedSolution> {
    5959
    6060    [StorableConstructor]
Note: See TracChangeset for help on using the changeset viewer.