Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Optimization/3.3/BasicProblems/BasicProblem.cs @ 15109

Last change on this file since 15109 was 14186, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

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