Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/13/15 18:47:19 (10 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/New/ProgrammableProblem.cs

    r11739 r11753  
    2828using HeuristicLab.Parameters;
    2929using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     30using HeuristicLab.Problems.Programmable.Interfaces;
    3031
    3132namespace HeuristicLab.Problems.Programmable {
     
    4647      protected set {
    4748        if (value == null) throw new ArgumentNullException("Encoding must not be null.");
    48         if (Encoding != null) Parameters.RemoveRange(Encoding.Parameters);
    4949        EncodingParameter.Value = value;
    50         Parameters.AddRange(Encoding.Parameters);
    5150      }
    5251    }
     52    //mkommend necessary for reuse of operators if the encoding changes
     53    private TEncoding oldEncoding;
    5354
    5455    public virtual IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) {
     
    5758
    5859    protected override IEnumerable<IItem> GetOperators() {
     60      if (Encoding == null) return base.GetOperators();
    5961      return base.GetOperators().Concat(Encoding.Operators);
    6062    }
    6163    public override IEnumerable<IParameterizedItem> ExecutionContextItems {
    62       get { return base.ExecutionContextItems.Concat(new[] { Encoding }); }
     64      get {
     65        if (Encoding == null) return base.ExecutionContextItems;
     66        return base.ExecutionContextItems.Concat(new[] { Encoding });
     67      }
    6368    }
    6469
    65     public ProgrammableProblem()
     70    protected ProgrammableProblem()
    6671      : base() {
    6772      Parameters.Add(new ValueParameter<TEncoding>("Encoding", "Describes the configuration of the encoding, what the variables are called, what type they are and their bounds if any."));
     73      oldEncoding = Encoding;
    6874      RegisterEvents();
    6975    }
     
    7177    protected ProgrammableProblem(ProgrammableProblem<TEncoding, TEvaluator> original, Cloner cloner)
    7278      : base(original, cloner) {
     79      oldEncoding = cloner.Clone(original.oldEncoding);
    7380      RegisterEvents();
    7481    }
    7582
    7683    [StorableConstructor]
    77     protected ProgrammableProblem(bool deserializing)
    78       : base(deserializing) {
     84    protected ProgrammableProblem(bool deserializing) : base(deserializing) { }
     85    [StorableHook(HookType.AfterDeserialization)]
     86    private void AfterDeserialization() {
     87      oldEncoding = Encoding;
    7988      RegisterEvents();
    8089    }
     
    8594
    8695    protected virtual void OnEncodingChanged() {
     96      if (oldEncoding != null) AdaptEncodingOperators(oldEncoding, Encoding);
     97      oldEncoding = Encoding;
     98
    8799      var solutionCreatorType = Encoding.GetType().BaseType.GetGenericArguments();
    88100      var paramType = typeof(ValueParameter<>).MakeGenericType(solutionCreatorType);
     
    91103      Parameters.Remove(SolutionCreatorParameter);
    92104      Parameters.Add(solutionCreatorParam);
     105
     106      OnOperatorsChanged();
     107      OnReset();
    93108    }
    94109
     
    106121    }
    107122
     123    private static void AdaptEncodingOperators(IEncoding oldEncoding, IEncoding newEncoding) {
     124      if (oldEncoding.GetType() != newEncoding.GetType()) return;
     125
     126      if (oldEncoding.GetType() == typeof(MultiEncoding)) {
     127        var oldMultiEncoding = (MultiEncoding)oldEncoding;
     128        var newMultiEncoding = (MultiEncoding)newEncoding;
     129        if (!oldMultiEncoding.Encodings.SequenceEqual(newMultiEncoding.Encodings, new TypeEqualityComparer<IEncoding>())) return;
     130
     131        var nestedEncodings = oldMultiEncoding.Encodings.Zip(newMultiEncoding.Encodings, (o, n) => new { oldEnc = o, newEnc = n });
     132        foreach (var multi in nestedEncodings)
     133          AdaptEncodingOperators(multi.oldEnc, multi.newEnc);
     134
     135        foreach (var op in oldMultiEncoding.Operators.OfType<IMultiEncodingOperator>())
     136          foreach (var multi in nestedEncodings)
     137            op.ReplaceEncoding(multi.oldEnc, multi.newEnc);
     138      }
     139
     140      var comparer = new TypeEqualityComparer<IOperator>();
     141      var oldOperators = oldEncoding.Operators;
     142      var newOperators = newEncoding.Operators;
     143
     144      var operators = oldOperators.Intersect(newOperators, comparer);
     145      operators = operators.Union(newOperators, comparer);
     146
     147      newEncoding.ConfigureOperators(operators);
     148      newEncoding.Operators = operators;
     149    }
     150
    108151  }
    109152}
Note: See TracChangeset for help on using the changeset viewer.