Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
lp_parser.cc
Go to the documentation of this file.
1// Copyright 2010-2024 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
15
16#include <memory>
17#include <string>
18
19#include "absl/memory/memory.h"
20#include "absl/status/status.h"
21#include "absl/status/statusor.h"
22#include "absl/strings/string_view.h"
25#include "ortools/base/path.h"
28#include "ortools/gscip/gscip.h"
31#include "ortools/math_opt/model.pb.h"
33#include "scip/def.h"
34#include "scip/scip_prob.h"
35
37namespace {
38
39absl::Status ScipConvertLpToMps(const std::string& lp_filename_in,
40 const std::string& mps_filename_out) {
41 ASSIGN_OR_RETURN(const std::unique_ptr<GScip> gscip, GScip::Create(""));
42 // Warning: puts gscip into an invalid state, but the underlying SCIP is fine.
44 SCIPreadProb(gscip->scip(), lp_filename_in.c_str(), "lp"));
45 RETURN_IF_SCIP_ERROR(SCIPwriteOrigProblem(
46 gscip->scip(), mps_filename_out.c_str(), "mps", /*genericnames=*/FALSE));
47 return absl::OkStatus();
48}
49
50} // namespace
51
52absl::StatusOr<ModelProto> ModelProtoFromLp(const absl::string_view lp_data) {
53 // Set up temporary files
54 const auto dir = absl::WrapUnique(TempPath::Create(TempPath::Local));
55 if (dir == nullptr) {
56 return absl::InternalError(
57 "creating temporary directory when parsing LP file failed");
58 }
59 const std::string lp_file = file::JoinPath(dir->path(), "model.lp");
61 const std::string mps_file = file::JoinPath(dir->path(), "model.mps");
62
63 // Do the conversion
64 RETURN_IF_ERROR(ScipConvertLpToMps(lp_file, mps_file))
65 << "failed to convert LP file with SCIP";
66 ASSIGN_OR_RETURN(const std::string mps_data,
69 ModelProto result, MpsToModelProto(mps_data),
70 _ << "failed to parse MPS (produced by SCIP from LP file)");
71 result.clear_name();
72 return result;
73}
74
75} // namespace operations_research::math_opt
#define ASSIGN_OR_RETURN(lhs, rexpr)
#define RETURN_IF_ERROR(expr)
static TempPath * Create(Location location)
Definition temp_path.cc:56
absl::StatusOr< std::string > GetContents(absl::string_view path, Options options)
Definition file.cc:191
absl::Status SetContents(absl::string_view filename, absl::string_view contents, Options options)
Definition file.cc:242
std::string JoinPath()
Definition path.h:82
Options Defaults()
Definition file.h:109
An object oriented wrapper for quadratic constraints in ModelStorage.
Definition gurobi_isv.cc:28
absl::StatusOr< ModelProto > MpsToModelProto(absl::string_view mps_data)
Converts a string with the contents of an MPS file into a ModelProto.
absl::StatusOr< ModelProto > ModelProtoFromLp(const absl::string_view lp_data)
Definition lp_parser.cc:52
#define RETURN_IF_SCIP_ERROR(x)
#define OR_ASSIGN_OR_RETURN3(lhs, rexpr, error_expression)