Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15051 was 15051, checked in by abeham, 7 years ago

#2783:

  • Implemented GetBestIndividual as public static and protected method in SingleObjectiveBasicProblem
  • Impblemented GetParetorFronts as public static and protected method in MultiObjectiveBasicProblem
  • Implemented AddOrUpdateResult as public method in ResultCollection
File size: 5.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Optimization {
32  [StorableClass]
33  public abstract class SingleObjectiveBasicProblem<TEncoding> : BasicProblem<TEncoding, SingleObjectiveEvaluator>,
34    ISingleObjectiveProblemDefinition, ISingleObjectiveHeuristicOptimizationProblem
35  where TEncoding : class, IEncoding {
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
52    [StorableConstructor]
53    protected SingleObjectiveBasicProblem(bool deserializing) : base(deserializing) { }
54
55    protected SingleObjectiveBasicProblem(SingleObjectiveBasicProblem<TEncoding> original, Cloner cloner)
56      : base(original, cloner) {
57      ParameterizeOperators();
58    }
59
60    protected SingleObjectiveBasicProblem()
61      : base() {
62      Parameters.Add(new FixedValueParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized.", (BoolValue)new BoolValue(Maximization).AsReadOnly()) { Hidden = true });
63      Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this problem."));
64
65      Operators.Add(Evaluator);
66      Operators.Add(new SingleObjectiveAnalyzer());
67      Operators.Add(new SingleObjectiveImprover());
68      Operators.Add(new SingleObjectiveMoveEvaluator());
69      Operators.Add(new SingleObjectiveMoveGenerator());
70      Operators.Add(new SingleObjectiveMoveMaker());
71
72      ParameterizeOperators();
73    }
74
75    [StorableHook(HookType.AfterDeserialization)]
76    private void AfterDeserialization() {
77      ParameterizeOperators();
78    }
79
80    public abstract bool Maximization { get; }
81    public abstract double Evaluate(Individual individual, IRandom random);
82    public virtual void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) { }
83    public virtual IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) {
84      return Enumerable.Empty<Individual>();
85    }
86
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
96    protected override void OnOperatorsChanged() {
97      base.OnOperatorsChanged();
98      if (Encoding != null) {
99        PruneMultiObjectiveOperators(Encoding);
100        var multiEncoding = Encoding as MultiEncoding;
101        if (multiEncoding != null) {
102          foreach (var encoding in multiEncoding.Encodings.ToList()) {
103            PruneMultiObjectiveOperators(encoding);
104          }
105        }
106      }
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();
112    }
113
114    protected override void OnEvaluatorChanged() {
115      base.OnEvaluatorChanged();
116      ParameterizeOperators();
117    }
118
119    private void ParameterizeOperators() {
120      foreach (var op in Operators.OfType<ISingleObjectiveEvaluationOperator>())
121        op.EvaluateFunc = Evaluate;
122      foreach (var op in Operators.OfType<ISingleObjectiveAnalysisOperator>())
123        op.AnalyzeAction = Analyze;
124      foreach (var op in Operators.OfType<INeighborBasedOperator>())
125        op.GetNeighborsFunc = GetNeighbors;
126    }
127
128    #region ISingleObjectiveHeuristicOptimizationProblem Members
129    IParameter ISingleObjectiveHeuristicOptimizationProblem.MaximizationParameter {
130      get { return Parameters["Maximization"]; }
131    }
132    IParameter ISingleObjectiveHeuristicOptimizationProblem.BestKnownQualityParameter {
133      get { return Parameters["BestKnownQuality"]; }
134    }
135    ISingleObjectiveEvaluator ISingleObjectiveHeuristicOptimizationProblem.Evaluator {
136      get { return Evaluator; }
137    }
138    #endregion
139  }
140}
Note: See TracBrowser for help on using the repository browser.