Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1614:

  • fixed a bug in GRASP where solutions in the elite set would be mutated
  • introduced termination criteria when reaching best-known quality
  • tweaked generating random numbers in StochasticNMoveSingleMoveGenerator
  • changed DiscreteLocationCrossover to use an allele from one of the parents instead of introducing a mutation in case no feasible insert location is found
  • changed OSGA maxselpress to 500
  • slight change to contexts, introduced single-objectiveness much earlier in the class hierachy
    • limited ContextAlgorithm to SingleObjectiveBasicProblems (doesn't matter here)
File size: 6.7 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    [Storable]
66    private IValueParameter<DoubleValue> bestQuality;
67    public double BestQuality {
68      get { return bestQuality.Value.Value; }
69      set { bestQuality.Value.Value = value; }
70    }
71
72    [StorableConstructor]
73    protected BasicContext(bool deserializing) : base(deserializing) { }
74    protected BasicContext(BasicContext original, Cloner cloner)
75    : base(original, cloner) {
76      scope = cloner.Clone(original.scope);
77      iterations = cloner.Clone(original.iterations);
78      evaluatedSolutions = cloner.Clone(original.evaluatedSolutions);
79      bestQuality = cloner.Clone(original.bestQuality);
80    }
81    protected BasicContext() : base() {
82      scope = new Scope("Global");
83      Parameters.Add(iterations = new FixedValueParameter<IntValue>("Iterations", new IntValue(0)));
84      Parameters.Add(evaluatedSolutions = new FixedValueParameter<IntValue>("EvaluatedSolutions", new IntValue(0)));
85      Parameters.Add(bestQuality = new ValueParameter<DoubleValue>("BestQuality", new DoubleValue(double.NaN)));
86    }
87    protected BasicContext(string name) : base(name) {
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, ParameterCollection parameters) : base(name, 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      Parameters.Add(bestQuality = new ValueParameter<DoubleValue>("BestQuality", new DoubleValue(double.NaN)));
98    }
99    protected BasicContext(string name, string description) : base(name, description) {
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, ParameterCollection parameters) : base(name, description, parameters) {
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
112    public override IDeepCloneable Clone(Cloner cloner) {
113      return new BasicContext(this, cloner);
114    }
115
116    public void RunOperator(IOperator op, IScope scope, CancellationToken cancellationToken) {
117      var stack = new Stack<IOperation>();
118      stack.Push(((IExecutionContext)this).CreateChildOperation(op, scope));
119
120      while (stack.Count > 0) {
121        cancellationToken.ThrowIfCancellationRequested();
122
123        var next = stack.Pop();
124        if (next is OperationCollection) {
125          var coll = (OperationCollection)next;
126          for (int i = coll.Count - 1; i >= 0; i--)
127            if (coll[i] != null) stack.Push(coll[i]);
128        } else if (next is IAtomicOperation) {
129          var operation = (IAtomicOperation)next;
130          try {
131            next = operation.Operator.Execute((IExecutionContext)operation, cancellationToken);
132          } catch (Exception ex) {
133            stack.Push(operation);
134            if (ex is OperationCanceledException) throw ex;
135            else throw new OperatorExecutionException(operation.Operator, ex);
136          }
137          if (next != null) stack.Push(next);
138        }
139      }
140    }
141
142    #region IExecutionContext members
143    IAtomicOperation IExecutionContext.CreateOperation(IOperator op) {
144      return new Core.ExecutionContext(this, op, Scope);
145    }
146    IAtomicOperation IExecutionContext.CreateOperation(IOperator op, IScope s) {
147      return new Core.ExecutionContext(this, op, s);
148    }
149    IAtomicOperation IExecutionContext.CreateChildOperation(IOperator op) {
150      return new Core.ExecutionContext(this, op, Scope);
151    }
152    IAtomicOperation IExecutionContext.CreateChildOperation(IOperator op, IScope s) {
153      return new Core.ExecutionContext(this, op, s);
154    }
155    #endregion
156  }
157}
Note: See TracBrowser for help on using the repository browser.