[10753] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 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 |
|
---|
[15051] | 22 | using System;
|
---|
[11786] | 23 | using System.Collections.Generic;
|
---|
[10753] | 24 | using System.Linq;
|
---|
[16794] | 25 | using HEAL.Attic;
|
---|
[10753] | 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
[11753] | 28 | using HeuristicLab.Data;
|
---|
| 29 | using HeuristicLab.Parameters;
|
---|
[10753] | 30 |
|
---|
[11949] | 31 | namespace HeuristicLab.Optimization {
|
---|
[16565] | 32 | [StorableType("2697320D-0259-44BB-BD71-7EE1B10F664C")]
|
---|
[11814] | 33 | public abstract class SingleObjectiveBasicProblem<TEncoding> : BasicProblem<TEncoding, SingleObjectiveEvaluator>,
|
---|
[11753] | 34 | ISingleObjectiveProblemDefinition, ISingleObjectiveHeuristicOptimizationProblem
|
---|
[11739] | 35 | where TEncoding : class, IEncoding {
|
---|
[11990] | 36 |
|
---|
| 37 | protected IValueParameter<DoubleValue> BestKnownQualityParameter {
|
---|
| 38 | get { return (IValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | public double BestKnownQuality {
|
---|
| 42 | get {
|
---|
| 43 | if (BestKnownQualityParameter.Value == null) return double.NaN;
|
---|
| 44 | return BestKnownQualityParameter.Value.Value;
|
---|
| 45 | }
|
---|
| 46 | set {
|
---|
| 47 | if (BestKnownQualityParameter.Value == null) BestKnownQualityParameter.Value = new DoubleValue(value);
|
---|
| 48 | else BestKnownQualityParameter.Value.Value = value;
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
[10753] | 52 | [StorableConstructor]
|
---|
[16565] | 53 | protected SingleObjectiveBasicProblem(StorableConstructorFlag _) : base(_) { }
|
---|
[10753] | 54 |
|
---|
[11814] | 55 | protected SingleObjectiveBasicProblem(SingleObjectiveBasicProblem<TEncoding> original, Cloner cloner)
|
---|
[10753] | 56 | : base(original, cloner) {
|
---|
[11739] | 57 | ParameterizeOperators();
|
---|
[10753] | 58 | }
|
---|
| 59 |
|
---|
[11814] | 60 | protected SingleObjectiveBasicProblem()
|
---|
[11739] | 61 | : base() {
|
---|
[11996] | 62 | Parameters.Add(new FixedValueParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized.", (BoolValue)new BoolValue(Maximization).AsReadOnly()) { Hidden = true });
|
---|
[11753] | 63 | Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this problem."));
|
---|
[10753] | 64 |
|
---|
[11753] | 65 | Operators.Add(Evaluator);
|
---|
[11396] | 66 | Operators.Add(new SingleObjectiveAnalyzer());
|
---|
[11753] | 67 | Operators.Add(new SingleObjectiveImprover());
|
---|
| 68 | Operators.Add(new SingleObjectiveMoveEvaluator());
|
---|
| 69 | Operators.Add(new SingleObjectiveMoveGenerator());
|
---|
| 70 | Operators.Add(new SingleObjectiveMoveMaker());
|
---|
[10753] | 71 |
|
---|
[11739] | 72 | ParameterizeOperators();
|
---|
[10753] | 73 | }
|
---|
| 74 |
|
---|
| 75 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 76 | private void AfterDeserialization() {
|
---|
[11739] | 77 | ParameterizeOperators();
|
---|
[10753] | 78 | }
|
---|
| 79 |
|
---|
[11739] | 80 | public abstract bool Maximization { get; }
|
---|
| 81 | public abstract double Evaluate(Individual individual, IRandom random);
|
---|
[11880] | 82 | public virtual void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) { }
|
---|
[11786] | 83 | public virtual IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) {
|
---|
| 84 | return Enumerable.Empty<Individual>();
|
---|
| 85 | }
|
---|
[10753] | 86 |
|
---|
[15051] | 87 | protected Tuple<Individual, double> GetBestIndividual(Individual[] individuals, double[] qualities) {
|
---|
| 88 | return GetBestIndividual(individuals, qualities, Maximization);
|
---|
| 89 | }
|
---|
| 90 | public static Tuple<Individual, double> GetBestIndividual(Individual[] individuals, double[] qualities, bool maximization) {
|
---|
| 91 | var zipped = individuals.Zip(qualities, (i, q) => new { Individual = i, Quality = q });
|
---|
| 92 | var best = (maximization ? zipped.OrderByDescending(z => z.Quality) : zipped.OrderBy(z => z.Quality)).First();
|
---|
| 93 | return Tuple.Create(best.Individual, best.Quality);
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[11970] | 96 | protected override void OnOperatorsChanged() {
|
---|
| 97 | if (Encoding != null) {
|
---|
| 98 | PruneMultiObjectiveOperators(Encoding);
|
---|
| 99 | var multiEncoding = Encoding as MultiEncoding;
|
---|
| 100 | if (multiEncoding != null) {
|
---|
| 101 | foreach (var encoding in multiEncoding.Encodings.ToList()) {
|
---|
| 102 | PruneMultiObjectiveOperators(encoding);
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
[16794] | 106 | base.OnOperatorsChanged();
|
---|
[11970] | 107 | }
|
---|
| 108 |
|
---|
| 109 | private void PruneMultiObjectiveOperators(IEncoding encoding) {
|
---|
| 110 | if (encoding.Operators.Any(x => x is IMultiObjectiveOperator && !(x is ISingleObjectiveOperator)))
|
---|
| 111 | encoding.Operators = encoding.Operators.Where(x => !(x is IMultiObjectiveOperator) || x is ISingleObjectiveOperator).ToList();
|
---|
[15084] | 112 |
|
---|
| 113 | foreach (var multiOp in Encoding.Operators.OfType<IMultiOperator>()) {
|
---|
| 114 | foreach (var moOp in multiOp.Operators.Where(x => x is IMultiObjectiveOperator).ToList()) {
|
---|
| 115 | multiOp.RemoveOperator(moOp);
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
[11970] | 118 | }
|
---|
| 119 |
|
---|
[11396] | 120 | protected override void OnEvaluatorChanged() {
|
---|
| 121 | base.OnEvaluatorChanged();
|
---|
[11739] | 122 | ParameterizeOperators();
|
---|
[11396] | 123 | }
|
---|
| 124 |
|
---|
[11786] | 125 | private void ParameterizeOperators() {
|
---|
[11739] | 126 | foreach (var op in Operators.OfType<ISingleObjectiveEvaluationOperator>())
|
---|
| 127 | op.EvaluateFunc = Evaluate;
|
---|
| 128 | foreach (var op in Operators.OfType<ISingleObjectiveAnalysisOperator>())
|
---|
| 129 | op.AnalyzeAction = Analyze;
|
---|
[11786] | 130 | foreach (var op in Operators.OfType<INeighborBasedOperator>())
|
---|
| 131 | op.GetNeighborsFunc = GetNeighbors;
|
---|
[11393] | 132 | }
|
---|
| 133 |
|
---|
[11753] | 134 | #region ISingleObjectiveHeuristicOptimizationProblem Members
|
---|
| 135 | IParameter ISingleObjectiveHeuristicOptimizationProblem.MaximizationParameter {
|
---|
| 136 | get { return Parameters["Maximization"]; }
|
---|
| 137 | }
|
---|
| 138 | IParameter ISingleObjectiveHeuristicOptimizationProblem.BestKnownQualityParameter {
|
---|
| 139 | get { return Parameters["BestKnownQuality"]; }
|
---|
| 140 | }
|
---|
| 141 | ISingleObjectiveEvaluator ISingleObjectiveHeuristicOptimizationProblem.Evaluator {
|
---|
| 142 | get { return Evaluator; }
|
---|
| 143 | }
|
---|
| 144 | #endregion
|
---|
[10753] | 145 | }
|
---|
| 146 | }
|
---|