[10753] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17226] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[10753] | 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 |
|
---|
[16692] | 22 | using System;
|
---|
[11786] | 23 | using System.Collections.Generic;
|
---|
[10753] | 24 | using System.Linq;
|
---|
[17320] | 25 | using System.Threading;
|
---|
[16751] | 26 | using HEAL.Attic;
|
---|
[10753] | 27 | using HeuristicLab.Common;
|
---|
| 28 | using HeuristicLab.Core;
|
---|
[11753] | 29 | using HeuristicLab.Data;
|
---|
| 30 | using HeuristicLab.Parameters;
|
---|
[10753] | 31 |
|
---|
[11949] | 32 | namespace HeuristicLab.Optimization {
|
---|
[16723] | 33 | [StorableType("2697320D-0259-44BB-BD71-7EE1B10F664C")]
|
---|
[16751] | 34 | public abstract class SingleObjectiveProblem<TEncoding, TEncodedSolution> :
|
---|
| 35 | Problem<TEncoding, TEncodedSolution, SingleObjectiveEvaluator<TEncodedSolution>>,
|
---|
| 36 | ISingleObjectiveProblem<TEncoding, TEncodedSolution>,
|
---|
| 37 | ISingleObjectiveProblemDefinition<TEncoding, TEncodedSolution>
|
---|
| 38 | where TEncoding : class, IEncoding<TEncodedSolution>
|
---|
| 39 | where TEncodedSolution : class, IEncodedSolution {
|
---|
[11990] | 40 |
|
---|
[17356] | 41 | [Storable] protected IValueParameter<DoubleValue> BestKnownQualityParameter { get; private set; }
|
---|
| 42 | [Storable] protected IValueParameter<BoolValue> MaximizationParameter { get; private set; }
|
---|
[11990] | 43 |
|
---|
| 44 | public double BestKnownQuality {
|
---|
| 45 | get {
|
---|
| 46 | if (BestKnownQualityParameter.Value == null) return double.NaN;
|
---|
| 47 | return BestKnownQualityParameter.Value.Value;
|
---|
| 48 | }
|
---|
| 49 | set {
|
---|
[13437] | 50 | if (double.IsNaN(value)) {
|
---|
| 51 | BestKnownQualityParameter.Value = null;
|
---|
| 52 | return;
|
---|
| 53 | }
|
---|
[11990] | 54 | if (BestKnownQualityParameter.Value == null) BestKnownQualityParameter.Value = new DoubleValue(value);
|
---|
| 55 | else BestKnownQualityParameter.Value.Value = value;
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[17270] | 59 | public bool Maximization {
|
---|
| 60 | get { return MaximizationParameter.Value.Value; }
|
---|
| 61 | protected set {
|
---|
[17317] | 62 | if (Maximization == value) return;
|
---|
[17567] | 63 | MaximizationParameter.Value = new BoolValue(value, @readonly: true);
|
---|
[17270] | 64 | }
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[10753] | 67 | [StorableConstructor]
|
---|
[16723] | 68 | protected SingleObjectiveProblem(StorableConstructorFlag _) : base(_) { }
|
---|
[10753] | 69 |
|
---|
[16751] | 70 | protected SingleObjectiveProblem(SingleObjectiveProblem<TEncoding, TEncodedSolution> original, Cloner cloner)
|
---|
[10753] | 71 | : base(original, cloner) {
|
---|
[17270] | 72 | BestKnownQualityParameter = cloner.Clone(original.BestKnownQualityParameter);
|
---|
| 73 | MaximizationParameter = cloner.Clone(original.MaximizationParameter);
|
---|
[17570] | 74 | Parameterize();
|
---|
[17546] | 75 | RegisterEventHandlers();
|
---|
[10753] | 76 | }
|
---|
[17279] | 77 |
|
---|
[16806] | 78 | protected SingleObjectiveProblem() : base() {
|
---|
[17279] | 79 | MaximizationParameter = new ValueParameter<BoolValue>("Maximization", "Whether the problem should be maximized (True) or minimized (False).", new BoolValue(false).AsReadOnly()) { Hidden = true, ReadOnly = true };
|
---|
| 80 | BestKnownQualityParameter = new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this problem.");
|
---|
| 81 |
|
---|
| 82 | Parameters.Add(MaximizationParameter);
|
---|
| 83 | Parameters.Add(BestKnownQualityParameter);
|
---|
| 84 |
|
---|
[11753] | 85 | Operators.Add(Evaluator);
|
---|
[16751] | 86 | Operators.Add(new SingleObjectiveAnalyzer<TEncodedSolution>());
|
---|
| 87 | Operators.Add(new SingleObjectiveImprover<TEncodedSolution>());
|
---|
| 88 | Operators.Add(new SingleObjectiveMoveEvaluator<TEncodedSolution>());
|
---|
| 89 | Operators.Add(new SingleObjectiveMoveGenerator<TEncodedSolution>());
|
---|
| 90 | Operators.Add(new SingleObjectiveMoveMaker<TEncodedSolution>());
|
---|
[10753] | 91 |
|
---|
[17570] | 92 | Parameterize();
|
---|
[17546] | 93 | RegisterEventHandlers();
|
---|
[10753] | 94 | }
|
---|
| 95 |
|
---|
[16806] | 96 | protected SingleObjectiveProblem(TEncoding encoding) : base(encoding) {
|
---|
[17270] | 97 | Parameters.Add(MaximizationParameter = new ValueParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized.", new BoolValue(false).AsReadOnly()) { Hidden = true, ReadOnly = true });
|
---|
| 98 | Parameters.Add(BestKnownQualityParameter = new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this problem."));
|
---|
[13396] | 99 |
|
---|
| 100 | Operators.Add(Evaluator);
|
---|
[16751] | 101 | Operators.Add(new SingleObjectiveAnalyzer<TEncodedSolution>());
|
---|
| 102 | Operators.Add(new SingleObjectiveImprover<TEncodedSolution>());
|
---|
| 103 | Operators.Add(new SingleObjectiveMoveEvaluator<TEncodedSolution>());
|
---|
| 104 | Operators.Add(new SingleObjectiveMoveGenerator<TEncodedSolution>());
|
---|
| 105 | Operators.Add(new SingleObjectiveMoveMaker<TEncodedSolution>());
|
---|
[13396] | 106 |
|
---|
[17570] | 107 | Parameterize();
|
---|
[17546] | 108 | RegisterEventHandlers();
|
---|
[13396] | 109 | }
|
---|
| 110 |
|
---|
[10753] | 111 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 112 | private void AfterDeserialization() {
|
---|
[17570] | 113 | Parameterize();
|
---|
[17546] | 114 | RegisterEventHandlers();
|
---|
[10753] | 115 | }
|
---|
| 116 |
|
---|
[17382] | 117 | public ISingleObjectiveEvaluationResult Evaluate(TEncodedSolution solution, IRandom random) {
|
---|
[17320] | 118 | return Evaluate(solution, random, CancellationToken.None);
|
---|
| 119 | }
|
---|
[17382] | 120 | public abstract ISingleObjectiveEvaluationResult Evaluate(TEncodedSolution solution, IRandom random, CancellationToken cancellationToken);
|
---|
[17363] | 121 |
|
---|
[17366] | 122 | public void Evaluate(ISingleObjectiveSolutionContext<TEncodedSolution> solutionContext, IRandom random) {
|
---|
[17363] | 123 | Evaluate(solutionContext, random, CancellationToken.None);
|
---|
| 124 | }
|
---|
| 125 | public virtual void Evaluate(ISingleObjectiveSolutionContext<TEncodedSolution> solutionContext, IRandom random, CancellationToken cancellationToken) {
|
---|
[17382] | 126 | var evaluationResult = Evaluate(solutionContext.EncodedSolution, random, cancellationToken);
|
---|
| 127 | solutionContext.EvaluationResult = evaluationResult;
|
---|
[17363] | 128 | }
|
---|
| 129 |
|
---|
[16751] | 130 | public virtual void Analyze(TEncodedSolution[] solutions, double[] qualities, ResultCollection results, IRandom random) { }
|
---|
[17363] | 131 | public virtual void Analyze(ISingleObjectiveSolutionContext<TEncodedSolution>[] solutionContexts, ResultCollection results, IRandom random) {
|
---|
| 132 | var solutions = solutionContexts.Select(c => c.EncodedSolution).ToArray();
|
---|
| 133 | var qualities = solutionContexts.Select(c => c.EvaluationResult.Quality).ToArray();
|
---|
| 134 | Analyze(solutions, qualities, results, random);
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | public virtual IEnumerable<TEncodedSolution> GetNeighbors(TEncodedSolution solutions, IRandom random) {
|
---|
[16751] | 138 | return Enumerable.Empty<TEncodedSolution>();
|
---|
[11786] | 139 | }
|
---|
[17363] | 140 | public virtual IEnumerable<ISingleObjectiveSolutionContext<TEncodedSolution>> GetNeighbors(ISingleObjectiveSolutionContext<TEncodedSolution> solutionContext, IRandom random) {
|
---|
| 141 | return GetNeighbors(solutionContext.EncodedSolution, random).Select(n => new SingleObjectiveSolutionContext<TEncodedSolution>(n));
|
---|
| 142 | }
|
---|
[10753] | 143 |
|
---|
[17279] | 144 | public static bool IsBetter(bool maximization, double quality, double bestQuality) {
|
---|
| 145 | return (maximization && quality > bestQuality || !maximization && quality < bestQuality);
|
---|
| 146 | }
|
---|
| 147 |
|
---|
[13336] | 148 | public virtual bool IsBetter(double quality, double bestQuality) {
|
---|
[17279] | 149 | return IsBetter(Maximization, quality, bestQuality);
|
---|
[11970] | 150 | }
|
---|
| 151 |
|
---|
[17363] | 152 | //TODO refactor to solution contexts
|
---|
[17522] | 153 | protected ISingleObjectiveSolutionContext<TEncodedSolution> GetBest(ISingleObjectiveSolutionContext<TEncodedSolution>[] solutionContexts) {
|
---|
| 154 | return Maximization ? solutionContexts.MaxItems(x => x.EvaluationResult.Quality).First()
|
---|
| 155 | : solutionContexts.MinItems(x => x.EvaluationResult.Quality).First();
|
---|
| 156 | }
|
---|
[16751] | 157 | protected Tuple<TEncodedSolution, double> GetBestSolution(TEncodedSolution[] solutions, double[] qualities) {
|
---|
[16692] | 158 | return GetBestSolution(solutions, qualities, Maximization);
|
---|
| 159 | }
|
---|
[16751] | 160 | public static Tuple<TEncodedSolution, double> GetBestSolution(TEncodedSolution[] solutions, double[] qualities, bool maximization) {
|
---|
[16692] | 161 | var zipped = solutions.Zip(qualities, (s, q) => new { Solution = s, Quality = q });
|
---|
| 162 | var best = (maximization ? zipped.OrderByDescending(z => z.Quality) : zipped.OrderBy(z => z.Quality)).First();
|
---|
| 163 | return Tuple.Create(best.Solution, best.Quality);
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | protected override void OnOperatorsChanged() {
|
---|
| 167 | if (Encoding != null) {
|
---|
| 168 | PruneMultiObjectiveOperators(Encoding);
|
---|
| 169 | var combinedEncoding = Encoding as CombinedEncoding;
|
---|
| 170 | if (combinedEncoding != null) {
|
---|
| 171 | foreach (var encoding in combinedEncoding.Encodings.ToList()) {
|
---|
| 172 | PruneMultiObjectiveOperators(encoding);
|
---|
| 173 | }
|
---|
| 174 | }
|
---|
| 175 | }
|
---|
[16801] | 176 | base.OnOperatorsChanged();
|
---|
[16692] | 177 | }
|
---|
| 178 |
|
---|
| 179 | private void PruneMultiObjectiveOperators(IEncoding encoding) {
|
---|
| 180 | if (encoding.Operators.Any(x => x is IMultiObjectiveOperator && !(x is ISingleObjectiveOperator)))
|
---|
| 181 | encoding.Operators = encoding.Operators.Where(x => !(x is IMultiObjectiveOperator) || x is ISingleObjectiveOperator).ToList();
|
---|
| 182 |
|
---|
| 183 | foreach (var multiOp in Encoding.Operators.OfType<IMultiOperator>()) {
|
---|
| 184 | foreach (var moOp in multiOp.Operators.Where(x => x is IMultiObjectiveOperator).ToList()) {
|
---|
| 185 | multiOp.RemoveOperator(moOp);
|
---|
| 186 | }
|
---|
| 187 | }
|
---|
| 188 | }
|
---|
| 189 |
|
---|
[11396] | 190 | protected override void OnEvaluatorChanged() {
|
---|
| 191 | base.OnEvaluatorChanged();
|
---|
[17570] | 192 | Evaluator.QualityParameter.ActualNameChanged += QualityParameterOnActualNameChanged;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | protected virtual void QualityParameterOnActualNameChanged(object sender, EventArgs e) {
|
---|
[11739] | 196 | ParameterizeOperators();
|
---|
[11396] | 197 | }
|
---|
| 198 |
|
---|
[17570] | 199 | protected override void ParameterizeOperators() {
|
---|
| 200 | base.ParameterizeOperators();
|
---|
| 201 | Parameterize();
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | private void Parameterize() {
|
---|
[16751] | 205 | foreach (var op in Operators.OfType<ISingleObjectiveEvaluationOperator<TEncodedSolution>>())
|
---|
[17363] | 206 | op.Evaluate = Evaluate;
|
---|
[16751] | 207 | foreach (var op in Operators.OfType<ISingleObjectiveAnalysisOperator<TEncodedSolution>>())
|
---|
[17363] | 208 | op.Analyze = Analyze;
|
---|
[16751] | 209 | foreach (var op in Operators.OfType<INeighborBasedOperator<TEncodedSolution>>())
|
---|
[17363] | 210 | op.GetNeighbors = GetNeighbors;
|
---|
[17570] | 211 | foreach (var op in Operators.OfType<ISolutionSimilarityCalculator>()) {
|
---|
| 212 | op.SolutionVariableName = Encoding.Name;
|
---|
| 213 | op.QualityVariableName = Evaluator.QualityParameter.ActualName;
|
---|
| 214 | }
|
---|
[11393] | 215 | }
|
---|
| 216 |
|
---|
[17546] | 217 | private void RegisterEventHandlers() {
|
---|
| 218 | BoolValueParameterChangeHandler.Create(MaximizationParameter, OnMaximizationChanged);
|
---|
| 219 | }
|
---|
| 220 |
|
---|
[11753] | 221 | #region ISingleObjectiveHeuristicOptimizationProblem Members
|
---|
| 222 | IParameter ISingleObjectiveHeuristicOptimizationProblem.MaximizationParameter {
|
---|
| 223 | get { return Parameters["Maximization"]; }
|
---|
| 224 | }
|
---|
| 225 | IParameter ISingleObjectiveHeuristicOptimizationProblem.BestKnownQualityParameter {
|
---|
| 226 | get { return Parameters["BestKnownQuality"]; }
|
---|
| 227 | }
|
---|
| 228 | ISingleObjectiveEvaluator ISingleObjectiveHeuristicOptimizationProblem.Evaluator {
|
---|
| 229 | get { return Evaluator; }
|
---|
| 230 | }
|
---|
| 231 | #endregion
|
---|
[17317] | 232 |
|
---|
| 233 | public event EventHandler MaximizationChanged;
|
---|
| 234 | protected void OnMaximizationChanged() {
|
---|
| 235 | MaximizationChanged?.Invoke(this, EventArgs.Empty);
|
---|
| 236 | }
|
---|
[10753] | 237 | }
|
---|
| 238 | }
|
---|