Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1614_GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/CPLEX/CplexSolver.cs @ 15713

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

#1614:

  • added additional constraint to benchmark data generator and updated one instance that was affected by this
  • added fitness landscape characteristics for the GQAP
  • fixed RLD analysis view to compensate for empty convergence graphs
  • fixed CPLEX solvers not using the obj value when the solver terminates (callback is not called if proven optimal solution is found)
  • added code for local solver to also check on final quality
File size: 4.6 KB
RevLine 
[15574]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.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.IntegerVectorEncoding;
28using HeuristicLab.Optimization;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using ILOG.CPLEX;
31using ILOG.OPL;
32
33namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms.CPLEX {
34  [Item("CPLEX Solver (GQAP)", "Base class")]
35  [StorableClass]
36  public abstract class CplexSolver : ContextAlgorithm<CplexContext, IntegerVectorEncoding> {
37    public override bool SupportsPause {
38      get { return false; }
39    }
40
41    public override Type ProblemType {
42      get { return typeof(GQAP); }
43    }
44
45    public new GQAP Problem {
46      get { return (GQAP)base.Problem; }
47      set { base.Problem = value; }
48    }
49
50    [StorableConstructor]
51    protected CplexSolver(bool deserializing) : base(deserializing) { }
52    protected CplexSolver(CplexSolver original, Cloner cloner)
53    : base(original, cloner) {
54    }
55    public CplexSolver() {
56      Problem = new GQAP();
57    }
58
59    protected override void Run(CancellationToken cancellationToken) {
[15700]60      base.Run(cancellationToken);
[15574]61      var factory = new OplFactory();
62      var cplex = factory.CreateCplex();
63      cplex.Use(new LogCallback(this, cancellationToken));
64      cplex.SetParam(Cplex.DoubleParam.TiLim, MaximumRuntime.TotalSeconds);
65      var dataSource = new GQAPDataSource(factory, Problem.ProblemInstance);
66      using (var settings = factory.CreateOplSettings(factory.CreateOplErrorHandler()))
67      using (var opl = GetModel(factory, settings, cplex)) {
68        opl.AddDataSource(dataSource);
69        opl.Generate();
[15713]70        if (cplex.Solve()) {
71          var obj = cplex.ObjValue;
72          if (double.IsNaN(Context.BestQuality) || obj < Context.BestQuality)
73            Context.BestQuality = obj;
74          IResult result;
75          if (Results.TryGetValue("BestQuality", out result))
76            ((DoubleValue)result.Value).Value = Context.BestQuality;
77          else Results.Add(new Result("BestQuality", new DoubleValue(Context.BestQuality)));
78
79          Context.RunOperator(Analyzer, CancellationToken.None);
80        }
[15574]81        cplex.End();
82      }
[15634]83      Context.RunOperator(Analyzer, CancellationToken.None);
[15574]84    }
85
86    protected abstract OplModel GetModel(OplFactory factory, OplSettings settings, Cplex cplex);
87  }
88
89  public class LogCallback : Cplex.MIPInfoCallback {
90    private CplexSolver algorithm;
91    private CancellationToken token;
92    private double prev;
93
94
95    public LogCallback(CplexSolver algorithm, CancellationToken token) {
96      this.algorithm = algorithm;
97      this.token = token;
98      prev = double.NaN;
99    }
100
101    protected override void Main() {
102      if (HasIncumbent()) {
103        var val = GetIncumbentObjValue();
[15575]104        if (val >= prev) return;
[15574]105        algorithm.Context.Iterations++;
106        algorithm.Context.BestQuality = val;
107        IResult result;
108        if (algorithm.Results.TryGetValue("Iterations", out result))
109          ((IntValue)result.Value).Value = algorithm.Context.Iterations;
110        else algorithm.Results.Add(new Result("Iterations", new IntValue(algorithm.Context.Iterations)));
111        if (algorithm.Results.TryGetValue("BestQuality", out result))
112          ((DoubleValue)result.Value).Value = algorithm.Context.BestQuality;
113        else algorithm.Results.Add(new Result("BestQuality", new DoubleValue(algorithm.Context.BestQuality)));
114        algorithm.Context.RunOperator(algorithm.Analyzer, CancellationToken.None);
115        prev = val;
116        if (val.IsAlmost(algorithm.Problem.BestKnownQuality))
117          Abort();
118      }
119      if (token.IsCancellationRequested) Abort();
120    }
121  }
122}
Note: See TracBrowser for help on using the repository browser.