Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/SingleObjectiveProblem.cs @ 13396

Last change on this file since 13396 was 13396, checked in by abeham, 8 years ago

#2521:

  • Refactored QuadraticAssignmentProblem to use new SingleObjectiveProblem
    • Removed QAPEvaluator
    • Adapted RobustTabooSearch
  • Introduced several interfaces in PermutationEncoding necessary for wiring
  • Changed all Encodings to use IItem instead of IOperator in ConfigureOperators (name still unchanged)
  • Added a protected MaximizationParameter property in SingleObjectiveProblem (necessary for wiring)
  • Changed AlleleFrequnencyAnalyzer to use ISolution interface instead of IItem
  • Added a comment to ISolutionCreator<TSolution> of some changes that would be welcomed
File size: 5.8 KB
RevLine 
[10753]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[10753]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
[11786]22using System.Collections.Generic;
[10753]23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
[11753]26using HeuristicLab.Data;
27using HeuristicLab.Parameters;
[10753]28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
[11949]30namespace HeuristicLab.Optimization {
[10753]31  [StorableClass]
[13361]32  public abstract class SingleObjectiveProblem<TEncoding, TSolution> :
33    Problem<TEncoding, TSolution, SingleObjectiveEvaluator<TSolution>>,
34    ISingleObjectiveProblem<TEncoding, TSolution>,
35    ISingleObjectiveProblemDefinition<TEncoding, TSolution>
[13339]36    where TEncoding : class, IEncoding<TSolution>
37    where TSolution : class, ISolution {
[11990]38
39    protected IValueParameter<DoubleValue> BestKnownQualityParameter {
40      get { return (IValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
41    }
42
[13396]43    protected IFixedValueParameter<BoolValue> MaximizationParameter {
44      get { return (IFixedValueParameter<BoolValue>)Parameters["Maximization"]; }
45    }
46
[11990]47    public double BestKnownQuality {
48      get {
49        if (BestKnownQualityParameter.Value == null) return double.NaN;
50        return BestKnownQualityParameter.Value.Value;
51      }
52      set {
53        if (BestKnownQualityParameter.Value == null) BestKnownQualityParameter.Value = new DoubleValue(value);
54        else BestKnownQualityParameter.Value.Value = value;
55      }
56    }
57
[10753]58    [StorableConstructor]
[13336]59    protected SingleObjectiveProblem(bool deserializing) : base(deserializing) { }
[10753]60
[13339]61    protected SingleObjectiveProblem(SingleObjectiveProblem<TEncoding, TSolution> original, Cloner cloner)
[10753]62      : base(original, cloner) {
[11739]63      ParameterizeOperators();
[10753]64    }
65
[13336]66    protected SingleObjectiveProblem()
[11739]67      : base() {
[11996]68      Parameters.Add(new FixedValueParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized.", (BoolValue)new BoolValue(Maximization).AsReadOnly()) { Hidden = true });
[11753]69      Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this problem."));
[10753]70
[11753]71      Operators.Add(Evaluator);
[13336]72      Operators.Add(new SingleObjectiveAnalyzer<TSolution>());
73      Operators.Add(new SingleObjectiveImprover<TSolution>());
74      Operators.Add(new SingleObjectiveMoveEvaluator<TSolution>());
75      Operators.Add(new SingleObjectiveMoveGenerator<TSolution>());
76      Operators.Add(new SingleObjectiveMoveMaker<TSolution>());
[10753]77
[11739]78      ParameterizeOperators();
[10753]79    }
80
[13396]81    protected SingleObjectiveProblem(TEncoding encoding)
82      : base(encoding) {
83      Parameters.Add(new FixedValueParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized.", (BoolValue)new BoolValue(Maximization).AsReadOnly()) { Hidden = true });
84      Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this problem."));
85
86      Operators.Add(Evaluator);
87      Operators.Add(new SingleObjectiveAnalyzer<TSolution>());
88      Operators.Add(new SingleObjectiveImprover<TSolution>());
89      Operators.Add(new SingleObjectiveMoveEvaluator<TSolution>());
90      Operators.Add(new SingleObjectiveMoveGenerator<TSolution>());
91      Operators.Add(new SingleObjectiveMoveMaker<TSolution>());
92
93      ParameterizeOperators();
94    }
95
[10753]96    [StorableHook(HookType.AfterDeserialization)]
97    private void AfterDeserialization() {
[11739]98      ParameterizeOperators();
[10753]99    }
100
[11739]101    public abstract bool Maximization { get; }
[13336]102    public abstract double Evaluate(TSolution individual, IRandom random);
103    public virtual void Analyze(TSolution[] individuals, double[] qualities, ResultCollection results, IRandom random) { }
104    public virtual IEnumerable<TSolution> GetNeighbors(TSolution individual, IRandom random) {
105      return Enumerable.Empty<TSolution>();
[11786]106    }
[10753]107
[13336]108    public virtual bool IsBetter(double quality, double bestQuality) {
109      return (Maximization && quality > bestQuality || !Maximization && quality < bestQuality);
[11970]110    }
111
[11396]112    protected override void OnEvaluatorChanged() {
113      base.OnEvaluatorChanged();
[11739]114      ParameterizeOperators();
[11396]115    }
116
[11786]117    private void ParameterizeOperators() {
[13336]118      foreach (var op in Operators.OfType<ISingleObjectiveEvaluationOperator<TSolution>>())
[11739]119        op.EvaluateFunc = Evaluate;
[13336]120      foreach (var op in Operators.OfType<ISingleObjectiveAnalysisOperator<TSolution>>())
[11739]121        op.AnalyzeAction = Analyze;
[13336]122      foreach (var op in Operators.OfType<INeighborBasedOperator<TSolution>>())
[11786]123        op.GetNeighborsFunc = GetNeighbors;
[11393]124    }
125
[11753]126    #region ISingleObjectiveHeuristicOptimizationProblem Members
127    IParameter ISingleObjectiveHeuristicOptimizationProblem.MaximizationParameter {
128      get { return Parameters["Maximization"]; }
129    }
130    IParameter ISingleObjectiveHeuristicOptimizationProblem.BestKnownQualityParameter {
131      get { return Parameters["BestKnownQuality"]; }
132    }
133    ISingleObjectiveEvaluator ISingleObjectiveHeuristicOptimizationProblem.Evaluator {
134      get { return Evaluator; }
135    }
136    #endregion
[10753]137  }
138}
Note: See TracBrowser for help on using the repository browser.