[10753] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 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 |
|
---|
[11786] | 22 | using System.Collections.Generic;
|
---|
[10753] | 23 | using System.Linq;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
[11753] | 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Parameters;
|
---|
[10753] | 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 |
|
---|
[11949] | 30 | namespace HeuristicLab.Optimization {
|
---|
[10753] | 31 | [StorableClass]
|
---|
[13339] | 32 | public abstract class SingleObjectiveProblem<TEncoding, TSolution> : Problem<TEncoding, TSolution, SingleObjectiveEvaluator<TSolution>>, ISingleObjectiveProblem<TEncoding, TSolution>
|
---|
| 33 | where TEncoding : class, IEncoding<TSolution>
|
---|
| 34 | where TSolution : class, ISolution {
|
---|
[11990] | 35 |
|
---|
| 36 | protected IValueParameter<DoubleValue> BestKnownQualityParameter {
|
---|
| 37 | get { return (IValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | public double BestKnownQuality {
|
---|
| 41 | get {
|
---|
| 42 | if (BestKnownQualityParameter.Value == null) return double.NaN;
|
---|
| 43 | return BestKnownQualityParameter.Value.Value;
|
---|
| 44 | }
|
---|
| 45 | set {
|
---|
| 46 | if (BestKnownQualityParameter.Value == null) BestKnownQualityParameter.Value = new DoubleValue(value);
|
---|
| 47 | else BestKnownQualityParameter.Value.Value = value;
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[10753] | 51 | [StorableConstructor]
|
---|
[13336] | 52 | protected SingleObjectiveProblem(bool deserializing) : base(deserializing) { }
|
---|
[10753] | 53 |
|
---|
[13339] | 54 | protected SingleObjectiveProblem(SingleObjectiveProblem<TEncoding, TSolution> original, Cloner cloner)
|
---|
[10753] | 55 | : base(original, cloner) {
|
---|
[11739] | 56 | ParameterizeOperators();
|
---|
[10753] | 57 | }
|
---|
| 58 |
|
---|
[13336] | 59 | protected SingleObjectiveProblem()
|
---|
[11739] | 60 | : base() {
|
---|
[11996] | 61 | Parameters.Add(new FixedValueParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized.", (BoolValue)new BoolValue(Maximization).AsReadOnly()) { Hidden = true });
|
---|
[11753] | 62 | Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this problem."));
|
---|
[10753] | 63 |
|
---|
[11753] | 64 | Operators.Add(Evaluator);
|
---|
[13336] | 65 | Operators.Add(new SingleObjectiveAnalyzer<TSolution>());
|
---|
| 66 | Operators.Add(new SingleObjectiveImprover<TSolution>());
|
---|
| 67 | Operators.Add(new SingleObjectiveMoveEvaluator<TSolution>());
|
---|
| 68 | Operators.Add(new SingleObjectiveMoveGenerator<TSolution>());
|
---|
| 69 | Operators.Add(new SingleObjectiveMoveMaker<TSolution>());
|
---|
[10753] | 70 |
|
---|
[11739] | 71 | ParameterizeOperators();
|
---|
[10753] | 72 | }
|
---|
| 73 |
|
---|
| 74 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 75 | private void AfterDeserialization() {
|
---|
[11739] | 76 | ParameterizeOperators();
|
---|
[10753] | 77 | }
|
---|
| 78 |
|
---|
[11739] | 79 | public abstract bool Maximization { get; }
|
---|
[13336] | 80 | public abstract double Evaluate(TSolution individual, IRandom random);
|
---|
| 81 | public virtual void Analyze(TSolution[] individuals, double[] qualities, ResultCollection results, IRandom random) { }
|
---|
| 82 | public virtual IEnumerable<TSolution> GetNeighbors(TSolution individual, IRandom random) {
|
---|
| 83 | return Enumerable.Empty<TSolution>();
|
---|
[11786] | 84 | }
|
---|
[10753] | 85 |
|
---|
[13336] | 86 | public virtual bool IsBetter(double quality, double bestQuality) {
|
---|
| 87 | return (Maximization && quality > bestQuality || !Maximization && quality < bestQuality);
|
---|
[11970] | 88 | }
|
---|
| 89 |
|
---|
[13336] | 90 | //TODO
|
---|
| 91 | //protected override void OnOperatorsChanged() {
|
---|
| 92 | // base.OnOperatorsChanged();
|
---|
| 93 | // if (Encoding != null) {
|
---|
| 94 | // PruneMultiObjectiveOperators(Encoding);
|
---|
| 95 | // var multiEncoding = Encoding as MultiEncoding;
|
---|
| 96 | // if (multiEncoding != null) {
|
---|
| 97 | // foreach (var encoding in multiEncoding.Encodings.ToList()) {
|
---|
| 98 | // PruneMultiObjectiveOperators(encoding);
|
---|
| 99 | // }
|
---|
| 100 | // }
|
---|
| 101 | // }
|
---|
| 102 | //}
|
---|
[11970] | 103 |
|
---|
[13336] | 104 | //private void PruneMultiObjectiveOperators(IEncoding<TSolution> encoding) {
|
---|
| 105 | // if (encoding.Operators.Any(x => x is IMultiObjectiveOperator && !(x is ISingleObjectiveOperator)))
|
---|
| 106 | // encoding.Operators = encoding.Operators.Where(x => !(x is IMultiObjectiveOperator) || x is ISingleObjectiveOperator).ToList();
|
---|
| 107 | //}
|
---|
| 108 |
|
---|
[11396] | 109 | protected override void OnEvaluatorChanged() {
|
---|
| 110 | base.OnEvaluatorChanged();
|
---|
[11739] | 111 | ParameterizeOperators();
|
---|
[11396] | 112 | }
|
---|
| 113 |
|
---|
[11786] | 114 | private void ParameterizeOperators() {
|
---|
[13336] | 115 | foreach (var op in Operators.OfType<ISingleObjectiveEvaluationOperator<TSolution>>())
|
---|
[11739] | 116 | op.EvaluateFunc = Evaluate;
|
---|
[13336] | 117 | foreach (var op in Operators.OfType<ISingleObjectiveAnalysisOperator<TSolution>>())
|
---|
[11739] | 118 | op.AnalyzeAction = Analyze;
|
---|
[13336] | 119 | foreach (var op in Operators.OfType<INeighborBasedOperator<TSolution>>())
|
---|
[11786] | 120 | op.GetNeighborsFunc = GetNeighbors;
|
---|
[11393] | 121 | }
|
---|
| 122 |
|
---|
[11753] | 123 | #region ISingleObjectiveHeuristicOptimizationProblem Members
|
---|
| 124 | IParameter ISingleObjectiveHeuristicOptimizationProblem.MaximizationParameter {
|
---|
| 125 | get { return Parameters["Maximization"]; }
|
---|
| 126 | }
|
---|
| 127 | IParameter ISingleObjectiveHeuristicOptimizationProblem.BestKnownQualityParameter {
|
---|
| 128 | get { return Parameters["BestKnownQuality"]; }
|
---|
| 129 | }
|
---|
| 130 | ISingleObjectiveEvaluator ISingleObjectiveHeuristicOptimizationProblem.Evaluator {
|
---|
| 131 | get { return Evaluator; }
|
---|
| 132 | }
|
---|
| 133 | #endregion
|
---|
[10753] | 134 | }
|
---|
| 135 | }
|
---|