Skip to content

Commit

Permalink
more work on mps reader; follow include files renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
lperron committed Jul 28, 2023
1 parent a4b5906 commit 422a7b2
Show file tree
Hide file tree
Showing 11 changed files with 444 additions and 339 deletions.
8 changes: 2 additions & 6 deletions ortools/lp_data/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ cc_library(

cc_library(
name = "mps_reader_template",
srcs = ["mps_reader_template.cc"],
hdrs = ["mps_reader_template.h"],
deps = [
"//ortools/base",
Expand All @@ -304,6 +305,7 @@ cc_library(
"//ortools/util:filelineiter",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/container:inlined_vector",
"@com_google_absl//absl/log",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
Expand All @@ -317,22 +319,16 @@ cc_library(
hdrs = ["mps_reader.h"],
copts = SAFE_FP_CODE,
deps = [
":base",
":lp_data",
":lp_print_utils",
":mps_reader_template",
"//ortools/base",
"//ortools/base:file",
"//ortools/base:hash",
"//ortools/base:map_util",
"//ortools/base:protobuf_util",
"//ortools/base:status_builder",
"//ortools/base:status_macros",
"//ortools/base:strong_vector",
"//ortools/linear_solver:linear_solver_cc_proto",
"//ortools/util:filelineiter",
"@com_google_absl//absl/container:btree",
"@com_google_absl//absl/container:node_hash_set",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
],
Expand Down
2 changes: 1 addition & 1 deletion ortools/lp_data/lp_print_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "ortools/base/integral_types.h"
#include "ortools/base/logging.h"
#include "ortools/base/types.h"
#include "ortools/lp_data/lp_types.h"
#include "ortools/util/rational_approximation.h"

Expand Down
2 changes: 1 addition & 1 deletion ortools/lp_data/lp_print_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#include "absl/strings/str_format.h"
#include "absl/strings/string_view.h"
#include "ortools/base/integral_types.h"
#include "ortools/base/types.h"
#include "ortools/lp_data/lp_types.h"

namespace operations_research {
Expand Down
2 changes: 1 addition & 1 deletion ortools/lp_data/lp_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
#include <type_traits>
#include <vector>

#include "ortools/base/integral_types.h"
#include "ortools/base/logging.h"
#include "ortools/base/strong_vector.h"
#include "ortools/base/types.h"
#include "ortools/util/bitset.h"
#include "ortools/util/strong_integers.h"

Expand Down
2 changes: 1 addition & 1 deletion ortools/lp_data/matrix_scaler.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@
#include <string>
#include <vector>

#include "ortools/base/integral_types.h"
#include "ortools/base/macros.h"
#include "ortools/base/types.h"
#include "ortools/glop/parameters.pb.h"
#include "ortools/glop/revised_simplex.h"
#include "ortools/glop/status.h"
Expand Down
9 changes: 8 additions & 1 deletion ortools/lp_data/mps_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@
#include <vector>

#include "absl/container/btree_set.h"
#include "absl/container/flat_hash_map.h"
#include "absl/log/check.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "ortools/base/logging.h"
#include "ortools/base/protobuf_util.h"
#include "ortools/base/status_builder.h"
#include "ortools/base/status_macros.h"
#include "ortools/linear_solver/linear_solver.pb.h"
#include "ortools/lp_data/lp_data.h"
#include "ortools/lp_data/lp_types.h"
#include "ortools/lp_data/mps_reader_template.h"

Expand Down
16 changes: 1 addition & 15 deletions ortools/lp_data/mps_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,13 @@
#ifndef OR_TOOLS_LP_DATA_MPS_READER_H_
#define OR_TOOLS_LP_DATA_MPS_READER_H_

#include <limits>
#include <memory>
#include <string>
#include <vector>

#include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h"
#include "absl/flags/declare.h"
#include "absl/base/attributes.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/numbers.h"
#include "ortools/base/hash.h"
#include "ortools/base/logging.h"
#include "ortools/base/macros.h" // for DISALLOW_COPY_AND_ASSIGN, NULL
#include "ortools/base/map_util.h"
#include "ortools/base/protobuf_util.h"
#include "ortools/base/status_macros.h"
#include "ortools/linear_solver/linear_solver.pb.h"
#include "ortools/lp_data/lp_data.h"
#include "ortools/lp_data/lp_types.h"
#include "ortools/util/filelineiter.h"

namespace operations_research {
namespace glop {
Expand Down
122 changes: 122 additions & 0 deletions ortools/lp_data/mps_reader_template.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// Copyright 2010-2022 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "ortools/lp_data/mps_reader_template.h"

#include <algorithm>
#include <cmath>
#include <cstdint>
#include <limits>
#include <string>
#include <vector>

#include "absl/log/check.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/match.h"
#include "absl/strings/numbers.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_split.h"
#include "absl/strings/string_view.h"
#include "absl/strings/strip.h"
#include "ortools/base/logging.h"
#include "ortools/base/status_macros.h"

namespace operations_research::internal {
namespace {

// Starting positions of each of the fields for fixed format.
static constexpr int kFieldStartPos[kNumMpsFields] = {1, 4, 14, 24, 39, 49};

// Lengths of each of the fields for fixed format.
static constexpr int kFieldLength[kNumMpsFields] = {2, 8, 8, 12, 8, 12};

// Positions where there should be spaces for fixed format.
static constexpr int kSpacePos[12] = {12, 13, 22, 23, 36, 37,
38, 47, 48, 61, 62, 63};

} // namespace

bool MPSLineInfo::IsFixedFormat() const {
constexpr int kMaxLineSize =
kFieldStartPos[kNumMpsFields - 1] + kFieldLength[kNumMpsFields - 1];
// Note that `line_` already has been stripped of trailing white spaces.
const int line_size = line_.size();
if (line_size > kMaxLineSize) return false;
for (const int i : kSpacePos) {
if (i >= line_size) break;
if (line_[i] != ' ') return false;
}
return true;
}

absl::Status MPSLineInfo::SplitLineIntoFields(const MPSSectionId section) {
if (free_form_) {
absl::string_view remaining_line = absl::StripLeadingAsciiWhitespace(line_);
// Although using `fields_ = StrSplit(line, ByAnyChar(" \t))` is shorter to
// write, this explicit loop, and checking the `kNumMpsFields` is
// significantly faster.
while (!remaining_line.empty()) {
if (fields_.size() == kNumMpsFields) {
return InvalidArgumentError("Found too many fields.");
}
// find_first_of() returns npos for "not found". substr() interprets
// len==npos as end of string.
const int pos = remaining_line.find_first_of(" \t");
fields_.push_back(remaining_line.substr(0, pos));
if (pos == absl::string_view::npos) {
// substr() will throw an exception if the start is npos.
break;
}
remaining_line =
absl::StripLeadingAsciiWhitespace(remaining_line.substr(pos));
}
} else {
// Note: the name should also comply with the fixed format guidelines
// (maximum 8 characters) but in practice there are many problem files in
// the netlib archive that are in fixed format and have a long name. We
// choose to ignore these cases and treat them as fixed format anyway.
if (section != MPSSectionId::kName && !IsFixedFormat()) {
return InvalidArgumentError("Line is not in fixed format.");
}
const int line_size = line_.size();
for (int i = 0; i < kNumMpsFields; ++i) {
if (kFieldStartPos[i] >= line_size) break;
fields_.push_back(absl::StripTrailingAsciiWhitespace(
line_.substr(kFieldStartPos[i], kFieldLength[i])));
}
}
return absl::OkStatus();
}

absl::string_view MPSLineInfo::GetFirstWord() const {
return line_.substr(0, line_.find(' '));
}

bool MPSLineInfo::IsCommentOrBlank() const {
// Trailing whitespace has already been trimmed, so a blank line has become
// empty.
return (line_.empty() || line_[0] == '*');
}

absl::Status MPSLineInfo::InvalidArgumentError(
absl::string_view error_message) const {
return AppendLineToError(absl::InvalidArgumentError(error_message));
}

absl::Status MPSLineInfo::AppendLineToError(const absl::Status& status) const {
return util::StatusBuilder(status).SetAppend()
<< " Line " << line_num_ << ": \"" << line_ << "\".";
}

} // namespace operations_research::internal
Loading

0 comments on commit 422a7b2

Please sign in to comment.