Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Optimization/3.3/BasicProblems/BasicProblem.cs

Last change on this file was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 7.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 HEAL.Attic;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Parameters;
30
31namespace HeuristicLab.Optimization {
32  [StorableType("D877082E-9E77-4CB1-ABDB-35F63878E116")]
33  public abstract class BasicProblem<TEncoding, TEvaluator> : HeuristicOptimizationProblem<TEvaluator, ISolutionCreator>, IProblemDefinition, IStorableContent
34    where TEncoding : class, IEncoding
35    where TEvaluator : class, IEvaluator {
36
37    public string Filename { get; set; }
38
39    protected IValueParameter<TEncoding> EncodingParameter {
40      get { return (IValueParameter<TEncoding>)Parameters["Encoding"]; }
41    }
42
43    //mkommend necessary for reuse of operators if the encoding changes
44    private TEncoding oldEncoding;
45
46    IEncoding IProblemDefinition.Encoding { get { return Encoding; } }
47    public TEncoding Encoding {
48      get { return EncodingParameter.Value; }
49      protected set {
50        if (value == null) throw new ArgumentNullException("Encoding must not be null.");
51        EncodingParameter.Value = value;
52      }
53    }
54
55    protected override IEnumerable<IItem> GetOperators() {
56      if (Encoding == null) return base.GetOperators();
57      return base.GetOperators().Concat(Encoding.Operators);
58    }
59    public override IEnumerable<IParameterizedItem> ExecutionContextItems {
60      get {
61        if (Encoding == null) return base.ExecutionContextItems;
62        return base.ExecutionContextItems.Concat(new[] { Encoding });
63      }
64    }
65
66    protected BasicProblem()
67      : base() {
68      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."));
69      oldEncoding = Encoding;
70      if(Encoding != null) Parameterize();
71      RegisterEvents();
72    }
73
74    protected BasicProblem(BasicProblem<TEncoding, TEvaluator> original, Cloner cloner)
75      : base(original, cloner) {
76      oldEncoding = cloner.Clone(original.oldEncoding);
77      RegisterEvents();
78    }
79
80    [StorableConstructor]
81    protected BasicProblem(StorableConstructorFlag _) : base(_) { }
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      var multiEncoding = Encoding as MultiEncoding;
91      if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged;
92    }
93
94    protected virtual void OnEncodingChanged() {
95      Parameterize();
96
97      OnOperatorsChanged();
98      OnReset();
99    }
100
101    private void Parameterize() {
102      if (oldEncoding != null) {
103        AdaptEncodingOperators(oldEncoding, Encoding);
104        var oldMultiEncoding = oldEncoding as MultiEncoding;
105        if (oldMultiEncoding != null)
106          oldMultiEncoding.EncodingsChanged -= MultiEncodingOnEncodingsChanged;
107      }
108      oldEncoding = Encoding;
109
110      foreach (var op in Operators.OfType<IEncodingOperator>())
111        op.EncodingParameter.ActualName = EncodingParameter.Name;
112
113      var solutionCreatorType = Encoding.GetType().BaseType.GetGenericArguments();
114      var paramType = typeof(ValueParameter<>).MakeGenericType(solutionCreatorType);
115      var solutionCreatorParam = (IParameter)Activator.CreateInstance(paramType, SolutionCreatorParameter.Name, SolutionCreatorParameter.Description,
116        Encoding.SolutionCreator);
117      Parameters.Remove(SolutionCreatorParameter);
118      Parameters.Add(solutionCreatorParam);
119      ((IValueParameter)solutionCreatorParam).ValueChanged += SolutionCreatorParameter_ValueChanged;
120
121      var multiEncoding = Encoding as MultiEncoding;
122      if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged;
123    }
124
125    protected override void OnSolutionCreatorChanged() {
126      base.OnSolutionCreatorChanged();
127      Encoding.SolutionCreator = SolutionCreator;
128    }
129
130    private static void AdaptEncodingOperators(IEncoding oldEncoding, IEncoding newEncoding) {
131      if (oldEncoding.GetType() != newEncoding.GetType()) return;
132
133      if (oldEncoding.GetType() == typeof(MultiEncoding)) {
134        var oldMultiEncoding = (MultiEncoding)oldEncoding;
135        var newMultiEncoding = (MultiEncoding)newEncoding;
136        if (!oldMultiEncoding.Encodings.SequenceEqual(newMultiEncoding.Encodings, new TypeEqualityComparer<IEncoding>())) return;
137
138        var nestedEncodings = oldMultiEncoding.Encodings.Zip(newMultiEncoding.Encodings, (o, n) => new { oldEnc = o, newEnc = n });
139        foreach (var multi in nestedEncodings)
140          AdaptEncodingOperators(multi.oldEnc, multi.newEnc);
141      }
142
143      var comparer = new TypeEqualityComparer<IOperator>();
144      var cloner = new Cloner();
145      var oldOperators = oldEncoding.Operators;
146      var newOperators = newEncoding.Operators;
147
148      cloner.RegisterClonedObject(oldEncoding, newEncoding);
149      var operators = oldOperators.Intersect(newOperators, comparer)
150                                  .Select(cloner.Clone)
151                                  .Union(newOperators, comparer).ToList();
152
153      newEncoding.ConfigureOperators(operators);
154      newEncoding.Operators = operators;
155    }
156
157    protected virtual void MultiEncodingOnEncodingsChanged(object sender, EventArgs e) {
158      OnOperatorsChanged();
159    }
160
161    protected override IEnumerable<KeyValuePair<string, IItem>> GetCollectedValues(IValueParameter param) {
162      if (param.Value == null) yield break;
163      if (param.GetsCollected) {
164        if (param == EncodingParameter) // store only the name of the encoding
165          yield return new KeyValuePair<string, IItem>(String.Empty, new StringValue(EncodingParameter.Value.Name));
166        else yield return new KeyValuePair<string, IItem>(String.Empty, param.Value);
167      }
168      var parameterizedItem = param.Value as IParameterizedItem;
169      if (parameterizedItem != null) {
170        var children = new Dictionary<string, IItem>();
171        parameterizedItem.CollectParameterValues(children);
172        foreach (var child in children) yield return child;
173      }
174    }
175  }
176}
Note: See TracBrowser for help on using the repository browser.