Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13337 was 13336, checked in by mkommend, 8 years ago

#2521: Refactored encodings and problems.

File size: 3.9 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<TSolution, TEvaluator> : HeuristicOptimizationProblem<TEvaluator, ISolutionCreator<TSolution>>, IProblemDefinition, IStorableContent
33    where TSolution : class, ISolution
34    where TEvaluator : class, IEvaluator {
35
36    public string Filename { get; set; }
37
38    protected IValueParameter<IEncoding<TSolution>> EncodingParameter {
39      get { return (IValueParameter<IEncoding<TSolution>>)Parameters["Encoding"]; }
40    }
41
42    public IEncoding<TSolution> Encoding {
43      get { return EncodingParameter.Value; }
44      protected set {
45        if (value == null) throw new ArgumentNullException("Encoding must not be null.");
46        EncodingParameter.Value = value;
47      }
48    }
49
50    protected override IEnumerable<IItem> GetOperators() {
51      if (Encoding == null) return base.GetOperators();
52      return base.GetOperators().Concat(Encoding.Operators);
53    }
54    public override IEnumerable<IParameterizedItem> ExecutionContextItems {
55      get {
56        if (Encoding == null) return base.ExecutionContextItems;
57        return base.ExecutionContextItems.Concat(new[] { Encoding });
58      }
59    }
60
61    protected Problem()
62      : base() {
63      Parameters.Add(new ValueParameter<IEncoding<TSolution>>("Encoding", "Describes the configuration of the encoding, what the variables are called, what type they are and their bounds if any."));
64      if (Encoding != null) Parameterize();
65      RegisterEvents();
66    }
67
68    protected Problem(Problem<TSolution, TEvaluator> original, Cloner cloner)
69      : base(original, cloner) {
70      RegisterEvents();
71    }
72
73    [StorableConstructor]
74    protected Problem(bool deserializing) : base(deserializing) { }
75    [StorableHook(HookType.AfterDeserialization)]
76    private void AfterDeserialization() {
77      RegisterEvents();
78    }
79
80    private void RegisterEvents() {
81      EncodingParameter.ValueChanged += (o, e) => OnEncodingChanged();
82      //var multiEncoding = Encoding as MultiEncoding;
83      //if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged;
84    }
85
86    protected virtual void OnEncodingChanged() {
87      Parameterize();
88
89      OnOperatorsChanged();
90      OnReset();
91    }
92
93    private void Parameterize() {
94      foreach (var op in Operators.OfType<IEncodingOperator<TSolution>>())
95        op.EncodingParameter.ActualName = EncodingParameter.Name;
96
97      //var multiEncoding = Encoding as MultiEncoding;
98      //if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged;
99    }
100
101    protected override void OnSolutionCreatorChanged() {
102      base.OnSolutionCreatorChanged();
103      Encoding.SolutionCreator = SolutionCreator;
104    }
105
106    protected virtual void MultiEncodingOnEncodingsChanged(object sender, EventArgs e) {
107      OnOperatorsChanged();
108    }
109  }
110}
Note: See TracBrowser for help on using the repository browser.