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]
|
---|
35 | public abstract class ContextAlgorithm<TContext> : BasicAlgorithm
|
---|
36 | where TContext : class, IContext, new() {
|
---|
37 |
|
---|
38 | [Storable]
|
---|
39 | private TContext context;
|
---|
40 | protected TContext Context {
|
---|
41 | get { return context; }
|
---|
42 | }
|
---|
43 |
|
---|
44 | [Storable]
|
---|
45 | private ValueParameter<IAnalyzer> analyzerParameter;
|
---|
46 | public IValueParameter<IAnalyzer> AnalyzerParameter {
|
---|
47 | get { return analyzerParameter; }
|
---|
48 | }
|
---|
49 | [Storable]
|
---|
50 | private FixedValueParameter<IntValue> maximumIterationsParameter;
|
---|
51 | public IFixedValueParameter<IntValue> MaximumIterationsParameter {
|
---|
52 | get { return maximumIterationsParameter; }
|
---|
53 | }
|
---|
54 | [Storable]
|
---|
55 | private FixedValueParameter<IntValue> maximumEvaluationsParameter;
|
---|
56 | public IFixedValueParameter<IntValue> MaximumEvaluationsParameter {
|
---|
57 | get { return maximumEvaluationsParameter; }
|
---|
58 | }
|
---|
59 | [Storable]
|
---|
60 | private FixedValueParameter<TimeSpanValue> maximumRuntimeParameter;
|
---|
61 | public IFixedValueParameter<TimeSpanValue> MaximumRuntimeParameter {
|
---|
62 | get { return maximumRuntimeParameter; }
|
---|
63 | }
|
---|
64 |
|
---|
65 | public IAnalyzer Analyzer {
|
---|
66 | get { return analyzerParameter.Value; }
|
---|
67 | set { analyzerParameter.Value = value; }
|
---|
68 | }
|
---|
69 | public int MaximumIterations {
|
---|
70 | get { return maximumIterationsParameter.Value.Value; }
|
---|
71 | set { maximumIterationsParameter.Value.Value = value; }
|
---|
72 | }
|
---|
73 | public int MaximumEvaluations {
|
---|
74 | get { return maximumEvaluationsParameter.Value.Value; }
|
---|
75 | set { maximumEvaluationsParameter.Value.Value = value; }
|
---|
76 | }
|
---|
77 | public TimeSpan MaximumRuntime {
|
---|
78 | get { return maximumRuntimeParameter.Value.Value; }
|
---|
79 | set { maximumRuntimeParameter.Value.Value = value; }
|
---|
80 | }
|
---|
81 |
|
---|
82 | [StorableConstructor]
|
---|
83 | protected ContextAlgorithm(bool deserializing) : base(deserializing) { }
|
---|
84 | protected ContextAlgorithm(ContextAlgorithm<TContext> original, Cloner cloner)
|
---|
85 | : base(original, cloner) {
|
---|
86 | context = cloner.Clone(original.context);
|
---|
87 | analyzerParameter = cloner.Clone(original.analyzerParameter);
|
---|
88 | maximumIterationsParameter = cloner.Clone(original.maximumIterationsParameter);
|
---|
89 | maximumEvaluationsParameter = cloner.Clone(original.maximumEvaluationsParameter);
|
---|
90 | maximumRuntimeParameter = cloner.Clone(original.maximumRuntimeParameter);
|
---|
91 | }
|
---|
92 | protected ContextAlgorithm()
|
---|
93 | : base() {
|
---|
94 | Parameters.Add(analyzerParameter = new ValueParameter<IAnalyzer>("Analyzer", "The analyzers that are used to perform an analysis of the solutions.", new MultiAnalyzer()));
|
---|
95 | Parameters.Add(maximumIterationsParameter = new FixedValueParameter<IntValue>("MaximumIterations", "The number of iterations that the algorithm should run or < 1 to ignore.", new IntValue(0)));
|
---|
96 | Parameters.Add(maximumEvaluationsParameter = new FixedValueParameter<IntValue>("MaximumEvaluations", "The number of evaluated solutions that the algorithm should perform or < 1 to ignore.", new IntValue(0)));
|
---|
97 | Parameters.Add(maximumRuntimeParameter = new FixedValueParameter<TimeSpanValue>("MaximumRuntime", "The timespan that the algorithm is allowed to run.", new TimeSpanValue(TimeSpan.FromMinutes(1))));
|
---|
98 | }
|
---|
99 |
|
---|
100 | protected override void Initialize(CancellationToken cancellationToken) {
|
---|
101 | base.Initialize(cancellationToken);
|
---|
102 | context = new TContext();
|
---|
103 | context.Scope.Variables.Add(new Variable("Results", Results));
|
---|
104 |
|
---|
105 | IExecutionContext ctxt = null;
|
---|
106 | foreach (var item in Problem.ExecutionContextItems)
|
---|
107 | ctxt = new Core.ExecutionContext(ctxt, item, Context.Scope);
|
---|
108 | ctxt = new Core.ExecutionContext(ctxt, this, Context.Scope);
|
---|
109 | context.Parent = ctxt;
|
---|
110 |
|
---|
111 | context.Iterations = 0;
|
---|
112 | context.EvaluatedSolutions = 0;
|
---|
113 | }
|
---|
114 |
|
---|
115 | public override void Prepare() {
|
---|
116 | context = null;
|
---|
117 | base.Prepare();
|
---|
118 | }
|
---|
119 |
|
---|
120 | protected virtual bool StoppingCriterion() {
|
---|
121 | return MaximumIterations > 0 && Context.Iterations > MaximumIterations
|
---|
122 | || MaximumEvaluations > 0 && Context.EvaluatedSolutions > MaximumEvaluations
|
---|
123 | || MaximumRuntime > TimeSpan.Zero && ExecutionTime > MaximumRuntime;
|
---|
124 | }
|
---|
125 | }
|
---|
126 | }
|
---|