Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/Infrastructure/Contexts/BasicContext.cs @ 15562

Last change on this file since 15562 was 15562, checked in by abeham, 6 years ago

#1614: added additional algorithms

File size: 5.9 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;
30
31namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms {
32  public class BasicContext : ParameterizedNamedItem, IContext {
33
34    private IExecutionContext parent;
35    public IExecutionContext Parent {
36      get { return parent; }
37      set { parent = value; }
38    }
39
40    [Storable]
41    private IScope scope;
42    public IScope Scope {
43      get { return scope; }
44      private set { scope = value; }
45    }
46
47    IKeyedItemCollection<string, IParameter> IExecutionContext.Parameters {
48      get { return Parameters; }
49    }
50
51    [Storable]
52    private IFixedValueParameter<IntValue> iterations;
53    public int Iterations {
54      get { return iterations.Value.Value; }
55      set { iterations.Value.Value = value; }
56    }
57
58    [Storable]
59    private IFixedValueParameter<IntValue> evaluatedSolutions;
60    public int EvaluatedSolutions {
61      get { return evaluatedSolutions.Value.Value; }
62      set { evaluatedSolutions.Value.Value = value; }
63    }
64
65    [StorableConstructor]
66    protected BasicContext(bool deserializing) : base(deserializing) { }
67    protected BasicContext(BasicContext original, Cloner cloner)
68    : base(original, cloner) {
69      scope = cloner.Clone(original.scope);
70      iterations = cloner.Clone(original.iterations);
71      evaluatedSolutions = cloner.Clone(original.evaluatedSolutions);
72    }
73    protected BasicContext() : base() {
74      scope = new Scope("Global");
75      Parameters.Add(iterations = new FixedValueParameter<IntValue>("Iterations", new IntValue(0)));
76      Parameters.Add(evaluatedSolutions = new FixedValueParameter<IntValue>("EvaluatedSolutions", new IntValue(0)));
77    }
78    protected BasicContext(string name) : base(name) {
79      scope = new Scope("Global");
80      Parameters.Add(iterations = new FixedValueParameter<IntValue>("Iterations", new IntValue(0)));
81      Parameters.Add(evaluatedSolutions = new FixedValueParameter<IntValue>("EvaluatedSolutions", new IntValue(0)));
82    }
83    protected BasicContext(string name, ParameterCollection parameters) : base(name, parameters) {
84      scope = new Scope("Global");
85      Parameters.Add(iterations = new FixedValueParameter<IntValue>("Iterations", new IntValue(0)));
86      Parameters.Add(evaluatedSolutions = new FixedValueParameter<IntValue>("EvaluatedSolutions", new IntValue(0)));
87    }
88    protected BasicContext(string name, string description) : base(name, description) {
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    }
93    protected BasicContext(string name, string description, ParameterCollection parameters) : base(name, description, parameters) {
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    }
98
99    public override IDeepCloneable Clone(Cloner cloner) {
100      return new BasicContext(this, cloner);
101    }
102
103    public void RunOperator(IOperator op, IScope scope, CancellationToken cancellationToken) {
104      var stack = new Stack<IOperation>();
105      stack.Push(((IExecutionContext)this).CreateChildOperation(op, scope));
106
107      while (stack.Count > 0) {
108        cancellationToken.ThrowIfCancellationRequested();
109
110        var next = stack.Pop();
111        if (next is OperationCollection) {
112          var coll = (OperationCollection)next;
113          for (int i = coll.Count - 1; i >= 0; i--)
114            if (coll[i] != null) stack.Push(coll[i]);
115        } else if (next is IAtomicOperation) {
116          var operation = (IAtomicOperation)next;
117          try {
118            next = operation.Operator.Execute((IExecutionContext)operation, cancellationToken);
119          } catch (Exception ex) {
120            stack.Push(operation);
121            if (ex is OperationCanceledException) throw ex;
122            else throw new OperatorExecutionException(operation.Operator, ex);
123          }
124          if (next != null) stack.Push(next);
125        }
126      }
127    }
128
129    #region IExecutionContext members
130    IAtomicOperation IExecutionContext.CreateOperation(IOperator op) {
131      return new Core.ExecutionContext(this, op, Scope);
132    }
133    IAtomicOperation IExecutionContext.CreateOperation(IOperator op, IScope s) {
134      return new Core.ExecutionContext(this, op, s);
135    }
136    IAtomicOperation IExecutionContext.CreateChildOperation(IOperator op) {
137      return new Core.ExecutionContext(this, op, Scope);
138    }
139    IAtomicOperation IExecutionContext.CreateChildOperation(IOperator op, IScope s) {
140      return new Core.ExecutionContext(this, op, s);
141    }
142    #endregion
143  }
144}
Note: See TracBrowser for help on using the repository browser.