Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2931_OR-Tools_LP_MIP/HeuristicLab.MathematicalOptimization/3.3/LinearProgramming/Algorithms/Solvers/Base/Solver.cs @ 16234

Last change on this file since 16234 was 16234, checked in by ddorfmei, 5 years ago

#2931:

  • updated plugin dependencies
  • added solver library name defaults to settings
File size: 4.0 KB
Line 
1using System;
2using System.Threading;
3using HeuristicLab.Common;
4using HeuristicLab.Core;
5using HeuristicLab.Data;
6using HeuristicLab.Parameters;
7using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
8
9namespace HeuristicLab.MathematicalOptimization.LinearProgramming.Algorithms.Solvers.Base {
10
11  [StorableClass]
12  public class Solver : ParameterizedNamedItem, ISolver, IDisposable {
13
14    [Storable]
15    protected IValueParameter<EnumValue<LinearProgrammingType>> programmingTypeParam;
16
17    protected LinearSolver solver;
18
19    public Solver() {
20      Parameters.Add(programmingTypeParam =
21        new ValueParameter<EnumValue<LinearProgrammingType>>(nameof(LinearProgrammingType),
22          new EnumValue<LinearProgrammingType>()));
23    }
24
25    [StorableConstructor]
26    protected Solver(bool deserializing)
27      : base(deserializing) {
28    }
29
30    protected Solver(Solver original, Cloner cloner)
31      : base(original, cloner) {
32      programmingTypeParam = cloner.Clone(original.programmingTypeParam);
33    }
34
35    public LinearProgrammingType LinearProgrammingType {
36      get => programmingTypeParam.Value.Value;
37      set => programmingTypeParam.Value.Value = value;
38    }
39
40    protected virtual OptimizationProblemType OptimizationProblemType { get; }
41    public virtual bool SupportsPause => false;
42    public virtual bool SupportsStop => false;
43
44    public override IDeepCloneable Clone(Cloner cloner) => new Solver(this, cloner);
45
46    public void Dispose() => solver?.Dispose();
47
48    public void Interrupt() => solver.Stop();
49
50    public virtual void Reset() {
51      solver = null;
52    }
53
54    public virtual void Solve(LinearProgrammingAlgorithm algorithm, CancellationToken cancellationToken) =>
55      Solve(algorithm);
56
57    public virtual void Solve(LinearProgrammingAlgorithm algorithm) =>
58      Solve(algorithm, algorithm.TimeLimit, false);
59
60    public virtual void Solve(LinearProgrammingAlgorithm algorithm, TimeSpan timeLimit, bool incrementality) {
61      string libraryName = null;
62      if (this is IExternalSolver externalSolver)
63        libraryName = externalSolver.LibraryName;
64
65      if (solver == null) {
66        solver = LinearSolver.CreateSolver(OptimizationProblemType, Name,
67          libraryName, s => algorithm.Problem.ProblemDefinition.BuildModel(s));
68      }
69
70      solver.TimeLimit = timeLimit;
71      solver.RelativeGapTolerance = algorithm.RelativeGapTolerance;
72      solver.PrimalTolerance = algorithm.PrimalTolerance;
73      solver.DualTolerance = algorithm.DualTolerance;
74      solver.Presolve = algorithm.Presolve;
75      solver.Scaling = algorithm.Scaling;
76      solver.LpAlgorithm = algorithm.LpAlgorithm;
77      solver.Incrementality = incrementality;
78
79      solver.Solve();
80
81      algorithm.Problem.ProblemDefinition.Analyze(solver.Solver, algorithm.Results);
82      algorithm.Results.AddOrUpdateResult("Result Status", new EnumValue<ResultStatus>(solver.ResultStatus));
83      algorithm.Results.AddOrUpdateResult("Best Objective Value",
84        new DoubleValue(solver.ObjectiveValue ?? double.NaN));
85      algorithm.Results.AddOrUpdateResult("Best Objective Bound",
86        new DoubleValue(solver.ObjectiveBound ?? double.NaN));
87      algorithm.Results.AddOrUpdateResult("Absolute Gap", new DoubleValue(solver.AbsoluteGap ?? double.NaN));
88      algorithm.Results.AddOrUpdateResult("Relative Gap", new DoubleValue(solver.RelativeGap ?? double.NaN));
89      algorithm.Results.AddOrUpdateResult("Number of Constraints", new IntValue(solver.NumberOfConstraints));
90      algorithm.Results.AddOrUpdateResult("Number of Variables", new IntValue(solver.NumberOfVariables));
91      algorithm.Results.AddOrUpdateResult("Number of Nodes", new DoubleValue(solver.NumberOfNodes));
92      algorithm.Results.AddOrUpdateResult("Iterations", new DoubleValue(solver.Iterations));
93      algorithm.Results.AddOrUpdateResult("Solver Version", new StringValue(solver.SolverVersion));
94      algorithm.Results.AddOrUpdateResult("Wall Time", new TimeSpanValue(solver.WallTime ?? TimeSpan.Zero));
95    }
96  }
97}
Note: See TracBrowser for help on using the repository browser.