Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/13/15 18:47:19 (9 years ago)
Author:
mkommend
Message:

#2174: First working version of refactored programmable problem.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/MultiEncodingOperator.cs

    r11737 r11753  
    2020#endregion
    2121using System;
     22using System.Collections.Generic;
    2223using System.Linq;
    2324using HeuristicLab.Common;
     
    3031namespace HeuristicLab.Problems.Programmable.Operators {
    3132  public abstract class MultiEncodingOperator<T> : Operator, IMultiEncodingOperator where T : class,IOperator {
     33    private List<IEncoding> encodings = new List<IEncoding>();
     34    [Storable(Name = "Encodings")]
     35    private IEnumerable<IEncoding> StorableEncodings {
     36      get { return encodings; }
     37      set { encodings = new List<IEncoding>(value); }
     38    }
     39
    3240    [StorableConstructor]
    33     protected MultiEncodingOperator(bool deserializing) : base(deserializing) { }
    34     protected MultiEncodingOperator(MultiEncodingOperator<T> original, Cloner cloner) : base(original, cloner) { }
     41    protected MultiEncodingOperator(bool deserializing)
     42      : base(deserializing) {
     43    }
     44
     45    protected MultiEncodingOperator(MultiEncodingOperator<T> original, Cloner cloner)
     46      : base(original, cloner) {
     47      encodings = new List<IEncoding>(original.encodings.Select(cloner.Clone));
     48      foreach (var encoding in encodings)
     49        encoding.OperatorsChanged += Encoding_OperatorsChanged;
     50    }
    3551
    3652    protected MultiEncodingOperator() : base() { }
     53
     54    [StorableHook(HookType.AfterDeserialization)]
     55    private void AfterDeserialization() {
     56      foreach (var encoding in encodings)
     57        encoding.OperatorsChanged += Encoding_OperatorsChanged;
     58    }
    3759
    3860
     
    4466    public virtual void AddEncoding(IEncoding encoding) {
    4567      if (Parameters.ContainsKey(encoding.Name)) throw new ArgumentException(string.Format("Encoding {0} was already added.", encoding.Name));
     68
     69      encodings.Add(encoding);
     70      encoding.OperatorsChanged += Encoding_OperatorsChanged;
     71
    4672      var param = new ConstrainedValueParameter<T>(encoding.Name, new ItemSet<T>(encoding.Operators.OfType<T>()));
    4773      param.Value = param.ValidValues.First();
     
    5076
    5177    public virtual bool RemoveEncoding(IEncoding encoding) {
     78      if (!encodings.Remove(encoding)) throw new ArgumentException(string.Format("Encoding {0} was not added to the MultiEncoding.", encoding.Name));
     79      encoding.OperatorsChanged -= Encoding_OperatorsChanged;
    5280      return Parameters.Remove(encoding.Name);
     81    }
     82
     83    public virtual void ReplaceEncoding(IEncoding oldEncoding, IEncoding newEncoding) {
     84      var oldParameterValue = GetParameter(oldEncoding).Value;
     85      RemoveEncoding(oldEncoding);
     86      AddEncoding(newEncoding);
     87
     88      var parameter = GetParameter(newEncoding);
     89      var newParameterValue = parameter.ValidValues.FirstOrDefault(op => op.GetType() == oldParameterValue.GetType());
     90      if (newParameterValue != null) parameter.Value = newParameterValue;
    5391    }
    5492
     
    5997    }
    6098
     99    private void Encoding_OperatorsChanged(object sender, EventArgs e) {
     100      var encoding = (IEncoding)sender;
     101      var param = GetParameter(encoding);
    61102
     103      var oldParameterValue = param.Value;
     104      param.ValidValues.Clear();
     105      foreach (var op in encoding.Operators.OfType<T>())
     106        param.ValidValues.Add(op);
    62107
     108      var newValue = param.ValidValues.FirstOrDefault(op => op.GetType() == oldParameterValue.GetType());
     109      if (newValue == null) newValue = param.ValidValues.First();
     110      param.Value = newValue;
     111    }
    63112  }
    64113}
Note: See TracChangeset for help on using the changeset viewer.