Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization/3.3/BasicProblems/SingleObjectiveBasicProblem.cs @ 11961

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

#2174: Distributed files in programmable problem branch to the correct plugins.

File size: 4.1 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.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 SingleObjectiveBasicProblem<TEncoding> : BasicProblem<TEncoding, SingleObjectiveEvaluator>,
33    ISingleObjectiveProblemDefinition, ISingleObjectiveHeuristicOptimizationProblem
34  where TEncoding : class, IEncoding {
35    [StorableConstructor]
36    protected SingleObjectiveBasicProblem(bool deserializing) : base(deserializing) { }
37
38    protected SingleObjectiveBasicProblem(SingleObjectiveBasicProblem<TEncoding> original, Cloner cloner)
39      : base(original, cloner) {
40      ParameterizeOperators();
41    }
42
43    protected SingleObjectiveBasicProblem()
44      : base() {
45      Parameters.Add(new FixedValueParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized.", new BoolValue()));
46      Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this problem."));
47
48      Operators.Add(Evaluator);
49      Operators.Add(new SingleObjectiveAnalyzer());
50      Operators.Add(new SingleObjectiveImprover());
51      Operators.Add(new SingleObjectiveMoveEvaluator());
52      Operators.Add(new SingleObjectiveMoveGenerator());
53      Operators.Add(new SingleObjectiveMoveMaker());
54
55      ParameterizeOperators();
56    }
57
58    [StorableHook(HookType.AfterDeserialization)]
59    private void AfterDeserialization() {
60      ParameterizeOperators();
61    }
62
63    public abstract bool Maximization { get; }
64    public abstract double Evaluate(Individual individual, IRandom random);
65    public virtual void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) { }
66    public virtual IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) {
67      return Enumerable.Empty<Individual>();
68    }
69
70
71    protected override void OnEncodingChanged() {
72      base.OnEncodingChanged();
73      var max = (BoolValue)Parameters["Maximization"].ActualValue;
74      max.Value = Maximization;
75    }
76
77    protected override void OnEvaluatorChanged() {
78      base.OnEvaluatorChanged();
79      ParameterizeOperators();
80    }
81
82    private void ParameterizeOperators() {
83      foreach (var op in Operators.OfType<ISingleObjectiveEvaluationOperator>())
84        op.EvaluateFunc = Evaluate;
85      foreach (var op in Operators.OfType<ISingleObjectiveAnalysisOperator>())
86        op.AnalyzeAction = Analyze;
87      foreach (var op in Operators.OfType<INeighborBasedOperator>())
88        op.GetNeighborsFunc = GetNeighbors;
89    }
90
91    #region ISingleObjectiveHeuristicOptimizationProblem Members
92    IParameter ISingleObjectiveHeuristicOptimizationProblem.MaximizationParameter {
93      get { return Parameters["Maximization"]; }
94    }
95    IParameter ISingleObjectiveHeuristicOptimizationProblem.BestKnownQualityParameter {
96      get { return Parameters["BestKnownQuality"]; }
97    }
98    ISingleObjectiveEvaluator ISingleObjectiveHeuristicOptimizationProblem.Evaluator {
99      get { return Evaluator; }
100    }
101    #endregion
102  }
103}
Note: See TracBrowser for help on using the repository browser.