#region License Information /* HeuristicLab * Copyright (C) 2002-2015 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.Collections.Generic; using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Optimization { [StorableClass] public abstract class SingleObjectiveProblem : Problem>, ISingleObjectiveProblem, ISingleObjectiveProblemDefinition where TEncoding : class, IEncoding where TSolution : class, ISolution { protected IValueParameter BestKnownQualityParameter { get { return (IValueParameter)Parameters["BestKnownQuality"]; } } protected IFixedValueParameter MaximizationParameter { get { return (IFixedValueParameter)Parameters["Maximization"]; } } public double BestKnownQuality { get { if (BestKnownQualityParameter.Value == null) return double.NaN; return BestKnownQualityParameter.Value.Value; } set { if (double.IsNaN(value)) { BestKnownQualityParameter.Value = null; return; } if (BestKnownQualityParameter.Value == null) BestKnownQualityParameter.Value = new DoubleValue(value); else BestKnownQualityParameter.Value.Value = value; } } [StorableConstructor] protected SingleObjectiveProblem(bool deserializing) : base(deserializing) { } protected SingleObjectiveProblem(SingleObjectiveProblem original, Cloner cloner) : base(original, cloner) { ParameterizeOperators(); } protected SingleObjectiveProblem() : base() { Parameters.Add(new FixedValueParameter("Maximization", "Set to false if the problem should be minimized.", (BoolValue)new BoolValue(Maximization).AsReadOnly()) { Hidden = true }); Parameters.Add(new OptionalValueParameter("BestKnownQuality", "The quality of the best known solution of this problem.")); Operators.Add(Evaluator); Operators.Add(new SingleObjectiveAnalyzer()); Operators.Add(new SingleObjectiveImprover()); Operators.Add(new SingleObjectiveMoveEvaluator()); Operators.Add(new SingleObjectiveMoveGenerator()); Operators.Add(new SingleObjectiveMoveMaker()); ParameterizeOperators(); } protected SingleObjectiveProblem(TEncoding encoding) : base(encoding) { Parameters.Add(new FixedValueParameter("Maximization", "Set to false if the problem should be minimized.", (BoolValue)new BoolValue(Maximization).AsReadOnly()) { Hidden = true }); Parameters.Add(new OptionalValueParameter("BestKnownQuality", "The quality of the best known solution of this problem.")); Operators.Add(Evaluator); Operators.Add(new SingleObjectiveAnalyzer()); Operators.Add(new SingleObjectiveImprover()); Operators.Add(new SingleObjectiveMoveEvaluator()); Operators.Add(new SingleObjectiveMoveGenerator()); Operators.Add(new SingleObjectiveMoveMaker()); ParameterizeOperators(); } [StorableHook(HookType.AfterDeserialization)] private void AfterDeserialization() { ParameterizeOperators(); } public abstract bool Maximization { get; } public abstract double Evaluate(TSolution solution, IRandom random); public virtual void Analyze(TSolution[] solutions, double[] qualities, ResultCollection results, IRandom random) { } public virtual IEnumerable GetNeighbors(TSolution solution, IRandom random) { return Enumerable.Empty(); } public virtual bool IsBetter(double quality, double bestQuality) { return (Maximization && quality > bestQuality || !Maximization && quality < bestQuality); } protected override void OnEvaluatorChanged() { base.OnEvaluatorChanged(); ParameterizeOperators(); } private void ParameterizeOperators() { foreach (var op in Operators.OfType>()) op.EvaluateFunc = Evaluate; foreach (var op in Operators.OfType>()) op.AnalyzeAction = Analyze; foreach (var op in Operators.OfType>()) op.GetNeighborsFunc = GetNeighbors; } #region ISingleObjectiveHeuristicOptimizationProblem Members IParameter ISingleObjectiveHeuristicOptimizationProblem.MaximizationParameter { get { return Parameters["Maximization"]; } } IParameter ISingleObjectiveHeuristicOptimizationProblem.BestKnownQualityParameter { get { return Parameters["BestKnownQuality"]; } } ISingleObjectiveEvaluator ISingleObjectiveHeuristicOptimizationProblem.Evaluator { get { return Evaluator; } } #endregion } }