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.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Parameters;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 | using HeuristicLab.Random;
|
---|
29 | using ExecutionContext = HeuristicLab.Core.ExecutionContext;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Optimization.Algorithms.SingleObjective {
|
---|
32 | [Item("AlgorithmContext", "")]
|
---|
33 | [StorableClass]
|
---|
34 | public abstract class HeuristicAlgorithmContext<TProblem, TEncoding, TSolution> : ParameterizedNamedItem,
|
---|
35 | IProblemContext<TProblem, TEncoding, TSolution>, IStochasticContext,
|
---|
36 | IEvaluatedSolutionsContext, IBestQualityContext, IBestSolutionContext<TSolution>,
|
---|
37 | ILongRunningOperationContext
|
---|
38 | where TProblem : class, ISingleObjectiveProblem<TEncoding, TSolution>, ISingleObjectiveProblemDefinition<TEncoding, TSolution>
|
---|
39 | where TEncoding : class, IEncoding<TSolution>
|
---|
40 | where TSolution : class, ISolution {
|
---|
41 |
|
---|
42 | private IExecutionContext parent;
|
---|
43 | public IExecutionContext Parent {
|
---|
44 | get { return parent; }
|
---|
45 | set { parent = value; }
|
---|
46 | }
|
---|
47 |
|
---|
48 | IKeyedItemCollection<string, IParameter> IExecutionContext.Parameters {
|
---|
49 | get { return Parameters; }
|
---|
50 | }
|
---|
51 |
|
---|
52 | public CancellationToken CancellationToken { get; set; }
|
---|
53 |
|
---|
54 | [Storable]
|
---|
55 | private IValueParameter<TProblem> problem;
|
---|
56 | public TProblem Problem {
|
---|
57 | get { return problem.Value; }
|
---|
58 | set { problem.Value = value; }
|
---|
59 | }
|
---|
60 |
|
---|
61 | [Storable]
|
---|
62 | private IScope scope;
|
---|
63 | public IScope Scope {
|
---|
64 | get { return scope; }
|
---|
65 | }
|
---|
66 |
|
---|
67 | [Storable]
|
---|
68 | private IValueParameter<BoolValue> initialized;
|
---|
69 | public bool Initialized {
|
---|
70 | get { return initialized.Value.Value; }
|
---|
71 | set { initialized.Value.Value = value; }
|
---|
72 | }
|
---|
73 |
|
---|
74 | [Storable]
|
---|
75 | private IValueParameter<IRandom> random;
|
---|
76 | public IRandom Random {
|
---|
77 | get { return random.Value; }
|
---|
78 | set { random.Value = value; }
|
---|
79 | }
|
---|
80 |
|
---|
81 | [Storable]
|
---|
82 | private IValueParameter<IntValue> evaluatedSolutions;
|
---|
83 | public int EvaluatedSolutions {
|
---|
84 | get { return evaluatedSolutions.Value.Value; }
|
---|
85 | set { evaluatedSolutions.Value.Value = value; }
|
---|
86 | }
|
---|
87 |
|
---|
88 | public void IncEvaluatedSolutions(int inc) {
|
---|
89 | EvaluatedSolutions += inc;
|
---|
90 | }
|
---|
91 |
|
---|
92 | [Storable]
|
---|
93 | private IValueParameter<DoubleValue> bestQuality;
|
---|
94 | public double BestQuality {
|
---|
95 | get { return bestQuality.Value.Value; }
|
---|
96 | set { bestQuality.Value.Value = value; }
|
---|
97 | }
|
---|
98 |
|
---|
99 | [Storable]
|
---|
100 | private IValueParameter<TSolution> bestSolution;
|
---|
101 | public TSolution BestSolution {
|
---|
102 | get { return bestSolution.Value; }
|
---|
103 | set { bestSolution.Value = value; }
|
---|
104 | }
|
---|
105 |
|
---|
106 | [StorableConstructor]
|
---|
107 | protected HeuristicAlgorithmContext(bool deserializing) : base(deserializing) { }
|
---|
108 | protected HeuristicAlgorithmContext(HeuristicAlgorithmContext<TProblem, TEncoding, TSolution> original, Cloner cloner)
|
---|
109 | : base(original, cloner) {
|
---|
110 | problem = cloner.Clone(original.problem);
|
---|
111 | scope = cloner.Clone(original.scope);
|
---|
112 | initialized = cloner.Clone(original.initialized);
|
---|
113 | random = cloner.Clone(original.random);
|
---|
114 | evaluatedSolutions = cloner.Clone(original.evaluatedSolutions);
|
---|
115 | bestQuality = cloner.Clone(original.bestQuality);
|
---|
116 | bestSolution = cloner.Clone(original.bestSolution);
|
---|
117 | }
|
---|
118 | protected HeuristicAlgorithmContext() : this(new Scope("Scope"), CancellationToken.None) { }
|
---|
119 | protected HeuristicAlgorithmContext(IScope s, CancellationToken token) {
|
---|
120 | scope = s;
|
---|
121 | CancellationToken = token;
|
---|
122 | Parameters.Add(problem = new ValueParameter<TProblem>("Problem", "The problem that is to be solved."));
|
---|
123 | Parameters.Add(initialized = new ValueParameter<BoolValue>("Initialized", "Whether the algorithm has finished the initialization phase.", new BoolValue(false)));
|
---|
124 | Parameters.Add(random = new ValueParameter<IRandom>("Random", "The random number generator.", new MersenneTwister()));
|
---|
125 | Parameters.Add(evaluatedSolutions = new ValueParameter<IntValue>("EvaluatedSolutions", "The number of evaluated solutions.", new IntValue(0)));
|
---|
126 | Parameters.Add(bestQuality = new ValueParameter<DoubleValue>("BestQuality", "The best quality found so far.", new DoubleValue(double.NaN)));
|
---|
127 | Parameters.Add(bestSolution = new OptionalValueParameter<TSolution>("BestSolution", "The best solution found so far."));
|
---|
128 |
|
---|
129 | }
|
---|
130 |
|
---|
131 | public IAtomicOperation CreateOperation(IOperator op) {
|
---|
132 | return new ExecutionContext(parent, op, scope);
|
---|
133 | }
|
---|
134 |
|
---|
135 | public IAtomicOperation CreateOperation(IOperator op, IScope s) {
|
---|
136 | return new ExecutionContext(parent, op, s);
|
---|
137 | }
|
---|
138 |
|
---|
139 | public IAtomicOperation CreateChildOperation(IOperator op) {
|
---|
140 | return new ExecutionContext(this, op, scope);
|
---|
141 | }
|
---|
142 |
|
---|
143 | public IAtomicOperation CreateChildOperation(IOperator op, IScope s) {
|
---|
144 | return new ExecutionContext(this, op, s);
|
---|
145 | }
|
---|
146 | }
|
---|
147 | }
|
---|