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

Last change on this file since 11753 was 11753, checked in by mkommend, 5 years ago

#2174: First working version of refactored programmable problem.

File size: 6.0 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    IEncoding IProblemDefinition.Encoding { get { return Encoding; } }
45    public TEncoding Encoding {
46      get { return EncodingParameter.Value; }
47      protected set {
48        if (value == null) throw new ArgumentNullException("Encoding must not be null.");
49        EncodingParameter.Value = value;
50      }
51    }
52    //mkommend necessary for reuse of operators if the encoding changes
53    private TEncoding oldEncoding;
54
55    public virtual IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) {
56      return Enumerable.Empty<Individual>();
57    }
58
59    protected override IEnumerable<IItem> GetOperators() {
60      if (Encoding == null) return base.GetOperators();
61      return base.GetOperators().Concat(Encoding.Operators);
62    }
63    public override IEnumerable<IParameterizedItem> ExecutionContextItems {
64      get {
65        if (Encoding == null) return base.ExecutionContextItems;
66        return base.ExecutionContextItems.Concat(new[] { Encoding });
67      }
68    }
69
70    protected ProgrammableProblem()
71      : base() {
72      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."));
73      oldEncoding = Encoding;
74      RegisterEvents();
75    }
76
77    protected ProgrammableProblem(ProgrammableProblem<TEncoding, TEvaluator> original, Cloner cloner)
78      : base(original, cloner) {
79      oldEncoding = cloner.Clone(original.oldEncoding);
80      RegisterEvents();
81    }
82
83    [StorableConstructor]
84    protected ProgrammableProblem(bool deserializing) : base(deserializing) { }
85    [StorableHook(HookType.AfterDeserialization)]
86    private void AfterDeserialization() {
87      oldEncoding = Encoding;
88      RegisterEvents();
89    }
90
91    private void RegisterEvents() {
92      EncodingParameter.ValueChanged += (o, e) => OnEncodingChanged();
93    }
94
95    protected virtual void OnEncodingChanged() {
96      if (oldEncoding != null) AdaptEncodingOperators(oldEncoding, Encoding);
97      oldEncoding = Encoding;
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
116    protected virtual void ParameterizeOperators() {
117      foreach (var op in Operators.OfType<IEncodingOperator>())
118        op.EncodingParameter.ActualName = EncodingParameter.Name;
119      foreach (var op in Operators.OfType<INeighborBasedOperator>())
120        op.GetNeighborsFunc = GetNeighbors;
121    }
122
123    private static void AdaptEncodingOperators(IEncoding oldEncoding, IEncoding newEncoding) {
124      if (oldEncoding.GetType() != newEncoding.GetType()) return;
125
126      if (oldEncoding.GetType() == typeof(MultiEncoding)) {
127        var oldMultiEncoding = (MultiEncoding)oldEncoding;
128        var newMultiEncoding = (MultiEncoding)newEncoding;
129        if (!oldMultiEncoding.Encodings.SequenceEqual(newMultiEncoding.Encodings, new TypeEqualityComparer<IEncoding>())) return;
130
131        var nestedEncodings = oldMultiEncoding.Encodings.Zip(newMultiEncoding.Encodings, (o, n) => new { oldEnc = o, newEnc = n });
132        foreach (var multi in nestedEncodings)
133          AdaptEncodingOperators(multi.oldEnc, multi.newEnc);
134
135        foreach (var op in oldMultiEncoding.Operators.OfType<IMultiEncodingOperator>())
136          foreach (var multi in nestedEncodings)
137            op.ReplaceEncoding(multi.oldEnc, multi.newEnc);
138      }
139
140      var comparer = new TypeEqualityComparer<IOperator>();
141      var oldOperators = oldEncoding.Operators;
142      var newOperators = newEncoding.Operators;
143
144      var operators = oldOperators.Intersect(newOperators, comparer);
145      operators = operators.Union(newOperators, comparer);
146
147      newEncoding.ConfigureOperators(operators);
148      newEncoding.Operators = operators;
149    }
150
151  }
152}
Note: See TracBrowser for help on using the repository browser.