Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/ProgrammableProblem.cs @ 11786

Last change on this file since 11786 was 11786, checked in by mkommend, 9 years ago

#2174: Changed operator parameterization in programmable problem.

File size: 5.6 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.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Problems.Programmable.Interfaces;
31
32namespace HeuristicLab.Problems.Programmable {
33  [StorableClass]
34  public abstract class ProgrammableProblem<TEncoding, TEvaluator> : HeuristicOptimizationProblem<TEvaluator, ISolutionCreator>, IProblemDefinition, IStorableContent
35    where TEncoding : class, IEncoding
36    where TEvaluator : class, IEvaluator {
37
38    public string Filename { get; set; }
39
40    protected IValueParameter<TEncoding> EncodingParameter {
41      get { return (IValueParameter<TEncoding>)Parameters["Encoding"]; }
42    }
43
44    //mkommend necessary for reuse of operators if the encoding changes
45    private TEncoding oldEncoding;
46
47    IEncoding IProblemDefinition.Encoding { get { return Encoding; } }
48    public TEncoding Encoding {
49      get { return EncodingParameter.Value; }
50      protected set {
51        if (value == null) throw new ArgumentNullException("Encoding must not be null.");
52        EncodingParameter.Value = value;
53      }
54    }
55
56    protected override IEnumerable<IItem> GetOperators() {
57      if (Encoding == null) return base.GetOperators();
58      return base.GetOperators().Concat(Encoding.Operators);
59    }
60    public override IEnumerable<IParameterizedItem> ExecutionContextItems {
61      get {
62        if (Encoding == null) return base.ExecutionContextItems;
63        return base.ExecutionContextItems.Concat(new[] { Encoding });
64      }
65    }
66
67    protected ProgrammableProblem()
68      : base() {
69      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."));
70      oldEncoding = Encoding;
71      RegisterEvents();
72    }
73
74    protected ProgrammableProblem(ProgrammableProblem<TEncoding, TEvaluator> original, Cloner cloner)
75      : base(original, cloner) {
76      oldEncoding = cloner.Clone(original.oldEncoding);
77      RegisterEvents();
78    }
79
80    [StorableConstructor]
81    protected ProgrammableProblem(bool deserializing) : base(deserializing) { }
82    [StorableHook(HookType.AfterDeserialization)]
83    private void AfterDeserialization() {
84      oldEncoding = Encoding;
85      RegisterEvents();
86    }
87
88    private void RegisterEvents() {
89      EncodingParameter.ValueChanged += (o, e) => OnEncodingChanged();
90    }
91
92    protected virtual void OnEncodingChanged() {
93      if (oldEncoding != null) AdaptEncodingOperators(oldEncoding, Encoding);
94      oldEncoding = Encoding;
95
96      foreach (var op in Operators.OfType<IEncodingOperator>())
97        op.EncodingParameter.ActualName = EncodingParameter.Name;
98
99      var solutionCreatorType = Encoding.GetType().BaseType.GetGenericArguments();
100      var paramType = typeof(ValueParameter<>).MakeGenericType(solutionCreatorType);
101      var solutionCreatorParam = (IParameter)Activator.CreateInstance(paramType, SolutionCreatorParameter.Name, SolutionCreatorParameter.Description,
102        Encoding.SolutionCreator);
103      Parameters.Remove(SolutionCreatorParameter);
104      Parameters.Add(solutionCreatorParam);
105
106      OnOperatorsChanged();
107      OnReset();
108    }
109
110    protected override void OnSolutionCreatorChanged() {
111      base.OnSolutionCreatorChanged();
112      Encoding.SolutionCreator = SolutionCreator;
113    }
114
115    private static void AdaptEncodingOperators(IEncoding oldEncoding, IEncoding newEncoding) {
116      if (oldEncoding.GetType() != newEncoding.GetType()) return;
117
118      if (oldEncoding.GetType() == typeof(MultiEncoding)) {
119        var oldMultiEncoding = (MultiEncoding)oldEncoding;
120        var newMultiEncoding = (MultiEncoding)newEncoding;
121        if (!oldMultiEncoding.Encodings.SequenceEqual(newMultiEncoding.Encodings, new TypeEqualityComparer<IEncoding>())) return;
122
123        var nestedEncodings = oldMultiEncoding.Encodings.Zip(newMultiEncoding.Encodings, (o, n) => new { oldEnc = o, newEnc = n });
124        foreach (var multi in nestedEncodings)
125          AdaptEncodingOperators(multi.oldEnc, multi.newEnc);
126
127        foreach (var op in oldMultiEncoding.Operators.OfType<IMultiEncodingOperator>())
128          foreach (var multi in nestedEncodings)
129            op.ReplaceEncoding(multi.oldEnc, multi.newEnc);
130      }
131
132      var comparer = new TypeEqualityComparer<IOperator>();
133      var oldOperators = oldEncoding.Operators;
134      var newOperators = newEncoding.Operators;
135
136      var operators = oldOperators.Intersect(newOperators, comparer);
137      operators = operators.Union(newOperators, comparer);
138
139      newEncoding.ConfigureOperators(operators);
140      newEncoding.Operators = operators;
141    }
142
143  }
144}
Note: See TracBrowser for help on using the repository browser.