#region License Information /* HeuristicLab * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using HEAL.Attic; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Parameters; namespace HeuristicLab.Optimization { [Item("Single-Objective Heuristic OptimizationProblem", "A base class for single-objective heuristic optimization problems.")] [StorableType("DFD5588E-6AB2-4712-9083-A405EF21226F")] public abstract class SingleObjectiveHeuristicOptimizationProblem : HeuristicOptimizationProblem, ISingleObjectiveHeuristicOptimizationProblem where T : class, ISingleObjectiveEvaluator where U : class, ISolutionCreator { private const string MaximizationParameterName = "Maximization"; private const string BestKnownQualityParameterName = "BestKnownQuality"; [StorableConstructor] protected SingleObjectiveHeuristicOptimizationProblem(StorableConstructorFlag _) : base(_) { } protected SingleObjectiveHeuristicOptimizationProblem(SingleObjectiveHeuristicOptimizationProblem original, Cloner cloner) : base(original, cloner) { RegisterEventHandlers(); } protected SingleObjectiveHeuristicOptimizationProblem() : base() { Parameters.Add(new ValueParameter(MaximizationParameterName, "Set to false if the problem should be minimized.", new BoolValue())); Parameters.Add(new OptionalValueParameter(BestKnownQualityParameterName, "The quality of the best known solution of this problem.")); RegisterEventHandlers(); } protected SingleObjectiveHeuristicOptimizationProblem(T evaluator, U solutionCreator) : base(evaluator, solutionCreator) { Parameters.Add(new ValueParameter(MaximizationParameterName, "Set to false if the problem should be minimized.", new BoolValue())); Parameters.Add(new OptionalValueParameter(BestKnownQualityParameterName, "The quality of the best known solution of this problem.")); RegisterEventHandlers(); } private void RegisterEventHandlers() { MaximizationParameter.ValueChanged += MaximizationParameterOnValueChanged; } private void MaximizationParameterOnValueChanged(object sender, EventArgs e) { OnMaximizationChanged(); } [StorableHook(HookType.AfterDeserialization)] private void AfterDeserialization() { // BackwardsCompatibility3.3 #region Backwards compatible code (remove with 3.4) if (BestKnownQualityParameter is ValueParameter) { Parameters.Remove(BestKnownQualityParameterName); Parameters.Add(new OptionalValueParameter(BestKnownQualityParameterName, "The quality of the best known solution of this problem.")); } #endregion RegisterEventHandlers(); } public ValueParameter MaximizationParameter { get { return (ValueParameter)Parameters[MaximizationParameterName]; } } IParameter ISingleObjectiveHeuristicOptimizationProblem.MaximizationParameter { get { return MaximizationParameter; } } public BoolValue Maximization { get { return MaximizationParameter.Value; } set { if (Maximization == value) return; MaximizationParameter.Value = value; } } public IValueParameter BestKnownQualityParameter { get { return (IValueParameter)Parameters[BestKnownQualityParameterName]; } } IParameter ISingleObjectiveHeuristicOptimizationProblem.BestKnownQualityParameter { get { return BestKnownQualityParameter; } } public DoubleValue BestKnownQuality { get { return BestKnownQualityParameter.Value; } set { BestKnownQualityParameter.Value = value; } } ISingleObjectiveEvaluator ISingleObjectiveHeuristicOptimizationProblem.Evaluator { get { return Evaluator; } } public event EventHandler MaximizationChanged; protected void OnMaximizationChanged() { MaximizationChanged?.Invoke(this, EventArgs.Empty); } } }