source: branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Problem.cs @ 13339

Last change on this file since 13339 was 13339, checked in by mkommend, 7 years ago

#2521: Rectored problems and encodings.

File size: 4.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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 Problem<TEncoding, TSolution, TEvaluator> : HeuristicOptimizationProblem<TEvaluator, ISolutionCreator<TSolution>>, IProblemDefinition<TEncoding, TSolution>, IStorableContent
33    where TEncoding : class, IEncoding<TSolution>
34    where TSolution : class, ISolution
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    public TEncoding Encoding {
44      get { return EncodingParameter.Value; }
45      protected set {
46        if (value == null) throw new ArgumentNullException("Encoding must not be null.");
47        EncodingParameter.Value = value;
48      }
49    }
50
51    protected override IEnumerable<IItem> GetOperators() {
52      if (Encoding == null) return base.GetOperators();
53      return base.GetOperators().Concat(Encoding.Operators);
54    }
55    public override IEnumerable<IParameterizedItem> ExecutionContextItems {
56      get {
57        if (Encoding == null) return base.ExecutionContextItems;
58        return base.ExecutionContextItems.Concat(new[] { Encoding });
59      }
60    }
61
62    protected Problem()
63      : base() {
64      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."));
65      if (Encoding != null) {
66        SolutionCreator = Encoding.SolutionCreator;
67        Parameterize();
68      }
69      RegisterEvents();
70    }
71
72    protected Problem(Problem<TEncoding, TSolution, TEvaluator> original, Cloner cloner)
73      : base(original, cloner) {
74      RegisterEvents();
75    }
76
77    [StorableConstructor]
78    protected Problem(bool deserializing) : base(deserializing) { }
79    [StorableHook(HookType.AfterDeserialization)]
80    private void AfterDeserialization() {
81      RegisterEvents();
82    }
83
84    private void RegisterEvents() {
85      EncodingParameter.ValueChanged += (o, e) => OnEncodingChanged();
86      //var multiEncoding = Encoding as MultiEncoding;
87      //if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged;
88    }
89
90    protected virtual void OnEncodingChanged() {
91      Parameterize();
92
93      OnOperatorsChanged();
94      OnReset();
95    }
96
97    private void Parameterize() {
98      foreach (var op in Operators.OfType<IEncodingOperator<TSolution>>())
99        op.EncodingParameter.ActualName = EncodingParameter.Name;
100
101      //var multiEncoding = Encoding as MultiEncoding;
102      //if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged;
103    }
104
105    protected override void OnSolutionCreatorChanged() {
106      base.OnSolutionCreatorChanged();
107      Encoding.SolutionCreator = SolutionCreator;
108    }
109
110    protected virtual void MultiEncodingOnEncodingsChanged(object sender, EventArgs e) {
111      OnOperatorsChanged();
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.