[10753] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12009] | 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]
|
---|
[11814] | 32 | public abstract class SingleObjectiveBasicProblem<TEncoding> : BasicProblem<TEncoding, SingleObjectiveEvaluator>,
|
---|
[11753] | 33 | ISingleObjectiveProblemDefinition, ISingleObjectiveHeuristicOptimizationProblem
|
---|
[11739] | 34 | where TEncoding : class, IEncoding {
|
---|
[12005] | 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]
|
---|
[11814] | 52 | protected SingleObjectiveBasicProblem(bool deserializing) : base(deserializing) { }
|
---|
[10753] | 53 |
|
---|
[11814] | 54 | protected SingleObjectiveBasicProblem(SingleObjectiveBasicProblem<TEncoding> original, Cloner cloner)
|
---|
[10753] | 55 | : base(original, cloner) {
|
---|
[11739] | 56 | ParameterizeOperators();
|
---|
[10753] | 57 | }
|
---|
| 58 |
|
---|
[11814] | 59 | protected SingleObjectiveBasicProblem()
|
---|
[11739] | 60 | : base() {
|
---|
[12005] | 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);
|
---|
[11396] | 65 | Operators.Add(new SingleObjectiveAnalyzer());
|
---|
[11753] | 66 | Operators.Add(new SingleObjectiveImprover());
|
---|
| 67 | Operators.Add(new SingleObjectiveMoveEvaluator());
|
---|
| 68 | Operators.Add(new SingleObjectiveMoveGenerator());
|
---|
| 69 | Operators.Add(new SingleObjectiveMoveMaker());
|
---|
[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; }
|
---|
| 80 | public abstract double Evaluate(Individual individual, IRandom random);
|
---|
[11880] | 81 | public virtual void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) { }
|
---|
[11786] | 82 | public virtual IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) {
|
---|
| 83 | return Enumerable.Empty<Individual>();
|
---|
| 84 | }
|
---|
[10753] | 85 |
|
---|
[12005] | 86 | protected override void OnOperatorsChanged() {
|
---|
| 87 | base.OnOperatorsChanged();
|
---|
| 88 | if (Encoding != null) {
|
---|
| 89 | PruneMultiObjectiveOperators(Encoding);
|
---|
| 90 | var multiEncoding = Encoding as MultiEncoding;
|
---|
| 91 | if (multiEncoding != null) {
|
---|
| 92 | foreach (var encoding in multiEncoding.Encodings.ToList()) {
|
---|
| 93 | PruneMultiObjectiveOperators(encoding);
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
[10753] | 98 |
|
---|
[12005] | 99 | private void PruneMultiObjectiveOperators(IEncoding encoding) {
|
---|
| 100 | if (encoding.Operators.Any(x => x is IMultiObjectiveOperator && !(x is ISingleObjectiveOperator)))
|
---|
| 101 | encoding.Operators = encoding.Operators.Where(x => !(x is IMultiObjectiveOperator) || x is ISingleObjectiveOperator).ToList();
|
---|
[11786] | 102 | }
|
---|
| 103 |
|
---|
[11396] | 104 | protected override void OnEvaluatorChanged() {
|
---|
| 105 | base.OnEvaluatorChanged();
|
---|
[11739] | 106 | ParameterizeOperators();
|
---|
[11396] | 107 | }
|
---|
| 108 |
|
---|
[11786] | 109 | private void ParameterizeOperators() {
|
---|
[11739] | 110 | foreach (var op in Operators.OfType<ISingleObjectiveEvaluationOperator>())
|
---|
| 111 | op.EvaluateFunc = Evaluate;
|
---|
| 112 | foreach (var op in Operators.OfType<ISingleObjectiveAnalysisOperator>())
|
---|
| 113 | op.AnalyzeAction = Analyze;
|
---|
[11786] | 114 | foreach (var op in Operators.OfType<INeighborBasedOperator>())
|
---|
| 115 | op.GetNeighborsFunc = GetNeighbors;
|
---|
[11393] | 116 | }
|
---|
| 117 |
|
---|
[11753] | 118 | #region ISingleObjectiveHeuristicOptimizationProblem Members
|
---|
| 119 | IParameter ISingleObjectiveHeuristicOptimizationProblem.MaximizationParameter {
|
---|
| 120 | get { return Parameters["Maximization"]; }
|
---|
| 121 | }
|
---|
| 122 | IParameter ISingleObjectiveHeuristicOptimizationProblem.BestKnownQualityParameter {
|
---|
| 123 | get { return Parameters["BestKnownQuality"]; }
|
---|
| 124 | }
|
---|
| 125 | ISingleObjectiveEvaluator ISingleObjectiveHeuristicOptimizationProblem.Evaluator {
|
---|
| 126 | get { return Evaluator; }
|
---|
| 127 | }
|
---|
| 128 | #endregion
|
---|
[10753] | 129 | }
|
---|
| 130 | }
|
---|