Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2936_GQAPIntegration/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/Infrastructure/Contexts/BasicContext.cs @ 17357

Last change on this file since 17357 was 16712, checked in by gkronber, 6 years ago

#2936: adapted branch to new persistence (works with HL trunk r16711)

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