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

Last change on this file since 16288 was 16288, checked in by ddorfmei, 7 months ago

#2931:

  • added license information to all files
  • added missing storable and cloning constructors
  • fixed a bug that caused an exception when a Hive Slave tried to delete the files in the PluginTemp folder
File size: 4.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.MathematicalOptimization.LinearProgramming.Algorithms.Solvers.Base {
31
32  [StorableClass]
33  public class Solver : ParameterizedNamedItem, ISolver, IDisposable {
34
35    [Storable]
36    protected IValueParameter<EnumValue<LinearProgrammingType>> programmingTypeParam;
37
38    protected LinearSolver solver;
39
40    public Solver() {
41      Parameters.Add(programmingTypeParam =
42        new ValueParameter<EnumValue<LinearProgrammingType>>(nameof(LinearProgrammingType),
43          new EnumValue<LinearProgrammingType>()));
44    }
45
46    [StorableConstructor]
47    protected Solver(bool deserializing)
48      : base(deserializing) {
49    }
50
51    protected Solver(Solver original, Cloner cloner)
52      : base(original, cloner) {
53      programmingTypeParam = cloner.Clone(original.programmingTypeParam);
54    }
55
56    public LinearProgrammingType LinearProgrammingType {
57      get => programmingTypeParam.Value.Value;
58      set => programmingTypeParam.Value.Value = value;
59    }
60
61    protected virtual OptimizationProblemType OptimizationProblemType { get; }
62    public virtual bool SupportsPause => false;
63    public virtual bool SupportsStop => false;
64
65    public override IDeepCloneable Clone(Cloner cloner) => new Solver(this, cloner);
66
67    public void Dispose() => solver?.Dispose();
68
69    public void Interrupt() => solver.Stop();
70
71    public virtual void Reset() {
72      solver?.Dispose();
73      solver = null;
74    }
75
76    public virtual void Solve(LinearProgrammingAlgorithm algorithm, CancellationToken cancellationToken) =>
77      Solve(algorithm);
78
79    public virtual void Solve(LinearProgrammingAlgorithm algorithm) =>
80      Solve(algorithm, algorithm.TimeLimit, false);
81
82    public virtual void Solve(LinearProgrammingAlgorithm algorithm, TimeSpan timeLimit, bool incrementality) {
83      string libraryName = null;
84      if (this is IExternalSolver externalSolver)
85        libraryName = externalSolver.LibraryName;
86
87      if (solver == null) {
88        solver = LinearSolver.CreateSolver(OptimizationProblemType, Name,
89          libraryName, s => algorithm.Problem.ProblemDefinition.BuildModel(s));
90      }
91
92      solver.TimeLimit = timeLimit;
93      solver.RelativeGapTolerance = algorithm.RelativeGapTolerance;
94      solver.PrimalTolerance = algorithm.PrimalTolerance;
95      solver.DualTolerance = algorithm.DualTolerance;
96      solver.Presolve = algorithm.Presolve;
97      solver.Scaling = algorithm.Scaling;
98      solver.LpAlgorithm = algorithm.LpAlgorithm;
99      solver.Incrementality = incrementality;
100
101      solver.Solve();
102
103      algorithm.Problem.ProblemDefinition.Analyze(solver.Solver, algorithm.Results);
104      algorithm.Results.AddOrUpdateResult("Result Status", new EnumValue<ResultStatus>(solver.ResultStatus));
105      algorithm.Results.AddOrUpdateResult("Best Objective Value",
106        new DoubleValue(solver.ObjectiveValue ?? double.NaN));
107      algorithm.Results.AddOrUpdateResult("Best Objective Bound",
108        new DoubleValue(solver.ObjectiveBound ?? double.NaN));
109      algorithm.Results.AddOrUpdateResult("Absolute Gap", new DoubleValue(solver.AbsoluteGap ?? double.NaN));
110      algorithm.Results.AddOrUpdateResult("Relative Gap", new DoubleValue(solver.RelativeGap ?? double.NaN));
111      algorithm.Results.AddOrUpdateResult("Number of Constraints", new IntValue(solver.NumberOfConstraints));
112      algorithm.Results.AddOrUpdateResult("Number of Variables", new IntValue(solver.NumberOfVariables));
113      algorithm.Results.AddOrUpdateResult("Number of Nodes", new DoubleValue(solver.NumberOfNodes));
114      algorithm.Results.AddOrUpdateResult("Iterations", new DoubleValue(solver.Iterations));
115      algorithm.Results.AddOrUpdateResult("Solver Version", new StringValue(solver.SolverVersion));
116      algorithm.Results.AddOrUpdateResult("Wall Time", new TimeSpanValue(solver.WallTime ?? TimeSpan.Zero));
117    }
118  }
119}
Note: See TracBrowser for help on using the repository browser.