1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 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.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Optimization {
|
---|
31 | [StorableClass]
|
---|
32 | public abstract class SingleObjectiveBasicProblem<TEncoding> : BasicProblem<TEncoding, SingleObjectiveEvaluator>,
|
---|
33 | ISingleObjectiveProblemDefinition, ISingleObjectiveHeuristicOptimizationProblem
|
---|
34 | where TEncoding : class, IEncoding {
|
---|
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 |
|
---|
51 | [StorableConstructor]
|
---|
52 | protected SingleObjectiveBasicProblem(bool deserializing) : base(deserializing) { }
|
---|
53 |
|
---|
54 | protected SingleObjectiveBasicProblem(SingleObjectiveBasicProblem<TEncoding> original, Cloner cloner)
|
---|
55 | : base(original, cloner) {
|
---|
56 | ParameterizeOperators();
|
---|
57 | }
|
---|
58 |
|
---|
59 | protected SingleObjectiveBasicProblem()
|
---|
60 | : base() {
|
---|
61 | Parameters.Add(new FixedValueParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized.", (BoolValue)new BoolValue(Maximization).AsReadOnly()) { Hidden = true });
|
---|
62 | Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this problem."));
|
---|
63 |
|
---|
64 | Operators.Add(Evaluator);
|
---|
65 | Operators.Add(new SingleObjectiveAnalyzer());
|
---|
66 | Operators.Add(new SingleObjectiveImprover());
|
---|
67 | Operators.Add(new SingleObjectiveMoveEvaluator());
|
---|
68 | Operators.Add(new SingleObjectiveMoveGenerator());
|
---|
69 | Operators.Add(new SingleObjectiveMoveMaker());
|
---|
70 |
|
---|
71 | ParameterizeOperators();
|
---|
72 | }
|
---|
73 |
|
---|
74 | [StorableHook(HookType.AfterDeserialization)]
|
---|
75 | private void AfterDeserialization() {
|
---|
76 | ParameterizeOperators();
|
---|
77 | }
|
---|
78 |
|
---|
79 | public abstract bool Maximization { get; }
|
---|
80 | public abstract double Evaluate(Individual individual, IRandom random);
|
---|
81 | public virtual void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) { }
|
---|
82 | public virtual IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) {
|
---|
83 | return Enumerable.Empty<Individual>();
|
---|
84 | }
|
---|
85 |
|
---|
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 | }
|
---|
98 |
|
---|
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();
|
---|
102 | }
|
---|
103 |
|
---|
104 | protected override void OnEvaluatorChanged() {
|
---|
105 | base.OnEvaluatorChanged();
|
---|
106 | ParameterizeOperators();
|
---|
107 | }
|
---|
108 |
|
---|
109 | private void ParameterizeOperators() {
|
---|
110 | foreach (var op in Operators.OfType<ISingleObjectiveEvaluationOperator>())
|
---|
111 | op.EvaluateFunc = Evaluate;
|
---|
112 | foreach (var op in Operators.OfType<ISingleObjectiveAnalysisOperator>())
|
---|
113 | op.AnalyzeAction = Analyze;
|
---|
114 | foreach (var op in Operators.OfType<INeighborBasedOperator>())
|
---|
115 | op.GetNeighborsFunc = GetNeighbors;
|
---|
116 | }
|
---|
117 |
|
---|
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
|
---|
129 | }
|
---|
130 | }
|
---|