Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def main():
# Create the solver.
solver = pywrapcp.Solver('Set covering Skiena')
#
# data
#
num_sets = 7
num_elements = 12
belongs = [
# 1 2 3 4 5 6 7 8 9 0 1 2 elements
[1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # Set 1
[0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0], # 2
[0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0], # 3
[0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0], # 4
[0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0], # 5
[1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0], # 6
[0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1] # 7
]
if cachekey not in capture_cache:
logger.debug('Capture cache miss, starting ortools calculation')
manager = pywrapcp.RoutingIndexManager(a.order(), 1, [w_start], [linkplan[0][0]])
routing = pywrapcp.RoutingModel(manager)
def distance_callback(from_index, to_index):
from_node = manager.IndexToNode(from_index)
to_node = manager.IndexToNode(to_index)
return get_portal_distance(from_node, to_node)
transit_callback_index = routing.RegisterTransitCallback(distance_callback)
routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)
search_parameters = pywrapcp.DefaultRoutingSearchParameters()
search_parameters.first_solution_strategy = (
routing_enums_pb2.FirstSolutionStrategy.AUTOMATIC
)
logger.debug('Starting solver')
assignment = routing.SolveWithParameters(search_parameters)
logger.debug('Ended solver')
if not assignment:
logger.debug('Could not solve for these constraints, ignoring plan')
capture_cache[cachekey] = None
return None
index = routing.Start(0)
dist_ordered = list()
while not routing.IsEnd(index):
node = manager.IndexToNode(index)
def main():
"""Entry point of the program."""
# Instantiate the data problem.
# [START data]
data = create_data_model()
# [END data]
# Create the routing index manager.
# [START index_manager]
manager = pywrapcp.RoutingIndexManager(len(data['distance_matrix']),
data['num_vehicles'], data['depot'])
# [END index_manager]
# Create Routing Model.
# [START routing_model]
routing = pywrapcp.RoutingModel(manager)
# [END routing_model]
# Create and register a transit callback.
# [START transit_callback]
def distance_callback(from_index, to_index):
"""Returns the distance between the two nodes."""
# Convert from routing variable Index to distance matrix NodeIndex.
from_node = manager.IndexToNode(from_index)
to_node = manager.IndexToNode(to_index)
def TSP(size):
# Create routing model
route_list = []
if size > 0:
# TSP of size args.tsp_size
# Second argument = 1 to build a single tour (it's a TSP).
# Nodes are indexed from 0 to parser_tsp_size - 1, by default the start of
# the route is node 0.
routing = pywrapcp.RoutingModel(size, 1, 0)
search_parameters = pywrapcp.RoutingModel.DefaultSearchParameters()
# Setting first solution heuristic (cheapest addition).
search_parameters.first_solution_strategy = (
routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC)
routing.SetArcCostEvaluatorOfAllVehicles(Distance)
# Forbid node connections (randomly).
rand = random.Random()
rand.seed(0)
assignment = routing.Solve()
if assignment:
# Solution cost.
# print(assignment.ObjectiveValue())
# Inspect solution.
def main(values, weights, n):
# Create the solver.
solver = pywrapcp.Solver("knapsack_cp")
#
# data
#
print("values:", values)
print("weights:", weights)
print("n:", n)
print()
# declare variables
#
# constraints
#
[x, z] = knapsack(solver, values, weights, n)
def main():
# Create the solver.
solver = pywrapcp.Solver('Ski assignment')
#
# data
#
num_skis = 6
num_skiers = 5
ski_heights = [1, 2, 5, 7, 13, 21]
skier_heights = [3, 4, 7, 11, 18]
#
# variables
#
# which ski to choose for each skier
x = [solver.IntVar(0, num_skis - 1, 'x[%i]' % i) for i in range(num_skiers)]
z = solver.IntVar(0, sum(ski_heights), 'z')
def main():
"""Entry point of the program"""
# Instantiate the data problem.
data = create_data_model()
# Create the routing index manager
manager = pywrapcp.RoutingIndexManager(data['num_locations'],
data['num_vehicles'], data['depot'])
# Create Routing Model
routing = pywrapcp.RoutingModel(manager)
# Define weight of each edge
distance_evaluator_index = routing.RegisterTransitCallback(
partial(create_distance_evaluator(data), manager))
routing.SetArcCostEvaluatorOfAllVehicles(distance_evaluator_index)
# Add Capacity constraint
demand_evaluator_index = routing.RegisterUnaryTransitCallback(
partial(create_demand_evaluator(data), manager))
add_capacity_constraints(routing, data, demand_evaluator_index)
# Add Time Window constraint
def main(coins, total):
# Create the solver.
solver = pywrapcp.Solver("n-queens")
#
# data
#
print("coins:", coins)
print("total:", total)
print()
#
# declare variables
#
#
# constraints
#
x, ss = subset_sum(solver, coins, total)
def main():
# Create the solver.
solver = pywrapcp.Solver("Einav puzzle")
#
# data
#
# small problem
# rows = 3;
# cols = 3;
# data = [
# [ 33, 30, -10],
# [-16, 19, 9],
# [-17, -12, -14]
# ]
# Full problem
rows = 27