Changeset 16582 for branches/2931_OR-Tools_LP_MIP/HeuristicLab.MathematicalOptimization/3.3/LinearProgramming/Algorithms/Solvers/Base/LinearSolver.cs
- Timestamp:
- 01/29/19 17:45:06 (5 years ago)
- 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 34 34 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 35 35 36 namespace HeuristicLab. MathematicalOptimization.LinearProgramming {36 namespace HeuristicLab.ExactOptimization.LinearProgramming { 37 37 38 38 [StorableClass] … … 49 49 public LinearSolver() { 50 50 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))); 52 53 Parameters.Add(solverSpecificParametersParam = 53 54 new FixedValueParameter<TextValue>(nameof(SolverSpecificParameters), new TextValue())); … … 65 66 } 66 67 67 public double DualTolerance { get; set; } = MPSolverParameters.kDefaultDualTolerance; 68 69 public string ExportModel { get; set; } 68 public double DualTolerance { get; set; } = SolverParameters.DefaultDualTolerance; 70 69 71 70 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; 79 77 80 78 public ProblemType ProblemType { … … 83 81 } 84 82 85 public double RelativeGapTolerance { get; set; } = MPSolverParameters.kDefaultRelativeMipGap;86 83 public IValueParameter<EnumValue<ProblemType>> ProblemTypeParameter => problemTypeParam; 84 public double RelativeGapTolerance { get; set; } = SolverParameters.DefaultRelativeMipGap; 87 85 public bool Scaling { get; set; } 88 86 … … 92 90 } 93 91 92 public IFixedValueParameter<TextValue> SolverSpecificParametersParameter => solverSpecificParametersParam; 94 93 public virtual bool SupportsPause => true; 95 94 public virtual bool SupportsStop => true; 96 95 public virtual TimeSpan TimeLimit { get; set; } = TimeSpan.Zero; 97 protected virtual OptimizationProblemType OptimizationProblemType { get; }98 96 99 97 public override IDeepCloneable Clone(Cloner cloner) => new LinearSolver(this, cloner); … … 118 116 119 117 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; 129 126 130 127 public bool InterruptSolve() => solver?.InterruptSolve() ?? false; … … 135 132 } 136 133 137 public virtual void Solve(ILinearPro grammingProblemDefinition problemDefintion, ref TimeSpan executionTime,134 public virtual void Solve(ILinearProblemDefinition problemDefintion, 138 135 ResultCollection results, CancellationToken cancellationToken) => 139 Solve(problemDefintion, re f executionTime, results);140 141 public virtual void Solve(ILinearPro grammingProblemDefinition problemDefinition, ref TimeSpan executionTime,136 Solve(problemDefintion, results); 137 138 public virtual void Solve(ILinearProblemDefinition problemDefinition, 142 139 ResultCollection results) => 143 140 Solve(problemDefinition, results, TimeLimit); 144 141 145 public virtual void Solve(ILinearPro grammingProblemDefinition problemDefinition, ResultCollection results,142 public virtual void Solve(ILinearProblemDefinition problemDefinition, ResultCollection results, 146 143 TimeSpan timeLimit) { 147 144 if (solver == null) { … … 158 155 ResultStatus resultStatus; 159 156 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)); 171 167 172 168 if (!solver.SetSolverSpecificParametersAsString(SolverSpecificParameters)) 173 169 throw new ArgumentException("Solver specific parameters could not be set."); 174 170 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-Tools197 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 207 171 resultStatus = (ResultStatus)solver.Solve(parameters); 208 172 } … … 211 175 212 176 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()) { 217 179 var objectiveBound = solver.Objective()?.BestBound(); 218 180 var absoluteGap = objectiveValue.HasValue && objectiveBound.HasValue … … 224 186 : (double?)null; 225 187 188 if (resultStatus == ResultStatus.Optimal && absoluteGap.HasValue && !absoluteGap.Value.IsAlmost(0)) { 189 resultStatus = ResultStatus.OptimalWithinTolerance; 190 } 191 226 192 results.AddOrUpdateResult("BestObjectiveBound", new DoubleValue(objectiveBound ?? double.NaN)); 227 193 results.AddOrUpdateResult("AbsoluteGap", new DoubleValue(absoluteGap ?? double.NaN)); … … 229 195 } 230 196 197 results.AddOrUpdateResult("ResultStatus", new EnumValue<ResultStatus>(resultStatus)); 198 results.AddOrUpdateResult("BestObjectiveValue", new DoubleValue(objectiveValue ?? double.NaN)); 199 231 200 results.AddOrUpdateResult("NumberOfConstraints", new IntValue(solver.NumConstraints())); 232 201 results.AddOrUpdateResult("NumberOfVariables", new IntValue(solver.NumVariables())); 233 202 234 if (solver.IsM IP() && solver.Nodes() >= 0) {203 if (solver.IsMip() && solver.Nodes() >= 0) { 235 204 results.AddOrUpdateResult(nameof(solver.Nodes), new DoubleValue(solver.Nodes())); 236 205 } … … 243 212 } 244 213 245 protected virtual Solver CreateSolver( OptimizationProblemType optimizationProblemType, string libraryName = null) {214 protected virtual Solver CreateSolver(Solver.OptimizationProblemType optimizationProblemType, string libraryName = null) { 246 215 if (!string.IsNullOrEmpty(libraryName) && !File.Exists(libraryName)) { 247 216 var paths = new List<string> { … … 256 225 257 226 try { 258 solver = new Solver(Name, (int)optimizationProblemType, libraryName ?? string.Empty);227 solver = new Solver(Name, optimizationProblemType, libraryName ?? string.Empty); 259 228 } catch { 260 229 throw new InvalidOperationException($"Could not create {optimizationProblemType}.");
Note: See TracChangeset
for help on using the changeset viewer.