[14429] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2016 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 |
|
---|
| 22 | using System.Threading;
|
---|
| 23 | using HeuristicLab.Analysis;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Optimization;
|
---|
| 28 | using HeuristicLab.Optimization.Algorithms.SingleObjective;
|
---|
| 29 | using HeuristicLab.Optimization.LocalSearch;
|
---|
| 30 | using HeuristicLab.Optimization.Manipulation;
|
---|
| 31 | using HeuristicLab.Parameters;
|
---|
| 32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 33 |
|
---|
| 34 | namespace HeuristicLab.Algorithms.LocalSearch {
|
---|
| 35 | [Item("Iterated Local Search (ILS)", "Performs a repeated local search by applying a kick to each previous local optimum.")]
|
---|
| 36 | [Creatable(CreatableAttribute.Categories.SingleSolutionAlgorithms, Priority = 999)]
|
---|
| 37 | [StorableClass]
|
---|
| 38 | public class IteratedLocalSearch<TProblem, TEncoding, TSolution> : HeuristicAlgorithm<LocalSearchContext<TProblem, TEncoding, TSolution>, TProblem, TEncoding, TSolution>
|
---|
| 39 | where TProblem : class, ISingleObjectiveProblem<TEncoding, TSolution>, ISingleObjectiveProblemDefinition<TEncoding, TSolution>
|
---|
| 40 | where TEncoding : class, IEncoding<TSolution>
|
---|
| 41 | where TSolution : class, ISolution {
|
---|
| 42 |
|
---|
| 43 | public IConstrainedValueParameter<ILocalSearch<LocalSearchContext<TProblem, TEncoding, TSolution>>> LocalSearchParameter {
|
---|
| 44 | get { return (IConstrainedValueParameter<ILocalSearch<LocalSearchContext<TProblem, TEncoding, TSolution>>>)Parameters["LocalSearch"]; }
|
---|
| 45 | }
|
---|
| 46 | public IConstrainedValueParameter<IManipulator<LocalSearchContext<TProblem, TEncoding, TSolution>>> KickerParameter {
|
---|
| 47 | get { return (IConstrainedValueParameter<IManipulator<LocalSearchContext<TProblem, TEncoding, TSolution>>>)Parameters["Kicker"]; }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | [Storable]
|
---|
| 51 | private BestAverageWorstQualityAnalyzer qualityAnalyzer;
|
---|
| 52 |
|
---|
| 53 | [StorableConstructor]
|
---|
| 54 | protected IteratedLocalSearch(bool deserializing) : base(deserializing) { }
|
---|
| 55 | protected IteratedLocalSearch(IteratedLocalSearch<TProblem, TEncoding, TSolution> original, Cloner cloner)
|
---|
| 56 | : base(original, cloner) {
|
---|
| 57 | qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
|
---|
| 58 | }
|
---|
| 59 | public IteratedLocalSearch() {
|
---|
| 60 | ProblemAnalyzer = new MultiAnalyzer();
|
---|
| 61 | AlgorithmAnalyzer = new MultiAnalyzer();
|
---|
| 62 | qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
|
---|
| 63 | AlgorithmAnalyzer.Operators.Add(qualityAnalyzer, true);
|
---|
| 64 |
|
---|
| 65 | Parameters.Add(new ConstrainedValueParameter<ILocalSearch<LocalSearchContext<TProblem, TEncoding, TSolution>>>("LocalSearch", "The local search operator to use."));
|
---|
| 66 | Parameters.Add(new ConstrainedValueParameter<IManipulator<LocalSearchContext<TProblem, TEncoding, TSolution>>>("Kicker", "The manipulator operator that performs the kick."));
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 70 | return new IteratedLocalSearch<TProblem, TEncoding, TSolution>(this, cloner);
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | protected override void OnProblemChanged() {
|
---|
| 74 | base.OnProblemChanged();
|
---|
| 75 | qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
| 76 | qualityAnalyzer.QualityParameter.Depth = 1;
|
---|
| 77 | qualityAnalyzer.QualityParameter.Hidden = true;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | protected override void PerformInitialize(CancellationToken token) {
|
---|
| 81 | Context.Solution = CreateEmptySolutionScope();
|
---|
| 82 | RunOperator(Problem.Encoding.SolutionCreator, Context.Solution, token);
|
---|
| 83 | Evaluate(Context.Solution, token);
|
---|
| 84 | DoLocalSearch();
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | protected override void PerformIterate(CancellationToken token) {
|
---|
| 88 | Context.Iterations++;
|
---|
| 89 | KickerParameter.Value.Manipulate(Context);
|
---|
| 90 | DoLocalSearch();
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | private void DoLocalSearch() {
|
---|
| 94 | LocalSearchParameter.Value.Optimize(Context);
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | protected override void PerformAnalyze(CancellationToken token) {
|
---|
| 98 | base.PerformAnalyze(token);
|
---|
| 99 | IResult res;
|
---|
| 100 | if (!Results.TryGetValue("Iterations", out res)) {
|
---|
| 101 | res = new Result("Iterations", new IntValue(Context.Iterations));
|
---|
| 102 | Results.Add(res);
|
---|
| 103 | }
|
---|
| 104 | ((IntValue)res.Value).Value = Context.Iterations;
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 | }
|
---|