Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
minor polish of examples
Browse files Browse the repository at this point in the history
  • Loading branch information
lperron committed May 28, 2019
1 parent c65d042 commit fe22bf0
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 7 deletions.
58 changes: 58 additions & 0 deletions examples/contrib/project_scheduling_sat.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion examples/cpp/dobble_ls.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion examples/cpp/pdptw.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<int64>(kScalingFactor * sqrt(1.0L * xd * xd + yd * yd));
return static_cast<int64>(kScalingFactor *
std::sqrt(1.0L * xd * xd + yd * yd));
}

// Returns the scaled service time at a given node, service_times holding the
Expand Down
4 changes: 2 additions & 2 deletions examples/python/flexible_job_shop_sat.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from __future__ import print_function

from collections import defaultdict
import collections
from ortools.sat.python import cp_model


Expand Down Expand Up @@ -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 = []
Expand Down
100 changes: 100 additions & 0 deletions makefiles/Makefile.gen.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down Expand Up @@ -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 \
Expand Down Expand Up @@ -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 \
Expand Down
1 change: 1 addition & 0 deletions ortools/algorithms/samples/knapsack.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# [START import]
from __future__ import print_function
from ortools.algorithms import pywrapknapsack_solver

# [END import]


Expand Down
5 changes: 2 additions & 3 deletions ortools/linear_solver/samples/SimpleLpProgram.java
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
1 change: 1 addition & 0 deletions ortools/linear_solver/samples/simple_lp_program.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# [START import]
from __future__ import print_function
from ortools.linear_solver import pywraplp

# [END import]


Expand Down
1 change: 1 addition & 0 deletions ortools/linear_solver/samples/simple_mip_program.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# [START import]
from __future__ import print_function
from ortools.linear_solver import pywraplp

# [END import]


Expand Down

0 comments on commit fe22bf0

Please sign in to comment.