Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/24/15 16:43:32 (9 years ago)
Author:
abeham
Message:

#2521: fixed recreation of programmable problem's encoding

Location:
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems
Files:
2 edited

Legend:

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

    r13359 r13365  
    8484    }
    8585
     86    public void Clear() {
     87      foreach (var enc in encodings.ToList()) Remove(enc);
     88    }
     89
    8690    public override void ConfigureOperators(IEnumerable<IOperator> operators) {
    8791      foreach (var encOp in operators.OfType<IMultiEncodingOperator>())
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Problem.cs

    r13339 r13365  
    4141    }
    4242
     43    //mkommend necessary for reuse of operators if the encoding changes
     44    private TEncoding oldEncoding;
     45
    4346    public TEncoding Encoding {
    4447      get { return EncodingParameter.Value; }
     
    6467      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."));
    6568      if (Encoding != null) {
     69        oldEncoding = Encoding;
    6670        SolutionCreator = Encoding.SolutionCreator;
    6771        Parameterize();
     
    7276    protected Problem(Problem<TEncoding, TSolution, TEvaluator> original, Cloner cloner)
    7377      : base(original, cloner) {
     78      oldEncoding = cloner.Clone(original.oldEncoding);
    7479      RegisterEvents();
    7580    }
     
    7984    [StorableHook(HookType.AfterDeserialization)]
    8085    private void AfterDeserialization() {
     86      oldEncoding = Encoding;
    8187      RegisterEvents();
    8288    }
     
    96102
    97103    private void Parameterize() {
     104      if (oldEncoding != null) {
     105        AdaptEncodingOperators(oldEncoding, Encoding);
     106        //var oldMultiEncoding = oldEncoding as MultiEncoding;
     107        //if (oldMultiEncoding != null)
     108        //  oldMultiEncoding.EncodingsChanged -= MultiEncodingOnEncodingsChanged;
     109      }
     110      oldEncoding = Encoding;
     111
    98112      foreach (var op in Operators.OfType<IEncodingOperator<TSolution>>())
    99113        op.EncodingParameter.ActualName = EncodingParameter.Name;
     114
     115      SolutionCreator = Encoding.SolutionCreator;
    100116
    101117      //var multiEncoding = Encoding as MultiEncoding;
     
    108124    }
    109125
    110     protected virtual void MultiEncodingOnEncodingsChanged(object sender, EventArgs e) {
    111       OnOperatorsChanged();
     126    private static void AdaptEncodingOperators(IEncoding oldEncoding, IEncoding newEncoding) {
     127      if (oldEncoding.GetType() != newEncoding.GetType()) return;
     128
     129      if (oldEncoding.GetType() == typeof(MultiEncoding)) {
     130        var oldMultiEncoding = (MultiEncoding)oldEncoding;
     131        var newMultiEncoding = (MultiEncoding)newEncoding;
     132        if (!oldMultiEncoding.Encodings.SequenceEqual(newMultiEncoding.Encodings, new TypeEqualityComparer<IEncoding>())) return;
     133
     134        var nestedEncodings = oldMultiEncoding.Encodings.Zip(newMultiEncoding.Encodings, (o, n) => new { oldEnc = o, newEnc = n });
     135        foreach (var multi in nestedEncodings)
     136          AdaptEncodingOperators(multi.oldEnc, multi.newEnc);
     137      }
     138
     139      var comparer = new TypeEqualityComparer<IOperator>();
     140      var cloner = new Cloner();
     141      var oldOperators = oldEncoding.Operators;
     142      var newOperators = newEncoding.Operators;
     143
     144      cloner.RegisterClonedObject(oldEncoding, newEncoding);
     145      var operators = oldOperators.Intersect(newOperators, comparer)
     146                                  .Select(cloner.Clone)
     147                                  .Union(newOperators, comparer).ToList();
     148
     149      newEncoding.ConfigureOperators(operators);
     150      newEncoding.Operators = operators;
    112151    }
    113152  }
Note: See TracChangeset for help on using the changeset viewer.