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

Last change on this file since 13361 was 13361, checked in by mkommend, 4 years ago

#2521: Adapted real vector encoding, test function problems, P3, CMA-ES and optimization.

File size: 5.6 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> :
33    Problem<TEncoding, TSolution, SingleObjectiveEvaluator<TSolution>>,
34    ISingleObjectiveProblem<TEncoding, TSolution>,
35    ISingleObjectiveProblemDefinition<TEncoding, TSolution>
36    where TEncoding : class, IEncoding<TSolution>
37    where TSolution : class, ISolution {
38
39    protected IValueParameter<DoubleValue> BestKnownQualityParameter {
40      get { return (IValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
41    }
42
43    public double BestKnownQuality {
44      get {
45        if (BestKnownQualityParameter.Value == null) return double.NaN;
46        return BestKnownQualityParameter.Value.Value;
47      }
48      set {
49        if (BestKnownQualityParameter.Value == null) BestKnownQualityParameter.Value = new DoubleValue(value);
50        else BestKnownQualityParameter.Value.Value = value;
51      }
52    }
53
54    [StorableConstructor]
55    protected SingleObjectiveProblem(bool deserializing) : base(deserializing) { }
56
57    protected SingleObjectiveProblem(SingleObjectiveProblem<TEncoding, TSolution> original, Cloner cloner)
58      : base(original, cloner) {
59      ParameterizeOperators();
60    }
61
62    protected SingleObjectiveProblem()
63      : base() {
64      Parameters.Add(new FixedValueParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized.", (BoolValue)new BoolValue(Maximization).AsReadOnly()) { Hidden = true });
65      Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this problem."));
66
67      Operators.Add(Evaluator);
68      Operators.Add(new SingleObjectiveAnalyzer<TSolution>());
69      Operators.Add(new SingleObjectiveImprover<TSolution>());
70      Operators.Add(new SingleObjectiveMoveEvaluator<TSolution>());
71      Operators.Add(new SingleObjectiveMoveGenerator<TSolution>());
72      Operators.Add(new SingleObjectiveMoveMaker<TSolution>());
73
74      ParameterizeOperators();
75    }
76
77    [StorableHook(HookType.AfterDeserialization)]
78    private void AfterDeserialization() {
79      ParameterizeOperators();
80    }
81
82    public abstract bool Maximization { get; }
83    public abstract double Evaluate(TSolution individual, IRandom random);
84    public virtual void Analyze(TSolution[] individuals, double[] qualities, ResultCollection results, IRandom random) { }
85    public virtual IEnumerable<TSolution> GetNeighbors(TSolution individual, IRandom random) {
86      return Enumerable.Empty<TSolution>();
87    }
88
89    public virtual bool IsBetter(double quality, double bestQuality) {
90      return (Maximization && quality > bestQuality || !Maximization && quality < bestQuality);
91    }
92
93    //TODO
94    //protected override void OnOperatorsChanged() {
95    //  base.OnOperatorsChanged();
96    //  if (Encoding != null) {
97    //    PruneMultiObjectiveOperators(Encoding);
98    //    var multiEncoding = Encoding as MultiEncoding;
99    //    if (multiEncoding != null) {
100    //      foreach (var encoding in multiEncoding.Encodings.ToList()) {
101    //        PruneMultiObjectiveOperators(encoding);
102    //      }
103    //    }
104    //  }
105    //}
106
107    //private void PruneMultiObjectiveOperators(IEncoding<TSolution> encoding) {
108    //  if (encoding.Operators.Any(x => x is IMultiObjectiveOperator && !(x is ISingleObjectiveOperator)))
109    //    encoding.Operators = encoding.Operators.Where(x => !(x is IMultiObjectiveOperator) || x is ISingleObjectiveOperator).ToList();
110    //}
111
112    protected override void OnEvaluatorChanged() {
113      base.OnEvaluatorChanged();
114      ParameterizeOperators();
115    }
116
117    private void ParameterizeOperators() {
118      foreach (var op in Operators.OfType<ISingleObjectiveEvaluationOperator<TSolution>>())
119        op.EvaluateFunc = Evaluate;
120      foreach (var op in Operators.OfType<ISingleObjectiveAnalysisOperator<TSolution>>())
121        op.AnalyzeAction = Analyze;
122      foreach (var op in Operators.OfType<INeighborBasedOperator<TSolution>>())
123        op.GetNeighborsFunc = GetNeighbors;
124    }
125
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
137  }
138}
Note: See TracBrowser for help on using the repository browser.