[15562] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2017 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;
|
---|
| 23 | using System.Threading;
|
---|
| 24 | using HeuristicLab.Analysis;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
| 28 | using HeuristicLab.Optimization;
|
---|
| 29 | using HeuristicLab.Parameters;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms {
|
---|
| 33 | [Item("Context-based Algorithm", "Algorithms that make use of contexts to facilitate applying operators.")]
|
---|
| 34 | [StorableClass]
|
---|
[15572] | 35 | public abstract class ContextAlgorithm<TContext, TEncoding> : BasicAlgorithm
|
---|
| 36 | where TContext : class, IContext, new()
|
---|
| 37 | where TEncoding : class, IEncoding {
|
---|
[15562] | 38 |
|
---|
[15572] | 39 | public override Type ProblemType {
|
---|
| 40 | get { return typeof(SingleObjectiveBasicProblem<TEncoding>); }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | public new SingleObjectiveBasicProblem<TEncoding> Problem {
|
---|
| 44 | get { return (SingleObjectiveBasicProblem<TEncoding>)base.Problem; }
|
---|
| 45 | set { base.Problem = value; }
|
---|
| 46 | }
|
---|
| 47 |
|
---|
[15562] | 48 | [Storable]
|
---|
| 49 | private TContext context;
|
---|
[15574] | 50 | public TContext Context {
|
---|
[15562] | 51 | get { return context; }
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | [Storable]
|
---|
| 55 | private ValueParameter<IAnalyzer> analyzerParameter;
|
---|
| 56 | public IValueParameter<IAnalyzer> AnalyzerParameter {
|
---|
| 57 | get { return analyzerParameter; }
|
---|
| 58 | }
|
---|
| 59 | [Storable]
|
---|
| 60 | private FixedValueParameter<IntValue> maximumIterationsParameter;
|
---|
| 61 | public IFixedValueParameter<IntValue> MaximumIterationsParameter {
|
---|
| 62 | get { return maximumIterationsParameter; }
|
---|
| 63 | }
|
---|
| 64 | [Storable]
|
---|
| 65 | private FixedValueParameter<IntValue> maximumEvaluationsParameter;
|
---|
| 66 | public IFixedValueParameter<IntValue> MaximumEvaluationsParameter {
|
---|
| 67 | get { return maximumEvaluationsParameter; }
|
---|
| 68 | }
|
---|
| 69 | [Storable]
|
---|
| 70 | private FixedValueParameter<TimeSpanValue> maximumRuntimeParameter;
|
---|
| 71 | public IFixedValueParameter<TimeSpanValue> MaximumRuntimeParameter {
|
---|
| 72 | get { return maximumRuntimeParameter; }
|
---|
| 73 | }
|
---|
[15572] | 74 | [Storable]
|
---|
| 75 | private FixedValueParameter<BoolValue> stopAtBestKnownQualityParameter;
|
---|
| 76 | public IFixedValueParameter<BoolValue> StopAtBestKnownQualityParameter {
|
---|
| 77 | get { return stopAtBestKnownQualityParameter; }
|
---|
| 78 | }
|
---|
[15562] | 79 |
|
---|
| 80 | public IAnalyzer Analyzer {
|
---|
| 81 | get { return analyzerParameter.Value; }
|
---|
| 82 | set { analyzerParameter.Value = value; }
|
---|
| 83 | }
|
---|
| 84 | public int MaximumIterations {
|
---|
| 85 | get { return maximumIterationsParameter.Value.Value; }
|
---|
| 86 | set { maximumIterationsParameter.Value.Value = value; }
|
---|
| 87 | }
|
---|
| 88 | public int MaximumEvaluations {
|
---|
| 89 | get { return maximumEvaluationsParameter.Value.Value; }
|
---|
| 90 | set { maximumEvaluationsParameter.Value.Value = value; }
|
---|
| 91 | }
|
---|
| 92 | public TimeSpan MaximumRuntime {
|
---|
| 93 | get { return maximumRuntimeParameter.Value.Value; }
|
---|
| 94 | set { maximumRuntimeParameter.Value.Value = value; }
|
---|
| 95 | }
|
---|
[15572] | 96 | public bool StopAtBestKnownQuality {
|
---|
| 97 | get { return stopAtBestKnownQualityParameter.Value.Value; }
|
---|
| 98 | set { stopAtBestKnownQualityParameter.Value.Value = value; }
|
---|
| 99 | }
|
---|
[15562] | 100 |
|
---|
| 101 | [StorableConstructor]
|
---|
| 102 | protected ContextAlgorithm(bool deserializing) : base(deserializing) { }
|
---|
[15572] | 103 | protected ContextAlgorithm(ContextAlgorithm<TContext, TEncoding> original, Cloner cloner)
|
---|
[15562] | 104 | : base(original, cloner) {
|
---|
| 105 | context = cloner.Clone(original.context);
|
---|
| 106 | analyzerParameter = cloner.Clone(original.analyzerParameter);
|
---|
| 107 | maximumIterationsParameter = cloner.Clone(original.maximumIterationsParameter);
|
---|
| 108 | maximumEvaluationsParameter = cloner.Clone(original.maximumEvaluationsParameter);
|
---|
| 109 | maximumRuntimeParameter = cloner.Clone(original.maximumRuntimeParameter);
|
---|
[15572] | 110 | stopAtBestKnownQualityParameter = cloner.Clone(original.stopAtBestKnownQualityParameter);
|
---|
[15562] | 111 | }
|
---|
| 112 | protected ContextAlgorithm()
|
---|
| 113 | : base() {
|
---|
| 114 | Parameters.Add(analyzerParameter = new ValueParameter<IAnalyzer>("Analyzer", "The analyzers that are used to perform an analysis of the solutions.", new MultiAnalyzer()));
|
---|
| 115 | Parameters.Add(maximumIterationsParameter = new FixedValueParameter<IntValue>("MaximumIterations", "The number of iterations that the algorithm should run or < 1 to ignore.", new IntValue(0)));
|
---|
| 116 | Parameters.Add(maximumEvaluationsParameter = new FixedValueParameter<IntValue>("MaximumEvaluations", "The number of evaluated solutions that the algorithm should perform or < 1 to ignore.", new IntValue(0)));
|
---|
| 117 | Parameters.Add(maximumRuntimeParameter = new FixedValueParameter<TimeSpanValue>("MaximumRuntime", "The timespan that the algorithm is allowed to run.", new TimeSpanValue(TimeSpan.FromMinutes(1))));
|
---|
[15572] | 118 | Parameters.Add(stopAtBestKnownQualityParameter = new FixedValueParameter<BoolValue>("StopAtBestKnownQuality", "Whether the algorithm should terminate if the best known quality has been found.", new BoolValue(true)));
|
---|
[15562] | 119 | }
|
---|
| 120 |
|
---|
| 121 | protected override void Initialize(CancellationToken cancellationToken) {
|
---|
| 122 | base.Initialize(cancellationToken);
|
---|
| 123 | context = new TContext();
|
---|
| 124 | context.Scope.Variables.Add(new Variable("Results", Results));
|
---|
| 125 |
|
---|
| 126 | IExecutionContext ctxt = null;
|
---|
| 127 | foreach (var item in Problem.ExecutionContextItems)
|
---|
| 128 | ctxt = new Core.ExecutionContext(ctxt, item, Context.Scope);
|
---|
| 129 | ctxt = new Core.ExecutionContext(ctxt, this, Context.Scope);
|
---|
| 130 | context.Parent = ctxt;
|
---|
| 131 |
|
---|
| 132 | context.Iterations = 0;
|
---|
| 133 | context.EvaluatedSolutions = 0;
|
---|
[15574] | 134 | context.BestQuality = double.NaN;
|
---|
[15562] | 135 | }
|
---|
| 136 |
|
---|
| 137 | public override void Prepare() {
|
---|
| 138 | context = null;
|
---|
| 139 | base.Prepare();
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | protected virtual bool StoppingCriterion() {
|
---|
| 143 | return MaximumIterations > 0 && Context.Iterations > MaximumIterations
|
---|
| 144 | || MaximumEvaluations > 0 && Context.EvaluatedSolutions > MaximumEvaluations
|
---|
[15572] | 145 | || MaximumRuntime > TimeSpan.Zero && ExecutionTime > MaximumRuntime
|
---|
| 146 | || StopAtBestKnownQuality && !double.IsNaN(Problem.BestKnownQuality)
|
---|
| 147 | && Context.BestQuality.IsAlmost(Problem.BestKnownQuality);
|
---|
[15562] | 148 | }
|
---|
| 149 | }
|
---|
| 150 | }
|
---|