Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2521: Rectored problems and encodings.

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