Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13402 was 13396, checked in by abeham, 9 years ago

#2521:

  • Refactored QuadraticAssignmentProblem to use new SingleObjectiveProblem
    • Removed QAPEvaluator
    • Adapted RobustTabooSearch
  • Introduced several interfaces in PermutationEncoding necessary for wiring
  • Changed all Encodings to use IItem instead of IOperator in ConfigureOperators (name still unchanged)
  • Added a protected MaximizationParameter property in SingleObjectiveProblem (necessary for wiring)
  • Changed AlleleFrequnencyAnalyzer to use ISolution interface instead of IItem
  • Added a comment to ISolutionCreator<TSolution> of some changes that would be welcomed
File size: 6.3 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; } // TODO: Really okay here?
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    protected Problem(TEncoding encoding) {
76      if (encoding == null) throw new ArgumentNullException("encoding");
77      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.", encoding));
78      oldEncoding = Encoding;
79      SolutionCreator = Encoding.SolutionCreator;
80      Parameterize();
81
82      RegisterEvents();
83    }
84
85    protected Problem(Problem<TEncoding, TSolution, TEvaluator> original, Cloner cloner)
86      : base(original, cloner) {
87      oldEncoding = cloner.Clone(original.oldEncoding);
88      RegisterEvents();
89    }
90
91    [StorableConstructor]
92    protected Problem(bool deserializing) : base(deserializing) { }
93    [StorableHook(HookType.AfterDeserialization)]
94    private void AfterDeserialization() {
95      oldEncoding = Encoding;
96      RegisterEvents();
97    }
98
99    private void RegisterEvents() {
100      EncodingParameter.ValueChanged += (o, e) => OnEncodingChanged();
101      //var multiEncoding = Encoding as MultiEncoding;
102      //if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged;
103    }
104
105    protected virtual void OnEncodingChanged() {
106      Parameterize();
107
108      OnOperatorsChanged();
109      OnReset();
110    }
111
112    private void Parameterize() {
113      if (oldEncoding != null) {
114        AdaptEncodingOperators(oldEncoding, Encoding);
115        //var oldMultiEncoding = oldEncoding as MultiEncoding;
116        //if (oldMultiEncoding != null)
117        //  oldMultiEncoding.EncodingsChanged -= MultiEncodingOnEncodingsChanged;
118      }
119      oldEncoding = Encoding;
120
121      foreach (var op in Operators.OfType<IEncodingOperator<TSolution>>())
122        op.EncodingParameter.ActualName = EncodingParameter.Name;
123
124      SolutionCreator = Encoding.SolutionCreator;
125
126      //var multiEncoding = Encoding as MultiEncoding;
127      //if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged;
128    }
129
130    protected override void OnSolutionCreatorChanged() {
131      base.OnSolutionCreatorChanged();
132      Encoding.SolutionCreator = SolutionCreator;
133    }
134
135    private static void AdaptEncodingOperators(IEncoding oldEncoding, IEncoding newEncoding) {
136      if (oldEncoding.GetType() != newEncoding.GetType()) return;
137
138      if (oldEncoding.GetType() == typeof(CombinedEncoding)) {
139        var oldMultiEncoding = (CombinedEncoding)oldEncoding;
140        var newMultiEncoding = (CombinedEncoding)newEncoding;
141        if (!oldMultiEncoding.Encodings.SequenceEqual(newMultiEncoding.Encodings, new TypeEqualityComparer<IEncoding>())) return;
142
143        var nestedEncodings = oldMultiEncoding.Encodings.Zip(newMultiEncoding.Encodings, (o, n) => new { oldEnc = o, newEnc = n });
144        foreach (var multi in nestedEncodings)
145          AdaptEncodingOperators(multi.oldEnc, multi.newEnc);
146      }
147
148      var comparer = new TypeEqualityComparer<IOperator>();
149      var cloner = new Cloner();
150      var oldOperators = oldEncoding.Operators;
151      var newOperators = newEncoding.Operators;
152
153      cloner.RegisterClonedObject(oldEncoding, newEncoding);
154      var operators = oldOperators.Intersect(newOperators, comparer)
155                                  .Select(cloner.Clone)
156                                  .Union(newOperators, comparer).ToList();
157
158      newEncoding.ConfigureOperators(operators);
159      newEncoding.Operators = operators;
160    }
161  }
162}
Note: See TracBrowser for help on using the repository browser.