[5578] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17226] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5578] | 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 |
|
---|
[17317] | 22 | using System;
|
---|
| 23 | using HEAL.Attic;
|
---|
[5578] | 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Parameters;
|
---|
| 28 |
|
---|
| 29 | namespace HeuristicLab.Optimization {
|
---|
| 30 | [Item("Single-Objective Heuristic OptimizationProblem", "A base class for single-objective heuristic optimization problems.")]
|
---|
[16723] | 31 | [StorableType("DFD5588E-6AB2-4712-9083-A405EF21226F")]
|
---|
[5578] | 32 | public abstract class SingleObjectiveHeuristicOptimizationProblem<T, U> : HeuristicOptimizationProblem<T, U>, ISingleObjectiveHeuristicOptimizationProblem
|
---|
| 33 | where T : class, ISingleObjectiveEvaluator
|
---|
| 34 | where U : class, ISolutionCreator {
|
---|
| 35 | private const string MaximizationParameterName = "Maximization";
|
---|
| 36 | private const string BestKnownQualityParameterName = "BestKnownQuality";
|
---|
| 37 |
|
---|
| 38 | [StorableConstructor]
|
---|
[16723] | 39 | protected SingleObjectiveHeuristicOptimizationProblem(StorableConstructorFlag _) : base(_) { }
|
---|
[17317] | 40 | protected SingleObjectiveHeuristicOptimizationProblem(SingleObjectiveHeuristicOptimizationProblem<T, U> original, Cloner cloner)
|
---|
| 41 | : base(original, cloner) {
|
---|
| 42 | RegisterEventHandlers();
|
---|
| 43 | }
|
---|
[5578] | 44 | protected SingleObjectiveHeuristicOptimizationProblem()
|
---|
| 45 | : base() {
|
---|
[5618] | 46 | Parameters.Add(new ValueParameter<BoolValue>(MaximizationParameterName, "Set to false if the problem should be minimized.", new BoolValue()));
|
---|
[7440] | 47 | Parameters.Add(new OptionalValueParameter<DoubleValue>(BestKnownQualityParameterName, "The quality of the best known solution of this problem."));
|
---|
[17317] | 48 |
|
---|
| 49 | RegisterEventHandlers();
|
---|
[5578] | 50 | }
|
---|
| 51 |
|
---|
[5618] | 52 | protected SingleObjectiveHeuristicOptimizationProblem(T evaluator, U solutionCreator)
|
---|
| 53 | : base(evaluator, solutionCreator) {
|
---|
| 54 | Parameters.Add(new ValueParameter<BoolValue>(MaximizationParameterName, "Set to false if the problem should be minimized.", new BoolValue()));
|
---|
[7440] | 55 | Parameters.Add(new OptionalValueParameter<DoubleValue>(BestKnownQualityParameterName, "The quality of the best known solution of this problem."));
|
---|
[17317] | 56 |
|
---|
| 57 | RegisterEventHandlers();
|
---|
[5618] | 58 | }
|
---|
| 59 |
|
---|
[17317] | 60 | private void RegisterEventHandlers() {
|
---|
| 61 | MaximizationParameter.ValueChanged += MaximizationParameterOnValueChanged;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | private void MaximizationParameterOnValueChanged(object sender, EventArgs e) {
|
---|
| 65 | OnMaximizationChanged();
|
---|
| 66 | }
|
---|
| 67 |
|
---|
[5872] | 68 | [StorableHook(HookType.AfterDeserialization)]
|
---|
[5899] | 69 | private void AfterDeserialization() {
|
---|
[5872] | 70 | // BackwardsCompatibility3.3
|
---|
| 71 | #region Backwards compatible code (remove with 3.4)
|
---|
| 72 | if (BestKnownQualityParameter is ValueParameter<DoubleValue>) {
|
---|
| 73 | Parameters.Remove(BestKnownQualityParameterName);
|
---|
| 74 | Parameters.Add(new OptionalValueParameter<DoubleValue>(BestKnownQualityParameterName, "The quality of the best known solution of this problem."));
|
---|
| 75 | }
|
---|
| 76 | #endregion
|
---|
[17317] | 77 | RegisterEventHandlers();
|
---|
[5872] | 78 | }
|
---|
[5578] | 79 | public ValueParameter<BoolValue> MaximizationParameter {
|
---|
| 80 | get { return (ValueParameter<BoolValue>)Parameters[MaximizationParameterName]; }
|
---|
| 81 | }
|
---|
| 82 | IParameter ISingleObjectiveHeuristicOptimizationProblem.MaximizationParameter {
|
---|
| 83 | get { return MaximizationParameter; }
|
---|
| 84 | }
|
---|
| 85 | public BoolValue Maximization {
|
---|
| 86 | get { return MaximizationParameter.Value; }
|
---|
[17317] | 87 | set {
|
---|
| 88 | if (Maximization == value) return;
|
---|
| 89 | MaximizationParameter.Value = value;
|
---|
| 90 | }
|
---|
[5578] | 91 | }
|
---|
| 92 |
|
---|
[5872] | 93 | public IValueParameter<DoubleValue> BestKnownQualityParameter {
|
---|
| 94 | get { return (IValueParameter<DoubleValue>)Parameters[BestKnownQualityParameterName]; }
|
---|
[5578] | 95 | }
|
---|
| 96 | IParameter ISingleObjectiveHeuristicOptimizationProblem.BestKnownQualityParameter {
|
---|
| 97 | get { return BestKnownQualityParameter; }
|
---|
| 98 | }
|
---|
| 99 | public DoubleValue BestKnownQuality {
|
---|
| 100 | get { return BestKnownQualityParameter.Value; }
|
---|
[7442] | 101 | set { BestKnownQualityParameter.Value = value; }
|
---|
[5578] | 102 | }
|
---|
| 103 |
|
---|
| 104 | ISingleObjectiveEvaluator ISingleObjectiveHeuristicOptimizationProblem.Evaluator {
|
---|
| 105 | get { return Evaluator; }
|
---|
| 106 | }
|
---|
[17317] | 107 |
|
---|
| 108 | public event EventHandler MaximizationChanged;
|
---|
| 109 | protected void OnMaximizationChanged() {
|
---|
| 110 | MaximizationChanged?.Invoke(this, EventArgs.Empty);
|
---|
| 111 | }
|
---|
[5578] | 112 | }
|
---|
| 113 | }
|
---|