source: branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Problem.cs @ 13376

Last change on this file since 13376 was 13376, checked in by abeham, 4 years ago

#2521: working on refactoring

File size: 5.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Optimization {
31  [StorableClass]
32  public abstract class Problem<TEncoding, TSolution, TEvaluator> : HeuristicOptimizationProblem<TEvaluator, ISolutionCreator<TSolution>>, IProblemDefinition<TEncoding, TSolution>, IStorableContent
33    where TEncoding : class, IEncoding<TSolution>
34    where TSolution : class, ISolution
35    where TEvaluator : class, IEvaluator {
36
37    public string Filename { get; set; }
38
39    protected IValueParameter<TEncoding> EncodingParameter {
40      get { return (IValueParameter<TEncoding>)Parameters["Encoding"]; }
41    }
42
43    //mkommend necessary for reuse of operators if the encoding changes
44    private TEncoding oldEncoding;
45
46    public TEncoding Encoding {
47      get { return EncodingParameter.Value; }
48      protected set {
49        if (value == null) throw new ArgumentNullException("Encoding must not be null.");
50        EncodingParameter.Value = value;
51      }
52    }
53
54    protected override IEnumerable<IItem> GetOperators() {
55      if (Encoding == null) return base.GetOperators();
56      return base.GetOperators().Concat(Encoding.Operators);
57    }
58    public override IEnumerable<IParameterizedItem> ExecutionContextItems {
59      get {
60        if (Encoding == null) return base.ExecutionContextItems;
61        return base.ExecutionContextItems.Concat(new[] { Encoding });
62      }
63    }
64
65    protected Problem()
66      : base() {
67      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."));
68      if (Encoding != null) {
69        oldEncoding = Encoding;
70        SolutionCreator = Encoding.SolutionCreator;
71        Parameterize();
72      }
73      RegisterEvents();
74    }
75
76    protected Problem(Problem<TEncoding, TSolution, TEvaluator> original, Cloner cloner)
77      : base(original, cloner) {
78      oldEncoding = cloner.Clone(original.oldEncoding);
79      RegisterEvents();
80    }
81
82    [StorableConstructor]
83    protected Problem(bool deserializing) : base(deserializing) { }
84    [StorableHook(HookType.AfterDeserialization)]
85    private void AfterDeserialization() {
86      oldEncoding = Encoding;
87      RegisterEvents();
88    }
89
90    private void RegisterEvents() {
91      EncodingParameter.ValueChanged += (o, e) => OnEncodingChanged();
92      //var multiEncoding = Encoding as MultiEncoding;
93      //if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged;
94    }
95
96    protected virtual void OnEncodingChanged() {
97      Parameterize();
98
99      OnOperatorsChanged();
100      OnReset();
101    }
102
103    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
112      foreach (var op in Operators.OfType<IEncodingOperator<TSolution>>())
113        op.EncodingParameter.ActualName = EncodingParameter.Name;
114
115      SolutionCreator = Encoding.SolutionCreator;
116
117      //var multiEncoding = Encoding as MultiEncoding;
118      //if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged;
119    }
120
121    protected override void OnSolutionCreatorChanged() {
122      base.OnSolutionCreatorChanged();
123      Encoding.SolutionCreator = SolutionCreator;
124    }
125
126    private static void AdaptEncodingOperators(IEncoding oldEncoding, IEncoding newEncoding) {
127      if (oldEncoding.GetType() != newEncoding.GetType()) return;
128
129      if (oldEncoding.GetType() == typeof(CombinedEncoding)) {
130        var oldMultiEncoding = (CombinedEncoding)oldEncoding;
131        var newMultiEncoding = (CombinedEncoding)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;
151    }
152  }
153}
Note: See TracBrowser for help on using the repository browser.