Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/SingleObjectiveProgrammableProblem.cs @ 11780

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

#2174: Corrected maximization in programmable problem base classes.

File size: 3.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.Linq;
23using HeuristicLab.Analysis;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Problems.Programmable {
32  [StorableClass]
33  public abstract class SingleObjectiveProgrammableProblem<TEncoding> : ProgrammableProblem<TEncoding, SingleObjectiveEvaluator>,
34    ISingleObjectiveProblemDefinition, ISingleObjectiveHeuristicOptimizationProblem
35  where TEncoding : class, IEncoding {
36    [StorableConstructor]
37    protected SingleObjectiveProgrammableProblem(bool deserializing) : base(deserializing) { }
38
39    protected SingleObjectiveProgrammableProblem(SingleObjectiveProgrammableProblem<TEncoding> original, Cloner cloner)
40      : base(original, cloner) {
41      ParameterizeOperators();
42    }
43
44    protected SingleObjectiveProgrammableProblem()
45      : base() {
46      Parameters.Add(new FixedValueParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized.", new BoolValue(Maximization)));
47      Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this problem."));
48
49      Operators.Add(Evaluator);
50      Operators.Add(new BestScopeSolutionAnalyzer());
51      Operators.Add(new SingleObjectiveAnalyzer());
52      Operators.Add(new SingleObjectiveImprover());
53      Operators.Add(new SingleObjectiveMoveEvaluator());
54      Operators.Add(new SingleObjectiveMoveGenerator());
55      Operators.Add(new SingleObjectiveMoveMaker());
56
57      ParameterizeOperators();
58    }
59
60    [StorableHook(HookType.AfterDeserialization)]
61    private void AfterDeserialization() {
62      ParameterizeOperators();
63    }
64
65    public abstract bool Maximization { get; }
66    public abstract double Evaluate(Individual individual, IRandom random);
67    public virtual void Analyze(Individual[] individuals, double[] qualities, ResultCollection results) { }
68
69
70    protected override void OnEvaluatorChanged() {
71      base.OnEvaluatorChanged();
72      ParameterizeOperators();
73    }
74
75    protected override void ParameterizeOperators() {
76      base.ParameterizeOperators();
77      foreach (var op in Operators.OfType<ISingleObjectiveEvaluationOperator>())
78        op.EvaluateFunc = Evaluate;
79      foreach (var op in Operators.OfType<ISingleObjectiveAnalysisOperator>())
80        op.AnalyzeAction = Analyze;
81    }
82
83    #region ISingleObjectiveHeuristicOptimizationProblem Members
84    IParameter ISingleObjectiveHeuristicOptimizationProblem.MaximizationParameter {
85      get { return Parameters["Maximization"]; }
86    }
87    IParameter ISingleObjectiveHeuristicOptimizationProblem.BestKnownQualityParameter {
88      get { return Parameters["BestKnownQuality"]; }
89    }
90    ISingleObjectiveEvaluator ISingleObjectiveHeuristicOptimizationProblem.Evaluator {
91      get { return Evaluator; }
92    }
93    #endregion
94  }
95}
Note: See TracBrowser for help on using the repository browser.