Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ALPS/HeuristicLab.Optimization/3.3/BasicProblems/SingleObjectiveBasicProblem.cs @ 11975

Last change on this file since 11975 was 11970, checked in by abeham, 10 years ago

#2174:

  • Added ISingleObjectiveOperator and IMultiObjectiveOperator interfaces
    • Added interface to all operators that would fall in either of these categories (excluding MainLoop operators)
  • Added some checks in BasicProblem on whether the MultiEncoding is being used
    • A new event handler is added that reacts on encodings being removed or added to the MultiEncoding
  • Added code to remove the non-fit operators in (Single|Multi)ObjectiveBasicProblem
File size: 4.8 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    [StorableConstructor]
36    protected SingleObjectiveBasicProblem(bool deserializing) : base(deserializing) { }
37
38    protected SingleObjectiveBasicProblem(SingleObjectiveBasicProblem<TEncoding> original, Cloner cloner)
39      : base(original, cloner) {
40      ParameterizeOperators();
41    }
42
43    protected SingleObjectiveBasicProblem()
44      : base() {
45      Parameters.Add(new FixedValueParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized.", new BoolValue()));
46      Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this problem."));
47
48      Operators.Add(Evaluator);
49      Operators.Add(new SingleObjectiveAnalyzer());
50      Operators.Add(new SingleObjectiveImprover());
51      Operators.Add(new SingleObjectiveMoveEvaluator());
52      Operators.Add(new SingleObjectiveMoveGenerator());
53      Operators.Add(new SingleObjectiveMoveMaker());
54
55      ParameterizeOperators();
56    }
57
58    [StorableHook(HookType.AfterDeserialization)]
59    private void AfterDeserialization() {
60      ParameterizeOperators();
61    }
62
63    public abstract bool Maximization { get; }
64    public abstract double Evaluate(Individual individual, IRandom random);
65    public virtual void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) { }
66    public virtual IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) {
67      return Enumerable.Empty<Individual>();
68    }
69
70
71    protected override void OnEncodingChanged() {
72      base.OnEncodingChanged();
73      var max = (BoolValue)Parameters["Maximization"].ActualValue;
74      max.Value = Maximization;
75    }
76
77    protected override void OnOperatorsChanged() {
78      base.OnOperatorsChanged();
79      if (Encoding != null) {
80        PruneMultiObjectiveOperators(Encoding);
81        var multiEncoding = Encoding as MultiEncoding;
82        if (multiEncoding != null) {
83          foreach (var encoding in multiEncoding.Encodings.ToList()) {
84            PruneMultiObjectiveOperators(encoding);
85          }
86        }
87      }
88    }
89
90    private void PruneMultiObjectiveOperators(IEncoding encoding) {
91      if (encoding.Operators.Any(x => x is IMultiObjectiveOperator && !(x is ISingleObjectiveOperator)))
92        encoding.Operators = encoding.Operators.Where(x => !(x is IMultiObjectiveOperator) || x is ISingleObjectiveOperator).ToList();
93    }
94
95    protected override void OnEvaluatorChanged() {
96      base.OnEvaluatorChanged();
97      ParameterizeOperators();
98    }
99
100    private void ParameterizeOperators() {
101      foreach (var op in Operators.OfType<ISingleObjectiveEvaluationOperator>())
102        op.EvaluateFunc = Evaluate;
103      foreach (var op in Operators.OfType<ISingleObjectiveAnalysisOperator>())
104        op.AnalyzeAction = Analyze;
105      foreach (var op in Operators.OfType<INeighborBasedOperator>())
106        op.GetNeighborsFunc = GetNeighbors;
107    }
108
109    #region ISingleObjectiveHeuristicOptimizationProblem Members
110    IParameter ISingleObjectiveHeuristicOptimizationProblem.MaximizationParameter {
111      get { return Parameters["Maximization"]; }
112    }
113    IParameter ISingleObjectiveHeuristicOptimizationProblem.BestKnownQualityParameter {
114      get { return Parameters["BestKnownQuality"]; }
115    }
116    ISingleObjectiveEvaluator ISingleObjectiveHeuristicOptimizationProblem.Evaluator {
117      get { return Evaluator; }
118    }
119    #endregion
120  }
121}
Note: See TracBrowser for help on using the repository browser.