def __init__(self, min_light_time=1, max_light_time=3, in_file='./example.in'): self.traffic = Traffic(in_file=in_file) self.scheduler = [] num_streets = len(self.traffic.streets) self._lower_bounds = num_streets * [None] self._upper_bounds = num_streets * [None] self._variable = num_streets * [True] self._discrete_values = num_streets * [[ x for x in range(min_light_time, max_light_time + 1) ]] # define all input parameters self._maximize = True # do we maximize or minimize? self._max_imp = 50000 # maximum number of improvisations self._hms = 100 # harmony memory size self._hmcr = 0.75 # harmony memory considering rate self._par = 0.5 # pitch adjusting rate self._mpap = 0.25 # maximum pitch adjustment proportion (new parameter defined in pitch_adjustment()) - used for continuous variables only self._mpai = 10 # maximum pitch adjustment index (also defined in pitch_adjustment()) - used for discrete variables only
def test_get_response(self): traffic = Traffic("foo", "bar") traffic.duration = "10 thousand years" expected = { "travel_time": "10 thousand years", "start_location": "foo", "destination": "bar", } actual = traffic.get_response()
def test_get_dynamo_data_with_bad_response_returns_none(self): traffic = Traffic("foo", "bar") test_table = TestTable() test_table.query = MagicMock(return_value="blargh") traffic.dynamo.Table = MagicMock(return_value=test_table) actual = traffic.get_dynamo_data() self.assertEqual(actual, None)
def test_google_maps_api_failure(self): traffic = Traffic("blah, blah", "ha, ha") traffic.gmaps.distance_matrix = MagicMock( side_effect=Exception("stuff's broke")) traffic.make_google_api_call() self.assertEqual(traffic.duration, "Unavailable") actual = traffic.time_in_hours_and_minutes(seconds) expected = {"hours": hours, "minutes": minutes} self.assertEqual(actual, expected)
def test_convert_seconds_to_hours_and_minutes(self): seconds = 55555 hours = "15" minutes = "25" traffic = Traffic("blah", "blah") actual = traffic.time_in_hours_and_minutes(seconds) expected = {"hours": hours, "minutes": minutes} self.assertEqual(actual, expected)
def test_set_dynamo_data(self): traffic = Traffic("foo", "bar") test_data = {"test": "data"} test_table = TestTable() test_table.put_item = MagicMock() traffic.dynamo.Table = MagicMock(return_value=test_table) traffic.set_dynamo_data(test_data) traffic.dynamo.Table.assert_called_with("TrafficData") test_table.put_item.assert_called_with(Item=test_data)
def test_make_dynamo_data(self): traffic = Traffic("foo", "bar") traffic.duration = {"hours": "1", "minutes": "30"} time.time = MagicMock(return_value=123456789) actual = traffic.make_dynamo_data() expected = { "identifier": "bar---foo", "hours": "1", "minutes": "30", "time": 123456789 } self.assertEqual(actual, expected)
def test_get_dynamo_data_returns_first_item(self): traffic = Traffic("foo", "bar") test_table = TestTable() test_table.query = MagicMock( return_value={"Items": ["Item 1", "Item 2"]}) traffic.dynamo.Table = MagicMock(return_value=test_table) actual = traffic.get_dynamo_data() test_table.query.assert_called_with( KeyConditionExpression=Key("identifier").eq("bar---foo"), ScanIndexForward=False, Limit=1) self.assertEqual(actual, "Item 1")
def test_google_maps_api(self): traffic = Traffic("blah, blah", "foo, foo") origin = ["foo, foo"] destination = ["blah, blah"] traffic.gmaps.distance_matrix = MagicMock(return_value={ "rows": [{ "elements": [{ "duration": { "value": 55555 } }] }] }) traffic.make_google_api_call() traffic.gmaps.distance_matrix.assert_called_with(origin, destination) self.assertEqual(traffic.duration, {"hours": "15", "minutes": "25"})
def test_get_traffic_calls_gmaps_if_needed(self): traffic = Traffic("foo", "bar") traffic.get_dynamo_data = MagicMock(return_value=None) traffic.make_google_api_call = MagicMock() traffic.get_traffic_data() traffic.make_google_api_call.assert_called()
def test_google_maps_api_failure(self): traffic = Traffic("blah, blah", "ha, ha") traffic.gmaps.distance_matrix = MagicMock( side_effect=Exception("stuff's broke")) traffic.set_dynamo_data = MagicMock() traffic.get_dynamo_data = MagicMock(return_value=None) traffic.make_google_api_call() self.assertEqual(traffic.duration, "unavailable")
def test_get_traffic_data_skips_gmaps_if_not_needed(self): traffic = Traffic("foo", "bar") traffic.get_dynamo_data = MagicMock(return_value={ "time": time.time(), "hours": "1", "minutes": "75" }) traffic.make_google_api_call = MagicMock() traffic.get_traffic_data() traffic.make_google_api_call.assert_not_called() self.assertEqual(traffic.duration, {"hours": "1", "minutes": "75"})
from traffic.traffic import Traffic import random import pandas as pd from copy import deepcopy MIN_LIGHT_TIME = 1 MAX_LIGHT_TIME = 3 t = Traffic('./example.in') new_green_light_time = [ random.randint(MIN_LIGHT_TIME, MAX_LIGHT_TIME) for _ in range(len(t.street_detail)) ] scheduler = pd.DataFrame({ 'street_name': deepcopy(t.street_detail['name']), 'green_time': new_green_light_time }) print("Calculating score of {} ...".format('scheduler'), end='\t') t.generate_intersection(scheduler=scheduler) cars = t.simulate() scheduler_score = t.calculate_simulation_score(cars) print("receive {} points".format(scheduler_score), end='\t')
from traffic.traffic import Traffic import vaex def set_queues_callback(time, queues): global street_queues street_queues[time] = queues in_setup_file = './hashcode.in' in_submission_file = 'submission.hashcode.txt' out_street_queues_hd5_file = 'street_queues.hashcode.h5' # in_setup_file='./example.in' # in_submission_file='submit.example.txt' # out_street_queues_hd5_file='street_queues.example.h5' m = Traffic(in_file=in_setup_file) num_streets = len(m.streets) callback = set_queues_callback street_queues = np.zeros((m.end_time, num_streets), dtype=int) m.read_submission_file(in_file_path=in_submission_file) m.simulate(progress_bar=True, override_end_time=None, queue_callback=callback) m_score = m.calculate_simulation_score() print("Final score: {}".format(m_score)) street_queues_df = vaex.from_pandas( pd.DataFrame(street_queues, columns=list(m.streets.keys()))) street_queues_df.export_hdf5(out_street_queues_hd5_file, progress=True)
class HarmonySearch(ObjectiveFunctionInterface): """ This is a toy objective function that contains a mixture of continuous and discrete variables. Goal: maximize -(x^2 + (y+1)^2) + 4 The maximum is 4 at (0, -1). In this implementation, x is a discrete variable with choices ranging from -100 to 100 in increments of 1. y is still a continuous variable. Warning: Stochastically solving a linear system is dumb. This is just a toy example. """ def __init__(self, min_light_time=1, max_light_time=3, in_file='./example.in'): self.traffic = Traffic(in_file=in_file) self.scheduler = [] num_streets = len(self.traffic.streets) self._lower_bounds = num_streets * [None] self._upper_bounds = num_streets * [None] self._variable = num_streets * [True] self._discrete_values = num_streets * [[ x for x in range(min_light_time, max_light_time + 1) ]] # define all input parameters self._maximize = True # do we maximize or minimize? self._max_imp = 50000 # maximum number of improvisations self._hms = 100 # harmony memory size self._hmcr = 0.75 # harmony memory considering rate self._par = 0.5 # pitch adjusting rate self._mpap = 0.25 # maximum pitch adjustment proportion (new parameter defined in pitch_adjustment()) - used for continuous variables only self._mpai = 10 # maximum pitch adjustment index (also defined in pitch_adjustment()) - used for discrete variables only def generate_sceduler(self, vector): self.scheduler = pd.DataFrame({ 'street_name': deepcopy(self.traffic.street_detail['name']), 'green_time': vector }) return self.scheduler def get_fitness(self, vector): self.generate_sceduler(vector) self.traffic.generate_intersection(scheduler=self.scheduler) cars = self.traffic.simulate() scheduler_score = self.traffic.calculate_simulation_score(cars) return scheduler_score def get_value(self, i, j=None): if self.is_discrete(i): if j: return self._discrete_values[i][j] return self._discrete_values[i][random.randint( 0, len(self._discrete_values[i]) - 1)] return random.uniform(self._lower_bounds[i], self._upper_bounds[i]) def get_lower_bound(self, i): """ This won't be called except for continuous variables, so we don't need to worry about returning None. """ return self._lower_bounds[i] def get_upper_bound(self, i): """ This won't be called except for continuous variables. """ return self._upper_bounds[i] def get_num_discrete_values(self, i): if self.is_discrete(i): return len(self._discrete_values[i]) return float('+inf') def get_index(self, i, v): """ Because self.discrete_values is in sorted order, we can use binary search. """ return HarmonySearch.binary_search(self._discrete_values[i], v) @staticmethod def binary_search(a, x): """ Code courtesy Python bisect module: http://docs.python.org/2/library/bisect.html#searching-sorted-lists """ i = bisect_left(a, x) if i != len(a) and a[i] == x: return i raise ValueError def is_variable(self, i): return self._variable[i] def is_discrete(self, i): return self._discrete_values[i] is not None def get_num_parameters(self): return len(self._lower_bounds) def use_random_seed(self): return hasattr(self, '_random_seed') and self._random_seed def get_max_imp(self): return self._max_imp def get_hmcr(self): return self._hmcr def get_par(self): return self._par def get_hms(self): return self._hms def get_mpai(self): return self._mpai def get_mpap(self): return self._mpap def maximize(self): return self._maximize
from traffic.traffic import Traffic import numpy as np m = Traffic(in_file='./hashcode.in') vector = list(np.random.randint(low=1, high=3, size=len(m.street_detail))) m.generate_intersection(vector=vector) m_cars = m.simulate(progress_bar=True) m_score = m.calculate_simulation_score(m_cars) print("Final score: {}".format(m_score))