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.Collections.Generic;
|
---|
24 | using System.Threading;
|
---|
25 | using HEAL.Attic;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms {
|
---|
32 | [Item("Basic Context", "A base class for algorithms' contexts.")]
|
---|
33 | [StorableType("633CC9F2-35B9-4193-A022-0D768532937A")]
|
---|
34 | public class BasicContext : ParameterizedNamedItem, IContext {
|
---|
35 |
|
---|
36 | private IExecutionContext parent;
|
---|
37 | public IExecutionContext Parent {
|
---|
38 | get { return parent; }
|
---|
39 | set { parent = value; }
|
---|
40 | }
|
---|
41 |
|
---|
42 | [Storable]
|
---|
43 | private IScope scope;
|
---|
44 | public IScope Scope {
|
---|
45 | get { return scope; }
|
---|
46 | private set { scope = value; }
|
---|
47 | }
|
---|
48 |
|
---|
49 | IKeyedItemCollection<string, IParameter> IExecutionContext.Parameters {
|
---|
50 | get { return Parameters; }
|
---|
51 | }
|
---|
52 |
|
---|
53 | [Storable]
|
---|
54 | private IFixedValueParameter<IntValue> iterations;
|
---|
55 | public int Iterations {
|
---|
56 | get { return iterations.Value.Value; }
|
---|
57 | set { iterations.Value.Value = value; }
|
---|
58 | }
|
---|
59 |
|
---|
60 | [Storable]
|
---|
61 | private IFixedValueParameter<IntValue> evaluatedSolutions;
|
---|
62 | public int EvaluatedSolutions {
|
---|
63 | get { return evaluatedSolutions.Value.Value; }
|
---|
64 | set { evaluatedSolutions.Value.Value = value; }
|
---|
65 | }
|
---|
66 |
|
---|
67 | [Storable]
|
---|
68 | private IValueParameter<DoubleValue> bestQuality;
|
---|
69 | public double BestQuality {
|
---|
70 | get { return bestQuality.Value.Value; }
|
---|
71 | set {
|
---|
72 | if (bestQuality.Value.Value == value) return;
|
---|
73 | bestQuality.Value.Value = value;
|
---|
74 | OnBestQualityChanged();
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | [StorableConstructor]
|
---|
79 | protected BasicContext(StorableConstructorFlag _) : base(_) { }
|
---|
80 | protected BasicContext(BasicContext original, Cloner cloner)
|
---|
81 | : base(original, cloner) {
|
---|
82 | scope = cloner.Clone(original.scope);
|
---|
83 | iterations = cloner.Clone(original.iterations);
|
---|
84 | evaluatedSolutions = cloner.Clone(original.evaluatedSolutions);
|
---|
85 | bestQuality = cloner.Clone(original.bestQuality);
|
---|
86 | }
|
---|
87 | protected BasicContext() : base() {
|
---|
88 | scope = new Scope("Global");
|
---|
89 | Parameters.Add(iterations = new FixedValueParameter<IntValue>("Iterations", new IntValue(0)));
|
---|
90 | Parameters.Add(evaluatedSolutions = new FixedValueParameter<IntValue>("EvaluatedSolutions", new IntValue(0)));
|
---|
91 | Parameters.Add(bestQuality = new ValueParameter<DoubleValue>("BestQuality", new DoubleValue(double.NaN)));
|
---|
92 | }
|
---|
93 | protected BasicContext(string name) : base(name) {
|
---|
94 | scope = new Scope("Global");
|
---|
95 | Parameters.Add(iterations = new FixedValueParameter<IntValue>("Iterations", new IntValue(0)));
|
---|
96 | Parameters.Add(evaluatedSolutions = new FixedValueParameter<IntValue>("EvaluatedSolutions", new IntValue(0)));
|
---|
97 | Parameters.Add(bestQuality = new ValueParameter<DoubleValue>("BestQuality", new DoubleValue(double.NaN)));
|
---|
98 | }
|
---|
99 | protected BasicContext(string name, ParameterCollection parameters) : base(name, parameters) {
|
---|
100 | scope = new Scope("Global");
|
---|
101 | Parameters.Add(iterations = new FixedValueParameter<IntValue>("Iterations", new IntValue(0)));
|
---|
102 | Parameters.Add(evaluatedSolutions = new FixedValueParameter<IntValue>("EvaluatedSolutions", new IntValue(0)));
|
---|
103 | Parameters.Add(bestQuality = new ValueParameter<DoubleValue>("BestQuality", new DoubleValue(double.NaN)));
|
---|
104 | }
|
---|
105 | protected BasicContext(string name, string description) : base(name, description) {
|
---|
106 | scope = new Scope("Global");
|
---|
107 | Parameters.Add(iterations = new FixedValueParameter<IntValue>("Iterations", new IntValue(0)));
|
---|
108 | Parameters.Add(evaluatedSolutions = new FixedValueParameter<IntValue>("EvaluatedSolutions", new IntValue(0)));
|
---|
109 | Parameters.Add(bestQuality = new ValueParameter<DoubleValue>("BestQuality", new DoubleValue(double.NaN)));
|
---|
110 | }
|
---|
111 | protected BasicContext(string name, string description, ParameterCollection parameters) : base(name, description, parameters) {
|
---|
112 | scope = new Scope("Global");
|
---|
113 | Parameters.Add(iterations = new FixedValueParameter<IntValue>("Iterations", new IntValue(0)));
|
---|
114 | Parameters.Add(evaluatedSolutions = new FixedValueParameter<IntValue>("EvaluatedSolutions", new IntValue(0)));
|
---|
115 | Parameters.Add(bestQuality = new ValueParameter<DoubleValue>("BestQuality", new DoubleValue(double.NaN)));
|
---|
116 | }
|
---|
117 |
|
---|
118 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
119 | return new BasicContext(this, cloner);
|
---|
120 | }
|
---|
121 |
|
---|
122 | public void RunOperator(IOperator op, CancellationToken cancellationToken) {
|
---|
123 | var stack = new Stack<IOperation>();
|
---|
124 | stack.Push(((IExecutionContext)this).CreateChildOperation(op, scope));
|
---|
125 |
|
---|
126 | while (stack.Count > 0) {
|
---|
127 | cancellationToken.ThrowIfCancellationRequested();
|
---|
128 |
|
---|
129 | var next = stack.Pop();
|
---|
130 | if (next is OperationCollection) {
|
---|
131 | var coll = (OperationCollection)next;
|
---|
132 | for (int i = coll.Count - 1; i >= 0; i--)
|
---|
133 | if (coll[i] != null) stack.Push(coll[i]);
|
---|
134 | } else if (next is IAtomicOperation) {
|
---|
135 | var operation = (IAtomicOperation)next;
|
---|
136 | try {
|
---|
137 | next = operation.Operator.Execute((IExecutionContext)operation, cancellationToken);
|
---|
138 | } catch (Exception ex) {
|
---|
139 | stack.Push(operation);
|
---|
140 | if (ex is OperationCanceledException) throw ex;
|
---|
141 | else throw new OperatorExecutionException(operation.Operator, ex);
|
---|
142 | }
|
---|
143 | if (next != null) stack.Push(next);
|
---|
144 | }
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 | public event EventHandler BestQualityChanged;
|
---|
149 | private void OnBestQualityChanged() {
|
---|
150 | BestQualityChanged?.Invoke(this, EventArgs.Empty);
|
---|
151 | }
|
---|
152 |
|
---|
153 | #region IExecutionContext members
|
---|
154 | IAtomicOperation IExecutionContext.CreateOperation(IOperator op) {
|
---|
155 | return new Core.ExecutionContext(this, op, Scope);
|
---|
156 | }
|
---|
157 | IAtomicOperation IExecutionContext.CreateOperation(IOperator op, IScope s) {
|
---|
158 | return new Core.ExecutionContext(this, op, s);
|
---|
159 | }
|
---|
160 | IAtomicOperation IExecutionContext.CreateChildOperation(IOperator op) {
|
---|
161 | return new Core.ExecutionContext(this, op, Scope);
|
---|
162 | }
|
---|
163 | IAtomicOperation IExecutionContext.CreateChildOperation(IOperator op, IScope s) {
|
---|
164 | return new Core.ExecutionContext(this, op, s);
|
---|
165 | }
|
---|
166 | #endregion
|
---|
167 | }
|
---|
168 | }
|
---|