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
Files:
4 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  }
  • branches/ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3/MultiObjectiveProgrammableProblem.cs

    r13350 r13365  
    7171      : base() {
    7272      Parameters.Add(new FixedValueParameter<MultiObjectiveProblemDefinitionScript<TEncoding, TSolution>>("ProblemScript", "Defines the problem.",
    73         new MultiObjectiveProblemDefinitionScript<TEncoding, TSolution>() { Name = Name, Encoding = Encoding }));
     73        new MultiObjectiveProblemDefinitionScript<TEncoding, TSolution>() { Name = Name }));
     74      ProblemScript.Encoding = Encoding = (TEncoding)Encoding.Clone();
    7475      RegisterEvents();
    7576    }
     
    8788      Parameters.Remove("Maximization");
    8889      Parameters.Add(new ValueParameter<BoolArray>("Maximization", "Set to false if the problem should be minimized.", (BoolArray)new BoolArray(Maximization).AsReadOnly()) { Hidden = true });
     90      var multiEnc = ProblemScript.Encoding as MultiEncoding;
     91      if (multiEnc != null) multiEnc.Clear();
     92      ProblemScript.Initialize();
     93      Encoding = (TEncoding)ProblemScript.Encoding.Clone();
    8994
    90       ProblemScript.Initialize();
    9195      OnOperatorsChanged();
    9296      OnReset();
  • branches/ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveProgrammableProblem.cs

    r13350 r13365  
    7373      : base() {
    7474      Parameters.Add(new FixedValueParameter<SingleObjectiveProblemDefinitionScript<TEncoding, TSolution>>("ProblemScript", "Defines the problem.",
    75         new SingleObjectiveProblemDefinitionScript<TEncoding, TSolution>() { Name = Name, Encoding = Encoding }));
     75        new SingleObjectiveProblemDefinitionScript<TEncoding, TSolution>() { Name = Name }));
     76      ProblemScript.Encoding = Encoding = (TEncoding)Encoding.Clone();
    7677      Operators.Add(new BestScopeSolutionAnalyzer());
    7778      RegisterEvents();
     
    9091      Parameters.Remove("Maximization");
    9192      Parameters.Add(new FixedValueParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized.", (BoolValue)new BoolValue(Maximization).AsReadOnly()) { Hidden = true });
     93      var multiEnc = ProblemScript.Encoding as MultiEncoding;
     94      if (multiEnc != null) multiEnc.Clear();
    9295      ProblemScript.Initialize();
     96      Encoding = (TEncoding)ProblemScript.Encoding.Clone();
    9397
    9498      OnOperatorsChanged();
Note: See TracChangeset for help on using the changeset viewer.