update examples after library changes

This commit is contained in:
Laurent Perron
2017-03-28 16:13:30 +02:00
parent 288be16e0a
commit 395e170563
11 changed files with 115 additions and 47 deletions

View File

@@ -32,6 +32,7 @@
#include "base/commandlineflags.h"
#include "base/integral_types.h"
#include "base/logging.h"
#include "base/join.h"
#include "constraint_solver/routing.h"
#include "constraint_solver/routing_enums.pb.h"
#include "constraint_solver/routing_flags.h"

View File

@@ -151,8 +151,8 @@ int main(int argc, char** argv) {
}
// Only one order can happen at the same time at a given location.
std::vector<int64> location_usage(stop_intervals.size(), 1);
solver->AddConstraint(solver->MakeCumulative(stop_intervals, location_usage,
1, StrCat("Client", stop)));
solver->AddConstraint(solver->MakeCumulative(
stop_intervals, location_usage, 1, StrCat("Client", stop)));
}
// Minimizing route duration.
for (int vehicle = 0; vehicle < routing.vehicles(); ++vehicle) {

View File

@@ -141,7 +141,8 @@ void Solve(const std::vector<std::vector<Task>>& tasks_per_job, int horizon) {
decision_variables.clear();
}
MinimizeIntegerVariableWithLinearScanAndLazyEncoding(
/*log_info=*/true, makespan, decision_variables,
/*log_info=*/true, makespan,
FirstUnassignedVarAtItsMinHeuristic(decision_variables, &model),
/*feasible_solution_observer=*/
[makespan](const Model& model) {
LOG(INFO) << "Makespan " << model.Get(LowerBound(makespan));

View File

@@ -219,12 +219,12 @@ bool LoadAndSolve(const std::string& pdp_file) {
const int customer_id = parsed_int[0];
const int x = parsed_int[1];
const int y = parsed_int[2];
const int delivery = parsed_int[8]; // Parse 'delivery' before 'demand'.
const int64 demand = delivery == 0 ? -parsed_int[3] : parsed_int[3];
const int64 demand = parsed_int[3];
const int64 open_time = parsed_int[4];
const int64 close_time = parsed_int[5];
const int64 service_time = parsed_int[6];
const int pickup = parsed_int[7];
const int delivery = parsed_int[8];
customer_ids.push_back(customer_id);
coords.push_back(std::make_pair(x, y));
demands.push_back(demand);
@@ -241,7 +241,8 @@ bool LoadAndSolve(const std::string& pdp_file) {
// Build pickup and delivery model.
const int num_nodes = customer_ids.size();
RoutingModel routing(num_nodes, num_vehicles, depot);
RoutingModelParameters model_parameters = BuildModelParametersFromFlags();
RoutingModel routing(num_nodes, num_vehicles, depot, model_parameters);
routing.SetArcCostEvaluatorOfAllVehicles(
NewPermanentCallback(Travel, const_cast<const Coordinates*>(&coords)));
routing.AddDimension(
@@ -288,6 +289,7 @@ bool LoadAndSolve(const std::string& pdp_file) {
// Solve pickup and delivery problem.
const Assignment* assignment = routing.SolveWithParameters(parameters);
LOG(INFO) << routing.solver()->LocalSearchProfile();
if (NULL != assignment) {
LOG(INFO) << "Cost: " << assignment->ObjectiveValue();
LOG(INFO) << VerboseOutput(routing, *assignment, coords, service_times);

View File

@@ -249,7 +249,8 @@ void LoadAndSolve(const std::string& file_name) {
}
MinimizeIntegerVariableWithLinearScanAndLazyEncoding(
/*log_info=*/true, objective_var, decision_variables,
/*log_info=*/true, objective_var,
FirstUnassignedVarAtItsMinHeuristic(decision_variables, &model),
/*feasible_solution_observer=*/
[objective_var](const Model& model) {
LOG(INFO) << "Objective " << model.Get(LowerBound(objective_var));

View File

@@ -11,6 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include <csignal>
#include <string>
#include <vector>
@@ -26,6 +27,7 @@
#include "google/protobuf/text_format.h"
#include "base/stringpiece_utils.h"
#include "base/strutil.h"
#include "base/threadpool.h"
#include "algorithms/sparse_permutation.h"
#include "sat/boolean_problem.h"
#include "sat/drat.h"
@@ -187,6 +189,10 @@ int Run() {
// The global time limit.
std::unique_ptr<TimeLimit> time_limit(TimeLimit::FromParameters(parameters));
// Catch ^C.
bool interrupt_solve = false;
time_limit->RegisterExternalBooleanAsLimit(&interrupt_solve);
// Read the problem.
LinearBooleanProblem problem;
LoadBooleanProblem(FLAGS_input, &problem);

View File

@@ -306,10 +306,7 @@ void LoadAndSolve(const std::string& file_name) {
if (FLAGS_use_core) {
const std::vector<int64> coeffs(num_workers, 1);
MinimizeWeightedLiteralSumWithCoreAndLazyEncoding(
/*log_info=*/true, active_workers, coeffs,
std::vector<IntegerVariable>(),
/*feasible_solution_observer=*/
[&](const Model& model) {}, &model);
/*log_info=*/true, active_workers, coeffs, nullptr, nullptr, &model);
return;
}
@@ -328,7 +325,7 @@ void LoadAndSolve(const std::string& file_name) {
model.Add(FixedWeightedSum(worker_vars, weights, 0));
MinimizeIntegerVariableWithLinearScanAndLazyEncoding(
/*log_info=*/true, objective_var, std::vector<IntegerVariable>(),
/*log_info=*/true, objective_var, nullptr,
/*feasible_solution_observer=*/
[&](const Model& model) {
LOG(INFO) << "Cost " << model.Get(Value(objective_var));

View File

@@ -39,6 +39,7 @@ DEFINE_string(input, "", "REQUIRED: Input file name.");
DEFINE_string(solver, "glop",
"The solver to use: bop, cbc, clp, glop, glpk_lp, glpk_mip, "
"gurobi_lp, gurobi_mip, scip, knapsack.");
DEFINE_string(params_file, "",
"Solver specific parameters file. "
"If this flag is set, the --params flag is ignored.");
@@ -66,7 +67,6 @@ DEFINE_string(dump_format, "text",
DEFINE_bool(dump_gzip, false,
"Whether to gzip dumped protos. Appends .gz to their name.");
DEFINE_string(dump_model, "", "If non-empty, dumps MPModelProto there.");
DEFINE_string(dump_request, "", "If non-empty, dumps MPModelRequest there.");
DEFINE_string(dump_response, "", "If non-empty, dumps MPModelResponse there.");
@@ -190,6 +190,19 @@ void Run() {
// }
printf("%-12s: '%s'\n", "File", FLAGS_input.c_str());
// Detect format to dump protos.
operations_research::ProtoWriteFormat write_format;
if (FLAGS_dump_format == "text") {
write_format = ProtoWriteFormat::kProtoText;
} else if (FLAGS_dump_format == "binary") {
write_format = ProtoWriteFormat::kProtoBinary;
} else if (FLAGS_dump_format == "json") {
write_format = ProtoWriteFormat::kJson;
} else {
LOG(FATAL) << "Unsupported --dump_format: " << FLAGS_dump_format;
}
// Create the solver, we use the name of the model as the solver name.
MPSolver solver(model_proto.name(), type);
solver.EnableOutput();
@@ -211,18 +224,6 @@ void Run() {
// Load the proto into the solver.
std::string error_message;
// Detect format to dump protos.
operations_research::ProtoWriteFormat write_format;
if (FLAGS_dump_format == "text") {
write_format = ProtoWriteFormat::kProtoText;
} else if (FLAGS_dump_format == "binary") {
write_format = ProtoWriteFormat::kProtoBinary;
} else if (FLAGS_dump_format == "json") {
write_format = ProtoWriteFormat::kJson;
} else {
LOG(FATAL) << "Unsupported --dump_format: " << FLAGS_dump_format;
}
// If requested, save the model to file.
if (!FLAGS_dump_model.empty()) {
CHECK(WriteProtoToFile(FLAGS_dump_model, model_proto, write_format,
@@ -230,7 +231,8 @@ void Run() {
}
const MPSolverResponseStatus status =
solver.LoadModelFromProto(model_proto, &error_message);
solver.LoadModelFromProtoWithUniqueNamesOrDie(model_proto,
&error_message);
if (request_proto.has_solver_time_limit_seconds()) {
solver.set_time_limit(
static_cast<int64>(1000.0 * request_proto.solver_time_limit_seconds()));

View File

@@ -32,6 +32,7 @@
#include "base/commandlineflags.h"
#include "base/integral_types.h"
#include "base/join.h"
#include "base/join.h"
#include "constraint_solver/routing.h"
#include "constraint_solver/routing_flags.h"
#include "base/random.h"
@@ -145,7 +146,7 @@ void Tsp() {
for (int64 node = routing.Start(route_number); !routing.IsEnd(node);
node = solution->Value(routing.NextVar(node))) {
StrAppend(&route, routing.IndexToNode(node).value(), " (", node,
") -> ");
") -> ");
}
const int64 end = routing.End(route_number);
StrAppend(&route, routing.IndexToNode(end).value(), " (", end, ")");

View File

@@ -95,7 +95,7 @@ void Solve(const std::vector<int>& durations, const std::vector<int>& due_dates,
tardiness_vars[i] = model.Get(EndVar(tasks[i]));
} else {
tardiness_vars[i] =
model.Add(NewIntegerVariable(0, horizon - due_dates[i]));
model.Add(NewIntegerVariable(0, std::max(0, horizon - due_dates[i])));
model.Add(LowerOrEqualWithOffset(model.Get(EndVar(tasks[i])),
tardiness_vars[i], -due_dates[i]));
}
@@ -163,9 +163,14 @@ void Solve(const std::vector<int>& durations, const std::vector<int>& due_dates,
}
// Solve it.
//
// Note that we only fully instanciate the start/end and only look at the
// lower bound for the objective and the tardiness variables.
model.Add(NewSatParameters(FLAGS_params));
MinimizeIntegerVariableWithLinearScanAndLazyEncoding(
/*log_info=*/true, objective_var, decision_vars,
/*log_info=*/true, objective_var,
/*next_decision=*/
UnassignedVarWithLowestMinAtItsMinHeuristic(decision_vars, &model),
/*feasible_solution_observer=*/
[&](const Model& model) {
const int64 objective = model.Get(LowerBound(objective_var));
@@ -177,9 +182,8 @@ void Solve(const std::vector<int>& durations, const std::vector<int>& due_dates,
for (int i = 0; i < num_tasks; ++i) {
tardiness_objective +=
weights[i] *
std::max(0ll,
model.Get(LowerBound(model.Get(EndVar(tasks[i])))) -
due_dates[i]);
std::max(0ll, model.Get(Value(model.Get(EndVar(tasks[i])))) -
due_dates[i]);
}
CHECK_EQ(objective, tardiness_objective);
@@ -195,8 +199,8 @@ void Solve(const std::vector<int>& durations, const std::vector<int>& due_dates,
std::vector<IntervalVariable> sorted_tasks = tasks;
std::sort(sorted_tasks.begin(), sorted_tasks.end(),
[&model](IntervalVariable v1, IntervalVariable v2) {
return model.Get(LowerBound(model.Get(StartVar(v1)))) <
model.Get(LowerBound(model.Get(StartVar(v2))));
return model.Get(Value(model.Get(StartVar(v1)))) <
model.Get(Value(model.Get(StartVar(v2))));
});
std::string solution = "0";
int end = 0;
@@ -208,11 +212,11 @@ void Solve(const std::vector<int>& durations, const std::vector<int>& due_dates,
// Display the cost in red.
solution += StringPrintf("\033[1;31m(+%lld) \033[0m", cost);
}
solution += StringPrintf("|%lld",
model.Get(LowerBound(model.Get(EndVar(v)))));
CHECK_EQ(end, model.Get(LowerBound(model.Get(StartVar(v)))));
solution +=
StringPrintf("|%lld", model.Get(Value(model.Get(EndVar(v)))));
CHECK_EQ(end, model.Get(Value(model.Get(StartVar(v)))));
end += durations[v.value()];
CHECK_EQ(end, model.Get(LowerBound(model.Get(EndVar(v)))));
CHECK_EQ(end, model.Get(Value(model.Get(EndVar(v)))));
}
LOG(INFO) << "solution: " << solution;
},
@@ -225,7 +229,7 @@ void LoadAndSolve() {
std::vector<int> numbers;
std::vector<std::string> entries;
for (const std::string& line : operations_research::FileLines(FLAGS_input)) {
entries = strings::Split(line, " ", strings::SkipEmpty());
entries = strings::Split(line, ' ', strings::SkipEmpty());
for (const std::string& entry : entries) {
numbers.push_back(atoi32(entry));
}