def testBalancedInterleave(self): bi = BalancedInterleave() r1 = DeterministicRankingFunction(None, self.weights_1) r2 = DeterministicRankingFunction(None, self.weights_2) (interleaved_list, assignments) = bi.interleave(r1, r2, self.query, 10) self.assertIn(interleaved_list.tolist(), [[0, 1, 2, 3], [1, 0, 2, 3], [0, 1, 3, 2], [1, 0, 3, 2]]) self.assertEqual(assignments[0].tolist(), [1, 3, 2, 0]) self.assertEqual(assignments[1].tolist(), [0, 1, 3, 2]) o = bi.infer_outcome([1, 0, 3, 2], [[1, 3, 2, 0], [0, 1, 3, 2]], [1, 0, 0, 0], self.query) self.assertEqual(o, -1, "l1 should win (1), o = %g" % o) o = bi.infer_outcome([1, 0, 3, 2], [[1, 3, 2, 0], [0, 1, 3, 2]], [1, 0, 1, 0], self.query) self.assertEqual(o, -1, "l1 should win (2), o = %g" % o) o = bi.infer_outcome([1, 0, 3, 2], [[1, 2, 3, 0], [0, 1, 3, 2]], [1, 0, 1, 0], self.query) self.assertEqual(o, 0, "The rankers should tie (1), o = %g" % o) o = bi.infer_outcome([0, 1, 2, 3], [[0, 1, 2, 3], [1, 2, 3, 0]], [0, 1, 0, 1], self.query) self.assertEqual(o, 1, "l1 should win, o = %g" % o) o = bi.infer_outcome([1, 0, 2, 3], [[0, 1, 2, 3], [1, 2, 3, 0]], [0, 1, 0, 1], self.query) self.assertEqual(o, 0, "The rankers should tie (2), o = %g" % o) o = bi.infer_outcome([0, 2, 1, 3], [[3, 0, 1, 2], [1, 3, 2, 0]], [1, 0, 1, 0], self.query) self.assertEqual(o, -1, "l1 should win (3), o = %g" % o) o = bi.infer_outcome([0, 2, 1, 3], [[3, 0, 1, 2], [4, 3, 2, 0]], [1, 0, 1, 0], self.query) self.assertEqual(o, -1, "l1 should win (4), o = %g" % o)
class HistBalancedInterleave(AbstractHistInterleavedComparison): """Balanced interleave method, applied to historical data.""" def __init__(self, arg_str=None): self.bi = BalancedInterleave() def _get_assignment(self, r1, r2, query, length): r1.init_ranking(query) r2.init_ranking(query) length = min(r1.document_count(), r2.document_count(), length) # get ranked list for each ranker l1, l2 = [], [] for _ in range(length): l1.append(r1.next()) l2.append(r2.next()) return (asarray(l1), asarray(l2)) def infer_outcome(self, l, a, c, target_r1, target_r2, query): """count clicks within the top-k interleaved list""" return self.bi.infer_outcome(l, self._get_assignment(target_r1, target_r2, query, len(l)), c, query)
class HistBalancedInterleave(AbstractHistInterleavedComparison): """Balanced interleave method, applied to historical data.""" def __init__(self, arg_str=None): self.bi = BalancedInterleave() def _get_assignment(self, r1, r2, query, length): r1.init_ranking(query) r2.init_ranking(query) length = min(r1.document_count(), r2.document_count(), length) # get ranked list for each ranker l1, l2 = [], [] for _ in range(length): l1.append(r1.next()) l2.append(r2.next()) return (asarray(l1), asarray(l2)) def infer_outcome(self, l, a, c, target_r1, target_r2, query): """count clicks within the top-k interleaved list""" return self.bi.infer_outcome( l, self._get_assignment(target_r1, target_r2, query, len(l)), c, query)
def __init__(self, arg_str=None): self.bi = BalancedInterleave()