Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/SingleObjectiveBasicProblem.cs @ 11813

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

#2174: Updated views and renamed programmable problem to basic problem and added individual extension methods.

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