1 // Copyright 2010-2021 Google LLC 2 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // you may not use this file except in compliance with the License. 4 // You may obtain a copy of the License at 5 // 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 #ifndef OR_TOOLS_LINEAR_SOLVER_MODEL_EXPORTER_H_ 15 #define OR_TOOLS_LINEAR_SOLVER_MODEL_EXPORTER_H_ 16 17 #include <string> 18 #include <vector> 19 20 #include "absl/status/statusor.h" 21 #include "absl/strings/str_format.h" 22 #include "ortools/base/hash.h" 23 #include "ortools/base/macros.h" 24 #include "ortools/linear_solver/linear_solver.pb.h" 25 26 namespace operations_research { 27 28 /// Export options. 29 struct MPModelExportOptions { 30 MPModelExportOptions() {} 31 32 /// Obfuscates variable and constraint names. 33 bool obfuscate = false; 34 /// Whether to log invalid variable and constraint names. 35 bool log_invalid_names = false; 36 37 /** 38 * For .lp files only. Decides whether variables unused in the objective and 39 * constraints are shown when exported to a file. 40 */ 41 bool show_unused_variables = false; 42 43 /** 44 * For .lp files only. Maximum line length in exported files. The default 45 * was chosen so that SCIP can read the files. 46 */ 47 int max_line_length = 10000; 48 }; 49 50 /** 51 * Outputs the current model (variables, constraints, objective) as a string 52 * encoded in the so-called "CPLEX LP file format" as generated by SCIP. 53 * The LP file format is easily readable by a human. 54 * 55 * Returns false if some error has occurred during execution. 56 * The validity of names is automatically checked. If a variable name or a 57 * constraint name is invalid or non-existent, a new valid name is 58 * automatically generated. 59 * 60 * If 'obfuscated' is true, the variable and constraint names of proto_ 61 * are not used. Variable and constraint names of the form "V12345" 62 * and "C12345" are used instead. 63 * 64 * For more information about the different LP file formats: 65 * http://lpsolve.sourceforge.net/5.5/lp-format.htm 66 * The following give a reasonable idea of the CPLEX LP file format: 67 * http://lpsolve.sourceforge.net/5.5/CPLEX-format.htm 68 * http://tinyurl.com/cplex-lp-format 69 * http://www.gurobi.com/documentation/5.1/reference-manual/node871 70 */ 71 absl::StatusOr<std::string> ExportModelAsLpFormat( 72 const MPModelProto& model, 73 const MPModelExportOptions& options = MPModelExportOptions()); 74 75 /** 76 * Outputs the current model (variables, constraints, objective) as a string 77 * encoded in MPS file format, using the "free" MPS format. 78 * 79 * Returns false if some error has occurred during execution. Models with 80 * maximization objectives trigger an error, because MPS can encode only 81 * minimization problems. 82 * 83 * The validity of names is automatically checked. If a variable name or a 84 * constraint name is invalid or non-existent, a new valid name is 85 * automatically generated. 86 * 87 * Name validity and obfuscation works exactly as in ExportModelAsLpFormat(). 88 * 89 * For more information about the MPS format: 90 * http://en.wikipedia.org/wiki/MPS_(format) 91 * A close-to-original description coming from OSL: 92 * http://tinyurl.com/mps-format-by-osl 93 * A recent description from CPLEX: 94 * http://tinyurl.com/mps-format-by-cplex 95 * CPLEX extensions: 96 * http://tinyurl.com/mps-extensions-by-cplex 97 * Gurobi's description: 98 * http://www.gurobi.com/documentation/5.1/reference-manual/node869 99 */ 100 absl::StatusOr<std::string> ExportModelAsMpsFormat( 101 const MPModelProto& model, 102 const MPModelExportOptions& options = MPModelExportOptions()); 103 104 } // namespace operations_research 105 106 #endif // OR_TOOLS_LINEAR_SOLVER_MODEL_EXPORTER_H_ 107