Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2521: Added encodings for schedules.

File size: 5.9 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 {
[13437]53        if (double.IsNaN(value)) {
54          BestKnownQualityParameter.Value = null;
55          return;
56        }
[11990]57        if (BestKnownQualityParameter.Value == null) BestKnownQualityParameter.Value = new DoubleValue(value);
58        else BestKnownQualityParameter.Value.Value = value;
59      }
60    }
61
[10753]62    [StorableConstructor]
[13336]63    protected SingleObjectiveProblem(bool deserializing) : base(deserializing) { }
[10753]64
[13339]65    protected SingleObjectiveProblem(SingleObjectiveProblem<TEncoding, TSolution> original, Cloner cloner)
[10753]66      : base(original, cloner) {
[11739]67      ParameterizeOperators();
[10753]68    }
69
[13336]70    protected SingleObjectiveProblem()
[11739]71      : base() {
[11996]72      Parameters.Add(new FixedValueParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized.", (BoolValue)new BoolValue(Maximization).AsReadOnly()) { Hidden = true });
[11753]73      Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this problem."));
[10753]74
[11753]75      Operators.Add(Evaluator);
[13336]76      Operators.Add(new SingleObjectiveAnalyzer<TSolution>());
77      Operators.Add(new SingleObjectiveImprover<TSolution>());
78      Operators.Add(new SingleObjectiveMoveEvaluator<TSolution>());
79      Operators.Add(new SingleObjectiveMoveGenerator<TSolution>());
80      Operators.Add(new SingleObjectiveMoveMaker<TSolution>());
[10753]81
[11739]82      ParameterizeOperators();
[10753]83    }
84
[13396]85    protected SingleObjectiveProblem(TEncoding encoding)
86      : base(encoding) {
87      Parameters.Add(new FixedValueParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized.", (BoolValue)new BoolValue(Maximization).AsReadOnly()) { Hidden = true });
88      Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this problem."));
89
90      Operators.Add(Evaluator);
91      Operators.Add(new SingleObjectiveAnalyzer<TSolution>());
92      Operators.Add(new SingleObjectiveImprover<TSolution>());
93      Operators.Add(new SingleObjectiveMoveEvaluator<TSolution>());
94      Operators.Add(new SingleObjectiveMoveGenerator<TSolution>());
95      Operators.Add(new SingleObjectiveMoveMaker<TSolution>());
96
97      ParameterizeOperators();
98    }
99
[10753]100    [StorableHook(HookType.AfterDeserialization)]
101    private void AfterDeserialization() {
[11739]102      ParameterizeOperators();
[10753]103    }
104
[11739]105    public abstract bool Maximization { get; }
[13404]106    public abstract double Evaluate(TSolution solution, IRandom random);
107    public virtual void Analyze(TSolution[] solutions, double[] qualities, ResultCollection results, IRandom random) { }
108    public virtual IEnumerable<TSolution> GetNeighbors(TSolution solution, IRandom random) {
[13336]109      return Enumerable.Empty<TSolution>();
[11786]110    }
[10753]111
[13336]112    public virtual bool IsBetter(double quality, double bestQuality) {
113      return (Maximization && quality > bestQuality || !Maximization && quality < bestQuality);
[11970]114    }
115
[11396]116    protected override void OnEvaluatorChanged() {
117      base.OnEvaluatorChanged();
[11739]118      ParameterizeOperators();
[11396]119    }
120
[11786]121    private void ParameterizeOperators() {
[13336]122      foreach (var op in Operators.OfType<ISingleObjectiveEvaluationOperator<TSolution>>())
[11739]123        op.EvaluateFunc = Evaluate;
[13336]124      foreach (var op in Operators.OfType<ISingleObjectiveAnalysisOperator<TSolution>>())
[11739]125        op.AnalyzeAction = Analyze;
[13336]126      foreach (var op in Operators.OfType<INeighborBasedOperator<TSolution>>())
[11786]127        op.GetNeighborsFunc = GetNeighbors;
[11393]128    }
129
[11753]130    #region ISingleObjectiveHeuristicOptimizationProblem Members
131    IParameter ISingleObjectiveHeuristicOptimizationProblem.MaximizationParameter {
132      get { return Parameters["Maximization"]; }
133    }
134    IParameter ISingleObjectiveHeuristicOptimizationProblem.BestKnownQualityParameter {
135      get { return Parameters["BestKnownQuality"]; }
136    }
137    ISingleObjectiveEvaluator ISingleObjectiveHeuristicOptimizationProblem.Evaluator {
138      get { return Evaluator; }
139    }
140    #endregion
[10753]141  }
142}
Note: See TracBrowser for help on using the repository browser.