Flexible jobshop problem with sequence dependent setup times - python

How do I add setup time depending on the sequence to the flexible job shop scheduling optimization problem. The code I have is as follows:
import collections
from ortools.sat.python import cp_model
class SolutionPrinter(cp_model.CpSolverSolutionCallback):
"""Print intermediate solutions."""
def __init__(self):
cp_model.CpSolverSolutionCallback.__init__(self)
self.__solution_count = 0
def on_solution_callback(self):
"""Called at each new solution."""
print('Solution %i, time = %f s, objective = %i' %
(self.__solution_count, self.WallTime(), self.ObjectiveValue()))
self.__solution_count += 1
def flexible_jobshop():
# Data part.
jobs = [ # task = (processing_time, machine_id)
[ # Job 0
[(3, 0), (1, 1), (5, 2)], # task 0 with 3 alternatives
[(2, 0), (4, 1), (6, 2)], # task 1 with 3 alternatives
[(2, 0), (3, 1), (1, 2)], # task 2 with 3 alternatives
],
[ # Job 1
[(2, 0), (3, 1), (4, 2)],
[(1, 0), (5, 1), (4, 2)],
],
[ # Job 2
[(2, 0), (1, 1), (4, 2)],
[(2, 0), (3, 1), (4, 2)],
[(3, 0), (1, 1), (5, 2)],
],
]
num_jobs = len(jobs)
all_jobs = range(num_jobs)
num_machines = 3
all_machines = range(num_machines)
# Model the flexible jobshop problem.
model = cp_model.CpModel()
horizon = 0
for job in jobs:
for task in job:
max_task_duration = 0
for alternative in task:
max_task_duration = max(max_task_duration, alternative[0])
horizon += max_task_duration
print('Horizon = %i' % horizon)
# Global storage of variables.
intervals_per_resources = collections.defaultdict(list)
starts = {} # indexed by (job_id, task_id).
presences = {} # indexed by (job_id, task_id, alt_id).
job_ends = []
# Scan the jobs and create the relevant variables and intervals.
for job_id in all_jobs:
job = jobs[job_id]
num_tasks = len(job)
previous_end = None
for task_id in range(num_tasks):
task = job[task_id]
min_duration = task[0][0]
max_duration = task[0][0]
num_alternatives = len(task)
all_alternatives = range(num_alternatives)
for alt_id in range(1, num_alternatives):
alt_duration = task[alt_id][0]
min_duration = min(min_duration, alt_duration)
max_duration = max(max_duration, alt_duration)
# Create main interval for the task.
suffix_name = '_j%i_t%i' % (job_id, task_id)
start = model.NewIntVar(0, horizon, 'start' + suffix_name)
duration = model.NewIntVar(min_duration, max_duration,
'duration' + suffix_name)
end = model.NewIntVar(0, horizon, 'end' + suffix_name)
interval = model.NewIntervalVar(start, duration, end,
'interval' + suffix_name)
# Store the start for the solution.
starts[(job_id, task_id)] = start
# Add precedence with previous task in the same job.
if previous_end is not None:
model.Add(start >= previous_end)
previous_end = end
# Create alternative intervals.
if num_alternatives > 1:
l_presences = []
for alt_id in all_alternatives:
alt_suffix = '_j%i_t%i_a%i' % (job_id, task_id, alt_id)
l_presence = model.NewBoolVar('presence' + alt_suffix)
l_start = model.NewIntVar(0, horizon, 'start' + alt_suffix)
l_duration = task[alt_id][0]
l_end = model.NewIntVar(0, horizon, 'end' + alt_suffix)
l_interval = model.NewOptionalIntervalVar(
l_start, l_duration, l_end, l_presence,
'interval' + alt_suffix)
l_presences.append(l_presence)
# Link the master variables with the local ones.
model.Add(start == l_start).OnlyEnforceIf(l_presence)
model.Add(duration == l_duration).OnlyEnforceIf(l_presence)
model.Add(end == l_end).OnlyEnforceIf(l_presence)
# Add the local interval to the right machine.
intervals_per_resources[task[alt_id][1]].append(l_interval)
# Store the presences for the solution.
presences[(job_id, task_id, alt_id)] = l_presence
# Select exactly one presence variable.
model.Add(sum(l_presences) == 1)
else:
intervals_per_resources[task[0][1]].append(interval)
presences[(job_id, task_id, 0)] = model.NewConstant(1)
job_ends.append(previous_end)
# Create machines constraints.
for machine_id in all_machines:
intervals = intervals_per_resources[machine_id]
if len(intervals) > 1:
model.AddNoOverlap(intervals)
# Makespan objective
makespan = model.NewIntVar(0, horizon, 'makespan')
model.AddMaxEquality(makespan, job_ends)
model.Minimize(makespan)
# Solve model.
solver = cp_model.CpSolver()
solution_printer = SolutionPrinter()
status = solver.Solve(model, solution_printer)
# Print final solution.
for job_id in all_jobs:
print('Job %i:' % job_id)
for task_id in range(len(jobs[job_id])):
start_value = solver.Value(starts[(job_id, task_id)])
machine = -1
duration = -1
selected = -1
for alt_id in range(len(jobs[job_id][task_id])):
if solver.Value(presences[(job_id, task_id, alt_id)]):
duration = jobs[job_id][task_id][alt_id][0]
machine = jobs[job_id][task_id][alt_id][1]
selected = alt_id
print(
' task_%i_%i starts at %i (alt %i, machine %i, duration %i)' %
(job_id, task_id, start_value, selected, machine, duration))
print('Solve status: %s' % solver.StatusName(status))
print('Optimal objective value: %i' % solver.ObjectiveValue())
flexible_jobshop()

This is implemented in the following example:
https://github.com/google/or-tools/blob/stable/examples/python/jobshop_ft06_distance_sat.py

Related

OR-Tools CVRP_reload with Pickup-Delivery

I use or-tools to solve problems such as picking up products from EC warehouses.
What I want to do is very similar to the cvrp_reload sample.
https://github.com/google/or-tools/blob/stable/ortools/constraint_solver/samples/cvrp_reload.py
The difference from the sample is that instead of bringing the load back to the depot, the picked up load is placed at a specific delivery point.
I changed the source of cvrp_reload while looking at the Pickup & Delivery sample in the guide. (Assuming that Node.1 to 5 are the delivery points)
Add Pickup & Delivery point list to data
data['pickups_deliveries'] = [
[6, 1], [7, 1],
[8, 1], [9, 1],
[10, 2], [11, 2],
[12, 2], [13, 3],
[14, 3], [15, 3],
[16, 4], [17, 4],
[18, 4], [19, 5],
[20, 5], [21, 5]
]
Simplification of demands and time_windows in data
data['demands'] = \
[0, # depot
-_capacity, # unload depot_first
-_capacity, # unload depot_second
-_capacity, # unload depot_third
-_capacity, # unload depot_fourth
-_capacity, # unload depot_fifth
4, 4, # 1, 2 changed from original
4, 4, # 3, 4 changed from original
4, 4, # 5, 6 changed from original
4, 4, # 7, 8 changed from original
4, 4, # 9,10 changed from original
4, 4, # 11,12 changed from original
4, 4, # 13, 14 changed from original
4, 4] # 15, 16 changed from original
data['time_windows'] = \
[(0, 0), # depot
(0, 1500), # unload depot_first. changed from original
(0, 1500), # unload depot_second. changed from original
(0, 1500), # unload depot_third. changed from original
(0, 1500), # unload depot_fourth. changed from original
(0, 1500), # unload depot_fifth. changed from original
(0, 750), (0, 750), # 1, 2 changed from original
(0, 750), (0, 750), # 3, 4 changed from original
(0, 750), (0, 750), # 5, 6 changed from original
(0, 750), (0, 750), # 7, 8 changed from original
(0, 750), (0, 750), # 9, 10 changed from original
(750, 1500), (750, 1500), # 11, 12 changed from original
(750, 1500), (750, 1500), # 13, 14 changed from original
(750, 1500), (750, 1500)] # 15, 16 changed from original
Define transportation requests in add_distance_dimension function
for request in data['pickups_deliveries']:
pickup_index = manager.NodeToIndex(request[0])
delivery_index = manager.NodeToIndex(request[1])
routing.AddPickupAndDelivery(pickup_index, delivery_index)
routing.solver().Add(
routing.VehicleVar(pickup_index) == routing.VehicleVar(
delivery_index))
routing.solver().Add(
distance_dimension.CumulVar(pickup_index) <=
distance_dimension.CumulVar(delivery_index))
Running this code will drop all nodes. However, limiting the list of P&Ds to just a few gives a solution.
data['pickups_deliveries'] = [
[6, 1], [7, 1],
[8, 1], [9, 1],
[10, 2], [11, 2],
# [12, 2], [13, 3],
# [14, 3], [15, 3],
# [16, 4], [17, 4],
# [18, 4], [19, 5],
# [20, 5], [21, 5]
]
Please give me a hint where my method is wrong.
This is the entire code. Thanks for reading my question.
from functools import partial
from ortools.constraint_solver import pywrapcp
from ortools.constraint_solver import routing_enums_pb2
###########################
# Problem Data Definition #
###########################
def create_data_model():
"""Stores the data for the problem"""
data = {}
_capacity = 15
# Locations in block unit
_locations = [
(4, 4), # depot
(4, 4), # unload depot_first
(4, 4), # unload depot_second
(4, 4), # unload depot_third
(4, 4), # unload depot_fourth
(4, 4), # unload depot_fifth
(2, 0),
(8, 0), # locations to visit
(0, 1),
(1, 1),
(5, 2),
(7, 2),
(3, 3),
(6, 3),
(5, 5),
(8, 5),
(1, 6),
(2, 6),
(3, 7),
(6, 7),
(0, 8),
(7, 8)
]
# Compute locations in meters using the block dimension defined as follow
# Manhattan average block: 750ft x 264ft -> 228m x 80m
# here we use: 114m x 80m city block
data['locations'] = [(l[0] * 114, l[1] * 80) for l in _locations]
data['num_locations'] = len(data['locations'])
data['demands'] = \
[0, # depot
-_capacity, # unload depot_first
-_capacity, # unload depot_second
-_capacity, # unload depot_third
-_capacity, # unload depot_fourth
-_capacity, # unload depot_fifth
4, 4, # 1, 2 changed from original
4, 4, # 3, 4 changed from original
4, 4, # 5, 6 changed from original
4, 4, # 7, 8 changed from original
4, 4, # 9,10 changed from original
4, 4, # 11,12 changed from original
4, 4, # 13, 14 changed from original
4, 4] # 15, 16 changed from original
data['time_per_demand_unit'] = 5 # 5 minutes/unit
data['time_windows'] = \
[(0, 0), # depot
(0, 1500), # unload depot_first. changed from original
(0, 1500), # unload depot_second. changed from original
(0, 1500), # unload depot_third. changed from original
(0, 1500), # unload depot_fourth. changed from original
(0, 1500), # unload depot_fifth. changed from original
(0, 750), (0, 750), # 1, 2 changed from original
(0, 750), (0, 750), # 3, 4 changed from original
(0, 750), (0, 750), # 5, 6 changed from original
(0, 750), (0, 750), # 7, 8 changed from original
(0, 750), (0, 750), # 9, 10 changed from original
(750, 1500), (750, 1500), # 11, 12 changed from original
(750, 1500), (750, 1500), # 13, 14 changed from original
(750, 1500), (750, 1500)] # 15, 16 changed from original
#added p&d list.##############################
data['pickups_deliveries'] = [
[6, 1], [7, 1],
[8, 1], [9, 1],
[10, 2], [11, 2],
[12, 2], [13, 3],
[14, 3], [15, 3],
[16, 4], [17, 4],
[18, 4], [19, 5],
[20, 5], [21, 5]
]
##############################################
data['num_vehicles'] = 3
data['vehicle_capacity'] = _capacity
data['vehicle_max_distance'] = 10_000
data['vehicle_max_time'] = 1_500
data[
'vehicle_speed'] = 5 * 60 / 3.6 # Travel speed: 5km/h to convert in m/min
data['depot'] = 0
return data
#######################
# Problem Constraints #
#######################
def manhattan_distance(position_1, position_2):
"""Computes the Manhattan distance between two points"""
return (abs(position_1[0] - position_2[0]) +
abs(position_1[1] - position_2[1]))
def create_distance_evaluator(data):
"""Creates callback to return distance between points."""
_distances = {}
# precompute distance between location to have distance callback in O(1)
for from_node in range(data['num_locations']):
_distances[from_node] = {}
for to_node in range(data['num_locations']):
if from_node == to_node:
_distances[from_node][to_node] = 0
# Forbid start/end/reload node to be consecutive.
elif from_node in range(6) and to_node in range(6):
_distances[from_node][to_node] = data['vehicle_max_distance']
else:
_distances[from_node][to_node] = (manhattan_distance(
data['locations'][from_node], data['locations'][to_node]))
def distance_evaluator(manager, from_node, to_node):
"""Returns the manhattan distance between the two nodes"""
return _distances[manager.IndexToNode(from_node)][manager.IndexToNode(
to_node)]
return distance_evaluator
def add_distance_dimension(routing, manager, data, distance_evaluator_index):
"""Add Global Span constraint"""
distance = 'Distance'
routing.AddDimension(
distance_evaluator_index,
0, # null slack
data['vehicle_max_distance'], # maximum distance per vehicle
True, # start cumul to zero
distance)
distance_dimension = routing.GetDimensionOrDie(distance)
# Try to minimize the max distance among vehicles.
# /!\ It doesn't mean the standard deviation is minimized
distance_dimension.SetGlobalSpanCostCoefficient(100)
# Define Transportation Requests. #######################################
for request in data['pickups_deliveries']:
pickup_index = manager.NodeToIndex(request[0])
delivery_index = manager.NodeToIndex(request[1])
routing.AddPickupAndDelivery(pickup_index, delivery_index)
routing.solver().Add(
routing.VehicleVar(pickup_index) == routing.VehicleVar(
delivery_index))
routing.solver().Add(
distance_dimension.CumulVar(pickup_index) <=
distance_dimension.CumulVar(delivery_index))
##########################################################################
def create_demand_evaluator(data):
"""Creates callback to get demands at each location."""
_demands = data['demands']
def demand_evaluator(manager, from_node):
"""Returns the demand of the current node"""
return _demands[manager.IndexToNode(from_node)]
return demand_evaluator
def add_capacity_constraints(routing, manager, data, demand_evaluator_index):
"""Adds capacity constraint"""
vehicle_capacity = data['vehicle_capacity']
capacity = 'Capacity'
routing.AddDimension(
demand_evaluator_index,
vehicle_capacity,
vehicle_capacity,
True, # start cumul to zero
capacity)
# Add Slack for reseting to zero unload depot nodes.
# e.g. vehicle with load 10/15 arrives at node 1 (depot unload)
# so we have CumulVar = 10(current load) + -15(unload) + 5(slack) = 0.
capacity_dimension = routing.GetDimensionOrDie(capacity)
# Allow to drop reloading nodes with zero cost.
for node in [1, 2, 3, 4, 5]:
node_index = manager.NodeToIndex(node)
routing.AddDisjunction([node_index], 0)
# Allow to drop regular node with a cost.
for node in range(6, len(data['demands'])):
node_index = manager.NodeToIndex(node)
capacity_dimension.SlackVar(node_index).SetValue(0)
routing.AddDisjunction([node_index], 100_000)
def create_time_evaluator(data):
"""Creates callback to get total times between locations."""
def service_time(data, node):
"""Gets the service time for the specified location."""
return abs(data['demands'][node]) * data['time_per_demand_unit']
def travel_time(data, from_node, to_node):
"""Gets the travel times between two locations."""
if from_node == to_node:
travel_time = 0
else:
travel_time = manhattan_distance(
data['locations'][from_node], data['locations'][to_node]) / data['vehicle_speed']
return travel_time
_total_time = {}
# precompute total time to have time callback in O(1)
for from_node in range(data['num_locations']):
_total_time[from_node] = {}
for to_node in range(data['num_locations']):
if from_node == to_node:
_total_time[from_node][to_node] = 0
else:
_total_time[from_node][to_node] = int(
service_time(data, from_node) + travel_time(
data, from_node, to_node))
def time_evaluator(manager, from_node, to_node):
"""Returns the total time between the two nodes"""
return _total_time[manager.IndexToNode(from_node)][manager.IndexToNode(
to_node)]
return time_evaluator
def add_time_window_constraints(routing, manager, data, time_evaluator):
"""Add Time windows constraint"""
time = 'Time'
max_time = data['vehicle_max_time']
routing.AddDimension(
time_evaluator,
max_time, # allow waiting time
max_time, # maximum time per vehicle
False, # don't force start cumul to zero since we are giving TW to start nodes
time)
time_dimension = routing.GetDimensionOrDie(time)
# Add time window constraints for each location except depot
# and 'copy' the slack var in the solution object (aka Assignment) to print it
for location_idx, time_window in enumerate(data['time_windows']):
if location_idx == 0:
continue
index = manager.NodeToIndex(location_idx)
time_dimension.CumulVar(index).SetRange(time_window[0], time_window[1])
routing.AddToAssignment(time_dimension.SlackVar(index))
# Add time window constraints for each vehicle start node
# and 'copy' the slack var in the solution object (aka Assignment) to print it
for vehicle_id in range(data['num_vehicles']):
index = routing.Start(vehicle_id)
time_dimension.CumulVar(index).SetRange(data['time_windows'][0][0],
data['time_windows'][0][1])
routing.AddToAssignment(time_dimension.SlackVar(index))
# Warning: Slack var is not defined for vehicle's end node
#routing.AddToAssignment(time_dimension.SlackVar(self.routing.End(vehicle_id)))
###########
# Printer #
###########
def print_solution(data, manager, routing, assignment): # pylint:disable=too-many-locals
"""Prints assignment on console"""
print(f'Objective: {assignment.ObjectiveValue()}')
total_distance = 0
total_load = 0
total_time = 0
capacity_dimension = routing.GetDimensionOrDie('Capacity')
time_dimension = routing.GetDimensionOrDie('Time')
dropped = []
for order in range(6, routing.nodes()):
index = manager.NodeToIndex(order)
if assignment.Value(routing.NextVar(index)) == index:
dropped.append(order)
print(f'dropped orders: {dropped}')
for reload in range(1, 6):
index = manager.NodeToIndex(reload)
if assignment.Value(routing.NextVar(index)) == index:
dropped.append(reload)
print(f'dropped reload stations: {dropped}')
for vehicle_id in range(data['num_vehicles']):
index = routing.Start(vehicle_id)
plan_output = f'Route for vehicle {vehicle_id}:\n'
distance = 0
while not routing.IsEnd(index):
load_var = capacity_dimension.CumulVar(index)
time_var = time_dimension.CumulVar(index)
plan_output += ' {0} Load({1}) Time({2},{3}) ->'.format(
manager.IndexToNode(index),
assignment.Value(load_var),
assignment.Min(time_var), assignment.Max(time_var))
previous_index = index
index = assignment.Value(routing.NextVar(index))
distance += routing.GetArcCostForVehicle(previous_index, index,
vehicle_id)
load_var = capacity_dimension.CumulVar(index)
time_var = time_dimension.CumulVar(index)
plan_output += ' {0} Load({1}) Time({2},{3})\n'.format(
manager.IndexToNode(index),
assignment.Value(load_var),
assignment.Min(time_var), assignment.Max(time_var))
plan_output += f'Distance of the route: {distance}m\n'
plan_output += f'Load of the route: {assignment.Value(load_var)}\n'
plan_output += f'Time of the route: {assignment.Value(time_var)}min\n'
print(plan_output)
total_distance += distance
total_load += assignment.Value(load_var)
total_time += assignment.Value(time_var)
print('Total Distance of all routes: {}m'.format(total_distance))
print('Total Load of all routes: {}'.format(total_load))
print('Total Time of all routes: {}min'.format(total_time))
########
# Main #
########
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 Distance constraint to minimize the longuest route
add_distance_dimension(routing, manager, data, distance_evaluator_index)
# Add Capacity constraint
demand_evaluator_index = routing.RegisterUnaryTransitCallback(
partial(create_demand_evaluator(data), manager))
add_capacity_constraints(routing, manager, data, demand_evaluator_index)
# Add Time Window constraint
time_evaluator_index = routing.RegisterTransitCallback(
partial(create_time_evaluator(data), manager))
add_time_window_constraints(routing, manager, data, time_evaluator_index)
# Setting first solution heuristic (cheapest addition).
search_parameters = pywrapcp.DefaultRoutingSearchParameters()
search_parameters.first_solution_strategy = (
routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC) # pylint: disable=no-member
search_parameters.local_search_metaheuristic = (
routing_enums_pb2.LocalSearchMetaheuristic.GUIDED_LOCAL_SEARCH)
search_parameters.time_limit.FromSeconds(3)
# Solve the problem.
solution = routing.SolveWithParameters(search_parameters)
if solution:
print_solution(data, manager, routing, solution)
else:
print("No solution found !")
if __name__ == '__main__':
main()
I got advice comments from Mizux and fixed the code.
I have a pair of Pickup and Delivery points and map the same Delivery points to the same coordinates. Thanks to Mizux for the great advice.
from functools import partial
from ortools.constraint_solver import pywrapcp
from ortools.constraint_solver import routing_enums_pb2
###########################
# Problem Data Definition #
###########################
def create_data_model():
"""Stores the data for the problem"""
data = {}
_capacity = 15
# Locations in block unit
_locations = [
(4, 4), # depot
(7, 3), (5, 5), #1,2 pickup_loc, delivery_loc
(6, 6), (5, 5), #3,4 pickup_loc, delivery_loc
(3, 4), (5, 5), #5,6 pickup_loc, delivery_loc
(3, 5), (3, 3), #7,8 pickup_loc, delivery_loc
(2, 7), (3, 3), #9,10 pickup_loc, delivery_loc
(6, 3), (3, 3), #11,12 pickup_loc, delivery_loc
(8, 5), (7, 7), #13,14 pickup_loc, delivery_loc
(2, 6), (7, 7), #15,16 pickup_loc, delivery_loc
(6, 7), (7, 7), #17,18 pickup_loc, delivery_loc
(7, 8), (8, 8), #19,20 pickup_loc, delivery_loc
(5, 7), (8, 8), #20,21 pickup_loc, delivery_loc
]
# Compute locations in meters using the block dimension defined as follow
# Manhattan average block: 750ft x 264ft -> 228m x 80m
# here we use: 114m x 80m city block
data['locations'] = [(l[0] * 114, l[1] * 80) for l in _locations]
data['num_locations'] = len(data['locations'])
data['demands'] = \
[0, # depot
4, -4, # 1, 2 load, unload
4, -4, # 3, 4 load, unload
4, -4, # 5, 6 load, unload
4, -4, # 7, 8 load, unload
4, -4, # 9, 10 load, unload
4, -4, # 11, 12 load, unload
4, -4, # 13, 14 load, unload
4, -4, # 15, 16 load, unload
4, -4, # 17, 18 load, unload
4, -4, # 19, 20 load, unload
4, -4] # 21, 22 load, unload
data['time_per_demand_unit'] = 5 # 5 minutes/unit
data['time_windows'] = \
[(0, 0), # depot
(0, 1500), (0, 750), # 1, 2 specify delivery time
(0, 1500), (0, 750), # 3, 4 specify delivery time
(0, 1500), (0, 750), # 5, 6 specify delivery time
(0, 1500), (0, 750), # 7, 8 specify delivery time
(0, 1500), (0, 750), # 9, 10 specify delivery time
(0, 1500), (0, 750), # 11, 12 specify delivery time
(0, 1500), (750, 1500), # 13, 14 specify delivery time
(0, 1500), (750, 1500), # 15, 16 specify delivery time
(0, 1500), (750, 1500), # 17, 18 specify delivery time
(0, 1500), (750, 1500), # 19, 20 specify delivery time
(0, 1500), (750, 1500)] # 21, 22 specify delivery time
#added p&d list.##############################
data['pickups_deliveries'] = [
[1, 2], [3, 4],
[5, 6], [7, 8],
[9, 10], [11, 12],
[13, 14], [15, 16],
[17, 18], [19, 20],
[21, 22]
]
##############################################
data['num_vehicles'] = 3
data['vehicle_capacity'] = _capacity
data['vehicle_max_distance'] = 10_000
data['vehicle_max_time'] = 1_500
data[
'vehicle_speed'] = 5 * 60 / 3.6 # Travel speed: 5km/h to convert in m/min
data['depot'] = 0
return data
#######################
# Problem Constraints #
#######################
def manhattan_distance(position_1, position_2):
"""Computes the Manhattan distance between two points"""
return (abs(position_1[0] - position_2[0]) +
abs(position_1[1] - position_2[1]))
def create_distance_evaluator(data):
"""Creates callback to return distance between points."""
_distances = {}
# precompute distance between location to have distance callback in O(1)
for from_node in range(data['num_locations']):
_distances[from_node] = {}
for to_node in range(data['num_locations']):
if from_node == to_node:
_distances[from_node][to_node] = 0
# commented out for unused duplicate depot######################
# elif from_node in range(6) and to_node in range(6):
# _distances[from_node][to_node] = data['vehicle_max_distance']
else:
_distances[from_node][to_node] = (manhattan_distance(
data['locations'][from_node], data['locations'][to_node]))
def distance_evaluator(manager, from_node, to_node):
"""Returns the manhattan distance between the two nodes"""
return _distances[manager.IndexToNode(from_node)][manager.IndexToNode(
to_node)]
return distance_evaluator
def add_distance_dimension(routing, manager, data, distance_evaluator_index):
"""Add Global Span constraint"""
distance = 'Distance'
routing.AddDimension(
distance_evaluator_index,
0, # null slack
data['vehicle_max_distance'], # maximum distance per vehicle
True, # start cumul to zero
distance)
distance_dimension = routing.GetDimensionOrDie(distance)
# Try to minimize the max distance among vehicles.
# /!\ It doesn't mean the standard deviation is minimized
distance_dimension.SetGlobalSpanCostCoefficient(100)
# Define Transportation Requests. #######################################
for request in data['pickups_deliveries']:
pickup_index = manager.NodeToIndex(request[0])
delivery_index = manager.NodeToIndex(request[1])
routing.AddPickupAndDelivery(pickup_index, delivery_index)
routing.solver().Add(
routing.VehicleVar(pickup_index) == routing.VehicleVar(
delivery_index))
routing.solver().Add(
distance_dimension.CumulVar(pickup_index) <=
distance_dimension.CumulVar(delivery_index))
##########################################################################
def create_demand_evaluator(data):
"""Creates callback to get demands at each location."""
_demands = data['demands']
def demand_evaluator(manager, from_node):
"""Returns the demand of the current node"""
return _demands[manager.IndexToNode(from_node)]
return demand_evaluator
def add_capacity_constraints(routing, manager, data, demand_evaluator_index):
"""Adds capacity constraint"""
vehicle_capacity = data['vehicle_capacity']
capacity = 'Capacity'
routing.AddDimension(
demand_evaluator_index,
vehicle_capacity,
vehicle_capacity,
True, # start cumul to zero
capacity)
# Add Slack for reseting to zero unload depot nodes.
# e.g. vehicle with load 10/15 arrives at node 1 (depot unload)
# so we have CumulVar = 10(current load) + -15(unload) + 5(slack) = 0.
capacity_dimension = routing.GetDimensionOrDie(capacity)
# Allow to drop reloading nodes with zero cost.
# for node in [1, 2, 3, 4, 5]:
# node_index = manager.NodeToIndex(node)
# routing.AddDisjunction([node_index], 0)
# Allow to drop regular node with a cost. (Pickup & Delivery node)
for node in range(1, len(data['demands'])):
node_index = manager.NodeToIndex(node)
capacity_dimension.SlackVar(node_index).SetValue(0)
routing.AddDisjunction([node_index], 100_000)
def create_time_evaluator(data):
"""Creates callback to get total times between locations."""
def service_time(data, node):
"""Gets the service time for the specified location."""
return abs(data['demands'][node]) * data['time_per_demand_unit']
def travel_time(data, from_node, to_node):
"""Gets the travel times between two locations."""
if from_node == to_node:
travel_time = 0
else:
travel_time = manhattan_distance(
data['locations'][from_node], data['locations'][to_node]) / data['vehicle_speed']
return travel_time
_total_time = {}
# precompute total time to have time callback in O(1)
for from_node in range(data['num_locations']):
_total_time[from_node] = {}
for to_node in range(data['num_locations']):
if from_node == to_node:
_total_time[from_node][to_node] = 0
else:
_total_time[from_node][to_node] = int(
service_time(data, from_node) + travel_time(
data, from_node, to_node))
def time_evaluator(manager, from_node, to_node):
"""Returns the total time between the two nodes"""
return _total_time[manager.IndexToNode(from_node)][manager.IndexToNode(
to_node)]
return time_evaluator
def add_time_window_constraints(routing, manager, data, time_evaluator):
"""Add Time windows constraint"""
time = 'Time'
max_time = data['vehicle_max_time']
routing.AddDimension(
time_evaluator,
max_time, # allow waiting time
max_time, # maximum time per vehicle
False, # don't force start cumul to zero since we are giving TW to start nodes
time)
time_dimension = routing.GetDimensionOrDie(time)
# Add time window constraints for each location except depot
# and 'copy' the slack var in the solution object (aka Assignment) to print it
for location_idx, time_window in enumerate(data['time_windows']):
if location_idx == 0:
continue
index = manager.NodeToIndex(location_idx)
time_dimension.CumulVar(index).SetRange(time_window[0], time_window[1])
routing.AddToAssignment(time_dimension.SlackVar(index))
# Add time window constraints for each vehicle start node
# and 'copy' the slack var in the solution object (aka Assignment) to print it
for vehicle_id in range(data['num_vehicles']):
index = routing.Start(vehicle_id)
time_dimension.CumulVar(index).SetRange(data['time_windows'][0][0],
data['time_windows'][0][1])
routing.AddToAssignment(time_dimension.SlackVar(index))
# Warning: Slack var is not defined for vehicle's end node
#routing.AddToAssignment(time_dimension.SlackVar(self.routing.End(vehicle_id)))
###########
# Printer #
###########
def print_solution(data, manager, routing, assignment): # pylint:disable=too-many-locals
"""Prints assignment on console"""
print(f'Objective: {assignment.ObjectiveValue()}')
total_distance = 0
total_load = 0
total_time = 0
capacity_dimension = routing.GetDimensionOrDie('Capacity')
time_dimension = routing.GetDimensionOrDie('Time')
dropped = []
for order in range(6, routing.nodes()):
index = manager.NodeToIndex(order)
if assignment.Value(routing.NextVar(index)) == index:
dropped.append(order)
print(f'dropped orders: {dropped}')
for reload in range(1, 6):
index = manager.NodeToIndex(reload)
if assignment.Value(routing.NextVar(index)) == index:
dropped.append(reload)
print(f'dropped reload stations: {dropped}')
for vehicle_id in range(data['num_vehicles']):
index = routing.Start(vehicle_id)
plan_output = f'Route for vehicle {vehicle_id}:\n'
distance = 0
while not routing.IsEnd(index):
load_var = capacity_dimension.CumulVar(index)
time_var = time_dimension.CumulVar(index)
plan_output += ' {0} Load({1}) Time({2},{3}) ->'.format(
manager.IndexToNode(index),
assignment.Value(load_var),
assignment.Min(time_var), assignment.Max(time_var))
previous_index = index
index = assignment.Value(routing.NextVar(index))
distance += routing.GetArcCostForVehicle(previous_index, index,
vehicle_id)
load_var = capacity_dimension.CumulVar(index)
time_var = time_dimension.CumulVar(index)
plan_output += ' {0} Load({1}) Time({2},{3})\n'.format(
manager.IndexToNode(index),
assignment.Value(load_var),
assignment.Min(time_var), assignment.Max(time_var))
plan_output += f'Distance of the route: {distance}m\n'
plan_output += f'Load of the route: {assignment.Value(load_var)}\n'
plan_output += f'Time of the route: {assignment.Value(time_var)}min\n'
print(plan_output)
total_distance += distance
total_load += assignment.Value(load_var)
total_time += assignment.Value(time_var)
print('Total Distance of all routes: {}m'.format(total_distance))
print('Total Load of all routes: {}'.format(total_load))
print('Total Time of all routes: {}min'.format(total_time))
########
# Main #
########
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 Distance constraint to minimize the longuest route
add_distance_dimension(routing, manager, data, distance_evaluator_index)
# Add Capacity constraint
demand_evaluator_index = routing.RegisterUnaryTransitCallback(
partial(create_demand_evaluator(data), manager))
add_capacity_constraints(routing, manager, data, demand_evaluator_index)
# Add Time Window constraint
time_evaluator_index = routing.RegisterTransitCallback(
partial(create_time_evaluator(data), manager))
add_time_window_constraints(routing, manager, data, time_evaluator_index)
# Setting first solution heuristic (cheapest addition).
search_parameters = pywrapcp.DefaultRoutingSearchParameters()
search_parameters.first_solution_strategy = (
routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC) # pylint: disable=no-member
search_parameters.local_search_metaheuristic = (
routing_enums_pb2.LocalSearchMetaheuristic.GUIDED_LOCAL_SEARCH)
search_parameters.time_limit.FromSeconds(3)
# Solve the problem.
solution = routing.SolveWithParameters(search_parameters)
if solution:
print_solution(data, manager, routing, solution)
else:
print("No solution found !")
if __name__ == '__main__':
main()

AttributeError: object has no attribute in for loop

In my simulation of a FIFO algorithm, I am currently trying to create an object for each of the seven simulated tasks, which will later be used to display some time parameters graphically in Excel. So I create all objects in a for loop, and in another one, I execute with each of these objects the corresponding function for transfer to Excel in another class.
But in the second for loop I always get the error message
File "G:\Schedulibg\PyScheduler\pyFIFOAlgorithm.py", line 48, in sched_StartScheduler
self.worksheet1.write('A' + str((self.tasksAdded * 4) + 1), 'Turn Around Time:')
AttributeError: 'pyFIFOAlgorithm' object has no attribute 'worksheet1' "
I don't know why it looks for worksheet1 in pyFIFOAlgorithm, although the object is from pyToExcel and the corresponding method comes from the class. I have already changed the name of taskTest, because I read that this could also be a problem. Before it was just called tsk.
import pyScheduler
from pyToExcel import pyToExcel
def alg_SortArrivedTasks(arrivedTasks):
sortedTasks = []
for taskx in arrivedTasks:
sortedTasks.append(taskx)
sortedTasks.sort(key=lambda x: x.tskArrival)
return sortedTasks
class pyFIFOAlgorithm(pyScheduler.pyScheduler):
def __init__(self, taskSet, sched_Alg, idleTime):
self.alg_Identifier = 2
self.alg_Name = 'FIFO'
self.completionTime = 0
self.turnAroundTime = 0
self.totalWaitingTime = 0
self.totalTurnAroundTime = 0
self.tasksAdded = 0
super(pyFIFOAlgorithm, self).__init__(taskSet, sched_Alg, idleTime)
def sched_StartScheduler(self, taskSet):
self.sched_SchedulingPoints.insert(0, self.sched_Clock)
taskList = []
taskNumber = 1
for task in alg_SortArrivedTasks(taskSet):
print("\nArrival time of ", task.tskName, ": ", task.tskArrival)
self.sched_ExecuteAvailableTasks(task)
self.completionTime = task.completiontime
self.turnAroundTime = ((self.completionTime) - task.tskArrival)
#taskList.append(pyToExcel(taskNumber, self.completionTime, self.turnAroundTime))
self.totalTurnAroundTime += self.turnAroundTime
print("Turn Around Time: ""{:.2f}".format(self.turnAroundTime))
print("Completion Time: ""{:.2f}".format(self.completionTime))
taskList.append(pyToExcel(task.tskName, self.completionTime, self.turnAroundTime))
for taskTest in taskList:
pyToExcel.inputData(pyToExcel, taskNumber, taskTest.turn, taskTest.completion) #Line with Error
taskNumber += 1
print("\nAll tasks executed at: ", "{:.2f}".format(self.sched_Clock))
print("Average Waiting Time: ", "{:.2f}".format((self.totalWaitingTime /len(taskSet))))
print("Average Turn Around Time: ", "{:.2f}".format((self.totalTurnAroundTime / len(taskSet))))
self.worksheet1.write('A' + str((self.tasksAdded * 4) + 1), 'Turn Around Time:')
self.worksheet1.write('B' + str((self.tasksAdded * 4) + 1), self.totalTurnAroundTime / len(taskSet))
self.workbook.close()
import xlsxwriter
class pyToExcel:
def __init__(self, task, completion, turn):
self.task = task
self.completion = completion
self.turn = turn
workbook = xlsxwriter.Workbook('AlgorithmData.xlsx')
worksheet1 = workbook.add_worksheet('FIFO')
worksheet2 = workbook.add_worksheet('Graphics')
cell_format = workbook.add_format(
{
"border": 1,
"border_color": "#000000"
}
)
cell_format.set_font_color('green')
cell_format.set_bold()
cell_format.set_align('center')
cell_format.set_align('vcenter')
worksheet1.set_column(0, 0, 17)
worksheet1.set_column(1, 1, 12)
worksheet2.write('A' + str(1), 'Task', cell_format)
worksheet2.write('B' + str(1), 'Completion Time', cell_format)
worksheet2.write('C' + str(1), 'Turn Around Time', cell_format)
worksheet2.write('D' + str(1), 'Waiting Time', cell_format)
worksheet2.set_column(0, 0, 4)
worksheet2.set_column(1, 1, 15)
worksheet2.set_column(2, 2, 17)
worksheet2.set_column(3, 3, 12)
def inputData(self, task, turnaround, completion):
pyToExcel.worksheet1.write('A' + str((task * 4) + 1), 'Turn Around Time:')
pyToExcel.worksheet1.write('B' + str((task * 4) + 1), turnaround)
pyToExcel.worksheet1.write('A' + str((task * 4) + 2), 'Completion Time:')
pyToExcel.worksheet1.write('B' + str((task * 4) + 2), completion)
pyToExcel.worksheet2.write('A' + str((task + 1)), task)
pyToExcel.worksheet2.write('B' + str((task + 1)), completion)
pyToExcel.worksheet2.write('C' + str((task + 1)), turnaround)
pyToExcel.worksheet2.write('D' + str((task + 1)), 'waiting time')
It's looking up the worksheet1 attribute because you told it to:
self.worksheet1.write('A' + str((self.tasksAdded * 4) + 1), 'Turn Around Time:')
^^^^^^^^^^
That line, which is also quoted in the error message, is line 48 of the program, as per the text you quote. The line you mark as being the location of the error is line 38, but the traceback certainly says line 48.
The method is part of the class definition for pyFIFOAlgorithm, so self is almost certainly a pyFIFOAlgorithm object. Perhaps self was a typo.

Different output for same function

I am implemented a KNN algorithm in python.
import math
#height,width,deepth,thickness,Label
data_set = [(2,9,8,4, "Good"),
(3,7,7,9, "Bad"),
(10,3,10,3, "Good"),
(2,9,6,10, "Good"),
(3,3,2,5, "Bad"),
(2,8,5,6, "Bad"),
(7,2,3,10, "Good"),
(1,10,8,10, "Bad"),
(2,8,1,10, "Good")
]
A = (3,2,1,5)
B = (8,3,1,2)
C = (6,10,8,3)
D = (9,6,4,1)
distances = []
labels = []
def calc_distance(datas,test):
for data in datas:
distances.append(
( round(math.sqrt(((data[0] - test[0])**2 + (data[1] - test[1])**2 + (data[2] - test[2])**2 + (data[3] - test[3])**2)), 3), data[4] ))
return distances
def most_frequent(list1):
return max(set(list1), key = list1.count)
def get_neibours(k):
distances.sort()
print(distances[:k])
for distance in distances[:k]:
labels.append(distance[1])
print("It can be classified as: ", end="")
print(most_frequent(labels))
calc_distance(data_set,D)
get_neibours(7)
calc_distance(data_set,D)
get_neibours(7)
I works well mostly and I get the correct label. For example for D, i do get the label "Good". However i discovered a bug that when I call it twice for example:
calc_distance(data_set,D)
get_neibours(7)
calc_distance(data_set,D)
get_neibours(7)
and I run it few times, i get different outputs- "Good" and "Bad" when I run the program couple of times..
There must be a bug somewhere I am unable to find out.
The problem is that you are using the same distances and label, sorting and getting the k first elements. Create the lists inside the functions and return it. Check the modifications bellow.
import math
data_set = [
(2,9,8,4, "Good"),
(3,7,7,9, "Bad"),
(10,3,10,3, "Good"),
(2,9,6,10, "Good"),
(3,3,2,5, "Bad"),
(2,8,5,6, "Bad"),
(7,2,3,10, "Good"),
(1,10,8,10, "Bad"),
(2,8,1,10, "Good"),
]
A = (3,2,1,5)
B = (8,3,1,2)
C = (6,10,8,3)
D = (9,6,4,1)
def calc_distance(datas, test):
distances = []
for data in datas:
distances.append(
( round(math.sqrt(((data[0] - test[0])**2 + (data[1] - test[1])**2 + (data[2] - test[2])**2 + (data[3] - test[3])**2)), 3), data[4] ))
return distances
def most_frequent(list1):
return max(set(list1), key = list1.count)
def get_neibours(distances, k):
labels = []
distances.sort()
print(distances[:k])
for distance in distances[:k]:
labels.append(distance[1])
print("It can be classified as: ", end="")
print(most_frequent(labels))
distances = calc_distance(data_set,D)
get_neibours(distances, 7)
distances = calc_distance(data_set,D)
get_neibours(distances, 7)
[(7.071, 'Good'), (8.062, 'Bad'), (8.888, 'Bad'), (9.11, 'Good'), (10.1, 'Good'), (10.488, 'Bad'), (11.958, 'Good')]
It can be classified as: Good
[(7.071, 'Good'), (8.062, 'Bad'), (8.888, 'Bad'), (9.11, 'Good'), (10.1, 'Good'), (10.488, 'Bad'), (11.958, 'Good')]
It can be classified as: Good

Collecting places using Python and Google Places API

I want to collect the places around my city, Pekanbaru, with latlong (0.507068, 101.447777) and I will convert it to the dataset. Dataset (it contains place_name, place_id, lat, long and type columns).
Below is the script that I tried.
import json
import urllib.request as url_req
import time
import pandas as pd
NATAL_CENTER = (0.507068,101.447777)
API_KEY = 'API'
API_NEARBY_SEARCH_URL = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json'
RADIUS = 30000
PLACES_TYPES = [('airport', 1), ('bank', 2)] ## TESTING
# PLACES_TYPES = [('airport', 1), ('bank', 2), ('bar', 3), ('beauty_salon', 3), ('book_store', 1), ('cafe', 1), ('church', 3), ('doctor', 3), ('dentist', 2), ('gym', 3), ('hair_care', 3), ('hospital', 2), ('pharmacy', 3), ('pet_store', 1), ('night_club', 2), ('movie_theater', 1), ('school', 3), ('shopping_mall', 1), ('supermarket', 3), ('store', 3)]
def request_api(url):
response = url_req.urlopen(url)
json_raw = response.read()
json_data = json.loads(json_raw)
return json_data
def get_places(types, pages):
location = str(NATAL_CENTER[0]) + "," + str(NATAL_CENTER[1])
mounted_url = ('%s'
'?location=%s'
'&radius=%s'
'&type=%s'
'&key=%s') % (API_NEARBY_SEARCH_URL, location, RADIUS, types, API_KEY)
results = []
next_page_token = None
if pages == None: pages = 1
for num_page in range(pages):
if num_page == 0:
api_response = request_api(mounted_url)
results = results + api_response['results']
else:
page_url = ('%s'
'?key=%s'
'&pagetoken=%s') % (API_NEARBY_SEARCH_URL, API_KEY, next_page_token)
api_response = request_api(str(page_url))
results += api_response['results']
if 'next_page_token' in api_response:
next_page_token = api_response['next_page_token']
else: break
time.sleep(1)
return results
def parse_place_to_list(place, type_name):
# Using name, place_id, lat, lng, rating, type_name
return [
place['name'],
place['place_id'],
place['geometry']['location']['lat'],
place['geometry']['location']['lng'],
type_name
]
def mount_dataset():
data = []
for place_type in PLACES_TYPES:
type_name = place_type[0]
type_pages = place_type[1]
print("Getting into " + type_name)
result = get_places(type_name, type_pages)
result_parsed = list(map(lambda x: parse_place_to_list(x, type_name), result))
data += result_parsed
dataframe = pd.DataFrame(data, columns=['place_name', 'place_id', 'lat', 'lng', 'type'])
dataframe.to_csv('places.csv')
mount_dataset()
But the script returned with Empty DataFrame.
How to solve and got the right Dataset?
I am afraid the scraping of the data and storing it is prohibited by the Terms of Service of Google Maps Platform.
Have a look at the Terms of Service prior to advance with the implementation. The paragraph 3.2.4 'Restrictions Against Misusing the Services' reads
(a) No Scraping. Customer will not extract, export, or otherwise scrape Google Maps Content for use outside the Services. For example, Customer will not: (i) pre-fetch, index, store, reshare, or rehost Google Maps Content outside the services; (ii) bulk download Google Maps tiles, Street View images, geocodes, directions, distance matrix results, roads information, places information, elevation values, and time zone details; (iii) copy and save business names, addresses, or user reviews; or (iv) use Google Maps Content with text-to-speech services.
source: https://cloud.google.com/maps-platform/terms/#3-license
Sorry to be bearer of bad news.

Creates user- defined function with multiple vectors and obtains the mean and variance

I am trying to define the Python function - obtains the mean and variance with given multiple vectors. There are only x-axis and y-axis.
I tried make some functions and created the code but it showed me just an incorrect value.
Below is my code.
def avr(*inn):
hap = 0
cnt = 0
for i in inn:
hap += i
cnt += 1
avrr = hap/cnt
return avrr
def varr(*inn):
jejob = 0
jegobhap = 0
cnt = 0
for i in inn:
cha = (i-avr(*inn))
jegob = cha**2
jegobhap += jegob
cnt += 1
varr_m = jegobhap/cnt
return varr_m
def mean_and_var(*val):
x_axis_avr = avr(*val[0])
y_axis_avr = avr(*val[1])
x_axis_boon = varr(*val[0])
y_axis_boon = varr(*val[1])
return ((x_axis_avr,y_axis_avr),(x_axis_boon,y_axis_boon))
v1=(0,1)
v2=(0.5, 0.5)
v3=(1,0)
v4=(50,30)
m, var = mean_and_var(v1,v2,v3,v4)
print("mean: ", m, "var: " , var)
and it gives me
mean: (0.5, 0.5) var: (0.25, 0.0)
The correct value should be;
mean (12.875, 7.875) var: (459.546875,163.296875)
What is the error?
I think you expect too much for Python.
var[0] gives first vector (0,1), not first values from all vectors (0, 0.5, 1, 50).
var[1] gives second vector (0.5, 0.5), not second values from all vectors (1, 0.5, 0, 30)
And it doesn't use values v3, v4
Try this and you get expected values
v1 = (0, 0.5, 1, 50)
v2 = (1, 0.5, 0, 30)
m, var = mean_and_var(v1, v2)
print("mean: ", m, "var: " , var)
EDIT: using numpy you can do something like this
(I removed all * before variables)
def avr(inn): # * removed
#print('avr:', inn)
hap = 0
cnt = 0
for i in inn:
hap += i
cnt += 1
avrr = hap/cnt
return avrr
def varr(inn): # * removed
#print('varr:', inn)
jejob = 0
jegobhap = 0
cnt = 0
for i in inn:
cha = (i-avr(inn)) # * removed
jegob = cha**2
jegobhap += jegob
cnt += 1
varr_m = jegobhap/cnt
return varr_m
def mean_and_var(val):
x_axis_avr = avr(val[:,0]) # * removed
y_axis_avr = avr(val[:,1]) # * removed
x_axis_boon = varr(val[:,0]) # * removed
y_axis_boon = varr(val[:,1]) # * removed
return (x_axis_avr, y_axis_avr), (x_axis_boon, y_axis_boon)
import numpy as np
v1 = (0, 1)
v2 = (0.5, 0.5)
v3 = (1, 0)
v4 = (50, 30)
matrix = np.array([v1,v2,v3,v4])
#print(matrix)
m, var = mean_and_var(matrix)
print("mean: ", m, "var: " , var)

Categories