Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization/3.3/BasicProblems/SingleObjectiveBasicProblem.cs @ 11990

Last change on this file since 11990 was 11990, checked in by mkommend, 9 years ago

#2282: Added best known quality parameter to single-objective basic problem and new one-max problem.

File size: 5.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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
22using System.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace 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.", new BoolValue()));
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
87    protected override void OnEncodingChanged() {
88      base.OnEncodingChanged();
89      var max = (BoolValue)Parameters["Maximization"].ActualValue;
90      max.Value = Maximization;
91    }
92
93    protected override void OnOperatorsChanged() {
94      base.OnOperatorsChanged();
95      if (Encoding != null) {
96        PruneMultiObjectiveOperators(Encoding);
97        var multiEncoding = Encoding as MultiEncoding;
98        if (multiEncoding != null) {
99          foreach (var encoding in multiEncoding.Encodings.ToList()) {
100            PruneMultiObjectiveOperators(encoding);
101          }
102        }
103      }
104    }
105
106    private void PruneMultiObjectiveOperators(IEncoding encoding) {
107      if (encoding.Operators.Any(x => x is IMultiObjectiveOperator && !(x is ISingleObjectiveOperator)))
108        encoding.Operators = encoding.Operators.Where(x => !(x is IMultiObjectiveOperator) || x is ISingleObjectiveOperator).ToList();
109    }
110
111    protected override void OnEvaluatorChanged() {
112      base.OnEvaluatorChanged();
113      ParameterizeOperators();
114    }
115
116    private void ParameterizeOperators() {
117      foreach (var op in Operators.OfType<ISingleObjectiveEvaluationOperator>())
118        op.EvaluateFunc = Evaluate;
119      foreach (var op in Operators.OfType<ISingleObjectiveAnalysisOperator>())
120        op.AnalyzeAction = Analyze;
121      foreach (var op in Operators.OfType<INeighborBasedOperator>())
122        op.GetNeighborsFunc = GetNeighbors;
123    }
124
125    #region ISingleObjectiveHeuristicOptimizationProblem Members
126    IParameter ISingleObjectiveHeuristicOptimizationProblem.MaximizationParameter {
127      get { return Parameters["Maximization"]; }
128    }
129    IParameter ISingleObjectiveHeuristicOptimizationProblem.BestKnownQualityParameter {
130      get { return Parameters["BestKnownQuality"]; }
131    }
132    ISingleObjectiveEvaluator ISingleObjectiveHeuristicOptimizationProblem.Evaluator {
133      get { return Evaluator; }
134    }
135    #endregion
136  }
137}
Note: See TracBrowser for help on using the repository browser.