Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2521: Refactored encodings and problems.

File size: 3.9 KB
RevLine 
[11739]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[11739]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
[11949]30namespace HeuristicLab.Optimization {
[11739]31  [StorableClass]
[13336]32  public abstract class Problem<TSolution, TEvaluator> : HeuristicOptimizationProblem<TEvaluator, ISolutionCreator<TSolution>>, IProblemDefinition, IStorableContent
33    where TSolution : class, ISolution
[11739]34    where TEvaluator : class, IEvaluator {
35
36    public string Filename { get; set; }
37
[13336]38    protected IValueParameter<IEncoding<TSolution>> EncodingParameter {
39      get { return (IValueParameter<IEncoding<TSolution>>)Parameters["Encoding"]; }
[11739]40    }
41
[13336]42    public IEncoding<TSolution> Encoding {
[11739]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() {
[11753]51      if (Encoding == null) return base.GetOperators();
[11739]52      return base.GetOperators().Concat(Encoding.Operators);
53    }
54    public override IEnumerable<IParameterizedItem> ExecutionContextItems {
[11753]55      get {
56        if (Encoding == null) return base.ExecutionContextItems;
57        return base.ExecutionContextItems.Concat(new[] { Encoding });
58      }
[11739]59    }
60
[13336]61    protected Problem()
[11739]62      : base() {
[13336]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();
[11739]65      RegisterEvents();
66    }
67
[13336]68    protected Problem(Problem<TSolution, TEvaluator> original, Cloner cloner)
[11739]69      : base(original, cloner) {
70      RegisterEvents();
71    }
72
73    [StorableConstructor]
[13336]74    protected Problem(bool deserializing) : base(deserializing) { }
[11753]75    [StorableHook(HookType.AfterDeserialization)]
76    private void AfterDeserialization() {
[11739]77      RegisterEvents();
78    }
79
80    private void RegisterEvents() {
81      EncodingParameter.ValueChanged += (o, e) => OnEncodingChanged();
[13336]82      //var multiEncoding = Encoding as MultiEncoding;
83      //if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged;
[11739]84    }
85
86    protected virtual void OnEncodingChanged() {
[11996]87      Parameterize();
88
89      OnOperatorsChanged();
90      OnReset();
91    }
92
93    private void Parameterize() {
[13336]94      foreach (var op in Operators.OfType<IEncodingOperator<TSolution>>())
[11786]95        op.EncodingParameter.ActualName = EncodingParameter.Name;
96
[13336]97      //var multiEncoding = Encoding as MultiEncoding;
98      //if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged;
[11739]99    }
100
101    protected override void OnSolutionCreatorChanged() {
102      base.OnSolutionCreatorChanged();
103      Encoding.SolutionCreator = SolutionCreator;
104    }
105
[11970]106    protected virtual void MultiEncodingOnEncodingsChanged(object sender, EventArgs e) {
107      OnOperatorsChanged();
108    }
[11739]109  }
110}
Note: See TracBrowser for help on using the repository browser.