Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/29/19 17:45:06 (5 years ago)
Author:
ddorfmei
Message:

#2931: solved the issues found during the review

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2931_OR-Tools_LP_MIP/HeuristicLab.MathematicalOptimization/3.3/LinearProgramming/Algorithms/Solvers/Base/LinearSolver.cs

    r16405 r16582  
    3434using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3535
    36 namespace HeuristicLab.MathematicalOptimization.LinearProgramming {
     36namespace HeuristicLab.ExactOptimization.LinearProgramming {
    3737
    3838  [StorableClass]
     
    4949    public LinearSolver() {
    5050      Parameters.Add(problemTypeParam =
    51         new ValueParameter<EnumValue<ProblemType>>(nameof(ProblemType), new EnumValue<ProblemType>()));
     51        new ValueParameter<EnumValue<ProblemType>>(nameof(ProblemType),
     52          new EnumValue<ProblemType>(ProblemType.MixedIntegerProgramming)));
    5253      Parameters.Add(solverSpecificParametersParam =
    5354        new FixedValueParameter<TextValue>(nameof(SolverSpecificParameters), new TextValue()));
     
    6566    }
    6667
    67     public double DualTolerance { get; set; } = MPSolverParameters.kDefaultDualTolerance;
    68 
    69     public string ExportModel { get; set; }
     68    public double DualTolerance { get; set; } = SolverParameters.DefaultDualTolerance;
    7069
    7170    public bool Incrementality { get; set; } =
    72           MPSolverParameters.kDefaultIncrementality == MPSolverParameters.INCREMENTALITY_ON;
    73 
    74     public LpAlgorithmValues LpAlgorithm { get; set; }
    75 
    76     public bool Presolve { get; set; } = MPSolverParameters.kDefaultPresolve == MPSolverParameters.PRESOLVE_ON;
    77 
    78     public double PrimalTolerance { get; set; } = MPSolverParameters.kDefaultPrimalTolerance;
     71      SolverParameters.DefaultIncrementality == SolverParameters.IncrementalityValues.IncrementalityOn;
     72
     73    public SolverParameters.LpAlgorithmValues LpAlgorithm { get; set; }
     74    protected virtual Solver.OptimizationProblemType OptimizationProblemType { get; }
     75    public bool Presolve { get; set; } = SolverParameters.DefaultPresolve == SolverParameters.PresolveValues.PresolveOn;
     76    public double PrimalTolerance { get; set; } = SolverParameters.DefaultPrimalTolerance;
    7977
    8078    public ProblemType ProblemType {
     
    8381    }
    8482
    85     public double RelativeGapTolerance { get; set; } = MPSolverParameters.kDefaultRelativeMipGap;
    86 
     83    public IValueParameter<EnumValue<ProblemType>> ProblemTypeParameter => problemTypeParam;
     84    public double RelativeGapTolerance { get; set; } = SolverParameters.DefaultRelativeMipGap;
    8785    public bool Scaling { get; set; }
    8886
     
    9290    }
    9391
     92    public IFixedValueParameter<TextValue> SolverSpecificParametersParameter => solverSpecificParametersParam;
    9493    public virtual bool SupportsPause => true;
    9594    public virtual bool SupportsStop => true;
    9695    public virtual TimeSpan TimeLimit { get; set; } = TimeSpan.Zero;
    97     protected virtual OptimizationProblemType OptimizationProblemType { get; }
    9896
    9997    public override IDeepCloneable Clone(Cloner cloner) => new LinearSolver(this, cloner);
     
    118116
    119117    public bool ExportAsProto(string fileName, ProtoWriteFormat writeFormat = ProtoWriteFormat.ProtoBinary) =>
    120       solver != null && solver.ExportModelAsProtoFormat(fileName, (int)writeFormat);
    121 
    122     public SolverResponseStatus ImportFromMps(string fileName, bool? fixedFormat) => solver == null
    123       ? SolverResponseStatus.Abnormal
    124       : (SolverResponseStatus)solver.ImportModelFromMpsFormat(fileName, fixedFormat.HasValue, fixedFormat ?? false);
    125 
    126     public SolverResponseStatus ImportFromProto(string fileName) => solver == null
    127       ? SolverResponseStatus.Abnormal
    128       : (SolverResponseStatus)solver.ImportModelFromProtoFormat(fileName);
     118      solver != null && solver.ExportModelAsProtoFormat(fileName, (Google.OrTools.LinearSolver.ProtoWriteFormat)writeFormat);
     119
     120    public MPSolverResponseStatus ImportFromMps(string fileName, bool? fixedFormat) =>
     121      solver?.ImportModelFromMpsFormat(fileName, fixedFormat.HasValue, fixedFormat ?? false) ??
     122      (MPSolverResponseStatus)SolverResponseStatus.Abnormal;
     123
     124    public MPSolverResponseStatus ImportFromProto(string fileName) =>
     125      solver?.ImportModelFromProtoFormat(fileName) ?? (MPSolverResponseStatus)SolverResponseStatus.Abnormal;
    129126
    130127    public bool InterruptSolve() => solver?.InterruptSolve() ?? false;
     
    135132    }
    136133
    137     public virtual void Solve(ILinearProgrammingProblemDefinition problemDefintion, ref TimeSpan executionTime,
     134    public virtual void Solve(ILinearProblemDefinition problemDefintion,
    138135      ResultCollection results, CancellationToken cancellationToken) =>
    139       Solve(problemDefintion, ref executionTime, results);
    140 
    141     public virtual void Solve(ILinearProgrammingProblemDefinition problemDefinition, ref TimeSpan executionTime,
     136      Solve(problemDefintion, results);
     137
     138    public virtual void Solve(ILinearProblemDefinition problemDefinition,
    142139      ResultCollection results) =>
    143140      Solve(problemDefinition, results, TimeLimit);
    144141
    145     public virtual void Solve(ILinearProgrammingProblemDefinition problemDefinition, ResultCollection results,
     142    public virtual void Solve(ILinearProblemDefinition problemDefinition, ResultCollection results,
    146143      TimeSpan timeLimit) {
    147144      if (solver == null) {
     
    158155      ResultStatus resultStatus;
    159156
    160       using (var parameters = new MPSolverParameters()) {
    161         parameters.SetDoubleParam(MPSolverParameters.RELATIVE_MIP_GAP, RelativeGapTolerance);
    162         parameters.SetDoubleParam(MPSolverParameters.PRIMAL_TOLERANCE, PrimalTolerance);
    163         parameters.SetDoubleParam(MPSolverParameters.DUAL_TOLERANCE, DualTolerance);
    164         parameters.SetIntegerParam(MPSolverParameters.PRESOLVE,
    165           Presolve ? MPSolverParameters.PRESOLVE_ON : MPSolverParameters.PRESOLVE_OFF);
    166         parameters.SetIntegerParam(MPSolverParameters.LP_ALGORITHM, (int)LpAlgorithm);
    167         parameters.SetIntegerParam(MPSolverParameters.INCREMENTALITY,
    168           Incrementality ? MPSolverParameters.INCREMENTALITY_ON : MPSolverParameters.INCREMENTALITY_OFF);
    169         parameters.SetIntegerParam(MPSolverParameters.SCALING,
    170           Scaling ? MPSolverParameters.SCALING_ON : MPSolverParameters.SCALING_OFF);
     157      using (var parameters = new SolverParameters()) {
     158        parameters.SetDoubleParam(SolverParameters.DoubleParam.RelativeMipGap, RelativeGapTolerance);
     159        parameters.SetDoubleParam(SolverParameters.DoubleParam.PrimalTolerance, PrimalTolerance);
     160        parameters.SetDoubleParam(SolverParameters.DoubleParam.DualTolerance, DualTolerance);
     161        parameters.SetIntegerParam(SolverParameters.IntegerParam.Presolve,
     162          (int)(Presolve ? SolverParameters.PresolveValues.PresolveOn : SolverParameters.PresolveValues.PresolveOff));
     163        parameters.SetIntegerParam(SolverParameters.IntegerParam.Incrementality,
     164          (int)(Incrementality ? SolverParameters.IncrementalityValues.IncrementalityOn : SolverParameters.IncrementalityValues.IncrementalityOff));
     165        parameters.SetIntegerParam(SolverParameters.IntegerParam.Scaling,
     166          (int)(Scaling ? SolverParameters.ScalingValues.ScalingOn : SolverParameters.ScalingValues.ScalingOff));
    171167
    172168        if (!solver.SetSolverSpecificParametersAsString(SolverSpecificParameters))
    173169          throw new ArgumentException("Solver specific parameters could not be set.");
    174170
    175         if (!string.IsNullOrWhiteSpace(ExportModel)) {
    176           var fileInfo = new FileInfo(ExportModel);
    177 
    178           if (!fileInfo.Directory?.Exists ?? false) {
    179             Directory.CreateDirectory(fileInfo.Directory.FullName);
    180           }
    181 
    182           bool exportSuccessful;
    183           switch (fileInfo.Extension) {
    184             case ".lp":
    185               exportSuccessful = ExportAsLp(ExportModel);
    186               break;
    187 
    188             case ".mps":
    189               exportSuccessful = ExportAsMps(ExportModel);
    190               break;
    191 
    192             case ".prototxt":
    193               exportSuccessful = ExportAsProto(ExportModel, ProtoWriteFormat.ProtoText);
    194               break;
    195 
    196             case ".bin": // remove file extension as it is added by OR-Tools
    197               exportSuccessful = ExportAsProto(Path.ChangeExtension(ExportModel, null));
    198               break;
    199 
    200             default:
    201               throw new NotSupportedException("File format selected to export model is not supported.");
    202           }
    203         }
    204 
    205         // TODO: show warning if file export didn't work (if exportSuccessful is false)
    206 
    207171        resultStatus = (ResultStatus)solver.Solve(parameters);
    208172      }
     
    211175
    212176      problemDefinition.Analyze(solver, results);
    213       results.AddOrUpdateResult("ResultStatus", new EnumValue<ResultStatus>(resultStatus));
    214       results.AddOrUpdateResult("BestObjectiveValue", new DoubleValue(objectiveValue ?? double.NaN));
    215 
    216       if (solver.IsMIP()) {
     177
     178      if (solver.IsMip()) {
    217179        var objectiveBound = solver.Objective()?.BestBound();
    218180        var absoluteGap = objectiveValue.HasValue && objectiveBound.HasValue
     
    224186          : (double?)null;
    225187
     188        if (resultStatus == ResultStatus.Optimal && absoluteGap.HasValue && !absoluteGap.Value.IsAlmost(0)) {
     189          resultStatus = ResultStatus.OptimalWithinTolerance;
     190        }
     191
    226192        results.AddOrUpdateResult("BestObjectiveBound", new DoubleValue(objectiveBound ?? double.NaN));
    227193        results.AddOrUpdateResult("AbsoluteGap", new DoubleValue(absoluteGap ?? double.NaN));
     
    229195      }
    230196
     197      results.AddOrUpdateResult("ResultStatus", new EnumValue<ResultStatus>(resultStatus));
     198      results.AddOrUpdateResult("BestObjectiveValue", new DoubleValue(objectiveValue ?? double.NaN));
     199
    231200      results.AddOrUpdateResult("NumberOfConstraints", new IntValue(solver.NumConstraints()));
    232201      results.AddOrUpdateResult("NumberOfVariables", new IntValue(solver.NumVariables()));
    233202
    234       if (solver.IsMIP() && solver.Nodes() >= 0) {
     203      if (solver.IsMip() && solver.Nodes() >= 0) {
    235204        results.AddOrUpdateResult(nameof(solver.Nodes), new DoubleValue(solver.Nodes()));
    236205      }
     
    243212    }
    244213
    245     protected virtual Solver CreateSolver(OptimizationProblemType optimizationProblemType, string libraryName = null) {
     214    protected virtual Solver CreateSolver(Solver.OptimizationProblemType optimizationProblemType, string libraryName = null) {
    246215      if (!string.IsNullOrEmpty(libraryName) && !File.Exists(libraryName)) {
    247216        var paths = new List<string> {
     
    256225
    257226      try {
    258         solver = new Solver(Name, (int)optimizationProblemType, libraryName ?? string.Empty);
     227        solver = new Solver(Name, optimizationProblemType, libraryName ?? string.Empty);
    259228      } catch {
    260229        throw new InvalidOperationException($"Could not create {optimizationProblemType}.");
Note: See TracChangeset for help on using the changeset viewer.