def infect_limmited_return_num_bad_edges(graph, numToInfect): infecter = Infecter() infecter.infect_limited(graph.users, numToInfect, 10) numInfected = infecter.get_num_infected(graph.users) return infecter.get_solution_quality(graph.users), numInfected
def test_infect_limited_no_errors(resource_semi_random_graph_users): users = resource_semi_random_graph_users infecter = Infecter() infecter.infect_limited(users, 25, 0) assert True
def infect_and_return_num_bad_edges(graph, numToInfect): infecter = Infecter() start = random.choice(graph.users) infecter.infect_limited_from(start, numToInfect) return infecter.get_solution_quality(graph.users)
def test_infect_from(resource_semi_random_graph_users): users = resource_semi_random_graph_users user = random.choice(users) infecter = Infecter() infecter.infect_from(user) subgraphs = graphTraverser.get_subgraphs(users) assert exactly_one_subgraph_infected(subgraphs)
def test_get_num_infected_large(): expectedInfected = 100 graph = CoachingGraph() graph.init_semi_random(expectedInfected) infecter = Infecter() infecter.infect_all(graph.users) actualInfected = infecter.get_num_infected(graph.users) assert actualInfected == expectedInfected
def test_infect_limited_doesnt_change_num_users(): originalUsers = 100 graph = CoachingGraph() graph.init_semi_random(originalUsers) infecter = Infecter() infecter.infect_limited(graph.users, 50, 10) remaingingUsers = len(graph.users) assert remaingingUsers == originalUsers
def test_infect_limited_infects_correct_number_connected(): graph = CoachingGraph() graph.init_semi_random_connected(200) numToInfect = 70 users = graph.users infecter = Infecter() infecter.infect_limited(users, numToInfect, 0, False) numInfected = infecter.get_num_infected(users) assert numInfected == numToInfect
def test_infect_limited_from(): graph = CoachingGraph() graph.init_semi_random_connected(200) numToInfect = 70 users = graph.users user = random.choice(users) infecter = Infecter() infecter.infect_limited_from(user, numToInfect) numInfected = infecter.get_num_infected(users) assert numInfected == numToInfect
def test_get_num_infected(): graph = CoachingGraph() A = User() B = User() C = User() graph.add_users([A, B, C]) A.infected = True B.infected = True infecter = Infecter() assert infecter.get_num_infected(graph.users) == 2
def test_find_user_with_fewest_connections(): subgraph = CoachingGraph() A = User() B = User() C = User() D = User() subgraph.add_users([A, B, D, C]) subgraph.create_coach_coachee_relationship(A, B) subgraph.create_coach_coachee_relationship(A, C) subgraph.create_coach_coachee_relationship(A, D) subgraph.create_coach_coachee_relationship(B, C) expected = D infecter = Infecter() actual = infecter.find_user_with_fewest_connections(subgraph.users) assert expected == actual
def test_find_neighbor_with_fewest_connections(): graph = CoachingGraph() A = User() B = User() C = User() D = User() E = User() F = User() graph.add_users([A, B, C, D, E, F]) graph.create_coach_coachee_relationship(A, B) graph.create_coach_coachee_relationship(B, C) graph.create_coach_coachee_relationship(B, D) graph.create_coach_coachee_relationship(C, E) graph.create_coach_coachee_relationship(A, F) infecter = Infecter() infecter.infect_single_user(A) userWithFewest = infecter.find_neighbor_with_fewest_connections(A) assert userWithFewest == F
def infect_limmited_return_num_bad_edges(graph, numToInfect): infecter = Infecter() infecter.infect_limited(graph.users, numToInfect, 10) numInfected = infecter.get_num_infected(graph.users) return infecter.get_solution_quality(graph.users), numInfected
def __init__(self): self.users = [] self.infecter = Infecter() self.visualizer = GraphVisualizer(self.users)
class CoachingGraph(): def __init__(self): self.users = [] self.infecter = Infecter() self.visualizer = GraphVisualizer(self.users) def init_random(self, numUsers): self.users = [] self.create_and_add_users(numUsers) self.make_random_connections(3) def create_and_add_users(self, numUsers): for i in range(numUsers): user = User() self.add_user(user) def make_random_connections(self, iterations): for i in range(iterations): for user in self.users: self.make_random_connection(user) # generates a random aproximation of coach coachee relations def init_semi_random(self, size, connectionFactor=10, coacheesFactor=2): self.users = [] numCoaches = (size // 6) + 1 remainingCoachees = size - numCoaches coacheesPerCoach = remainingCoachees // numCoaches remaining = size while remaining > 0: numCoachees = random.randint( 1, math.floor(coacheesPerCoach * coacheesFactor)) remaining -= 1 numCoachees = min(numCoachees, remaining) self.add_coach(numCoachees) remaining -= numCoachees self.make_sparse_random_connections(size // connectionFactor) def init_semi_random_connected(self, size): self.init_semi_random(size, connectionFactor=1) def add_coach(self, numCoachees): coach = User() self.add_user(coach) self.add_coachees(coach, numCoachees) def add_coachees(self, user, numToAdd): if user not in self.users: return for i in range(numToAdd): coachee = User() self.add_user(coachee) self.create_coach_coachee_relationship(user, coachee) def make_sparse_random_connections(self, numConnections): for user in random.sample(self.users, numConnections): self.make_random_connection(user) def make_random_connection(self, user): coachee = user while coachee == user: coachee = random.choice(self.users) if coachee in user.neighbors: continue self.create_coach_coachee_relationship(user, coachee) def add_user(self, user): if user in self.users: return self.users.append(user) def add_users(self, users): for user in users: self.add_user(user) def create_coach_coachee_relationship(self, userA, userB): if userA not in self.users or userB not in self.users: return userA.add_coachee(userB) userB.add_coach(userA) def clean(self): for user in self.users: user.infected = False user.numConnections = 0 user.epicenter = False user.visited = False def draw(self): self.visualizer.update(self.users) self.visualizer.draw(self.infecter.get_solution_quality(self.users)) def save_as(self, filename): self.visualizer.update(self.users) data = [self.users, self.visualizer] pickler.save_as(data, filename) def load_from(self, filename): try: data = pickler.load_file(filename) except FileNotFoundError: logger.error('Failed to load, File not found', exc_info=True) return except Exception: logger.error('Failed to load, something else went wrong', exc_info=True) return self.users, self.visualizer = data
def __init__(self): self.users = [] self.infecter = Infecter() self.visualizer = GraphVisualizer(self.users)
class CoachingGraph(): def __init__(self): self.users = [] self.infecter = Infecter() self.visualizer = GraphVisualizer(self.users) def init_random(self, numUsers): self.users = [] self.create_and_add_users(numUsers) self.make_random_connections(3) def create_and_add_users(self, numUsers): for i in range(numUsers): user = User() self.add_user(user) def make_random_connections(self, iterations): for i in range(iterations): for user in self.users: self.make_random_connection(user) # generates a random aproximation of coach coachee relations def init_semi_random(self, size, connectionFactor=10, coacheesFactor=2): self.users = [] numCoaches = (size // 6) + 1 remainingCoachees = size - numCoaches coacheesPerCoach = remainingCoachees // numCoaches remaining = size while remaining > 0: numCoachees = random.randint(1, math.floor(coacheesPerCoach * coacheesFactor)) remaining -= 1 numCoachees = min(numCoachees, remaining) self.add_coach(numCoachees) remaining -= numCoachees self.make_sparse_random_connections(size // connectionFactor) def init_semi_random_connected(self, size): self.init_semi_random(size, connectionFactor=1) def add_coach(self, numCoachees): coach = User() self.add_user(coach) self.add_coachees(coach, numCoachees) def add_coachees(self, user, numToAdd): if user not in self.users: return for i in range(numToAdd): coachee = User() self.add_user(coachee) self.create_coach_coachee_relationship(user, coachee) def make_sparse_random_connections(self, numConnections): for user in random.sample(self.users, numConnections): self.make_random_connection(user) def make_random_connection(self, user): coachee = user while coachee == user: coachee = random.choice(self.users) if coachee in user.neighbors: continue self.create_coach_coachee_relationship(user, coachee) def add_user(self, user): if user in self.users: return self.users.append(user) def add_users(self, users): for user in users: self.add_user(user) def create_coach_coachee_relationship(self, userA, userB): if userA not in self.users or userB not in self.users: return userA.add_coachee(userB) userB.add_coach(userA) def clean(self): for user in self.users: user.infected = False user.numConnections = 0 user.epicenter = False user.visited = False def draw(self): self.visualizer.update(self.users) self.visualizer.draw(self.infecter.get_solution_quality(self.users)) def save_as(self, filename): self.visualizer.update(self.users) data = [self.users, self.visualizer] pickler.save_as(data, filename) def load_from(self, filename): try: data = pickler.load_file(filename) except FileNotFoundError: logger.error('Failed to load, File not found', exc_info=True) return except Exception: logger.error('Failed to load, something else went wrong', exc_info=True) return self.users, self.visualizer = data