Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/CPLEX/CplexSolver.cs @ 15574

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

#1614: Added CPLEX algorithms

File size: 4.3 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.Encodings.IntegerVectorEncoding;
29using HeuristicLab.Optimization;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using ILOG.CPLEX;
32using ILOG.OPL;
33
34namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms.CPLEX {
35  [Item("CPLEX Solver (GQAP)", "Base class")]
36  [StorableClass]
37  public abstract class CplexSolver : ContextAlgorithm<CplexContext, IntegerVectorEncoding> {
38    public override bool SupportsPause {
39      get { return false; }
40    }
41
42    public override Type ProblemType {
43      get { return typeof(GQAP); }
44    }
45
46    public new GQAP Problem {
47      get { return (GQAP)base.Problem; }
48      set { base.Problem = value; }
49    }
50
51    [StorableConstructor]
52    protected CplexSolver(bool deserializing) : base(deserializing) { }
53    protected CplexSolver(CplexSolver original, Cloner cloner)
54    : base(original, cloner) {
55    }
56    public CplexSolver() {
57      Problem = new GQAP();
58      ((MultiAnalyzer)Analyzer).AddOperator(new QualityPerClockAnalyzer());
59    }
60
61    protected override void Initialize(CancellationToken cancellationToken) {
62      base.Initialize(cancellationToken);
63
64      try {
65        Context.RunOperator(Analyzer, cancellationToken);
66      } catch (OperationCanceledException) { }
67    }
68
69    protected override void Run(CancellationToken cancellationToken) {
70      var factory = new OplFactory();
71      var cplex = factory.CreateCplex();
72      cplex.Use(new LogCallback(this, cancellationToken));
73      cplex.SetParam(Cplex.DoubleParam.TiLim, MaximumRuntime.TotalSeconds);
74      var dataSource = new GQAPDataSource(factory, Problem.ProblemInstance);
75      using (var settings = factory.CreateOplSettings(factory.CreateOplErrorHandler()))
76      using (var opl = GetModel(factory, settings, cplex)) {
77        opl.AddDataSource(dataSource);
78        opl.Generate();
79        cplex.Solve();
80        cplex.End();
81      }
82    }
83
84    protected abstract OplModel GetModel(OplFactory factory, OplSettings settings, Cplex cplex);
85  }
86
87  public class LogCallback : Cplex.MIPInfoCallback {
88    private CplexSolver algorithm;
89    private CancellationToken token;
90    private double prev;
91
92
93    public LogCallback(CplexSolver algorithm, CancellationToken token) {
94      this.algorithm = algorithm;
95      this.token = token;
96      prev = double.NaN;
97    }
98
99    protected override void Main() {
100      if (HasIncumbent()) {
101        var val = GetIncumbentObjValue();
102        if (val == prev) return;
103        algorithm.Context.Iterations++;
104        algorithm.Context.BestQuality = val;
105        IResult result;
106        if (algorithm.Results.TryGetValue("Iterations", out result))
107          ((IntValue)result.Value).Value = algorithm.Context.Iterations;
108        else algorithm.Results.Add(new Result("Iterations", new IntValue(algorithm.Context.Iterations)));
109        if (algorithm.Results.TryGetValue("BestQuality", out result))
110          ((DoubleValue)result.Value).Value = algorithm.Context.BestQuality;
111        else algorithm.Results.Add(new Result("BestQuality", new DoubleValue(algorithm.Context.BestQuality)));
112        algorithm.Context.RunOperator(algorithm.Analyzer, CancellationToken.None);
113        prev = val;
114        if (val.IsAlmost(algorithm.Problem.BestKnownQuality))
115          Abort();
116      }
117      if (token.IsCancellationRequested) Abort();
118    }
119  }
120}
Note: See TracBrowser for help on using the repository browser.