def _utility_at_locality(self, l, agents, memoize): agents = tuple(sorted(agents)) if memoize and agents in self._memoization[l]: return self._memoization[l][agents] sum_utilities = 0 for _ in range(self.random_samples): num_jobs = self.locality_num_jobs[l] # agent i has node id `i`, job j has node id `offset + j` offset = self.num_agents edges = [] for i in agents: for j in range(num_jobs): probability = self.compatibility_probabilities[i][l][j] if probability == 0: # Improves simulation performance because random is not # called continue if random() < probability: edges.append((i, offset + j)) graph = Graph.Bipartite([0] * self.num_agents + [1] * num_jobs, edges) matching = graph.maximum_bipartite_matching() sum_utilities += len(matching) utility = sum_utilities / self.random_samples self._memoization[l][agents] = utility return utility
def assert_has_requests_any_order(self, *expected_requests: ExpectedHTTPRequest): """ Asserts that all of the expected requests exclusively match a one of the recorded requests, in any order. Args: *expected_requests: the expected requests. """ if not expected_requests: raise TypeError('at least one expected request must be provided') if len(self) < len(expected_requests): raise AssertionError(f"expected {len(expected_requests)} sequential requests, but found {len(self)}: " f"{self}") # this problem is equivalent to a maximum bipartite matching in a graph, where one set holds the recorded # requests, and the other, the expected requests. We construct an igraph object to represent the problem. edges = [] # since recorded requests are unhashable, we store the why_nots for requests by their ids why_nots: Dict[ExpectedHTTPRequest, Dict[int, MismatchReason]] = {expected: {} for expected in expected_requests} for i, expected in enumerate(expected_requests): for j, request in enumerate(self): match = expected.matches(request) if match: edges.append((i, j + len(expected_requests))) else: assert isinstance(match, MismatchReason) why_nots[expected][id(request)] = match graph = Graph.Bipartite([0] * len(expected_requests) + [1] * len(self), edges) max_matching = graph.maximum_bipartite_matching() unmatched_strs = [] for i, expected in enumerate(expected_requests): if not max_matching.is_matched(i): unmatched_strs.append(f'could not match expected request {expected}:' + ''.join((f'\n\t{req}- {why_nots[expected][id(req)]}' if id(req) in why_nots[expected] else f'\n\t{req}- matched expected request ' + str(expected_requests[max_matching.match_of(node_index)])) for node_index, req in enumerate(self, len(expected_requests)))) if unmatched_strs: raise AssertionError('\n'.join(unmatched_strs))
def graphScope(segment, newSegment): encodingCostSegment = totalCostForSegment(segment, segment.segments) encodingCostNewGraph = totalCostForSegment(newSegment, newSegment.segments) unionGraph = Graph.Bipartite(segment.g.vs["type"], segment.g.get_edgelist(), directed=False) unionGraph.add_edges(newSegment.g.get_edgelist()) resultUnion = partitionGraph(unionGraph, 1, segment.segments + 1) unionSegment = GraphSegment(unionGraph, resultUnion[0], resultUnion[1], resultUnion[2]) encodingCostUnion = totalCostForSegment(unionSegment, segment.segments + 1) print("encodingCostUnion", encodingCostUnion) print("encodingCostSegment", encodingCostSegment) print("encodingCostNewGraph", encodingCostNewGraph) if encodingCostUnion - encodingCostSegment < encodingCostNewGraph: unionSegment.segments = segment.segments + 1 return [unionSegment] else: return [segment, newSegment] print("time.graphScope after: %f " % time.time())