<jupyter_start><jupyter_text>Test LocationManager<jupyter_code>from routing.managers import LocationManager
location_manager = LocationManager(db, depot=depot)
location_manager.size()
if None and None:
    print('None')
location_manager.remove(all_locations[1])
location_manager.get_locations()
location_manager = LocationManager(db, depot=depot)
location_manager.add(all_locations[0])
print(location_manager.size() == 1)
test_set = set()
test_set.add(all_locations[0])
print(location_manager.get_locations() == list(test_set))
# location_manager.delete(all_locations[1]) # Raise KeyError
location_manager.remove(all_locations[0])
print(location_manager.size() == 0)
location_manager.add_collection(all_locations[:5])
print(location_manager.size() == 5)
# Get distance is symmetric
print(location_manager.get_distance(all_locations[0], all_locations[1]) == location_manager.get_distance(all_locations[1], all_locations[0]))

# Get duration is symmetric
print(location_manager.get_duration(all_locations[0], all_locations[1]) == location_manager.get_duration(all_locations[1], all_locations[0]))

# Get distance savings
print(location_manager.get_distance_savings(all_locations[1], all_locations[2]))
print(location_manager.get_distance_savings(all_locations[2], all_locations[1]))

# Get distance savings is symmetric
print(location_manager.get_distance_savings(all_locations[1], all_locations[2]) == location_manager.get_distance_savings(all_locations[2], all_locations[1]))

# Get duration savings
print(location_manager.get_duration_savings(all_locations[1], all_locations[2]))
print(location_manager.get_duration_savings(all_locations[2], all_locations[1]))

# Get duration savings is symmetric
print(location_manager.get_duration_savings(all_locations[1], all_locations[2]) == location_manager.get_duration_savings(all_locations[2], all_locations[1]))
for l in location_manager.get_locations():
    print(l.address, l.is_center, l.is_assigned, l.latitude, l.longitude)
location_manager.remove(1)<jupyter_output><empty_output><jupyter_text>Test SavingsManager<jupyter_code>from routing.managers import SavingsManager
savings_manager = SavingsManager(db_connection=db, depot=depot, locations=all_locations[1:6])
savings_manager.depot
while True:
    try:
        savings, pair = next(savings_manager)
        print(savings, pair.get_pair())
    except IndexError:
        break<jupyter_output><empty_output><jupyter_text>Test BingGeocodeService<jupyter_code>from routing.services import BingGeocodeService
text = BingGeocodeService.get_geocode(depot)
text<jupyter_output><empty_output><jupyter_text>Test BingMatrixService<jupyter_code>from routing.services import BingMatrixService
from routing.services import BingGeocodeService
add = Address(address='Address', city='City', state='State', zipcode=9999)
add
d.keys()
d = m.json()
d = d['resourceSets'][0]
d.keys()
d = d['resources'][0]
d.keys()
dest = d['destinations']
orig = d['origins']
result = d['results']
dest
orig
result
for d in result:
    destinationIndex = d['destinationIndex']
    originIndex = d['originIndex']
    destination = dest[destinationIndex]
    origin = orig[originIndex]
    print(destination, origin)
Location.nodes.get(latitude=41.238347, longitude=-95.998385)<jupyter_output><empty_output><jupyter_text>Test Route<jupyter_code>from routing.models.route import Route
from itertools import combinations
from routing.models.location import Pair
from random import randint, shuffle
r = Route()
r.departure = depot
r.departure
drivers = all_drivers[:5]
for driver in drivers:
    print(driver)
locs = all_locations[1:6]
pairs = []
for p in combinations(locs, 2):
    pairs.append(Pair(p[0], p[1]))
    
# Shuffle pairs
shuffle(pairs)
r.is_open
print(r.get_created_on())
print(r.get_total_demand() == 0)
print(r.get_total_distance() == 0)
print(r.get_total_duration() == 0)
print(r.last_location() == None)<jupyter_output><empty_output><jupyter_text>Test add<jupyter_code>for l in locs:
    if l != depot:
        l.demand = randint(1, 10)
    else:
        l.demand = None
    print(l, l.demand)
    l.is_assigned = False
r.locations_dict
r.locations_dict
r.locations_queue
r.locations_queue
last_inserted = r.locations_queue.pop()
last_inserted
print(hex(id(last_inserted)))
print(r.locations_dict[hex(id(last_inserted))])
print(r.locations_dict[hex(id(last_inserted))].next == None)
r.locations_dict[hex(id(last_inserted))].previous.next = r.locations_dict[hex(id(last_inserted))].next
print(r.locations_dict['0x2411ba4ed00'].next)

r.last_location()
n = r.departure
while n is not None:
    if n.next is None:
        print(n)
    else:
        print(n, "\033[1m --> \033[0m", end="")
    n = n.next
n = r.last_location()
while n is not None:
    if n.previous is None:
        print(n)
    else:
        print(n, "\033[1m --> \033[0m", end="")
    n = n.previous
r.undo()
while True:
    try:
        savings, p = next(savings_manager)
        print(f"Processing pair ({p.first()}, {p.last()})")
        for l in p.get_pair():
            print(f"\tProcessing location {l}")
            r.add(l, p)
    except IndexError:
        break
r
next(savings_manager)
savings
r.last_location()
r.get_total_demand()
r.locations_dict
r.locations_queue
hex(id(r.previous))
r.last_location()
hex(id(r.last_location()))
r.undo()
hex(id(r.locations_dict['0x2411ba3fb80'].next.next.next))
hex(id(r.locations_dict['0x2411ba4e4f0'].previous.previous.previous.previous))
r.locations_dict
for i in range(0, 15, 10):
    if i + 10 < 15:
        print(f'From i = {i} to j = {i + 10}')
    else:
        print(f'From i = {i} to end')
[all_locations[1] for i in range(10)]<jupyter_output><empty_output><jupyter_text>Test RouteManager<jupyter_code>from routing.managers import RouteManager
from random import randint
from datetime import datetime, timedelta
# Set driver's capacity
for driver in all_drivers:
    driver.capacity = randint(15, 20)

# Set driver's start and end time
for driver in all_drivers:
    driver.start_time = datetime(year=2021, month=10, day=31, hour=randint(9, 12), minute=0)
    driver.end_time = datetime(year=2021, month=10, day=31, hour=randint(12, 13), minute=0)
    
locations = [all_locations[1] for i in range(1)]
route_manager = RouteManager(db_connection=db, depot=depot, drivers=all_drivers[:5], locations=locations)
# Set locations demand
for location in route_manager.locations:
    location.demand = randint(5, 10)
for location in route_manager.locations:
    print(location, location.demand)

print()

for driver in route_manager.drivers:
    print(driver, driver.capacity, driver.start_time, driver.end_time, (driver.end_time - driver.start_time).total_seconds())
route_manager.build_routes()
for p in route_manager.savings_manager:
    print(p)
for driver in route_manager.best_allocation:
    print('\033[1m', 'Driver: ', driver, '\033[0m', '\033[1m', 'Capacity: ', '\033[0m', driver.capacity)
    if driver.route:
        print('\t', '\033[1m', 'Route Statistics', '\033[0m')
        print('\t\t', '\033[1m', 'Demand: \033[0m', driver.route.total_quantity, 
              '\033[1m', 'Duration: ', '\033[0m', driver.route.total_duration, 
              '\033[1m', 'Distance: ', '\033[0m', driver.route.total_distance,
              '\033[1m', 'Open: ', '\033[0m', driver.route.is_open)
        location = driver.route.departure
        while location is not None:
            if location.next is None:
                print('\t\t ', location, '(Demand: ', location.demand, ')')
            else:
                print('\t\t ', location, '\n',
                      '\t\t', ' {', '\n'
                      '\t\t\t', ' \"Demand\": ', location.demand, '\n',
                      '\t\t\t', ' \"Distance [previous]\": ', route_manager.location_manager.get_distance(location, location.previous),
                      '\n', '\t\t\t', ' \"Duration [previous]\": ', route_manager.location_manager.get_duration(location, location.previous),
                      '\n', '\t\t', ' }', '\033[1m --> \033[0m', end='\n')
            location = location.next
    else:
        print('\t', 'Not used!')
    print()
for location in route_manager.locations:
    print(location, location.is_assigned, location.demand)
for driver in route_manager.drivers:
    print(driver, driver.capacity, driver.is_volunteer(), driver.is_full_time(), driver.employee_status)
all_drivers[0].route.is_open
all_drivers[1].route
for r in route_manager.savings_manager:
    print(r)
l3 = Location(address='6001 Dodge Street', city = 'Omaha', state='NE', zipcode=68182)
route_manager.objective_function_heap
route_manager.objective_function_value
for driver in route_manager.best_allocation:
    if driver.route:
        print('\033[1m', 'Driver: ', driver, '\033[0m', '\033[1m', 'Capacity: ', '\033[0m', driver.capacity)
        print('\t', '\033[1m', 'Route Statistics', '\033[0m')
        print('\t\t', '\033[1m', 'Demand: \033[0m', driver.route.total_quantity, 
              '\033[1m', 'Duration: ', '\033[0m', driver.route.total_duration, 
              '\033[1m', 'Distance: ', '\033[0m', driver.route.total_distance,
              '\033[1m', 'Open: ', '\033[0m', driver.route.is_open)
        location = driver.route.departure
        while location is not None:
            if location.next is None:
                print('\t\t ', location, '(Demand: ', location.demand, ')')
            else:
                print('\t\t ', location, '\n',
                      '\t\t', ' {', '\n'
                      '\t\t\t', ' \"Demand\": ', location.demand, '\n',
                      '\t\t\t', ' \"Distance [previous]\": ', route_manager.location_manager.get_distance(location, location.previous),
                      '\n', '\t\t\t', ' \"Duration [previous]\": ', route_manager.location_manager.get_duration(location, location.previous),
                      '\n', '\t\t', ' }', '\033[1m --> \033[0m', end='\n')
            location = location.next
        print()
route_manager.objective_function_value
l3.created_on
for driver in route_manager.drivers:
    if driver.get_departure():
        print(driver, driver.get_departure().next)
Location.nodes.get(address__iexact='6001 dodge Street', city='Omaha', state='NE', zipcode=68182)
l = [1, 2, 3, 4, 5, 6]
for i in l[::-1]:
    print(i)
location_manager.get_distance(all_locations[1], all_locations[0])
location_manager.get_duration(all_locations[0], all_locations[1])
location_manager.get_duration_savings(all_locations[0], all_locations[1], all_locations[2])
location_manager.get_distance_savings(all_locations[0], all_locations[1], all_locations[2])
!{sys.executable} -m pip install django_neomodel --user
import json, datetime, time
print(json.dumps({'user_name': 'user', 'dob': str(datetime.datetime.now())}, indent=4))<jupyter_output><empty_output><jupyter_text>Test Customer<jupyter_code>from routing.models.customer import Customer
all_customers = Customer.nodes.all()
all_customers
c = all_customers[0]
r = c.address.relationship(all_locations[1])
c.language.connect(all_languages[0])
c.address.manager.relationship(all_locations[1])
a = c.address.get_or_none()
a.next = all_locations[3]
hex(id(a))
a.save()
c.location.next
a.next
hex(id(c.location))
hex(id(all_customers[1].location))
hex(id(b))
b.next
c.next
c.address.get().next
for ad in c.address:
    print(ad.address, ad.city, ad.next, ad.is_center)
ad = c.address
type(c.address)
c.address.get()
print(c.next)
c.next = None
a = c.address.get()
a
b = a
b
print(b.distance(a))
c.address.get()
c.locate.distance(c.locate)
import copy
s = copy.deepcopy(None)
c.location = c.locate
c.location
c.location = all_locations[4]
c
c.locate
c.location.get_or_none()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14858611825192802, 'fraction_numerical': 0.0286203941730934, 'mean_word_length': 4.456287985039738, 'pattern_counts': {'":': 6, '<': 34, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '177678', 'license': 'MIT', 'n_tokens_mistral': 4252, 'n_tokens_neox': 3958, 'n_words': 760, 'path': 'routing/tests/Testing GraphDB.ipynb', 'repo_name': 'Atari4800/fast-backend', 'size': '11670'}