Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ortools/base/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ or_cc_test(
":recordio",
":recordio_test_cc_proto",
":temp_file",
"@abseil-cpp//absl/base:core_headers",
"//ortools/port:attributes",
"@abseil-cpp//absl/log:check",
],
)
Expand Down
6 changes: 3 additions & 3 deletions ortools/base/recordio_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include <optional>
#include <string>

#include "absl/base/attributes.h"
#include "absl/log/check.h"
#include "gtest/gtest.h"
#include "ortools/base/file.h"
Expand All @@ -29,14 +28,15 @@
#include "ortools/base/options.h"
#include "ortools/base/recordio_test.pb.h"
#include "ortools/base/temp_file.h"
#include "ortools/port/attributes.h"

namespace ORTOOLS_RECORDIO_NAMESPACE {
namespace {

// Parameters for RecordReaderInvalidPayloadTest.
struct RecordReaderInvalidPayloadTestParams {
// Name of the test; must be snake_case.
std::string name ABSL_REQUIRE_EXPLICIT_INIT;
std::string name ORTOOLS_REQUIRE_EXPLICIT_INIT;

// All tests should fail, but we also want to make sure the test code itself
// is correct enough so that valid data don't fail as well.%
Expand All @@ -48,7 +48,7 @@ struct RecordReaderInvalidPayloadTestParams {

// Change the value of RecordWriter::set_use_compression() used to create the
// valid data.
bool use_compression ABSL_REQUIRE_EXPLICIT_INIT;
bool use_compression ORTOOLS_REQUIRE_EXPLICIT_INIT;

// Function to change the valid data and make them invalid (unless
// expect_failure is false).
Expand Down
2 changes: 1 addition & 1 deletion ortools/glop/initial_basis.cc
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ void InitialBasis::GetMarosBasis(ColIndex num_cols, RowToColMapping* basis) {
residual_pattern.Reset(num_rows, num_cols);
for (ColIndex col(0); col < first_slack; ++col) {
for (const SparseColumn::Entry e : compact_matrix_.column(col)) {
if (available[RowToColIndex(e.row())] && available[col]) {
if (available[first_slack + RowToColIndex(e.row())] && available[col]) {
residual_pattern.AddEntry(e.row(), col);
}
}
Expand Down
2 changes: 1 addition & 1 deletion ortools/julia/ORTools.jl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
This is the
[MathOptInterface.jl](https://github.com/jump-dev/MathOptInterface.jl) Julia
wrapper for Google's
[MathOpt](https://developers.google.com/optimization/math_opt).
[MathOpt](https://developers.google.com/optimization/math_opt) and CP-SAT.
38 changes: 38 additions & 0 deletions ortools/julia/ORToolsGenerated.jl/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# ORToolsGenerated

This package only contains generated code for use by `ORTools.jl`.

For now, there is a single type of generated files: those from
[ProtoBuf.jl](https://github.com/JuliaIO/ProtoBuf.jl), the Julia compiler for
[Protocol Buffers](https://protobuf.dev/).

## For users

You probably do not need to use this package directly in your application. The
MathOptInterface wrapper in `ORTools.jl` should provide you with all the
required features. This package is likely to be installed as a dependency of
`ORTools.jl`

## For maintainers

Human and agent-authored commits in `src` are discouraged, apart from the entry
point `ORToolsGenerated.jl` to include the relevant generated files (which
should not change regularly if at all).

To regenerate the files in `src/genproto`, use the script in
`scripts/run_gen_pb_docker.sh`. Technically, it will use Docker or Podman with a
predefined LTS version of Julia to run `ProtoBuf.jl` and update all the
generated files. You can then proceed with making a new release of
`ORToolsGenerated.jl`.

The script requires a single argument, the path to the `ORToolsGenerated.jl`
package (where the output is written). For instance, if running from the
`scripts` folder, use:

```
bash run_gen_pb_docker.sh ../
```

**Warning:** This script uses the proto files from the installed version of
`ORTools_jll`. You must therefore update the JLL package before running the
script.
5 changes: 0 additions & 5 deletions ortools/julia/ORToolsGenerated.jl/scripts/gen_pb.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
# Run this script as:
# hgd / g4d ...
# cd ortools/julia/ORToolsGenerated.jl/scripts/
# julia gen_pb.jl

using ORTools_jll
using ProtoBuf
using Pkg
Expand Down
12 changes: 8 additions & 4 deletions ortools/linear_solver/proto_solver/sat_proto_solver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ MPSolutionResponse TimeLimitResponse(SolverLogger& logger) {
std::optional<MPSolutionResponse> ConvertComponentsToCpModelProto(
const sat::SatParameters& params, int num_clamped,
const sat::MPModelProtoWrapper& mp_model_wrapper,
absl::Span<const double> pre_var_scaling,
std::vector<sat::MipVariableState>* states, sat::CpModelProto* cp_model,
SolverLogger* logger) {
const int num_variables = mp_model_wrapper.num_variables();
Expand Down Expand Up @@ -382,8 +383,11 @@ std::optional<MPSolutionResponse> ConvertComponentsToCpModelProto(
final_shift_stats.max_shift =
std::max(final_shift_stats.max_shift, tum_stats.shift_stats.max_shift);
sat::LogShiftStats(final_shift_stats, logger);
sat::DisplayConstraintScalingStats(
params, scaler, num_clamped + num_extra_clamp, num_small_domains, logger);
const double max_var_unscaling_error = sat::ComputeMaxVariableUnscalingError(
mp_model_wrapper, *states, *cp_model, pre_var_scaling);
sat::DisplayConstraintScalingStats(params, max_var_unscaling_error, scaler,
num_clamped + num_extra_clamp,
num_small_domains, logger);
return std::nullopt;
}

Expand Down Expand Up @@ -607,8 +611,8 @@ MPSolutionResponse SatSolveProtoInternal(
sat::CpModelProto cp_model;
if (std::optional<MPSolutionResponse> error_response =
ConvertComponentsToCpModelProto(params, num_clamped,
*mp_model_wrapper, &states, &cp_model,
logger);
*mp_model_wrapper, var_scaling,
&states, &cp_model, logger);
error_response.has_value()) {
return *std::move(error_response);
}
Expand Down
14 changes: 3 additions & 11 deletions ortools/routing/search.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1274,17 +1274,9 @@ bool GlobalCheapestInsertionFilteredHeuristic::InsertPairs(
&delivery_to_entries);
}
}
// In case all pairs could not be inserted, pushing uninserted ones to the
// next bucket.
for (auto it = pair_indices_to_insert.begin(),
last = pair_indices_to_insert.end();
it != last;) {
if (pair_is_performed(*it)) {
pair_indices_to_insert.erase(it++);
} else {
++it;
}
}
// Keep uninserted pairs as we move to the next bucket.
absl::erase_if(pair_indices_to_insert,
[&](int p) { return pair_is_performed(p); });
}
return true;
}
Expand Down
46 changes: 21 additions & 25 deletions ortools/sat/2d_rectangle_presolve.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "ortools/graph/strongly_connected_components.h"
#include "ortools/sat/diffn_util.h"
#include "ortools/sat/integer_base.h"
#include "ortools/sat/sat_status.h"

namespace operations_research {
namespace sat {
Expand Down Expand Up @@ -122,13 +123,13 @@ std::vector<Rectangle> FindSpacesThatCannotBeOccupied(

} // namespace

bool PresolveFixed2dRectangles(
ModificationStatus PresolveFixed2dRectangles(
absl::Span<const RectangleInRange> non_fixed_boxes,
std::vector<Rectangle>* fixed_boxes) {
// This implementation compiles a set of areas that cannot be occupied by any
// item, then calls ReduceNumberofBoxes() to use these areas to minimize
// `fixed_boxes`.
bool changed = false;
ModificationStatus status = kUnchanged;

DCHECK(FindPartialRectangleIntersections(*fixed_boxes).empty());
IntegerValue original_area = 0;
Expand All @@ -148,9 +149,7 @@ bool PresolveFixed2dRectangles(
// reduce the size of large trivial instances.
if (fixed_boxes->size() > 1) {
std::vector<Rectangle> empty_vec;
if (ReduceNumberofBoxesGreedy(fixed_boxes, &empty_vec)) {
changed = true;
}
status |= ReduceNumberofBoxesGreedy(fixed_boxes, &empty_vec);
}

IntegerValue min_x_size = std::numeric_limits<IntegerValue>::max();
Expand All @@ -177,25 +176,25 @@ bool PresolveFixed2dRectangles(
DCHECK_GT(rectangle.SizeY(), 0);
if (rectangle.x_min < bounding_box.x_min) {
rectangle.x_min = bounding_box.x_min;
changed = true;
status = kModified;
}
if (rectangle.x_max > bounding_box.x_max) {
rectangle.x_max = bounding_box.x_max;
changed = true;
status = kModified;
}
if (rectangle.y_min < bounding_box.y_min) {
rectangle.y_min = bounding_box.y_min;
changed = true;
status = kModified;
}
if (rectangle.y_max > bounding_box.y_max) {
rectangle.y_max = bounding_box.y_max;
changed = true;
status = kModified;
}
if (rectangle.SizeX() <= 0 || rectangle.SizeY() <= 0) {
// The whole rectangle was outside of the domain, remove it.
std::swap(rectangle, (*fixed_boxes)[fixed_boxes->size() - 1]);
fixed_boxes->resize(fixed_boxes->size() - 1);
changed = true;
status = kModified;
continue;
} else {
new_size++;
Expand Down Expand Up @@ -224,14 +223,10 @@ bool PresolveFixed2dRectangles(
//
// Since fewer turns mean fewer edges, this should be a good way to reduce the
// number of boxes.
if (ReduceNumberofBoxesGreedy(fixed_boxes, &optional_boxes)) {
changed = true;
}
status |= ReduceNumberofBoxesGreedy(fixed_boxes, &optional_boxes);
const int num_after_first_pass = fixed_boxes->size();
if (ReduceNumberOfBoxesExactMandatory(fixed_boxes, &optional_boxes)) {
changed = true;
}
if (changed && VLOG_IS_ON(1)) {
status |= ReduceNumberOfBoxesExactMandatory(fixed_boxes, &optional_boxes);
if (status.modified() && VLOG_IS_ON(1)) {
IntegerValue area = 0;
for (const Rectangle& r : *fixed_boxes) {
area += r.Area();
Expand All @@ -251,7 +246,7 @@ bool PresolveFixed2dRectangles(
RenderDot(bounding_box,
optional_boxes)));
}
return changed;
return status;
}

namespace {
Expand Down Expand Up @@ -303,8 +298,9 @@ struct Edge {
};
} // namespace

bool ReduceNumberofBoxesGreedy(std::vector<Rectangle>* mandatory_rectangles,
std::vector<Rectangle>* optional_rectangles) {
ModificationStatus ReduceNumberofBoxesGreedy(
std::vector<Rectangle>* mandatory_rectangles,
std::vector<Rectangle>* optional_rectangles) {
// The current implementation just greedily merges rectangles that share an
// edge.
std::deque<Rectangle> rectangle_storage;
Expand Down Expand Up @@ -426,7 +422,7 @@ bool ReduceNumberofBoxesGreedy(std::vector<Rectangle>* mandatory_rectangles,
}
*optional_rectangles = std::move(new_rectangles);
}
return changed_mandatory;
return ModificationStatus(changed_mandatory);
}

Neighbours BuildNeighboursGraph(absl::Span<const Rectangle> rectangles) {
Expand Down Expand Up @@ -1229,10 +1225,10 @@ std::vector<Rectangle> CutShapeIntoRectangles(const SingleShape& shape) {
return result;
}

bool ReduceNumberOfBoxesExactMandatory(
ModificationStatus ReduceNumberOfBoxesExactMandatory(
std::vector<Rectangle>* mandatory_rectangles,
std::vector<Rectangle>* optional_rectangles) {
if (mandatory_rectangles->empty()) return false;
if (mandatory_rectangles->empty()) return kUnchanged;
std::vector<Rectangle> result = *mandatory_rectangles;
std::vector<Rectangle> new_optional_rectangles = *optional_rectangles;

Expand Down Expand Up @@ -1292,10 +1288,10 @@ bool ReduceNumberOfBoxesExactMandatory(

// It is possible that the algorithm actually increases the number of boxes.
// See the "Problematic2" test.
if (result.size() >= mandatory_rectangles->size()) return false;
if (result.size() >= mandatory_rectangles->size()) return kUnchanged;
mandatory_rectangles->swap(result);
optional_rectangles->swap(new_optional_rectangles);
return true;
return kModified;
}

Disjoint2dPackingResult DetectDisjointRegionIn2dPacking(
Expand Down
12 changes: 7 additions & 5 deletions ortools/sat/2d_rectangle_presolve.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "absl/types/span.h"
#include "ortools/sat/diffn_util.h"
#include "ortools/sat/integer_base.h"
#include "ortools/sat/sat_status.h"
#include "ortools/sat/util.h"

namespace operations_research {
Expand All @@ -35,7 +36,7 @@ namespace sat {
// uses "equivalent" in the sense that a placement of the non-fixed boxes will
// be non-overlapping with all other boxes if and only if it was with the
// original set of fixed boxes too.
bool PresolveFixed2dRectangles(
ModificationStatus PresolveFixed2dRectangles(
absl::Span<const RectangleInRange> non_fixed_boxes,
std::vector<Rectangle>* fixed_boxes);

Expand Down Expand Up @@ -112,18 +113,19 @@ Disjoint2dPackingResult DetectDisjointRegionIn2dPacking(
//
// The function updates the vector of `mandatory_rectangles` with `R` and
// `optional_rectangles` with `optional_rectangles \setdiff R`. It returns
// true if `mandatory_rectangles` was updated.
// `kModified` if `mandatory_rectangles` was updated.
//
// This function uses a greedy algorithm that merges rectangles that share an
// edge.
bool ReduceNumberofBoxesGreedy(std::vector<Rectangle>* mandatory_rectangles,
std::vector<Rectangle>* optional_rectangles);
ModificationStatus ReduceNumberofBoxesGreedy(
std::vector<Rectangle>* mandatory_rectangles,
std::vector<Rectangle>* optional_rectangles);

// Same as above, but this implementation returns the optimal solution in
// minimizing the number of boxes if `optional_rectangles` is empty. On the
// other hand, its handling of optional boxes is rather limited. It simply fills
// the holes in the mandatory boxes with optional boxes, if possible.
bool ReduceNumberOfBoxesExactMandatory(
ModificationStatus ReduceNumberOfBoxesExactMandatory(
std::vector<Rectangle>* mandatory_rectangles,
std::vector<Rectangle>* optional_rectangles);

Expand Down
11 changes: 6 additions & 5 deletions ortools/sat/2d_rectangle_presolve_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "ortools/sat/2d_orthogonal_packing_testing.h"
#include "ortools/sat/diffn_util.h"
#include "ortools/sat/integer_base.h"
#include "ortools/sat/sat_status.h"

namespace operations_research {
namespace sat {
Expand Down Expand Up @@ -90,7 +91,7 @@ TEST(RectanglePresolve, Basic) {
.x_size = 5,
.y_size = 5});

EXPECT_TRUE(PresolveFixed2dRectangles(input_in_range, &input));
EXPECT_EQ(PresolveFixed2dRectangles(input_in_range, &input), kModified);
EXPECT_EQ(input.size(), 1);
}

Expand All @@ -104,7 +105,7 @@ TEST(RectanglePresolve, Trim) {
.x_size = 5,
.y_size = 5});

EXPECT_TRUE(PresolveFixed2dRectangles(input_in_range, &input));
EXPECT_EQ(PresolveFixed2dRectangles(input_in_range, &input), kModified);
EXPECT_THAT(input, ElementsAre(Rectangle{
.x_min = 1, .x_max = 5, .y_min = 1, .y_max = 5}));
}
Expand All @@ -119,7 +120,7 @@ TEST(RectanglePresolve, FillBoundingBoxEdge) {
.x_size = 5,
.y_size = 5});

EXPECT_TRUE(PresolveFixed2dRectangles(input_in_range, &input));
EXPECT_EQ(PresolveFixed2dRectangles(input_in_range, &input), kModified);
EXPECT_THAT(input, ElementsAre(Rectangle{
.x_min = 0, .x_max = 5, .y_min = 0, .y_max = 5}));
}
Expand All @@ -144,7 +145,7 @@ TEST(RectanglePresolve, UseAreaNotOccupiable) {
.x_size = 5,
.y_size = 5});

EXPECT_TRUE(PresolveFixed2dRectangles(input_in_range, &input));
EXPECT_EQ(PresolveFixed2dRectangles(input_in_range, &input), kModified);
EXPECT_THAT(input, ElementsAre(Rectangle{
.x_min = 15, .x_max = 25, .y_min = 0, .y_max = 10}));
}
Expand All @@ -159,7 +160,7 @@ TEST(RectanglePresolve, RemoveOutsideBB) {
.x_size = 5,
.y_size = 5});

EXPECT_TRUE(PresolveFixed2dRectangles(input_in_range, &input));
EXPECT_EQ(PresolveFixed2dRectangles(input_in_range, &input), kModified);
EXPECT_THAT(input, IsEmpty());
}

Expand Down
Loading
Loading