def test_makes_best_swap_from_unhappiest_group(self): a = Arrangement(self.default_json_arrangement, 2) self.assertEqual(a.get_score(), -400) a.make_best_swap_from_unhappiest_group() self.assertEqual(a.get_score(), -200) a.make_best_swap_from_unhappiest_group() self.assertEqual(a.get_score(), 1)
def test_optimize(self): arrangement_list = ArrangementFormatter.create_arrangement_from_csv( self.csv_file) arrangement = Arrangement(arrangement_list, 2) grouper = Grouper(2, arrangement) score = grouper.optimize() self.assertEqual(score, -97) score = grouper.optimize() self.assertEqual(score, 2)
def test_randomize(self): """This is a test to catch regressions and may intermittently fail. We are calling #randomize a large number of times and keeping track of the location of participant 'a'. I have chosen some arbitrary values for the number of samples and an acceptable variation that gives me confidence #randomize is working. TODO: Look into the math behind calculating a confidence interval that a sample is random. If we flip coin 100 times and get 65 heads, how confident can we be that the coin is 'fair' (50/50)? """ participants_per_group = 2 arrangement = Arrangement( self.default_json_arrangement, participants_per_group ) num_participants = len(arrangement.participants) position_counts = {} # Initialize position counts for i in range(num_participants): position_counts[i] = 0 num_samples = 100 for i in range(num_samples): arrangement.randomize() groups = arrangement.groups # Flatten groups for easy participant selection by single index participants = reduce(lambda g1, g2: g1+g2, groups) for idx, p in enumerate(participants): if p['name'] == 'a': position_counts[idx] += 1 mean = float(sum(position_counts.values()) / max(num_participants, 1)) squared_differences = map(lambda x: (x - mean)**2, position_counts.values()) variance = sum(squared_differences) / max(num_participants, 1) std_dev = math.sqrt(variance) expected_max_std_dev = 6 self.assertTrue(std_dev < expected_max_std_dev, "Expected std_dev of {} participants divided ".format(num_participants) + "into {} groups over {} samples to be < {} but got {}".format( len(arrangement.groups), num_samples, expected_max_std_dev, std_dev))
def test_randomize(self): # This is currently delegating to Arrangement#randomize # and this test is different but redundant. # The idea with this test is to randomly select a participant # and then see how many randomizations we have to do to see that # same participant in the same location. Values chosen are # are currently arbitrary. arrangement_list = ArrangementFormatter.create_arrangement_from_csv( self.csv_file) arrangement = Arrangement(arrangement_list, 2) grouper = Grouper(2, arrangement) # Flatten groups participants = reduce(lambda g1, g2: g1+g2, grouper.arrangement.groups) idx_to_track = random.randint(0, len(participants)-1) participant_to_track = participants[idx_to_track] appearance_count = 1 goal_appearance_count = 10 attempt_count = 0 min_num_attempts_considered_success = 25 max_num_attempts_considered_success = 75 break_guard = 1000 while appearance_count < goal_appearance_count: if attempt_count > break_guard: break participants = reduce(lambda g1, g2: g1+g2, grouper.arrangement.groups) if participants[idx_to_track] == participant_to_track: appearance_count += 1 grouper.randomize() attempt_count += 1 assertion_message = "Expected between {} and {} randomizations " + \ "to see participant with name {} appear at position {} " + \ "but it appeared {} times in {} attempts." assertion_message = assertion_message.format( min_num_attempts_considered_success, max_num_attempts_considered_success, participant_to_track['name'], idx_to_track, appearance_count, attempt_count) self.assertTrue(attempt_count < max_num_attempts_considered_success, assertion_message)
def test_get_unhappiest_group(self): a = Arrangement(self.default_json_arrangement, 2) self.assertEqual(a.get_unhappiest_group(), a.groups[2])
def test_optimize(self): arrangement = Arrangement(self.default_json_arrangement, 2) arrangement.optimize() self.assertEqual(arrangement.get_score(), 2)
def test_get(self): arrangement = Arrangement(self.default_json_arrangement, 2) individual = arrangement[0, 0] self.assertEqual(individual['id'], 0)