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

Last change on this file since 13339 was 13339, checked in by mkommend, 3 years ago

#2521: Rectored problems and encodings.

File size: 5.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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 SingleObjectiveProblem<TEncoding, TSolution> : Problem<TEncoding, TSolution, SingleObjectiveEvaluator<TSolution>>, ISingleObjectiveProblem<TEncoding, TSolution>
33    where TEncoding : class, IEncoding<TSolution>
34    where TSolution : class, ISolution {
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 SingleObjectiveProblem(bool deserializing) : base(deserializing) { }
53
54    protected SingleObjectiveProblem(SingleObjectiveProblem<TEncoding, TSolution> original, Cloner cloner)
55      : base(original, cloner) {
56      ParameterizeOperators();
57    }
58
59    protected SingleObjectiveProblem()
60      : base() {
61      Parameters.Add(new FixedValueParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized.", (BoolValue)new BoolValue(Maximization).AsReadOnly()) { Hidden = true });
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<TSolution>());
66      Operators.Add(new SingleObjectiveImprover<TSolution>());
67      Operators.Add(new SingleObjectiveMoveEvaluator<TSolution>());
68      Operators.Add(new SingleObjectiveMoveGenerator<TSolution>());
69      Operators.Add(new SingleObjectiveMoveMaker<TSolution>());
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(TSolution individual, IRandom random);
81    public virtual void Analyze(TSolution[] individuals, double[] qualities, ResultCollection results, IRandom random) { }
82    public virtual IEnumerable<TSolution> GetNeighbors(TSolution individual, IRandom random) {
83      return Enumerable.Empty<TSolution>();
84    }
85
86    public virtual bool IsBetter(double quality, double bestQuality) {
87      return (Maximization && quality > bestQuality || !Maximization && quality < bestQuality);
88    }
89
90    //TODO
91    //protected override void OnOperatorsChanged() {
92    //  base.OnOperatorsChanged();
93    //  if (Encoding != null) {
94    //    PruneMultiObjectiveOperators(Encoding);
95    //    var multiEncoding = Encoding as MultiEncoding;
96    //    if (multiEncoding != null) {
97    //      foreach (var encoding in multiEncoding.Encodings.ToList()) {
98    //        PruneMultiObjectiveOperators(encoding);
99    //      }
100    //    }
101    //  }
102    //}
103
104    //private void PruneMultiObjectiveOperators(IEncoding<TSolution> encoding) {
105    //  if (encoding.Operators.Any(x => x is IMultiObjectiveOperator && !(x is ISingleObjectiveOperator)))
106    //    encoding.Operators = encoding.Operators.Where(x => !(x is IMultiObjectiveOperator) || x is ISingleObjectiveOperator).ToList();
107    //}
108
109    protected override void OnEvaluatorChanged() {
110      base.OnEvaluatorChanged();
111      ParameterizeOperators();
112    }
113
114    private void ParameterizeOperators() {
115      foreach (var op in Operators.OfType<ISingleObjectiveEvaluationOperator<TSolution>>())
116        op.EvaluateFunc = Evaluate;
117      foreach (var op in Operators.OfType<ISingleObjectiveAnalysisOperator<TSolution>>())
118        op.AnalyzeAction = Analyze;
119      foreach (var op in Operators.OfType<INeighborBasedOperator<TSolution>>())
120        op.GetNeighborsFunc = GetNeighbors;
121    }
122
123    #region ISingleObjectiveHeuristicOptimizationProblem Members
124    IParameter ISingleObjectiveHeuristicOptimizationProblem.MaximizationParameter {
125      get { return Parameters["Maximization"]; }
126    }
127    IParameter ISingleObjectiveHeuristicOptimizationProblem.BestKnownQualityParameter {
128      get { return Parameters["BestKnownQuality"]; }
129    }
130    ISingleObjectiveEvaluator ISingleObjectiveHeuristicOptimizationProblem.Evaluator {
131      get { return Evaluator; }
132    }
133    #endregion
134  }
135}
Note: See TracBrowser for help on using the repository browser.