Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2457_ExpertSystem/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/CPLEX/CplexSolver.cs @ 17935

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

#1614: updated to new persistence and .NET 4.6.1

File size: 4.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 HEAL.Attic;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.IntegerVectorEncoding;
29using HeuristicLab.Optimization;
30using ILOG.CPLEX;
31using ILOG.OPL;
32
33namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms.CPLEX {
34  [Item("CPLEX Solver (GQAP)", "Base class")]
35  [StorableType("09AC71C5-20C7-4AC0-9E5A-405BA5075552")]
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(StorableConstructorFlag _) : base(_) { }
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) {
60      base.Run(cancellationToken);
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      cplex.SetParam(Cplex.IntParam.Threads, 1);
66      var dataSource = new GQAPDataSource(factory, Problem.ProblemInstance);
67      using (var settings = factory.CreateOplSettings(factory.CreateOplErrorHandler()))
68      using (var opl = GetModel(factory, settings, cplex)) {
69        opl.AddDataSource(dataSource);
70        opl.Generate();
71        if (cplex.Solve()) {
72          var obj = cplex.ObjValue;
73          if (double.IsNaN(Context.BestQuality) || obj < Context.BestQuality)
74            Context.BestQuality = obj;
75          IResult result;
76          if (Results.TryGetValue("BestQuality", out result))
77            ((DoubleValue)result.Value).Value = Context.BestQuality;
78          else Results.Add(new Result("BestQuality", new DoubleValue(Context.BestQuality)));
79
80          Context.RunOperator(Analyzer, CancellationToken.None);
81        }
82        cplex.End();
83      }
84      Context.RunOperator(Analyzer, CancellationToken.None);
85    }
86
87    protected abstract OplModel GetModel(OplFactory factory, OplSettings settings, Cplex cplex);
88  }
89
90  public class LogCallback : Cplex.MIPInfoCallback {
91    private CplexSolver algorithm;
92    private CancellationToken token;
93    private double prev;
94
95
96    public LogCallback(CplexSolver algorithm, CancellationToken token) {
97      this.algorithm = algorithm;
98      this.token = token;
99      prev = double.NaN;
100    }
101
102    protected override void Main() {
103      if (HasIncumbent()) {
104        var val = GetIncumbentObjValue();
105        if (val >= prev) return;
106        algorithm.Context.Iterations++;
107        algorithm.Context.BestQuality = val;
108        IResult result;
109        if (algorithm.Results.TryGetValue("Iterations", out result))
110          ((IntValue)result.Value).Value = algorithm.Context.Iterations;
111        else algorithm.Results.Add(new Result("Iterations", new IntValue(algorithm.Context.Iterations)));
112        if (algorithm.Results.TryGetValue("BestQuality", out result))
113          ((DoubleValue)result.Value).Value = algorithm.Context.BestQuality;
114        else algorithm.Results.Add(new Result("BestQuality", new DoubleValue(algorithm.Context.BestQuality)));
115        algorithm.Context.RunOperator(algorithm.Analyzer, CancellationToken.None);
116        prev = val;
117        if (val.IsAlmost(algorithm.Problem.BestKnownQuality))
118          Abort();
119      }
120      if (token.IsCancellationRequested) Abort();
121    }
122  }
123}
Note: See TracBrowser for help on using the repository browser.