Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1614:

  • Added missing Item and StorableClass attributes
  • Removed running analyzer on empty solutions in GRASP
File size: 6.8 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  [Item("Basic Context", "A base class for algorithms' contexts.")]
33  [StorableClass]
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 { bestQuality.Value.Value = value; }
72    }
73
74    [StorableConstructor]
75    protected BasicContext(bool deserializing) : base(deserializing) { }
76    protected BasicContext(BasicContext original, Cloner cloner)
77    : base(original, cloner) {
78      scope = cloner.Clone(original.scope);
79      iterations = cloner.Clone(original.iterations);
80      evaluatedSolutions = cloner.Clone(original.evaluatedSolutions);
81      bestQuality = cloner.Clone(original.bestQuality);
82    }
83    protected BasicContext() : base() {
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      Parameters.Add(bestQuality = new ValueParameter<DoubleValue>("BestQuality", new DoubleValue(double.NaN)));
88    }
89    protected BasicContext(string name) : base(name) {
90      scope = new Scope("Global");
91      Parameters.Add(iterations = new FixedValueParameter<IntValue>("Iterations", new IntValue(0)));
92      Parameters.Add(evaluatedSolutions = new FixedValueParameter<IntValue>("EvaluatedSolutions", new IntValue(0)));
93      Parameters.Add(bestQuality = new ValueParameter<DoubleValue>("BestQuality", new DoubleValue(double.NaN)));
94    }
95    protected BasicContext(string name, ParameterCollection parameters) : base(name, parameters) {
96      scope = new Scope("Global");
97      Parameters.Add(iterations = new FixedValueParameter<IntValue>("Iterations", new IntValue(0)));
98      Parameters.Add(evaluatedSolutions = new FixedValueParameter<IntValue>("EvaluatedSolutions", new IntValue(0)));
99      Parameters.Add(bestQuality = new ValueParameter<DoubleValue>("BestQuality", new DoubleValue(double.NaN)));
100    }
101    protected BasicContext(string name, string description) : base(name, description) {
102      scope = new Scope("Global");
103      Parameters.Add(iterations = new FixedValueParameter<IntValue>("Iterations", new IntValue(0)));
104      Parameters.Add(evaluatedSolutions = new FixedValueParameter<IntValue>("EvaluatedSolutions", new IntValue(0)));
105      Parameters.Add(bestQuality = new ValueParameter<DoubleValue>("BestQuality", new DoubleValue(double.NaN)));
106    }
107    protected BasicContext(string name, string description, ParameterCollection parameters) : base(name, description, parameters) {
108      scope = new Scope("Global");
109      Parameters.Add(iterations = new FixedValueParameter<IntValue>("Iterations", new IntValue(0)));
110      Parameters.Add(evaluatedSolutions = new FixedValueParameter<IntValue>("EvaluatedSolutions", new IntValue(0)));
111      Parameters.Add(bestQuality = new ValueParameter<DoubleValue>("BestQuality", new DoubleValue(double.NaN)));
112    }
113
114    public override IDeepCloneable Clone(Cloner cloner) {
115      return new BasicContext(this, cloner);
116    }
117
118    public void RunOperator(IOperator op, CancellationToken cancellationToken) {
119      var stack = new Stack<IOperation>();
120      stack.Push(((IExecutionContext)this).CreateChildOperation(op, scope));
121
122      while (stack.Count > 0) {
123        cancellationToken.ThrowIfCancellationRequested();
124
125        var next = stack.Pop();
126        if (next is OperationCollection) {
127          var coll = (OperationCollection)next;
128          for (int i = coll.Count - 1; i >= 0; i--)
129            if (coll[i] != null) stack.Push(coll[i]);
130        } else if (next is IAtomicOperation) {
131          var operation = (IAtomicOperation)next;
132          try {
133            next = operation.Operator.Execute((IExecutionContext)operation, cancellationToken);
134          } catch (Exception ex) {
135            stack.Push(operation);
136            if (ex is OperationCanceledException) throw ex;
137            else throw new OperatorExecutionException(operation.Operator, ex);
138          }
139          if (next != null) stack.Push(next);
140        }
141      }
142    }
143
144    #region IExecutionContext members
145    IAtomicOperation IExecutionContext.CreateOperation(IOperator op) {
146      return new Core.ExecutionContext(this, op, Scope);
147    }
148    IAtomicOperation IExecutionContext.CreateOperation(IOperator op, IScope s) {
149      return new Core.ExecutionContext(this, op, s);
150    }
151    IAtomicOperation IExecutionContext.CreateChildOperation(IOperator op) {
152      return new Core.ExecutionContext(this, op, Scope);
153    }
154    IAtomicOperation IExecutionContext.CreateChildOperation(IOperator op, IScope s) {
155      return new Core.ExecutionContext(this, op, s);
156    }
157    #endregion
158  }
159}
Note: See TracBrowser for help on using the repository browser.