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.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Optimization.Model2 {
|
---|
31 | [Item("Context-based Algorithm", "Algorithms that make use of contexts to facilitate applying operators.")]
|
---|
32 | [StorableClass]
|
---|
33 | public abstract class ContextAlgorithm<TContext> : BasicAlgorithm
|
---|
34 | where TContext : class, IContext, new() {
|
---|
35 |
|
---|
36 | [Storable]
|
---|
37 | private TContext context;
|
---|
38 | public TContext Context {
|
---|
39 | get { return context; }
|
---|
40 | }
|
---|
41 |
|
---|
42 | [Storable]
|
---|
43 | private ValueParameter<IAnalyzer> analyzerParameter;
|
---|
44 | public IValueParameter<IAnalyzer> AnalyzerParameter {
|
---|
45 | get { return analyzerParameter; }
|
---|
46 | }
|
---|
47 | [Storable]
|
---|
48 | private FixedValueParameter<MultiTerminator> terminationParameter;
|
---|
49 | public IFixedValueParameter<MultiTerminator> TerminationParameter {
|
---|
50 | get { return terminationParameter; }
|
---|
51 | }
|
---|
52 |
|
---|
53 | public IAnalyzer Analyzer {
|
---|
54 | get { return analyzerParameter.Value; }
|
---|
55 | set { analyzerParameter.Value = value; }
|
---|
56 | }
|
---|
57 | public MultiTerminator Termination {
|
---|
58 | get { return terminationParameter.Value; }
|
---|
59 | }
|
---|
60 |
|
---|
61 | [StorableConstructor]
|
---|
62 | protected ContextAlgorithm(bool deserializing) : base(deserializing) { }
|
---|
63 | protected ContextAlgorithm(ContextAlgorithm<TContext> original, Cloner cloner)
|
---|
64 | : base(original, cloner) {
|
---|
65 | context = cloner.Clone(original.context);
|
---|
66 | analyzerParameter = cloner.Clone(original.analyzerParameter);
|
---|
67 | terminationParameter = cloner.Clone(original.terminationParameter);
|
---|
68 | }
|
---|
69 | protected ContextAlgorithm()
|
---|
70 | : base() {
|
---|
71 | Parameters.Add(analyzerParameter = new ValueParameter<IAnalyzer>("Analyzer", "The analyzers that are used to perform an analysis of the solutions."));
|
---|
72 | Parameters.Add(terminationParameter = new FixedValueParameter<MultiTerminator>("Termination", "The termination criteria that are being used.", new MultiTerminator()));
|
---|
73 |
|
---|
74 | var generationsTerminator = new ComparisonTerminator<IntValue>("Iterations", ComparisonType.Less, new IntValue(1000)) { Name = "Iterations" };
|
---|
75 | var evaluationsTerminator = new ComparisonTerminator<IntValue>("EvaluatedSolutions", ComparisonType.Less, new IntValue(int.MaxValue)) { Name = "Evaluations" };
|
---|
76 | var executionTimeTerminator = new ExecutionTimeTerminator(this, new TimeSpanValue(TimeSpan.FromMinutes(5)));
|
---|
77 |
|
---|
78 | Termination.AddOperator(generationsTerminator);
|
---|
79 | Termination.AddOperator(evaluationsTerminator);
|
---|
80 | Termination.AddOperator(executionTimeTerminator);
|
---|
81 |
|
---|
82 | }
|
---|
83 |
|
---|
84 | protected override void Initialize(CancellationToken cancellationToken) {
|
---|
85 | base.Initialize(cancellationToken);
|
---|
86 | context = new TContext();
|
---|
87 | context.Scope.Variables.Add(new Variable("Results", Results));
|
---|
88 |
|
---|
89 | IExecutionContext ctxt = null;
|
---|
90 | foreach (var item in Problem.ExecutionContextItems)
|
---|
91 | ctxt = new Core.ExecutionContext(ctxt, item, Context.Scope);
|
---|
92 | ctxt = new Core.ExecutionContext(ctxt, this, Context.Scope);
|
---|
93 | context.Parent = ctxt;
|
---|
94 |
|
---|
95 | context.Iterations = 0;
|
---|
96 | context.EvaluatedSolutions = 0;
|
---|
97 | context.BestQuality = double.NaN;
|
---|
98 | }
|
---|
99 |
|
---|
100 | public override void Prepare() {
|
---|
101 | context = null;
|
---|
102 | base.Prepare();
|
---|
103 | }
|
---|
104 |
|
---|
105 | protected virtual bool StoppingCriterion() {
|
---|
106 | Context.RunOperator(Termination, CancellationToken.None);
|
---|
107 | return Context.Terminate;
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|