def _filter(self, input): """Return a subset of the input probes. """ # Ensure that the input is a list input = list(input) # In the instance of set cover, we have a set S for each probe P # in which S consists of P as well as all other probes redundant # to P. Construct these sets. sets = defaultdict(set) for i in range(len(input)): if i % 100 == 0: logger.info("Making set for candidate probe %d of %d", i + 1, len(input)) probe_a = input[i] # Put probe_a into its set sets[i].add(probe_a) # Find all other probes redundant to probe_a for j in range(i + 1, len(input)): probe_b = input[j] if self.are_redundant_fn(probe_a, probe_b): # Put probe_b into probe_a's set (set[i]), and also put # probe_a into probe_b's set (set[j]) sets[i].add(probe_b) sets[j].add(probe_a) # Run the set cover approximation algorithm set_ids_in_cover = set_cover.approx(sets) return [input[id] for id in set_ids_in_cover]
def test_partial_weighted5(self): input = { 0: {1, 2}, 1: {2, 3, 4, 5}, 2: {3}, 3: {4}, 4: {5} } costs = {0: 3, 1: 4, 2: 1, 3: 1, 4: 2} desired_output = {1} self.assertEqual(sc.approx(input, costs=costs, p=0.8), desired_output) costs = {0: 3, 1: 4.1, 2: 1, 3: 1, 4: 2} desired_output = {0, 2, 3} # The optimal solution is [1], but the approximation fails to # find it self.assertEqual(sc.approx(input, costs=costs, p=0.8), desired_output)
def test_partial_unweighted2(self): input = { 0: {1, 2}, 1: {1, 2, 4}, 2: {2, 4}, 3: {4, 5}, 4: {2, 3, 6} } desired_output = {1, 4} self.assertEqual(sc.approx(input, p=0.81), desired_output)
def test_complete_unweighted(self): input = { 0: {1, 2}, 1: {1, 2, 4}, 2: {2, 4}, 3: {4, 5}, 4: {3} } desired_output = {1, 3, 4} self.assertEqual(sc.approx(input), desired_output)
def test_partial_weighted2(self): input = { 0: {1, 2}, 1: {2, 3}, 2: {4, 5}, 3: {5}, 4: {4} } costs = {0: 2, 1: 1000, 2: 100, 3: 10, 4: 10} desired_output = {0, 3, 4} self.assertEqual(sc.approx(input, costs=costs, p=0.7), desired_output)
def test_partial_weighted1(self): input = { 0: {1, 2}, 1: {1, 2, 3, 4, 5}, 2: {4}, 3: {5}, 4: {3} } costs = {0: 2, 1: 1000, 2: 3, 3: 1, 4: 10} desired_output = {3} self.assertEqual(sc.approx(input, costs=costs, p=0.1), desired_output)
def test_complete_weighted2(self): input = { 0: {1, 2}, 1: {1, 2, 3, 4, 5}, 2: {4}, 3: {5}, 4: {3} } costs = {0: 2, 1: 1000, 2: 3, 3: 1, 4: 10} desired_output = {0, 2, 3, 4} self.assertEqual(sc.approx(input, costs=costs), desired_output)
def test_partial_weighted4(self): input = { 0: {1, 2}, 1: {3, 4, 5}, 2: {3}, 3: {4}, 4: {5} } costs = {0: 2.1, 1: 3, 2: 2, 3: 2, 4: 2} desired_output = {1} self.assertEqual(sc.approx(input, costs=costs, p=0.6), desired_output)
def test_partial_weighted3(self): input = { 0: {1, 2}, 1: {3}, 2: {4}, 3: {2, 5}, 4: {1} } costs = {0: 2, 1: 1000, 2: 999, 3: 10, 4: 10} desired_output = {0, 2, 3} self.assertEqual(sc.approx(input, costs=costs, p=0.8), desired_output)
def test_one_element(self): input = {0: {1}} self.assertEqual(sc.approx(input), {0})
def test_no_elements(self): input = {} self.assertEqual(sc.approx(input), set([])) inptut = {0: set([])} self.assertEqual(sc.approx(input), set([]))