Skip to content

Commit

Permalink
Export examples/cpp/ from google3
Browse files Browse the repository at this point in the history
* Rework network_routing_sat
  • Loading branch information
Mizux committed Jun 30, 2023
1 parent 036b219 commit aa0e719
Show file tree
Hide file tree
Showing 32 changed files with 586 additions and 492 deletions.
559 changes: 315 additions & 244 deletions examples/cpp/BUILD.bazel

Large diffs are not rendered by default.

23 changes: 1 addition & 22 deletions examples/cpp/binpacking_2d_sat.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@
// too), and tries to fit all rectangles in the minimum numbers of bins (they
// have the size of the main rectangle.)

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

#include "absl/flags/flag.h"
#include "google/protobuf/text_format.h"
#include "ortools/base/commandlineflags.h"
#include "ortools/base/init_google.h"
#include "ortools/base/logging.h"
#include "ortools/packing/binpacking_2d_parser.h"
Expand All @@ -38,15 +35,13 @@ ABSL_FLAG(int, max_bins, 0,
"Maximum number of bins. The 0 default value implies the code will "
"use some heuristics to compute this number.");
ABSL_FLAG(bool, symmetry_breaking, true, "Use symmetry breaking constraints");
ABSL_FLAG(bool, global_area_constraint, false,
"Redundant constraint to link the global area covered");
ABSL_FLAG(bool, alternate_model, true,
"A different way to express the objective");

namespace operations_research {
namespace sat {

// Load a 2D binpacking problem and solve it.
// Load a 2D bin packing problem and solve it.
void LoadAndSolve(const std::string& file_name, int instance) {
packing::BinPacking2dParser parser;
if (!parser.Load2BPFile(file_name, instance)) {
Expand Down Expand Up @@ -139,20 +134,6 @@ void LoadAndSolve(const std::string& file_name, int instance) {
LOG(FATAL) << num_dimensions << " dimensions not supported.";
}

// Redundant constraint.
// The sum of areas in each bin is the sum of all items area.
if (absl::GetFlag(FLAGS_global_area_constraint)) {
LinearExpr sum_of_areas;
for (int item = 0; item < num_items; ++item) {
const int64_t item_area = problem.items(item).shapes(0).dimensions(0) *
problem.items(item).shapes(0).dimensions(1);
for (int b = 0; b < max_bins; ++b) {
sum_of_areas += item_to_bin[item][b] * item_area;
}
}
cp_model.AddEquality(sum_of_areas, sum_of_items_area);
}

if (absl::GetFlag(FLAGS_alternate_model)) {
const IntVar obj = cp_model.NewIntVar(Domain(trivial_lb, max_bins));
cp_model.Minimize(obj);
Expand Down Expand Up @@ -203,8 +184,6 @@ void LoadAndSolve(const std::string& file_name, int instance) {
// Setup parameters.
SatParameters parameters;
parameters.set_log_search_progress(true);
parameters.set_use_timetabling_in_no_overlap_2d(true);
parameters.set_use_energetic_reasoning_in_no_overlap_2d(true);

// Parse the --params flag.
if (!absl::GetFlag(FLAGS_params).empty()) {
Expand Down
4 changes: 3 additions & 1 deletion examples/cpp/constraint_programming_cp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

// Constraint programming example that shows how to use the API.

#include <vector>

#include "ortools/base/init_google.h"
#include "ortools/base/logging.h"
#include "ortools/constraint_solver/constraint_solver.h"
Expand Down Expand Up @@ -55,8 +57,8 @@ void RunConstraintProgrammingExample() {
} // namespace operations_research

int main(int argc, char** argv) {
google::InitGoogleLogging(argv[0]);
absl::SetFlag(&FLAGS_stderrthreshold, 0);
InitGoogle(argv[0], &argc, &argv, true);
operations_research::RunConstraintProgrammingExample();
return EXIT_SUCCESS;
}
1 change: 0 additions & 1 deletion examples/cpp/costas_array_sat.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//
// Costas Array Problem
//
// Finds an NxN matrix of 0s and 1s, with only one 1 per row,
Expand Down
5 changes: 3 additions & 2 deletions examples/cpp/course_scheduling.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <limits>
#include <utility>
#include <vector>
Expand Down Expand Up @@ -169,7 +170,7 @@ CourseSchedulingResult CourseSchedulingSolver::Solve(
if (!validation_status.ok()) {
result.set_solver_status(
CourseSchedulingResultStatus::SOLVER_MODEL_INVALID);
result.set_message(std::string(validation_status.message()));
result.set_message(validation_status.message());
return result;
}

Expand All @@ -184,7 +185,7 @@ CourseSchedulingResult CourseSchedulingSolver::Solve(
const auto verifier_status = VerifyCourseSchedulingResult(model, result);
if (!verifier_status.ok()) {
result.set_solver_status(CourseSchedulingResultStatus::ABNORMAL);
result.set_message(std::string(verifier_status.message()));
result.set_message(verifier_status.message());
}

return result;
Expand Down
2 changes: 1 addition & 1 deletion examples/cpp/course_scheduling.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace operations_research {
class CourseSchedulingSolver {
public:
CourseSchedulingSolver() : solve_for_rooms_(false) {}
virtual ~CourseSchedulingSolver() {}
virtual ~CourseSchedulingSolver() = default;

using ConflictPairs = absl::flat_hash_set<std::pair<int, int>>;

Expand Down
11 changes: 5 additions & 6 deletions examples/cpp/course_scheduling_run.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@

#include <cstdlib>

#include "absl/flags/parse.h"
#include "absl/flags/usage.h"
#include "examples/cpp/course_scheduling.h"
#include "examples/cpp/course_scheduling.pb.h"
#include "ortools/base/commandlineflags.h"
#include "ortools/base/file.h"
#include "ortools/base/helpers.h"
#include "ortools/base/init_google.h"
#include "ortools/base/options.h"
#include "ortools/base/timer.h"
#include "ortools/scheduling/course_scheduling.pb.h"

ABSL_FLAG(std::string, input, "",
"Input file containing a CourseSchedulingModel in text format.");
Expand Down Expand Up @@ -105,8 +105,7 @@ void Main() {
} // namespace operations_research

int main(int argc, char** argv) {
google::InitGoogleLogging(argv[0]);
absl::ParseCommandLine(argc, argv);
InitGoogle(argv[0], &argc, &argv, /*remove_flags=*/true);
operations_research::Main();
return EXIT_SUCCESS;
}
10 changes: 5 additions & 5 deletions examples/cpp/dobble_ls.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class SymbolsSharedByTwoCardsConstraint : public Constraint {
}
}

~SymbolsSharedByTwoCardsConstraint() override {}
~SymbolsSharedByTwoCardsConstraint() override = default;

// Adds observers (named Demon) to variable events. These demons are
// responsible for implementing the propagation algorithm of the
Expand Down Expand Up @@ -255,7 +255,7 @@ class DobbleOperator : public IntVarLocalSearchOperator {
}
}

~DobbleOperator() override {}
~DobbleOperator() override = default;

protected:
// OnStart() simply stores the current symbols per card in
Expand Down Expand Up @@ -314,7 +314,7 @@ class SwapSymbols : public DobbleOperator {
current_symbol1_(-1),
current_symbol2_(-1) {}

~SwapSymbols() override {}
~SwapSymbols() override = default;

// Finds the next swap, returns false when it has finished.
bool MakeOneNeighbor() override {
Expand Down Expand Up @@ -385,7 +385,7 @@ class SwapSymbolsOnCardPairs : public DobbleOperator {
CHECK_GE(max_num_swaps, 2);
}

~SwapSymbolsOnCardPairs() override {}
~SwapSymbolsOnCardPairs() override = default;

protected:
bool MakeOneNeighbor() override {
Expand Down Expand Up @@ -547,7 +547,7 @@ class DobbleFilter : public IntVarLocalSearchFilter {
void ClearBitset() { temporary_bitset_ = 0; }

// For each touched card, compare against all others to compute the
// delta in term of cost. We use an bitset to avoid counting twice
// delta in term of cost. We use a bitset to avoid counting twice
// between two cards appearing in the local search move.
int ComputeNewCost(const std::vector<int>& touched_cards) {
ClearBitset();
Expand Down
5 changes: 3 additions & 2 deletions examples/cpp/fap_model_printer.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#ifndef OR_TOOLS_EXAMPLES_FAP_MODEL_PRINTER_H_
#define OR_TOOLS_EXAMPLES_FAP_MODEL_PRINTER_H_

#include <map>
#include <string>
#include <vector>

Expand All @@ -33,7 +34,7 @@ class FapModelPrinter {
public:
FapModelPrinter(const absl::btree_map<int, FapVariable>& variables,
const std::vector<FapConstraint>& constraints,
const std::string& objective, const std::vector<int>& values);
absl::string_view objective, const std::vector<int>& values);
~FapModelPrinter();

void PrintFapObjective();
Expand All @@ -51,7 +52,7 @@ class FapModelPrinter {

FapModelPrinter::FapModelPrinter(const absl::btree_map<int, FapVariable>& variables,
const std::vector<FapConstraint>& constraints,
const std::string& objective,
absl::string_view objective,
const std::vector<int>& values)
: variables_(variables),
constraints_(constraints),
Expand Down
Loading

0 comments on commit aa0e719

Please sign in to comment.