def create_rand_graph(config_file): """ :param config_file: :return: """ new_graph = GraphObject(config_file) config = Utils.read_game_config_file(config_file) for i in range(config.getint("GeneralParams", "NodeCount")): while True: xRandom = random.randint( config.getint("NodeData", "NodeSize"), config.getint("GeneralParams", "GraphSizeX") - config.getint("NodeData", "NodeSize")) yRandom = random.randint( config.getint("NodeData", "NodeSize"), config.getint("GeneralParams", "GraphSizeY") - config.getint("NodeData", "NodeSize")) if not check_collisions(xRandom, yRandom, new_graph, config.getint("NodeData", "NodeSize"), config.getint("NodeData", "ExtraDistance")): break randColor = random.choice(Colours.values()) new_graph.add_node(xRandom, yRandom, randColor, Shapes['circle'], config.getint("NodeData", "NodeSize")) connect_graph(new_graph, config.getint("NodeData", "MaxNeighbors"), config.getint("NodeData", "MinNeighbors")) return new_graph
def test_get_node_by_serial(self): # Arrange new_graph = GraphObject(max_x=1000, max_y=1000, node_count=20, max_neighbors=5, extra_distance=10) serial_1 = new_graph.add_node(50, 50).serial_num serial_2 = new_graph.add_node(50, 100).serial_num serial_3 = new_graph.add_node(50, 200).serial_num # Assert self.assertEqual(new_graph.get_node_by_serial(serial_1).y, 50) self.assertEqual(new_graph.get_node_by_serial(serial_2).y, 100) self.assertEqual(new_graph.get_node_by_serial(serial_3).y, 200)
def test_get_possible_connections(self): # Arrange new_graph = GraphObject(max_x=1000, max_y=1000, node_count=20, max_neighbors=5, extra_distance=10) # Act serial1 = new_graph.add_node(50, 50).serial_num serial2 = new_graph.add_node(50, 100).serial_num serial3 = new_graph.add_node(50, 200).serial_num list = new_graph.get_possible_connections(serial1) # Assert self.assertTrue(list.__contains__(serial2)) self.assertFalse(list.__contains__(serial3))
def test_get_best_connection(self): # Arrange new_graph = GraphObject(max_x=1000, max_y=1000, node_count=20, max_neighbors=5, extra_distance=10) serial_1 = new_graph.add_node(50, 50).serial_num serial_2 = new_graph.add_node(50, 100).serial_num serial_3 = new_graph.add_node(50, 200).serial_num # Act connection_list = new_graph.get_possible_connections(serial_1) best_connection_id = new_graph.get_best_connection(new_graph.get_node_by_serial(serial_1), connection_list) # Assert self.assertEqual(best_connection_id, serial_2)
def test_add_node(self): # Arrange x_coor = 50 y_coor = 50 size = 4 new_graph = GraphObject(max_x=1000, max_y=1000, node_count=20, max_neighbors=5, extra_distance=10) # Act new_graph.add_node(x_coor, y_coor, node_size=size) # Assert self.assertEqual(new_graph.node_list[0].x,x_coor) self.assertEqual(new_graph.node_list[0].y,y_coor) self.assertEqual(new_graph.node_list[0].size,size)
def test_is_node_far_enough(self): # Arrange new_graph = GraphObject(max_x=1000, max_y=1000, node_count=20, max_neighbors=5, extra_distance=10) new_graph.add_node(50, 50) new_graph.add_node(50, 100) new_graph.add_node(50, 200) serial = new_graph.node_list[0].serial_num serial2 = new_graph.node_list[1].serial_num serial3 = new_graph.node_list[2].serial_num # Act # Assert self.assertFalse(new_graph.is_node_far_enough(new_graph.node_list[1], new_graph.node_list[0], new_graph.node_list[2])) self.assertTrue(new_graph.is_node_far_enough(new_graph.node_list[0], new_graph.node_list[1], new_graph.node_list[2]))
class GameDataHandler: graph = None def __init__(self, config, graph_size=None): # basic_config - self.edges = [] self.graph = GraphObject(config) self.graph.size = graph_size if graph_size is not None else self.graph.size self.extra_edges = [] self.new_edges = [] self.edges_to_add = [] self.log = logging.getLogger() self.config = Utils.graph_config_data self.log.setLevel(Utils.game_config_data['Default']['log_level']) def get_number_of_known_nodes(self): return len([real_node for real_node in self.graph.node_list if real_node.real]) def add_view_to_db(self, view): """ Go over the new view gotten from the game. For each new node add it to self.graph.node_list For each edge check both ends. if the node has real=False we know it's only a placeholder. Update the graph of BasicGamer :param view: A dictionary containing the data gotten from the screen :return: None """ # Innumerate over the nodes for node in view['nodes']: if self.graph.get_node_by_serial(node.serial_num) is None: self.graph.add_node(node.x, node.y, node_colour=node.colour, node_size=node.size, serial=node.serial_num) num = self.graph.get_node_by_serial(node.serial_num).dummy_num #self.log.info("Adding node: num="+ str(num)+ ", real=True", location="{}:{}".format(node.x, node.y),serial=node.serial_num) # Innumerate over the edges for edge in view['edges']: if self.graph.get_node_by_serial(edge[0].serial_num) is not None: node_0 = self.graph.get_node_by_serial(edge[0].serial_num) else: node_0 = self.graph.add_node(edge[0].x, edge[0].y, node_size=1, real=False, serial=edge[0].serial_num) num = self.graph.get_node_by_serial(node_0.serial_num).dummy_num GLogger.log(logging.DEBUG,Utils.format_log_msg("Adding node",num=num, real=False, location="{}:{}".format(node_0.x, node_0.y), serial=node_0.serial_num)) if self.graph.get_node_by_serial(edge[1].serial_num) is not None: node_1 = self.graph.get_node_by_serial(edge[1].serial_num) else: node_1 = self.graph.add_node(edge[1].x, edge[1].y, node_size=1, real=False, serial=edge[1].serial_num) num = self.graph.get_node_by_serial(node_1.serial_num).dummy_num GLogger.log(logging.DEBUG, Utils.format_log_msg("Adding node",num=num, real=False, location="{}:{}".format(node_1.x, node_1.y), serial=node_1.serial_num)) if node_1.serial_num not in node_0.possible_neighbors: node_0.possible_neighbors.add(node_1.serial_num) if node_0.serial_num not in node_1.possible_neighbors: node_1.possible_neighbors.add(node_0.serial_num) self.graph.connect_nodes(node_0, node_1, allow_overflow=True) if edge not in self.extra_edges: self.extra_edges.append(edge) self.edges_to_add = [] GLogger.log(logging.DEBUG, "Triming data from graph") self.trim_data() GLogger.log(logging.DEBUG, "Adding extra edges to edge list") for item in self.edges_to_add: self.extra_edges.append(item) self.clear_empty_nodes() GLogger.log(logging.DEBUG, Utils.format_log_msg("Finished triming data:", num_of_node=len(self.graph.node_list), num_real_node=(len([item for item in self.graph.node_list if item.is_real()])), num_of_edges=len(self.extra_edges))) GLogger.log(logging.DEBUG, Utils.format_log_msg("edge list:", edges=self.extra_edges)) def trim_data(self): """ Goes over all of the data that is saved and trims nodes and edges. Connects fake nodes to real ones if we found the actual node connects open edges together if possible """ slope_set = set() for edge in self.extra_edges: slope_set.add(edge[3].slope) sorted(slope_set) GLogger.log(logging.DEBUG,"Number of slops found = {}".format(len(slope_set))) for slope in list(slope_set): edges_to_check = [] for edge in self.extra_edges: if edge[3].slope == slope: edges_to_check.append(edge) GLogger.log(logging.DEBUG,Utils.format_log_msg("Number of edges in slope: ",slope="{} = {}".format(slope, len(edges_to_check)), edges=edges_to_check)) if len(edges_to_check) > 1: # we have two edges with the same slope! # Removing all edges from list. We add only the relevant ones later on for item in edges_to_check: self.extra_edges.remove(item) while True: first_edge = edges_to_check.pop() edge_reconstructed = False for second_edge in edges_to_check: if self.two_edges_are_one(first_edge, second_edge): GLogger.log(logging.DEBUG, "two edges are one") edge_reconstructed = True edges_to_check.remove(second_edge) edges_to_check.append(self.connect_edges(first_edge, second_edge)) break if not edge_reconstructed: # edge does not match any other on the list. We can leave it alone self.edges_to_add.append(first_edge) if len(edges_to_check) <= 1: self.edges_to_add.append(edges_to_check[0]) break def two_edges_are_one(self, edge_1, edge_2): """ Checks if the two edges are actually a single edge. :return: True if edges are 100% the same one """ eq1 = LineEquation(slope=edge_1[3].slope, const=edge_1[3].const, edge1=edge_1[0], edge2=edge_1[1]) eq2 = LineEquation(slope=edge_2[3].slope, const=edge_2[3].const, edge1=edge_2[0], edge2=edge_2[1]) # Check collision point collision_point = LineEquation.get_equation_collision_point(eq1, eq2) GLogger.log(logging.DEBUG, Utils.format_log_msg("Found collision point of both edges", point=collision_point, eq1=eq1, eq2=eq2)) if collision_point == LINES_ALWAYS_MEET: # Lines have the same slope + const. Big change they are the same one. if LineEquation.check_collision_point(eq1, eq2): GLogger.log(logging.DEBUG, "Lines meet and intersect with each other - They are the same line") return True else: GLogger.log(logging.DEBUG, "Lines have the same parameters but we are not sure if they meet") return False return False def connect_edges(self, edge_1, edge_2): """ The longest distance in an edge is the distance between the real nodes of that edge. We clean all existing connections and then connect the two nodes that are furthest from each other. :param edge_1, edge_2: An edge defined by a tuple of NodeObjects :returns the new edge created """ # Cleaning all existing connection # self.log.debug("Connecting edges", edge1=edge_1, edge2=edge_2) if edge_1 in self.extra_edges: GLogger.log(logging.DEBUG, "Removing edge from extra edges", removed_edge=edge_1) self.extra_edges.remove(edge_1) if edge_2 in self.extra_edges: GLogger.log(logging.DEBUG, "Removing edge from extra edges", removed_edge=edge_2) self.extra_edges.remove(edge_2) if edge_1[0] is None or edge_1[1] is None or edge_2[0] is None or edge_2[1] is None: raise Exception("Cleaning Nodes failed - At least one of the nodes does not exist") self.clean_connection(edge_1[0], edge_1[1]) self.clean_connection(edge_1[1], edge_1[0]) if edge_2[0] != edge_1[0] or edge_2[1] != edge_2[1]: self.clean_connection(edge_2[0], edge_2[1]) self.clean_connection(edge_2[1], edge_2[0]) node_1_serial, node_2_serial = self.get_furthest_nodes(edge_1[0], edge_1[1], edge_2[0], edge_2[1]) node_1 = self.graph.get_node_by_serial(node_1_serial) node_2 = self.graph.get_node_by_serial(node_2_serial) # connect the right nodes self.connect_nodes(node_1, node_2) if node_1.x < node_2.x: new_edge = (node_1, node_2, edge_1[2], LineEquation(slope=edge_1[3].slope, const=edge_1[3].const, edge1=node_1, edge2=node_2)) else: new_edge = (node_2, node_1, edge_1[2], LineEquation(slope=edge_1[3].slope, const=edge_1[3].const, edge1=node_2, edge2=node_1)) #self.edges_to_add.append(new_edge) return new_edge def clean_connection(self, main_node, node_to_remove): """ Removed all connection from main_node regarding node_to_remove :param node_to_remove: The node to remove - node object :param main_node: The node we want to remove data from - node object :return: """ GLogger.log(logging.DEBUG, Utils.format_log_msg("Cleaning connection to another node", main_node=main_node.dummy_num, node_to_remove=node_to_remove.dummy_num)) node = self.graph.get_node_by_serial(main_node.serial_num) if node is None: raise Exception("Node '{}' was not found in node list. Node list = {}" .format(main_node.dummy_num, [found_nodes.dummy_num for found_nodes in self.graph.node_list])) if node_to_remove.serial_num in node.neighbors: node.neighbors.remove(node_to_remove.serial_num) if node_to_remove.serial_num in node.possible_neighbors: node.possible_neighbors.remove(node_to_remove.serial_num) def connect_nodes(self, first_node, second_node): """ Connect the two nodes in the graph. """ node_0 = self.graph.get_node_by_serial(first_node.serial_num) node_1 = self.graph.get_node_by_serial(second_node.serial_num) node_0.possible_neighbors.add(node_1.serial_num) node_1.possible_neighbors.add(node_0.serial_num) self.graph.connect_nodes(node_0, node_1, allow_overflow=True) @staticmethod def get_furthest_nodes(*args): """ :param args: a list of NodeObjects :return: The serial numbers of the two furthest nodes from each of other. """ node_list = {} DistanceTuple = namedtuple('Distance', ['point', 'distance']) for node in args: if type(node) != NodeObject: raise Exception("One of the arguments is not of type NodeObjects - {}".format(node)) distance_list = [] for other_node in args: if other_node != node: distance_list.append(DistanceTuple(point=other_node, distance=node.distance(other_node))) node_list[node.serial_num] = distance_list best_distance = 0 best_pair = () for node in args: for other_node in node_list[node.serial_num]: if other_node.distance > best_distance: best_pair = (node.serial_num, other_node[0].serial_num) best_distance = other_node[1] return best_pair def clear_empty_nodes(self): """ Go over node list and see if two nodes are the same. :return: """ remove_list = [] GLogger.log(logging.DEBUG, "removing nodes with no neighbors") for node in self.graph.node_list: if len(node.neighbors) == 0: GLogger.log(logging.DEBUG, Utils.format_log_msg("Found node with no neighbors - deleting:", serial=node.serial_num, real=node.is_real())) remove_list.append(node.serial_num) for serial in remove_list: self.graph.node_list.remove(self.graph.get_node_by_serial(serial)) GLogger.log(logging.DEBUG, "removed {} nodes".format(len(remove_list))) def cleaned_graph(self): """ Called at the end of a run. Cleans the graph of none real connections This is really just a patch because we enter bad connections. We should probably fix the source of the issue :return: the cleaned graph """ self.graph.connections = [] for edge in self.extra_edges: if edge[0].is_real() and edge[1].is_real(): self.graph.connections.append((min(edge[0].serial_num, edge[1].serial_num), max(edge[0].serial_num, edge[1].serial_num))) else: self.clean_connection(edge[0], edge[1]) self.clean_connection(edge[1], edge[0]) self.extra_edges = [] real_nodes = [] for node in self.graph.node_list: if node.is_real(): nodes_to_remove = [] for neightbor in node.neighbors: if not self.graph.get_node_by_serial(neightbor).is_real(): nodes_to_remove.append(self.graph.get_node_by_serial(neightbor)) for item in nodes_to_remove: self.clean_connection(node, item) real_nodes.append(node) # Make sure we see only the same edge once self.graph.connections = list(set(self.graph.connections)) GLogger.log(logging.DEBUG, Utils.format_log_msg("Finished cleaning graph before continuing:", num_of_nodes=len(self.graph.node_list), num_real_nodes=(len([item for item in self.graph.node_list if item.is_real()])), num_of_connections=len(self.graph.connections))) self.graph.node_list = real_nodes return self.graph def get_real_nodes(self): return [item for item in self.graph.node_list if item.is_real()]
def create_draft_graph_1_rotate(): GLogger('file', 'handmade_graph_logger.txt', 'ERROR') draft_graph = GraphObject(max_x=2850, max_y=2750, node_count=15, max_neighbors=5, extra_distance=1) draft_graph.add_node(x_loc=687, y_loc=2169, node_colour=Colours['yellow'], serial='n1') draft_graph.add_node(x_loc=58, y_loc=2450, node_colour=Colours['red'], serial='n2') draft_graph.add_node(x_loc=294, y_loc=1255, node_colour=Colours['blue'], serial='n3') draft_graph.add_node(x_loc=939, y_loc=1879, node_colour=Colours['blue'], serial='n4') draft_graph.add_node(x_loc=730, y_loc=772, node_colour=Colours['red'], serial='n5') draft_graph.add_node(x_loc=1394, y_loc=343, node_colour=Colours['red'], serial='n6') draft_graph.add_node(x_loc=839, y_loc=319, node_colour=Colours['yellow'], serial='n7') draft_graph.add_node(x_loc=2109, y_loc=902, node_colour=Colours['blue'], serial='n8') draft_graph.add_node(x_loc=2017, y_loc=345, node_colour=Colours['red'], serial='n9') draft_graph.add_node(x_loc=1319, y_loc=1322, node_colour=Colours['red'], serial='n10') draft_graph.add_node(x_loc=1973, y_loc=2006, node_colour=Colours['blue'], serial='n11') draft_graph.add_node(x_loc=2476, y_loc=2289, node_colour=Colours['red'], serial='n12') draft_graph.add_node(x_loc=1737, y_loc=1514, node_colour=Colours['yellow'], serial='n13') draft_graph.add_node(x_loc=1768, y_loc=2676, node_colour=Colours['yellow'], serial='n14') draft_graph.add_node(x_loc=416, y_loc=748, node_colour=Colours['blue'], serial='n15') draft_graph.center_node = "n1" for node in draft_graph.node_list: draft_graph.get_possible_connections(node.serial_num) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n1"), draft_graph.get_node_by_serial("n2")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n1"), draft_graph.get_node_by_serial("n3")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n3"), draft_graph.get_node_by_serial("n4")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n3"), draft_graph.get_node_by_serial("n5")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n6"), draft_graph.get_node_by_serial("n7")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n8"), draft_graph.get_node_by_serial("n9")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n8"), draft_graph.get_node_by_serial("n10")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n12"), draft_graph.get_node_by_serial("n11")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n5"), draft_graph.get_node_by_serial("n6")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n6"), draft_graph.get_node_by_serial("n8")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n11"), draft_graph.get_node_by_serial("n8")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n10"), draft_graph.get_node_by_serial("n13")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n14"), draft_graph.get_node_by_serial("n12")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n7"), draft_graph.get_node_by_serial("n15")) question_one = QuestionObject( "How many red nodes do not have links to yellow nodes?", QuestionTypes['NUMBER'], 5, Colours['red'], Colours['yellow']) question_two = QuestionObject("Is the number of red nodes even?", QuestionTypes['BOOLEAN'], 15, Colours['red'], 0) question_three = QuestionObject( "Does all the blue nodes have an odd number of links?", QuestionTypes['BOOLEAN'], 13, Colours['blue'], 1) question_four = QuestionObject( "What is the color that contains the smallest total number of links?", QuestionTypes['MULTIPLE_CHOICE'], 7) question_five = QuestionObject( "Does every yellow node have a link to a red node?", QuestionTypes['BOOLEAN'], 9, Colours['yellow'], Colours['red']) question_six = QuestionObject( "Are there more blue nodes than yellow nodes?", QuestionTypes['BOOLEAN'], 10, Colours['blue'], Colours['yellow']) question_seven = QuestionObject( "Is there a red nodes that has a link to another red node?", QuestionTypes['BOOLEAN'], 8, Colours['red']) draft_graph.question_object_list = [ question_one, question_two, question_three, question_four, question_five, question_six, question_seven ] save_graph_json(draft_graph, "Graph_1_rotate.json") #create_draft_graph_1_Transpose() #create_draft_graph_1_rotate() #create_draft_graph_1() #create_draft_graph_2() #create_draft_graph_3() #create_draft_graph_4() #create_draft_graph_5()
def create_draft_graph_5(): GLogger('file', 'handmade_graph_logger.txt', 'ERROR') draft_graph = GraphObject(max_x=2350, max_y=3050, node_count=15, max_neighbors=5, extra_distance=1) draft_graph.add_node(x_loc=405, y_loc=2786, node_colour=Colours['red'], serial='n1') draft_graph.add_node(x_loc=340, y_loc=441, node_colour=Colours['red'], serial='n2') draft_graph.add_node(x_loc=657, y_loc=1902, node_colour=Colours['red'], serial='n3') draft_graph.add_node(x_loc=187, y_loc=2512, node_colour=Colours['blue'], serial='n4') draft_graph.add_node(x_loc=336, y_loc=1153, node_colour=Colours['blue'], serial='n5') draft_graph.add_node(x_loc=1747, y_loc=2763, node_colour=Colours['yellow'], serial='n6') draft_graph.add_node(x_loc=1809, y_loc=514, node_colour=Colours['blue'], serial='n7') draft_graph.add_node(x_loc=1845, y_loc=2169, node_colour=Colours['blue'], serial='n8') draft_graph.add_node(x_loc=1125, y_loc=2383, node_colour=Colours['blue'], serial='n9') draft_graph.add_node(x_loc=1681, y_loc=1621, node_colour=Colours['yellow'], serial='n10') draft_graph.add_node(x_loc=1456, y_loc=152, node_colour=Colours['blue'], serial='n11') draft_graph.add_node(x_loc=2011, y_loc=1486, node_colour=Colours['red'], serial='n12') draft_graph.add_node(x_loc=2008, y_loc=345, node_colour=Colours['yellow'], serial='n13') draft_graph.add_node(x_loc=1449, y_loc=2993, node_colour=Colours['red'], serial='n14') draft_graph.add_node(x_loc=2296, y_loc=812, node_colour=Colours['red'], serial='n15') draft_graph.center_node = "n5" for node in draft_graph.node_list: draft_graph.get_possible_connections(node.serial_num) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n5"), draft_graph.get_node_by_serial("n4")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n5"), draft_graph.get_node_by_serial("n3")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n5"), draft_graph.get_node_by_serial("n2")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n3"), draft_graph.get_node_by_serial("n9")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n3"), draft_graph.get_node_by_serial("n10")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n1"), draft_graph.get_node_by_serial("n9")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n12"), draft_graph.get_node_by_serial("n10")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n12"), draft_graph.get_node_by_serial("n11")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n12"), draft_graph.get_node_by_serial("n13")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n6"), draft_graph.get_node_by_serial("n8")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n13"), draft_graph.get_node_by_serial("n15")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n7"), draft_graph.get_node_by_serial("n13")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n14"), draft_graph.get_node_by_serial("n8")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n8"), draft_graph.get_node_by_serial("n12")) question_one = QuestionObject( 'Is there a yellow node that has at least 3 links to another yellow node?', QuestionTypes['BOOLEAN'], 11, Colours['yellow'], 3) question_two = QuestionObject( 'Does all the red nodes have an odd number of links?', QuestionTypes['BOOLEAN'], 13, Colours['red'], 1) question_three = QuestionObject('How many yellow nodes are there?', QuestionTypes['NUMBER'], 1, Colours['yellow']) question_four = QuestionObject( 'What is the color that contains the smallest total number of links?', QuestionTypes['MULTIPLE_CHOICE'], 7) question_five = QuestionObject( 'Does every blue node have a link to a yellow node?', QuestionTypes['BOOLEAN'], 9, Colours['blue'], Colours['yellow']) question_six = QuestionObject('Is the number of blue nodes even?', QuestionTypes['BOOLEAN'], 15, Colours['blue'], 0) question_seven = QuestionObject( 'How many yellow nodes do not have links to red nodes?', QuestionTypes['NUMBER'], 5, Colours['yellow'], Colours['red']) draft_graph.question_object_list = [ question_one, question_two, question_three, question_four, question_five, question_six, question_seven ] save_graph_json(draft_graph, "Graph_5.json")
def create_draft_graph_4(): GLogger('file', 'handmade_graph_logger.txt', 'ERROR') draft_graph = GraphObject(max_x=4750, max_y=2400, node_count=15, max_neighbors=5, extra_distance=1) draft_graph.add_node(x_loc=936, y_loc=1671, node_colour=Colours['blue'], serial='n1') draft_graph.add_node(x_loc=480, y_loc=1998, node_colour=Colours['yellow'], serial='n2') draft_graph.add_node(x_loc=96, y_loc=908, node_colour=Colours['blue'], serial='n3') draft_graph.add_node(x_loc=1668, y_loc=2007, node_colour=Colours['red'], serial='n4') draft_graph.add_node(x_loc=2112, y_loc=1816, node_colour=Colours['yellow'], serial='n5') draft_graph.add_node(x_loc=2976, y_loc=1825, node_colour=Colours['yellow'], serial='n6') draft_graph.add_node(x_loc=2392, y_loc=2356, node_colour=Colours['blue'], serial='n7') draft_graph.add_node(x_loc=3360, y_loc=1271, node_colour=Colours['blue'], serial='n8') draft_graph.add_node(x_loc=1788, y_loc=1453, node_colour=Colours['red'], serial='n9') draft_graph.add_node(x_loc=4440, y_loc=1280, node_colour=Colours['blue'], serial='n10') draft_graph.add_node(x_loc=3600, y_loc=1734, node_colour=Colours['yellow'], serial='n11') draft_graph.add_node(x_loc=4680, y_loc=1635, node_colour=Colours['red'], serial='n12') draft_graph.add_node(x_loc=480, y_loc=1380, node_colour=Colours['red'], serial='n13') draft_graph.add_node(x_loc=2880, y_loc=566, node_colour=Colours['red'], serial='n14') draft_graph.add_node(x_loc=3372, y_loc=1998, node_colour=Colours['red'], serial='n15') draft_graph.center_node = "n1" for node in draft_graph.node_list: draft_graph.get_possible_connections(node.serial_num) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n1"), draft_graph.get_node_by_serial("n2")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n1"), draft_graph.get_node_by_serial("n13")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n1"), draft_graph.get_node_by_serial("n9")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n1"), draft_graph.get_node_by_serial("n4")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n2"), draft_graph.get_node_by_serial("n3")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n2"), draft_graph.get_node_by_serial("n4")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n3"), draft_graph.get_node_by_serial("n4")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n3"), draft_graph.get_node_by_serial("n13")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n4"), draft_graph.get_node_by_serial("n5")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n5"), draft_graph.get_node_by_serial("n9")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n5"), draft_graph.get_node_by_serial("n7")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n6"), draft_graph.get_node_by_serial("n8")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n6"), draft_graph.get_node_by_serial("n11")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n6"), draft_graph.get_node_by_serial("n15")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n6"), draft_graph.get_node_by_serial("n7")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n7"), draft_graph.get_node_by_serial("n15")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n8"), draft_graph.get_node_by_serial("n9")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n8"), draft_graph.get_node_by_serial("n10")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n8"), draft_graph.get_node_by_serial("n14")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n10"), draft_graph.get_node_by_serial("n12")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n10"), draft_graph.get_node_by_serial("n14")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n10"), draft_graph.get_node_by_serial("n15")) question_one = QuestionObject( 'Is there a red node that has at least two links to another red node?', QuestionTypes['BOOLEAN'], 11, Colours['red'], 2) question_two = QuestionObject( 'Is the sum of the links of all the red nodes even?', QuestionTypes['BOOLEAN'], 14, Colours['red'], 0) question_three = QuestionObject( 'Which color has the largest number of nodes?', QuestionTypes['MULTIPLE_CHOICE'], 16) question_four = QuestionObject( 'How many blue nodes have links to yellow nodes?', QuestionTypes['NUMBER'], 2, Colours['blue'], Colours['yellow']) question_five = QuestionObject( 'What is the color that contains the largest total number of links?', QuestionTypes['MULTIPLE_CHOICE'], 4) question_six = QuestionObject( 'Does every yellow node have a link to a red node?', QuestionTypes['BOOLEAN'], 9, Colours['yellow'], Colours['red']) question_seven = QuestionObject( 'Are there more blue nodes than yellow nodes?', QuestionTypes['BOOLEAN'], 10, Colours['blue'], Colours['yellow']) draft_graph.question_object_list = [ question_one, question_two, question_three, question_four, question_five, question_six, question_seven ] save_graph_json(draft_graph, "Graph_4.json")
def create_draft_graph_3(): GLogger('file', 'handmade_graph_logger.txt', 'ERROR') draft_graph = GraphObject(max_x=2950, max_y=2850, node_count=15, max_neighbors=5, extra_distance=1) draft_graph.add_node(x_loc=62, y_loc=90, node_colour=Colours['yellow'], serial='n1') draft_graph.add_node(x_loc=1082, y_loc=726, node_colour=Colours['blue'], serial='n2') draft_graph.add_node(x_loc=482, y_loc=944, node_colour=Colours['blue'], serial='n3') draft_graph.add_node(x_loc=1800, y_loc=581, node_colour=Colours['blue'], serial='n4') draft_graph.add_node(x_loc=1584, y_loc=1358, node_colour=Colours['yellow'], serial='n5') draft_graph.add_node(x_loc=2880, y_loc=726, node_colour=Colours['red'], serial='n6') draft_graph.add_node(x_loc=1896, y_loc=90, node_colour=Colours['yellow'], serial='n7') draft_graph.add_node(x_loc=2090, y_loc=1911, node_colour=Colours['yellow'], serial='n8') draft_graph.add_node(x_loc=2496, y_loc=1235, node_colour=Colours['blue'], serial='n9') draft_graph.add_node(x_loc=1130, y_loc=2147, node_colour=Colours['red'], serial='n10') draft_graph.add_node(x_loc=2904, y_loc=2328, node_colour=Colours['red'], serial='n11') draft_graph.add_node(x_loc=132, y_loc=1653, node_colour=Colours['blue'], serial='n12') draft_graph.add_node(x_loc=484, y_loc=2610, node_colour=Colours['yellow'], serial='n13') draft_graph.add_node(x_loc=1586, y_loc=2739, node_colour=Colours['red'], serial='n14') draft_graph.add_node(x_loc=2378, y_loc=2801, node_colour=Colours['blue'], serial='n15') draft_graph.center_node = "n2" for node in draft_graph.node_list: draft_graph.get_possible_connections(node.serial_num) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n1"), draft_graph.get_node_by_serial("n2")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n1"), draft_graph.get_node_by_serial("n3")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n2"), draft_graph.get_node_by_serial("n4")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n4"), draft_graph.get_node_by_serial("n5")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n4"), draft_graph.get_node_by_serial("n6")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n6"), draft_graph.get_node_by_serial("n7")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n8"), draft_graph.get_node_by_serial("n10")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n11"), draft_graph.get_node_by_serial("n8")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n10"), draft_graph.get_node_by_serial("n5")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n9"), draft_graph.get_node_by_serial("n11")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n10"), draft_graph.get_node_by_serial("n12")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n13"), draft_graph.get_node_by_serial("n10")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n8"), draft_graph.get_node_by_serial("n14")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n15"), draft_graph.get_node_by_serial("n14")) question_one = QuestionObject( 'Is there a red node with an odd number of links?', QuestionTypes['BOOLEAN'], 12, Colours['red'], 1) question_two = QuestionObject( 'Which color has the smallest number of nodes?', QuestionTypes['MULTIPLE_CHOICE'], 17) question_three = QuestionObject('How many blue nodes are there?', QuestionTypes['NUMBER'], 1, Colours['blue']) question_four = QuestionObject( 'What is the color of the node with the largest number of links?', QuestionTypes['MULTIPLE_CHOICE'], 3) question_five = QuestionObject( 'Does every red node have a link to a yellow node?', QuestionTypes['BOOLEAN'], 9, Colours['red'], Colours['yellow']) question_six = QuestionObject( 'Does all the blue nodes have an even number of links?', QuestionTypes['BOOLEAN'], 13, Colours['blue'], 0) question_seven = QuestionObject( 'Are there more red nodes than blue nodes?', QuestionTypes['BOOLEAN'], 10, Colours['red'], Colours['blue']) draft_graph.question_object_list = [ question_one, question_two, question_three, question_four, question_five, question_six, question_seven ] save_graph_json(draft_graph, "Graph_3.json")
def create_draft_graph_2(): draft_graph = GraphObject(max_x=2450, max_y=3200, node_count=15, max_neighbors=5, extra_distance=1) draft_graph.add_node(x_loc=144, y_loc=2252, node_colour=Colours['blue'], serial='n1') draft_graph.add_node(x_loc=506, y_loc=2525, node_colour=Colours['yellow'], serial='n2') draft_graph.add_node(x_loc=1224, y_loc=2052, node_colour=Colours['blue'], serial='n3') draft_graph.add_node(x_loc=1704, y_loc=2434, node_colour=Colours['blue'], serial='n4') draft_graph.add_node(x_loc=1824, y_loc=1526, node_colour=Colours['red'], serial='n5') draft_graph.add_node(x_loc=2064, y_loc=1071, node_colour=Colours['red'], serial='n6') draft_graph.add_node(x_loc=1344, y_loc=890, node_colour=Colours['red'], serial='n7') draft_graph.add_node(x_loc=1464, y_loc=617, node_colour=Colours['blue'], serial='n8') draft_graph.add_node(x_loc=2376, y_loc=617, node_colour=Colours['yellow'], serial='n9') draft_graph.add_node(x_loc=746, y_loc=981, node_colour=Colours['blue'], serial='n10') draft_graph.add_node(x_loc=360, y_loc=1529, node_colour=Colours['red'], serial='n11') draft_graph.add_node(x_loc=72, y_loc=1635, node_colour=Colours['yellow'], serial='n12') draft_graph.add_node(x_loc=508, y_loc=1689, node_colour=Colours['blue'], serial='n13') draft_graph.add_node(x_loc=1226, y_loc=163, node_colour=Colours['red'], serial='n14') draft_graph.add_node(x_loc=196, y_loc=3161, node_colour=Colours['blue'], serial='n15') draft_graph.center_node = "n2" for node in draft_graph.node_list: draft_graph.get_possible_connections(node.serial_num) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n1"), draft_graph.get_node_by_serial("n2")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n2"), draft_graph.get_node_by_serial("n3")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n3"), draft_graph.get_node_by_serial("n4")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n3"), draft_graph.get_node_by_serial("n5")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n5"), draft_graph.get_node_by_serial("n6")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n6"), draft_graph.get_node_by_serial("n8")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n6"), draft_graph.get_node_by_serial("n9")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n8"), draft_graph.get_node_by_serial("n10")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n10"), draft_graph.get_node_by_serial("n11")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n11"), draft_graph.get_node_by_serial("n12")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n11"), draft_graph.get_node_by_serial("n13")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n8"), draft_graph.get_node_by_serial("n14")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n7"), draft_graph.get_node_by_serial("n8")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n2"), draft_graph.get_node_by_serial("n15")) question_one = QuestionObject("How many red nodes are there?", QuestionTypes['NUMBER'], 1, Colours['red']) question_two = QuestionObject( "How many blue nodes do not have links to yellow nodes", QuestionTypes['NUMBER'], 5, Colours['blue'], Colours['yellow']) question_three = QuestionObject( "Which color has the largest number of nodes?", QuestionTypes['MULTIPLE_CHOICE'], 16) question_four = QuestionObject( "Is there a blue node that has at least 2 links to another blue node?", QuestionTypes['BOOLEAN'], 11, Colours['blue'], 2) question_five = QuestionObject( "What is the color of the node with the largest number of links?", QuestionTypes['MULTIPLE_CHOICE'], 3) question_six = QuestionObject("Is every blue node linked to a red node?", QuestionTypes['BOOLEAN'], 9, Colours['blue'], Colours['red']) question_seven = QuestionObject("Is there an even number of yellow nodes?", QuestionTypes['BOOLEAN'], 15, Colours['yellow'], 0) draft_graph.question_object_list = [ question_one, question_two, question_three, question_four, question_five, question_six, question_seven ] save_graph_json(draft_graph, "Graph_2.json")
def create_draft_graph_1(): GLogger('file', 'handmade_graph_logger.txt', 'ERROR') draft_graph = GraphObject(max_x=2750, max_y=2850, node_count=15, max_neighbors=5, extra_distance=1) draft_graph.add_node(x_loc=2169, y_loc=2163, node_colour=Colours['yellow'], serial='n1') draft_graph.add_node(x_loc=2450, y_loc=2792, node_colour=Colours['red'], serial='n2') draft_graph.add_node(x_loc=1255, y_loc=2556, node_colour=Colours['blue'], serial='n3') draft_graph.add_node(x_loc=1879, y_loc=1911, node_colour=Colours['blue'], serial='n4') draft_graph.add_node(x_loc=772, y_loc=2120, node_colour=Colours['red'], serial='n5') draft_graph.add_node(x_loc=343, y_loc=1456, node_colour=Colours['red'], serial='n6') draft_graph.add_node(x_loc=319, y_loc=2011, node_colour=Colours['yellow'], serial='n7') draft_graph.add_node(x_loc=902, y_loc=741, node_colour=Colours['blue'], serial='n8') draft_graph.add_node(x_loc=345, y_loc=833, node_colour=Colours['red'], serial='n9') draft_graph.add_node(x_loc=1322, y_loc=1531, node_colour=Colours['red'], serial='n10') draft_graph.add_node(x_loc=2006, y_loc=877, node_colour=Colours['blue'], serial='n11') draft_graph.add_node(x_loc=2289, y_loc=374, node_colour=Colours['red'], serial='n12') draft_graph.add_node(x_loc=1514, y_loc=1113, node_colour=Colours['yellow'], serial='n13') draft_graph.add_node(x_loc=2676, y_loc=1082, node_colour=Colours['yellow'], serial='n14') draft_graph.add_node(x_loc=748, y_loc=2434, node_colour=Colours['blue'], serial='n15') draft_graph.center_node = "n1" for node in draft_graph.node_list: draft_graph.get_possible_connections(node.serial_num) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n1"), draft_graph.get_node_by_serial("n2")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n1"), draft_graph.get_node_by_serial("n3")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n3"), draft_graph.get_node_by_serial("n4")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n3"), draft_graph.get_node_by_serial("n5")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n6"), draft_graph.get_node_by_serial("n7")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n8"), draft_graph.get_node_by_serial("n9")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n8"), draft_graph.get_node_by_serial("n10")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n12"), draft_graph.get_node_by_serial("n11")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n5"), draft_graph.get_node_by_serial("n6")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n6"), draft_graph.get_node_by_serial("n8")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n11"), draft_graph.get_node_by_serial("n8")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n10"), draft_graph.get_node_by_serial("n13")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n14"), draft_graph.get_node_by_serial("n12")) draft_graph.connect_nodes(draft_graph.get_node_by_serial("n7"), draft_graph.get_node_by_serial("n15")) save_graph_json(draft_graph, "Graph_1.json") return draft_graph