From fe22bf057a9fbef167496480ca7885dccb5eaf83 Mon Sep 17 00:00:00 2001 From: Laurent Perron Date: Tue, 28 May 2019 15:28:01 +0200 Subject: [PATCH] minor polish of examples --- examples/contrib/project_scheduling_sat.py | 58 ++++++++++ examples/cpp/dobble_ls.cc | 2 +- examples/cpp/pdptw.cc | 3 +- examples/python/flexible_job_shop_sat.py | 4 +- makefiles/Makefile.gen.mk | 100 ++++++++++++++++++ ortools/algorithms/samples/knapsack.py | 1 + .../samples/SimpleLpProgram.java | 5 +- .../samples/simple_lp_program.py | 1 + .../samples/simple_mip_program.py | 1 + 9 files changed, 168 insertions(+), 7 deletions(-) create mode 100644 examples/contrib/project_scheduling_sat.py diff --git a/examples/contrib/project_scheduling_sat.py b/examples/contrib/project_scheduling_sat.py new file mode 100644 index 0000000000..255990b74a --- /dev/null +++ b/examples/contrib/project_scheduling_sat.py @@ -0,0 +1,58 @@ +from __future__ import division +from __future__ import print_function + +from ortools.sat.python import cp_model + +#project name, duration, starts earliest, ends latest, demand role A, demand role S, demand role J +projects = [ + ['P1',3, 0, 9, 1, 0, 1], + ['P2',8, 0, 9, 1, 1, 0], + ['P3',3, 0, 9, 1, 0, 2], + ['P4',4, 0, 9, 1, 0, 1] + ] + +num_projects = len(projects) + +roles = ['A','S','J'] + +#Roles available at each time step +available_roles = [ + [2,2,2,2,2,2,2,2,2,2], #Role A + [1,1,1,1,1,1,1,1,1,1], #Role S + [1,1,1,1,1,1,1,2,2,2] #Role J +] + +all_projects = range(num_projects) +all_time_steps = range(time_steps) +all_roles = range(len(roles)) + +# Creates the model. +model = cp_model.CpModel() + +#Creating decision variables + +#starts and ends of the projects +starts = [model.NewIntVar(projects[j][2], projects[j][3] + 1 , 'start_%i' % j) for j in all_projects] +ends = [model.NewIntVar(projects[j][2], projects[j][3] + 1, 'end_%i' % j) for j in all_projects] +intervals = [model.NewIntervalVar(starts[j], projects[j][1], ends[j], 'interval_%i' % j) for j in all_projects] + +# Role A has a capacity 2. Every project uses it. +demands = [1 for _ in all_projects] +model.AddCumulative(intervals, demands, 2) + +# Role S has a capacity of 1 +model.AddNoOverlap([intervals[i] for i in all_projects if projects[i][5]]) + +# Project J has a capacity of 1 or 2. +used_capacity = model.NewIntervalVar(0, 7, 7, 'unavailable') +intervals_for_project_j = intervals + [used_capacity] +demands_for_project_j = [projects[j][6] for j in all_projects] + [1] +model.AddCumulative(intervals_for_project_j, demands_for_project_j, 2) + +#We want the projects to start as early as possible +model.Minimize(sum(starts)) + +# Solve model. +solver = cp_model.CpSolver() +solver.parameters.log_search_progress = True +status=solver.Solve(model) diff --git a/examples/cpp/dobble_ls.cc b/examples/cpp/dobble_ls.cc index 2b4214b404..c963f26500 100644 --- a/examples/cpp/dobble_ls.cc +++ b/examples/cpp/dobble_ls.cc @@ -18,7 +18,7 @@ // generalized: we have N cards, each with K different symbols, and // there are N different symbols overall. // -// This is a feasability problem. We transform that into an +// This is a feasibility problem. We transform that into an // optimization problem where we penalize cards whose intersection is // of cardinality different from 1. A feasible solution of the // original problem is a solution with a zero cost. diff --git a/examples/cpp/pdptw.cc b/examples/cpp/pdptw.cc index c92438a425..096a9522aa 100644 --- a/examples/cpp/pdptw.cc +++ b/examples/cpp/pdptw.cc @@ -83,7 +83,8 @@ int64 Travel(const Coordinates* const coords, const int xd = coords->at(from.value()).first - coords->at(to.value()).first; const int yd = coords->at(from.value()).second - coords->at(to.value()).second; - return static_cast(kScalingFactor * sqrt(1.0L * xd * xd + yd * yd)); + return static_cast(kScalingFactor * + std::sqrt(1.0L * xd * xd + yd * yd)); } // Returns the scaled service time at a given node, service_times holding the diff --git a/examples/python/flexible_job_shop_sat.py b/examples/python/flexible_job_shop_sat.py index d20e26eb3a..dd2c858f11 100644 --- a/examples/python/flexible_job_shop_sat.py +++ b/examples/python/flexible_job_shop_sat.py @@ -14,7 +14,7 @@ from __future__ import print_function -from collections import defaultdict +import collections from ortools.sat.python import cp_model @@ -60,7 +60,7 @@ def flexible_jobshop(): print('Horizon = %i' % horizon) # Global storage of variables. - intervals_per_resources = defaultdict(list) + intervals_per_resources = collections.defaultdict(list) starts = {} # indexed by (job_id, task_id). presences = {} # indexed by (job_id, task_id, alt_id). job_ends = [] diff --git a/makefiles/Makefile.gen.mk b/makefiles/Makefile.gen.mk index 1d3f670491..7fe445ea46 100644 --- a/makefiles/Makefile.gen.mk +++ b/makefiles/Makefile.gen.mk @@ -2866,12 +2866,14 @@ CP_LIB_OBJS = \ $(OBJ_DIR)/constraint_solver/range_cst.$O \ $(OBJ_DIR)/constraint_solver/resource.$O \ $(OBJ_DIR)/constraint_solver/routing.$O \ + $(OBJ_DIR)/constraint_solver/routing_breaks.$O \ $(OBJ_DIR)/constraint_solver/routing_flags.$O \ $(OBJ_DIR)/constraint_solver/routing_flow.$O \ $(OBJ_DIR)/constraint_solver/routing_index_manager.$O \ $(OBJ_DIR)/constraint_solver/routing_lp_scheduling.$O \ $(OBJ_DIR)/constraint_solver/routing_neighborhoods.$O \ $(OBJ_DIR)/constraint_solver/routing_parameters.$O \ + $(OBJ_DIR)/constraint_solver/routing_sat.$O \ $(OBJ_DIR)/constraint_solver/routing_search.$O \ $(OBJ_DIR)/constraint_solver/sched_constraints.$O \ $(OBJ_DIR)/constraint_solver/sched_expr.$O \ @@ -3238,6 +3240,54 @@ objs/constraint_solver/routing.$O: ortools/constraint_solver/routing.cc \ ortools/util/zvector.h ortools/graph/min_cost_flow.h | $(OBJ_DIR)/constraint_solver $(CCC) $(CFLAGS) -c $(SRC_DIR)$Sortools$Sconstraint_solver$Srouting.cc $(OBJ_OUT)$(OBJ_DIR)$Sconstraint_solver$Srouting.$O +objs/constraint_solver/routing_breaks.$O: \ + ortools/constraint_solver/routing_breaks.cc \ + ortools/constraint_solver/routing.h \ + ortools/base/adjustable_priority_queue-inl.h \ + ortools/base/adjustable_priority_queue.h ortools/base/basictypes.h \ + ortools/base/integral_types.h ortools/base/logging.h \ + ortools/base/macros.h ortools/base/commandlineflags.h \ + ortools/base/hash.h ortools/base/int_type_indexed_vector.h \ + ortools/base/int_type.h ortools/constraint_solver/constraint_solver.h \ + ortools/base/map_util.h ortools/base/random.h ortools/base/sysinfo.h \ + ortools/base/timer.h \ + ortools/gen/ortools/constraint_solver/solver_parameters.pb.h \ + ortools/util/piecewise_linear_function.h \ + ortools/util/saturated_arithmetic.h ortools/util/bitset.h \ + ortools/util/sorted_interval_list.h ortools/util/tuple_set.h \ + ortools/constraint_solver/constraint_solveri.h ortools/util/vector_map.h \ + ortools/constraint_solver/routing_index_manager.h \ + ortools/constraint_solver/routing_types.h \ + ortools/gen/ortools/constraint_solver/routing_parameters.pb.h \ + ortools/gen/ortools/constraint_solver/routing_enums.pb.h \ + ortools/gen/ortools/util/optional_boolean.pb.h ortools/glop/lp_solver.h \ + ortools/gen/ortools/glop/parameters.pb.h ortools/glop/preprocessor.h \ + ortools/glop/revised_simplex.h ortools/glop/basis_representation.h \ + ortools/glop/lu_factorization.h ortools/glop/markowitz.h \ + ortools/glop/status.h ortools/lp_data/lp_types.h \ + ortools/lp_data/sparse.h ortools/lp_data/permutation.h \ + ortools/util/return_macros.h ortools/lp_data/sparse_column.h \ + ortools/lp_data/sparse_vector.h ortools/graph/iterators.h \ + ortools/util/stats.h ortools/glop/rank_one_update.h \ + ortools/lp_data/lp_utils.h ortools/base/accurate_sum.h \ + ortools/glop/dual_edge_norms.h ortools/lp_data/lp_data.h \ + ortools/util/fp_utils.h ortools/glop/entering_variable.h \ + ortools/glop/primal_edge_norms.h ortools/glop/update_row.h \ + ortools/glop/variables_info.h ortools/glop/reduced_costs.h \ + ortools/util/random_engine.h ortools/glop/variable_values.h \ + ortools/lp_data/lp_print_utils.h ortools/lp_data/sparse_row.h \ + ortools/util/time_limit.h ortools/util/running_stat.h \ + ortools/lp_data/matrix_scaler.h ortools/graph/graph.h \ + ortools/sat/theta_tree.h ortools/sat/integer.h ortools/sat/model.h \ + ortools/base/typeid.h ortools/sat/sat_base.h ortools/sat/sat_solver.h \ + ortools/sat/clause.h ortools/sat/drat_proof_handler.h \ + ortools/sat/drat_checker.h ortools/sat/drat_writer.h ortools/base/file.h \ + ortools/base/status.h ortools/gen/ortools/sat/sat_parameters.pb.h \ + ortools/sat/pb_constraint.h ortools/sat/restart.h \ + ortools/sat/sat_decision.h ortools/util/integer_pq.h ortools/util/rev.h \ + ortools/util/range_query_function.h | $(OBJ_DIR)/constraint_solver + $(CCC) $(CFLAGS) -c $(SRC_DIR)$Sortools$Sconstraint_solver$Srouting_breaks.cc $(OBJ_OUT)$(OBJ_DIR)$Sconstraint_solver$Srouting_breaks.$O + objs/constraint_solver/routing_flags.$O: \ ortools/constraint_solver/routing_flags.cc \ ortools/constraint_solver/routing_flags.h \ @@ -3400,6 +3450,56 @@ objs/constraint_solver/routing_parameters.$O: \ ortools/util/sorted_interval_list.h ortools/util/tuple_set.h | $(OBJ_DIR)/constraint_solver $(CCC) $(CFLAGS) -c $(SRC_DIR)$Sortools$Sconstraint_solver$Srouting_parameters.cc $(OBJ_OUT)$(OBJ_DIR)$Sconstraint_solver$Srouting_parameters.$O +objs/constraint_solver/routing_sat.$O: \ + ortools/constraint_solver/routing_sat.cc \ + ortools/constraint_solver/routing.h \ + ortools/base/adjustable_priority_queue-inl.h \ + ortools/base/adjustable_priority_queue.h ortools/base/basictypes.h \ + ortools/base/integral_types.h ortools/base/logging.h \ + ortools/base/macros.h ortools/base/commandlineflags.h \ + ortools/base/hash.h ortools/base/int_type_indexed_vector.h \ + ortools/base/int_type.h ortools/constraint_solver/constraint_solver.h \ + ortools/base/map_util.h ortools/base/random.h ortools/base/sysinfo.h \ + ortools/base/timer.h \ + ortools/gen/ortools/constraint_solver/solver_parameters.pb.h \ + ortools/util/piecewise_linear_function.h \ + ortools/util/saturated_arithmetic.h ortools/util/bitset.h \ + ortools/util/sorted_interval_list.h ortools/util/tuple_set.h \ + ortools/constraint_solver/constraint_solveri.h ortools/util/vector_map.h \ + ortools/constraint_solver/routing_index_manager.h \ + ortools/constraint_solver/routing_types.h \ + ortools/gen/ortools/constraint_solver/routing_parameters.pb.h \ + ortools/gen/ortools/constraint_solver/routing_enums.pb.h \ + ortools/gen/ortools/util/optional_boolean.pb.h ortools/glop/lp_solver.h \ + ortools/gen/ortools/glop/parameters.pb.h ortools/glop/preprocessor.h \ + ortools/glop/revised_simplex.h ortools/glop/basis_representation.h \ + ortools/glop/lu_factorization.h ortools/glop/markowitz.h \ + ortools/glop/status.h ortools/lp_data/lp_types.h \ + ortools/lp_data/sparse.h ortools/lp_data/permutation.h \ + ortools/util/return_macros.h ortools/lp_data/sparse_column.h \ + ortools/lp_data/sparse_vector.h ortools/graph/iterators.h \ + ortools/util/stats.h ortools/glop/rank_one_update.h \ + ortools/lp_data/lp_utils.h ortools/base/accurate_sum.h \ + ortools/glop/dual_edge_norms.h ortools/lp_data/lp_data.h \ + ortools/util/fp_utils.h ortools/glop/entering_variable.h \ + ortools/glop/primal_edge_norms.h ortools/glop/update_row.h \ + ortools/glop/variables_info.h ortools/glop/reduced_costs.h \ + ortools/util/random_engine.h ortools/glop/variable_values.h \ + ortools/lp_data/lp_print_utils.h ortools/lp_data/sparse_row.h \ + ortools/util/time_limit.h ortools/util/running_stat.h \ + ortools/lp_data/matrix_scaler.h ortools/graph/graph.h \ + ortools/sat/theta_tree.h ortools/sat/integer.h ortools/sat/model.h \ + ortools/base/typeid.h ortools/sat/sat_base.h ortools/sat/sat_solver.h \ + ortools/sat/clause.h ortools/sat/drat_proof_handler.h \ + ortools/sat/drat_checker.h ortools/sat/drat_writer.h ortools/base/file.h \ + ortools/base/status.h ortools/gen/ortools/sat/sat_parameters.pb.h \ + ortools/sat/pb_constraint.h ortools/sat/restart.h \ + ortools/sat/sat_decision.h ortools/util/integer_pq.h ortools/util/rev.h \ + ortools/util/range_query_function.h ortools/sat/cp_model.h \ + ortools/gen/ortools/sat/cp_model.pb.h ortools/sat/cp_model_solver.h \ + ortools/sat/cp_model_utils.h | $(OBJ_DIR)/constraint_solver + $(CCC) $(CFLAGS) -c $(SRC_DIR)$Sortools$Sconstraint_solver$Srouting_sat.cc $(OBJ_OUT)$(OBJ_DIR)$Sconstraint_solver$Srouting_sat.$O + objs/constraint_solver/routing_search.$O: \ ortools/constraint_solver/routing_search.cc ortools/base/map_util.h \ ortools/base/logging.h ortools/base/integral_types.h \ diff --git a/ortools/algorithms/samples/knapsack.py b/ortools/algorithms/samples/knapsack.py index 1fd433f2a3..279002fd79 100644 --- a/ortools/algorithms/samples/knapsack.py +++ b/ortools/algorithms/samples/knapsack.py @@ -15,6 +15,7 @@ # [START import] from __future__ import print_function from ortools.algorithms import pywrapknapsack_solver + # [END import] diff --git a/ortools/linear_solver/samples/SimpleLpProgram.java b/ortools/linear_solver/samples/SimpleLpProgram.java index 7890b0b26d..ba80f884a6 100644 --- a/ortools/linear_solver/samples/SimpleLpProgram.java +++ b/ortools/linear_solver/samples/SimpleLpProgram.java @@ -29,9 +29,8 @@ public class SimpleLpProgram { public static void main(String[] args) throws Exception { // [START solver] // Create the linear solver with the GLOP backend. - MPSolver solver = new MPSolver( - "SimpleLpProgram", - MPSolver.OptimizationProblemType.GLOP_LINEAR_PROGRAMMING); + MPSolver solver = + new MPSolver("SimpleLpProgram", MPSolver.OptimizationProblemType.GLOP_LINEAR_PROGRAMMING); // [END solver] // [START variables] diff --git a/ortools/linear_solver/samples/simple_lp_program.py b/ortools/linear_solver/samples/simple_lp_program.py index 0f4b4561f1..68912fe76d 100644 --- a/ortools/linear_solver/samples/simple_lp_program.py +++ b/ortools/linear_solver/samples/simple_lp_program.py @@ -15,6 +15,7 @@ # [START import] from __future__ import print_function from ortools.linear_solver import pywraplp + # [END import] diff --git a/ortools/linear_solver/samples/simple_mip_program.py b/ortools/linear_solver/samples/simple_mip_program.py index 167b5584db..d6598ace74 100644 --- a/ortools/linear_solver/samples/simple_mip_program.py +++ b/ortools/linear_solver/samples/simple_mip_program.py @@ -15,6 +15,7 @@ # [START import] from __future__ import print_function from ortools.linear_solver import pywraplp + # [END import]