Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1614_GeneralizedQAP/HeuristicLab.Optimization/3.3/BasicProblems/BasicProblem.cs @ 17975

Last change on this file since 17975 was 16728, checked in by abeham, 6 years ago

#1614: updated to new persistence and .NET 4.6.1

File size: 6.6 KB
RevLine 
[11739]1#region License Information
2/* HeuristicLab
[16728]3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[11739]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;
[16728]28using HEAL.Attic;
[11739]29
[11949]30namespace HeuristicLab.Optimization {
[16728]31  [StorableType("D877082E-9E77-4CB1-ABDB-35F63878E116")]
[11814]32  public abstract class BasicProblem<TEncoding, TEvaluator> : HeuristicOptimizationProblem<TEvaluator, ISolutionCreator>, IProblemDefinition, IStorableContent
[11739]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
[11786]42    //mkommend necessary for reuse of operators if the encoding changes
43    private TEncoding oldEncoding;
44
[11739]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() {
[11753]55      if (Encoding == null) return base.GetOperators();
[11739]56      return base.GetOperators().Concat(Encoding.Operators);
57    }
58    public override IEnumerable<IParameterizedItem> ExecutionContextItems {
[11753]59      get {
60        if (Encoding == null) return base.ExecutionContextItems;
61        return base.ExecutionContextItems.Concat(new[] { Encoding });
62      }
[11739]63    }
64
[11814]65    protected BasicProblem()
[11739]66      : base() {
[15490]67      var encodingParameter = 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      // ABE: disable showing the encoding itself in the run by default as it wastes a lot of space
69      //      note that parameters of the encoding might still show up in the run
70      encodingParameter.GetsCollected = false;
71      Parameters.Add(encodingParameter);
[11753]72      oldEncoding = Encoding;
[11998]73      if(Encoding != null) Parameterize();
[11739]74      RegisterEvents();
75    }
76
[11814]77    protected BasicProblem(BasicProblem<TEncoding, TEvaluator> original, Cloner cloner)
[11739]78      : base(original, cloner) {
[11753]79      oldEncoding = cloner.Clone(original.oldEncoding);
[11739]80      RegisterEvents();
81    }
82
83    [StorableConstructor]
[16728]84    protected BasicProblem(StorableConstructorFlag _) : base(_) { }
[11753]85    [StorableHook(HookType.AfterDeserialization)]
86    private void AfterDeserialization() {
87      oldEncoding = Encoding;
[11739]88      RegisterEvents();
89    }
90
91    private void RegisterEvents() {
92      EncodingParameter.ValueChanged += (o, e) => OnEncodingChanged();
[11970]93      var multiEncoding = Encoding as MultiEncoding;
94      if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged;
[11739]95    }
96
97    protected virtual void OnEncodingChanged() {
[11996]98      Parameterize();
99
100      OnOperatorsChanged();
101      OnReset();
102    }
103
104    private void Parameterize() {
[11970]105      if (oldEncoding != null) {
106        AdaptEncodingOperators(oldEncoding, Encoding);
107        var oldMultiEncoding = oldEncoding as MultiEncoding;
108        if (oldMultiEncoding != null)
109          oldMultiEncoding.EncodingsChanged -= MultiEncodingOnEncodingsChanged;
110      }
[11753]111      oldEncoding = Encoding;
112
[11786]113      foreach (var op in Operators.OfType<IEncodingOperator>())
114        op.EncodingParameter.ActualName = EncodingParameter.Name;
115
[11739]116      var solutionCreatorType = Encoding.GetType().BaseType.GetGenericArguments();
117      var paramType = typeof(ValueParameter<>).MakeGenericType(solutionCreatorType);
118      var solutionCreatorParam = (IParameter)Activator.CreateInstance(paramType, SolutionCreatorParameter.Name, SolutionCreatorParameter.Description,
119        Encoding.SolutionCreator);
120      Parameters.Remove(SolutionCreatorParameter);
[11996]121      Parameters.Add(solutionCreatorParam);
122      ((IValueParameter)solutionCreatorParam).ValueChanged += SolutionCreatorParameter_ValueChanged;
[11753]123
[11970]124      var multiEncoding = Encoding as MultiEncoding;
125      if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged;
[11739]126    }
127
128    protected override void OnSolutionCreatorChanged() {
129      base.OnSolutionCreatorChanged();
130      Encoding.SolutionCreator = SolutionCreator;
131    }
132
[11753]133    private static void AdaptEncodingOperators(IEncoding oldEncoding, IEncoding newEncoding) {
134      if (oldEncoding.GetType() != newEncoding.GetType()) return;
135
136      if (oldEncoding.GetType() == typeof(MultiEncoding)) {
137        var oldMultiEncoding = (MultiEncoding)oldEncoding;
138        var newMultiEncoding = (MultiEncoding)newEncoding;
139        if (!oldMultiEncoding.Encodings.SequenceEqual(newMultiEncoding.Encodings, new TypeEqualityComparer<IEncoding>())) return;
140
141        var nestedEncodings = oldMultiEncoding.Encodings.Zip(newMultiEncoding.Encodings, (o, n) => new { oldEnc = o, newEnc = n });
142        foreach (var multi in nestedEncodings)
143          AdaptEncodingOperators(multi.oldEnc, multi.newEnc);
144      }
145
146      var comparer = new TypeEqualityComparer<IOperator>();
[11813]147      var cloner = new Cloner();
[11753]148      var oldOperators = oldEncoding.Operators;
149      var newOperators = newEncoding.Operators;
150
[11885]151      cloner.RegisterClonedObject(oldEncoding, newEncoding);
152      var operators = oldOperators.Intersect(newOperators, comparer)
153                                  .Select(cloner.Clone)
154                                  .Union(newOperators, comparer).ToList();
[11813]155
[11753]156      newEncoding.ConfigureOperators(operators);
157      newEncoding.Operators = operators;
158    }
159
[11970]160    protected virtual void MultiEncodingOnEncodingsChanged(object sender, EventArgs e) {
161      OnOperatorsChanged();
162    }
[11739]163  }
164}
Note: See TracBrowser for help on using the repository browser.