source: branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/Infrastructure/Algorithms/ContextAlgorithm.cs @ 15574

Last change on this file since 15574 was 15574, checked in by abeham, 2 years ago

#1614: Added CPLEX algorithms

File size: 6.6 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.Threading;
24using HeuristicLab.Analysis;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms {
33  [Item("Context-based Algorithm", "Algorithms that make use of contexts to facilitate applying operators.")]
34  [StorableClass]
35  public abstract class ContextAlgorithm<TContext, TEncoding> : BasicAlgorithm
36    where TContext : class, IContext, new()
37    where TEncoding : class, IEncoding {
38
39    public override Type ProblemType {
40      get { return typeof(SingleObjectiveBasicProblem<TEncoding>); }
41    }
42
43    public new SingleObjectiveBasicProblem<TEncoding> Problem {
44      get { return (SingleObjectiveBasicProblem<TEncoding>)base.Problem; }
45      set { base.Problem = value; }
46    }
47
48    [Storable]
49    private TContext context;
50    public TContext Context {
51      get { return context; }
52    }
53
54    [Storable]
55    private ValueParameter<IAnalyzer> analyzerParameter;
56    public IValueParameter<IAnalyzer> AnalyzerParameter {
57      get { return analyzerParameter; }
58    }
59    [Storable]
60    private FixedValueParameter<IntValue> maximumIterationsParameter;
61    public IFixedValueParameter<IntValue> MaximumIterationsParameter {
62      get { return maximumIterationsParameter; }
63    }
64    [Storable]
65    private FixedValueParameter<IntValue> maximumEvaluationsParameter;
66    public IFixedValueParameter<IntValue> MaximumEvaluationsParameter {
67      get { return maximumEvaluationsParameter; }
68    }
69    [Storable]
70    private FixedValueParameter<TimeSpanValue> maximumRuntimeParameter;
71    public IFixedValueParameter<TimeSpanValue> MaximumRuntimeParameter {
72      get { return maximumRuntimeParameter; }
73    }
74    [Storable]
75    private FixedValueParameter<BoolValue> stopAtBestKnownQualityParameter;
76    public IFixedValueParameter<BoolValue> StopAtBestKnownQualityParameter {
77      get { return stopAtBestKnownQualityParameter; }
78    }
79
80    public IAnalyzer Analyzer {
81      get { return analyzerParameter.Value; }
82      set { analyzerParameter.Value = value; }
83    }
84    public int MaximumIterations {
85      get { return maximumIterationsParameter.Value.Value; }
86      set { maximumIterationsParameter.Value.Value = value; }
87    }
88    public int MaximumEvaluations {
89      get { return maximumEvaluationsParameter.Value.Value; }
90      set { maximumEvaluationsParameter.Value.Value = value; }
91    }
92    public TimeSpan MaximumRuntime {
93      get { return maximumRuntimeParameter.Value.Value; }
94      set { maximumRuntimeParameter.Value.Value = value; }
95    }
96    public bool StopAtBestKnownQuality {
97      get { return stopAtBestKnownQualityParameter.Value.Value; }
98      set { stopAtBestKnownQualityParameter.Value.Value = value; }
99    }
100
101    [StorableConstructor]
102    protected ContextAlgorithm(bool deserializing) : base(deserializing) { }
103    protected ContextAlgorithm(ContextAlgorithm<TContext, TEncoding> original, Cloner cloner)
104      : base(original, cloner) {
105      context = cloner.Clone(original.context);
106      analyzerParameter = cloner.Clone(original.analyzerParameter);
107      maximumIterationsParameter = cloner.Clone(original.maximumIterationsParameter);
108      maximumEvaluationsParameter = cloner.Clone(original.maximumEvaluationsParameter);
109      maximumRuntimeParameter = cloner.Clone(original.maximumRuntimeParameter);
110      stopAtBestKnownQualityParameter = cloner.Clone(original.stopAtBestKnownQualityParameter);
111    }
112    protected ContextAlgorithm()
113      : base() {
114      Parameters.Add(analyzerParameter = new ValueParameter<IAnalyzer>("Analyzer", "The analyzers that are used to perform an analysis of the solutions.", new MultiAnalyzer()));
115      Parameters.Add(maximumIterationsParameter = new FixedValueParameter<IntValue>("MaximumIterations", "The number of iterations that the algorithm should run or < 1 to ignore.", new IntValue(0)));
116      Parameters.Add(maximumEvaluationsParameter = new FixedValueParameter<IntValue>("MaximumEvaluations", "The number of evaluated solutions that the algorithm should perform or < 1 to ignore.", new IntValue(0)));
117      Parameters.Add(maximumRuntimeParameter = new FixedValueParameter<TimeSpanValue>("MaximumRuntime", "The timespan that the algorithm is allowed to run.", new TimeSpanValue(TimeSpan.FromMinutes(1))));
118      Parameters.Add(stopAtBestKnownQualityParameter = new FixedValueParameter<BoolValue>("StopAtBestKnownQuality", "Whether the algorithm should terminate if the best known quality has been found.", new BoolValue(true)));
119    }
120
121    protected override void Initialize(CancellationToken cancellationToken) {
122      base.Initialize(cancellationToken);
123      context = new TContext();
124      context.Scope.Variables.Add(new Variable("Results", Results));
125
126      IExecutionContext ctxt = null;
127      foreach (var item in Problem.ExecutionContextItems)
128        ctxt = new Core.ExecutionContext(ctxt, item, Context.Scope);
129      ctxt = new Core.ExecutionContext(ctxt, this, Context.Scope);
130      context.Parent = ctxt;
131
132      context.Iterations = 0;
133      context.EvaluatedSolutions = 0;
134      context.BestQuality = double.NaN;
135    }
136
137    public override void Prepare() {
138      context = null;
139      base.Prepare();
140    }
141
142    protected virtual bool StoppingCriterion() {
143      return MaximumIterations > 0 && Context.Iterations > MaximumIterations
144        || MaximumEvaluations > 0 && Context.EvaluatedSolutions > MaximumEvaluations
145        || MaximumRuntime > TimeSpan.Zero && ExecutionTime > MaximumRuntime
146        || StopAtBestKnownQuality && !double.IsNaN(Problem.BestKnownQuality)
147          && Context.BestQuality.IsAlmost(Problem.BestKnownQuality);
148    }
149  }
150}
Note: See TracBrowser for help on using the repository browser.