Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization/3.3/BasicProblems/BasicProblem.cs @ 11982

Last change on this file since 11982 was 11982, checked in by mkommend, 10 years ago

#2174: Reregistered solution creator changed event in basic problem.

File size: 6.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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 BasicProblem<TEncoding, TEvaluator> : HeuristicOptimizationProblem<TEvaluator, ISolutionCreator>, IProblemDefinition, IStorableContent
33    where TEncoding : class, IEncoding
34    where TEvaluator : class, IEvaluator {
35
36    public string Filename { get; set; }
37
38    protected IValueParameter<TEncoding> EncodingParameter {
39      get { return (IValueParameter<TEncoding>)Parameters["Encoding"]; }
40    }
41
42    //mkommend necessary for reuse of operators if the encoding changes
43    private TEncoding oldEncoding;
44
45    IEncoding IProblemDefinition.Encoding { get { return Encoding; } }
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 BasicProblem()
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      oldEncoding = Encoding;
69      RegisterEvents();
70    }
71
72    protected BasicProblem(BasicProblem<TEncoding, TEvaluator> original, Cloner cloner)
73      : base(original, cloner) {
74      oldEncoding = cloner.Clone(original.oldEncoding);
75      RegisterEvents();
76    }
77
78    [StorableConstructor]
79    protected BasicProblem(bool deserializing) : base(deserializing) { }
80    [StorableHook(HookType.AfterDeserialization)]
81    private void AfterDeserialization() {
82      oldEncoding = Encoding;
83      RegisterEvents();
84    }
85
86    private void RegisterEvents() {
87      EncodingParameter.ValueChanged += (o, e) => OnEncodingChanged();
88      var multiEncoding = Encoding as MultiEncoding;
89      if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged;
90    }
91
92    protected virtual void OnEncodingChanged() {
93      if (oldEncoding != null) {
94        AdaptEncodingOperators(oldEncoding, Encoding);
95        var oldMultiEncoding = oldEncoding as MultiEncoding;
96        if (oldMultiEncoding != null)
97          oldMultiEncoding.EncodingsChanged -= MultiEncodingOnEncodingsChanged;
98      }
99      oldEncoding = Encoding;
100
101      foreach (var op in Operators.OfType<IEncodingOperator>())
102        op.EncodingParameter.ActualName = EncodingParameter.Name;
103
104      var solutionCreatorType = Encoding.GetType().BaseType.GetGenericArguments();
105      var paramType = typeof(ValueParameter<>).MakeGenericType(solutionCreatorType);
106      var solutionCreatorParam = (IParameter)Activator.CreateInstance(paramType, SolutionCreatorParameter.Name, SolutionCreatorParameter.Description,
107        Encoding.SolutionCreator);
108      Parameters.Remove(SolutionCreatorParameter);
109      Parameters.Add(solutionCreatorParam); 
110      ((IValueParameter)solutionCreatorParam).ValueChanged += SolutionCreatorParameter_ValueChanged;     
111
112      var multiEncoding = Encoding as MultiEncoding;
113      if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged;
114
115      OnOperatorsChanged();
116      OnReset();
117    }
118
119    protected override void OnSolutionCreatorChanged() {
120      base.OnSolutionCreatorChanged();
121      Encoding.SolutionCreator = SolutionCreator;
122    }
123
124    private static void AdaptEncodingOperators(IEncoding oldEncoding, IEncoding newEncoding) {
125      if (oldEncoding.GetType() != newEncoding.GetType()) return;
126
127      if (oldEncoding.GetType() == typeof(MultiEncoding)) {
128        var oldMultiEncoding = (MultiEncoding)oldEncoding;
129        var newMultiEncoding = (MultiEncoding)newEncoding;
130        if (!oldMultiEncoding.Encodings.SequenceEqual(newMultiEncoding.Encodings, new TypeEqualityComparer<IEncoding>())) return;
131
132        var nestedEncodings = oldMultiEncoding.Encodings.Zip(newMultiEncoding.Encodings, (o, n) => new { oldEnc = o, newEnc = n });
133        foreach (var multi in nestedEncodings)
134          AdaptEncodingOperators(multi.oldEnc, multi.newEnc);
135      }
136
137      var comparer = new TypeEqualityComparer<IOperator>();
138      var cloner = new Cloner();
139      var oldOperators = oldEncoding.Operators;
140      var newOperators = newEncoding.Operators;
141
142      cloner.RegisterClonedObject(oldEncoding, newEncoding);
143      var operators = oldOperators.Intersect(newOperators, comparer)
144                                  .Select(cloner.Clone)
145                                  .Union(newOperators, comparer).ToList();
146
147      newEncoding.ConfigureOperators(operators);
148      newEncoding.Operators = operators;
149    }
150
151    protected virtual void MultiEncodingOnEncodingsChanged(object sender, EventArgs e) {
152      OnOperatorsChanged();
153    }
154  }
155}
Note: See TracBrowser for help on using the repository browser.