[10753] | 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 |
|
---|
[11786] | 22 | using System.Collections.Generic;
|
---|
[10753] | 23 | using System.Linq;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
[11753] | 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Parameters;
|
---|
[10753] | 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 |
|
---|
[11949] | 30 | namespace HeuristicLab.Optimization {
|
---|
[10753] | 31 | [StorableClass]
|
---|
[11814] | 32 | public abstract class SingleObjectiveBasicProblem<TEncoding> : BasicProblem<TEncoding, SingleObjectiveEvaluator>,
|
---|
[11753] | 33 | ISingleObjectiveProblemDefinition, ISingleObjectiveHeuristicOptimizationProblem
|
---|
[11739] | 34 | where TEncoding : class, IEncoding {
|
---|
[10753] | 35 | [StorableConstructor]
|
---|
[11814] | 36 | protected SingleObjectiveBasicProblem(bool deserializing) : base(deserializing) { }
|
---|
[10753] | 37 |
|
---|
[11814] | 38 | protected SingleObjectiveBasicProblem(SingleObjectiveBasicProblem<TEncoding> original, Cloner cloner)
|
---|
[10753] | 39 | : base(original, cloner) {
|
---|
[11739] | 40 | ParameterizeOperators();
|
---|
[10753] | 41 | }
|
---|
| 42 |
|
---|
[11814] | 43 | protected SingleObjectiveBasicProblem()
|
---|
[11739] | 44 | : base() {
|
---|
[11786] | 45 | Parameters.Add(new FixedValueParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized.", new BoolValue()));
|
---|
[11753] | 46 | Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this problem."));
|
---|
[10753] | 47 |
|
---|
[11753] | 48 | Operators.Add(Evaluator);
|
---|
[11396] | 49 | Operators.Add(new SingleObjectiveAnalyzer());
|
---|
[11753] | 50 | Operators.Add(new SingleObjectiveImprover());
|
---|
| 51 | Operators.Add(new SingleObjectiveMoveEvaluator());
|
---|
| 52 | Operators.Add(new SingleObjectiveMoveGenerator());
|
---|
| 53 | Operators.Add(new SingleObjectiveMoveMaker());
|
---|
[10753] | 54 |
|
---|
[11739] | 55 | ParameterizeOperators();
|
---|
[10753] | 56 | }
|
---|
| 57 |
|
---|
| 58 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 59 | private void AfterDeserialization() {
|
---|
[11739] | 60 | ParameterizeOperators();
|
---|
[10753] | 61 | }
|
---|
| 62 |
|
---|
[11739] | 63 | public abstract bool Maximization { get; }
|
---|
| 64 | public abstract double Evaluate(Individual individual, IRandom random);
|
---|
[11880] | 65 | public virtual void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) { }
|
---|
[11786] | 66 | public virtual IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) {
|
---|
| 67 | return Enumerable.Empty<Individual>();
|
---|
| 68 | }
|
---|
[10753] | 69 |
|
---|
| 70 |
|
---|
[11786] | 71 | protected override void OnEncodingChanged() {
|
---|
| 72 | base.OnEncodingChanged();
|
---|
| 73 | var max = (BoolValue)Parameters["Maximization"].ActualValue;
|
---|
| 74 | max.Value = Maximization;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[11396] | 77 | protected override void OnEvaluatorChanged() {
|
---|
| 78 | base.OnEvaluatorChanged();
|
---|
[11739] | 79 | ParameterizeOperators();
|
---|
[11396] | 80 | }
|
---|
| 81 |
|
---|
[11786] | 82 | private void ParameterizeOperators() {
|
---|
[11739] | 83 | foreach (var op in Operators.OfType<ISingleObjectiveEvaluationOperator>())
|
---|
| 84 | op.EvaluateFunc = Evaluate;
|
---|
| 85 | foreach (var op in Operators.OfType<ISingleObjectiveAnalysisOperator>())
|
---|
| 86 | op.AnalyzeAction = Analyze;
|
---|
[11786] | 87 | foreach (var op in Operators.OfType<INeighborBasedOperator>())
|
---|
| 88 | op.GetNeighborsFunc = GetNeighbors;
|
---|
[11393] | 89 | }
|
---|
| 90 |
|
---|
[11753] | 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
|
---|
[10753] | 102 | }
|
---|
| 103 | }
|
---|