1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Threading;
|
---|
26 | using HEAL.Attic;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Data;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Optimization {
|
---|
33 | [StorableType("2697320D-0259-44BB-BD71-7EE1B10F664C")]
|
---|
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 {
|
---|
40 |
|
---|
41 | [Storable] protected IValueParameter<DoubleValue> BestKnownQualityParameter { get; private set; }
|
---|
42 | [Storable] protected IValueParameter<BoolValue> MaximizationParameter { get; private set; }
|
---|
43 |
|
---|
44 | public double BestKnownQuality {
|
---|
45 | get {
|
---|
46 | if (BestKnownQualityParameter.Value == null) return double.NaN;
|
---|
47 | return BestKnownQualityParameter.Value.Value;
|
---|
48 | }
|
---|
49 | set {
|
---|
50 | if (double.IsNaN(value)) {
|
---|
51 | BestKnownQualityParameter.Value = null;
|
---|
52 | return;
|
---|
53 | }
|
---|
54 | if (BestKnownQualityParameter.Value == null) BestKnownQualityParameter.Value = new DoubleValue(value);
|
---|
55 | else BestKnownQualityParameter.Value.Value = value;
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | public bool Maximization {
|
---|
60 | get { return MaximizationParameter.Value.Value; }
|
---|
61 | protected set {
|
---|
62 | if (Maximization == value) return;
|
---|
63 | MaximizationParameter.ForceValue(new BoolValue(value, @readonly: true));
|
---|
64 | OnMaximizationChanged();
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | [StorableConstructor]
|
---|
69 | protected SingleObjectiveProblem(StorableConstructorFlag _) : base(_) { }
|
---|
70 |
|
---|
71 | protected SingleObjectiveProblem(SingleObjectiveProblem<TEncoding, TEncodedSolution> original, Cloner cloner)
|
---|
72 | : base(original, cloner) {
|
---|
73 | BestKnownQualityParameter = cloner.Clone(original.BestKnownQualityParameter);
|
---|
74 | MaximizationParameter = cloner.Clone(original.MaximizationParameter);
|
---|
75 | ParameterizeOperators();
|
---|
76 | }
|
---|
77 |
|
---|
78 | protected SingleObjectiveProblem() : base() {
|
---|
79 |
|
---|
80 | MaximizationParameter = new ValueParameter<BoolValue>("Maximization", "Whether the problem should be maximized (True) or minimized (False).", new BoolValue(false).AsReadOnly()) { Hidden = true, ReadOnly = true };
|
---|
81 | BestKnownQualityParameter = new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this problem.");
|
---|
82 |
|
---|
83 | Parameters.Add(MaximizationParameter);
|
---|
84 | Parameters.Add(BestKnownQualityParameter);
|
---|
85 |
|
---|
86 | Operators.Add(Evaluator);
|
---|
87 | Operators.Add(new SingleObjectiveAnalyzer<TEncodedSolution>());
|
---|
88 | Operators.Add(new SingleObjectiveImprover<TEncodedSolution>());
|
---|
89 | Operators.Add(new SingleObjectiveMoveEvaluator<TEncodedSolution>());
|
---|
90 | Operators.Add(new SingleObjectiveMoveGenerator<TEncodedSolution>());
|
---|
91 | Operators.Add(new SingleObjectiveMoveMaker<TEncodedSolution>());
|
---|
92 |
|
---|
93 | ParameterizeOperators();
|
---|
94 | }
|
---|
95 |
|
---|
96 | protected SingleObjectiveProblem(TEncoding encoding) : base(encoding) {
|
---|
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."));
|
---|
99 |
|
---|
100 | Operators.Add(Evaluator);
|
---|
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>());
|
---|
106 |
|
---|
107 | ParameterizeOperators();
|
---|
108 | }
|
---|
109 |
|
---|
110 | [StorableHook(HookType.AfterDeserialization)]
|
---|
111 | private void AfterDeserialization() {
|
---|
112 | ParameterizeOperators();
|
---|
113 | }
|
---|
114 |
|
---|
115 | public virtual double Evaluate(TEncodedSolution solution, IRandom random) {
|
---|
116 | return Evaluate(solution, random, CancellationToken.None);
|
---|
117 | }
|
---|
118 | public abstract double Evaluate(TEncodedSolution solution, IRandom random, CancellationToken cancellationToken);
|
---|
119 | public virtual void Analyze(TEncodedSolution[] solutions, double[] qualities, ResultCollection results, IRandom random) { }
|
---|
120 | public virtual IEnumerable<TEncodedSolution> GetNeighbors(TEncodedSolution solution, IRandom random) {
|
---|
121 | return Enumerable.Empty<TEncodedSolution>();
|
---|
122 | }
|
---|
123 |
|
---|
124 | public static bool IsBetter(bool maximization, double quality, double bestQuality) {
|
---|
125 | return (maximization && quality > bestQuality || !maximization && quality < bestQuality);
|
---|
126 | }
|
---|
127 |
|
---|
128 | public virtual bool IsBetter(double quality, double bestQuality) {
|
---|
129 | return IsBetter(Maximization, quality, bestQuality);
|
---|
130 | }
|
---|
131 |
|
---|
132 | protected Tuple<TEncodedSolution, double> GetBestSolution(TEncodedSolution[] solutions, double[] qualities) {
|
---|
133 | return GetBestSolution(solutions, qualities, Maximization);
|
---|
134 | }
|
---|
135 | public static Tuple<TEncodedSolution, double> GetBestSolution(TEncodedSolution[] solutions, double[] qualities, bool maximization) {
|
---|
136 | var zipped = solutions.Zip(qualities, (s, q) => new { Solution = s, Quality = q });
|
---|
137 | var best = (maximization ? zipped.OrderByDescending(z => z.Quality) : zipped.OrderBy(z => z.Quality)).First();
|
---|
138 | return Tuple.Create(best.Solution, best.Quality);
|
---|
139 | }
|
---|
140 |
|
---|
141 | protected override void OnOperatorsChanged() {
|
---|
142 | if (Encoding != null) {
|
---|
143 | PruneMultiObjectiveOperators(Encoding);
|
---|
144 | var combinedEncoding = Encoding as CombinedEncoding;
|
---|
145 | if (combinedEncoding != null) {
|
---|
146 | foreach (var encoding in combinedEncoding.Encodings.ToList()) {
|
---|
147 | PruneMultiObjectiveOperators(encoding);
|
---|
148 | }
|
---|
149 | }
|
---|
150 | }
|
---|
151 | base.OnOperatorsChanged();
|
---|
152 | }
|
---|
153 |
|
---|
154 | private void PruneMultiObjectiveOperators(IEncoding encoding) {
|
---|
155 | if (encoding.Operators.Any(x => x is IMultiObjectiveOperator && !(x is ISingleObjectiveOperator)))
|
---|
156 | encoding.Operators = encoding.Operators.Where(x => !(x is IMultiObjectiveOperator) || x is ISingleObjectiveOperator).ToList();
|
---|
157 |
|
---|
158 | foreach (var multiOp in Encoding.Operators.OfType<IMultiOperator>()) {
|
---|
159 | foreach (var moOp in multiOp.Operators.Where(x => x is IMultiObjectiveOperator).ToList()) {
|
---|
160 | multiOp.RemoveOperator(moOp);
|
---|
161 | }
|
---|
162 | }
|
---|
163 | }
|
---|
164 |
|
---|
165 | protected override void OnEvaluatorChanged() {
|
---|
166 | base.OnEvaluatorChanged();
|
---|
167 | ParameterizeOperators();
|
---|
168 | }
|
---|
169 |
|
---|
170 | private void ParameterizeOperators() {
|
---|
171 | foreach (var op in Operators.OfType<ISingleObjectiveEvaluationOperator<TEncodedSolution>>())
|
---|
172 | op.EvaluateFunc = Evaluate;
|
---|
173 | foreach (var op in Operators.OfType<ISingleObjectiveAnalysisOperator<TEncodedSolution>>())
|
---|
174 | op.AnalyzeAction = Analyze;
|
---|
175 | foreach (var op in Operators.OfType<INeighborBasedOperator<TEncodedSolution>>())
|
---|
176 | op.GetNeighborsFunc = GetNeighbors;
|
---|
177 | }
|
---|
178 |
|
---|
179 | #region ISingleObjectiveHeuristicOptimizationProblem Members
|
---|
180 | IParameter ISingleObjectiveHeuristicOptimizationProblem.MaximizationParameter {
|
---|
181 | get { return Parameters["Maximization"]; }
|
---|
182 | }
|
---|
183 | IParameter ISingleObjectiveHeuristicOptimizationProblem.BestKnownQualityParameter {
|
---|
184 | get { return Parameters["BestKnownQuality"]; }
|
---|
185 | }
|
---|
186 | ISingleObjectiveEvaluator ISingleObjectiveHeuristicOptimizationProblem.Evaluator {
|
---|
187 | get { return Evaluator; }
|
---|
188 | }
|
---|
189 | #endregion
|
---|
190 |
|
---|
191 | public event EventHandler MaximizationChanged;
|
---|
192 | protected void OnMaximizationChanged() {
|
---|
193 | MaximizationChanged?.Invoke(this, EventArgs.Empty);
|
---|
194 | }
|
---|
195 | }
|
---|
196 | }
|
---|