Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Optimization/3.3/Problems/HeuristicOptimizationProblem.cs @ 15325

Last change on this file since 15325 was 14186, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 4.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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 HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Parameters;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Optimization {
29  [Item("Heuristic Optimization Problem", "Represents the base class for a heuristic optimization problem.")]
30  [StorableClass]
31  public abstract class HeuristicOptimizationProblem<T, U> : Problem, IHeuristicOptimizationProblem
32    where T : class,IEvaluator
33    where U : class,ISolutionCreator {
34    private const string EvaluatorParameterName = "Evaluator";
35    private const string SolutionCreateParameterName = "SolutionCreator";
36
37    [StorableConstructor]
38    protected HeuristicOptimizationProblem(bool deserializing) : base(deserializing) { }
39    protected HeuristicOptimizationProblem(HeuristicOptimizationProblem<T, U> original, Cloner cloner)
40      : base(original, cloner) {
41      RegisterEventHandlers();
42    }
43
44    protected HeuristicOptimizationProblem()
45      : base() {
46      Parameters.Add(new ValueParameter<T>(EvaluatorParameterName, "The operator used to evaluate a solution."));
47      Parameters.Add(new ValueParameter<U>(SolutionCreateParameterName, "The operator to create a solution."));
48      RegisterEventHandlers();
49    }
50
51    protected HeuristicOptimizationProblem(T evaluator, U solutionCreator)
52      : base() {
53      Parameters.Add(new ValueParameter<T>(EvaluatorParameterName, "The operator used to evaluate a solution.", evaluator));
54      Parameters.Add(new ValueParameter<U>(SolutionCreateParameterName, "The operator to create a solution.", solutionCreator));
55      RegisterEventHandlers();
56    }
57
58    [StorableHook(HookType.AfterDeserialization)]
59    private void AfterDeserialization() {
60      RegisterEventHandlers();
61    }
62
63    private void RegisterEventHandlers() {
64      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
65      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
66    }
67
68    #region properties
69    public T Evaluator {
70      get { return EvaluatorParameter.Value; }
71      protected set { EvaluatorParameter.Value = value; }
72    }
73    public ValueParameter<T> EvaluatorParameter {
74      get { return (ValueParameter<T>)Parameters[EvaluatorParameterName]; }
75    }
76    IEvaluator IHeuristicOptimizationProblem.Evaluator { get { return Evaluator; } }
77    IParameter IHeuristicOptimizationProblem.EvaluatorParameter { get { return EvaluatorParameter; } }
78
79    public U SolutionCreator {
80      get { return (U)SolutionCreatorParameter.Value; }
81      protected set { SolutionCreatorParameter.Value = value; }
82    }
83    public IValueParameter SolutionCreatorParameter {
84      get { return (IValueParameter)Parameters[SolutionCreateParameterName]; }
85    }
86    ISolutionCreator IHeuristicOptimizationProblem.SolutionCreator { get { return SolutionCreator; } }
87    IParameter IHeuristicOptimizationProblem.SolutionCreatorParameter { get { return SolutionCreatorParameter; } }
88    #endregion
89
90    #region events
91    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
92      OnEvaluatorChanged();
93    }
94    public event EventHandler EvaluatorChanged;
95    protected virtual void OnEvaluatorChanged() {
96      EventHandler handler = EvaluatorChanged;
97      if (handler != null)
98        handler(this, EventArgs.Empty);
99    }
100
101    protected virtual void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
102      OnSolutionCreatorChanged();
103    }
104    public event EventHandler SolutionCreatorChanged;
105    protected virtual void OnSolutionCreatorChanged() {
106      EventHandler handler = SolutionCreatorChanged;
107      if (handler != null)
108        handler(this, EventArgs.Empty);
109    }
110    #endregion
111  }
112}
Note: See TracBrowser for help on using the repository browser.