Advertisement
HeUsesPython

Solve function.

Feb 13th, 2012
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.15 KB | None | 0 0
  1. def solve(self, step): #Completes the "template" as the student will.
  2.     #Variable 'step' determines whether or not the user is led through the algorithm
  3.    
  4.     #Get all the essential data
  5.     distance_matrix = self.get_distance_matrix()
  6.     current_id = self.get_current_id()
  7.     unvisited_list = self.get_unvisited_list() #Gets list of unvisited nodes.
  8.     visited_list = self.get_visited_list() #Gets list of visited nodes.
  9.  
  10.     #Update the start node boxes
  11.     id_coords_map = core.gui.ui.canvas.coords(core.core_dict[visited_list[0].name].order_box)
  12.     id_coords = map_to_list(id_coords_map) #If the .coords() method returns a map object it will be translated into a list.
  13.     core.core_dict[visited_list[0].name].order = core.gui.ui.canvas.create_text((id_coords[0]+id_coords[2])/2, (id_coords[1]+id_coords[3])/2, text="1")
  14.  
  15.     value_coords_map = core.gui.ui.canvas.coords(core.core_dict[visited_list[0].name].value_box)
  16.     value_coords = map_to_list(value_coords_map)
  17.     core.core_dict[visited_list[0].name].value = core.gui.ui.canvas.create_text((value_coords[0]+value_coords[2])/2, (value_coords[1]+value_coords[3])/2, text="0")
  18.  
  19.     if step == 1: #If step-through is on, make initial alterations.          
  20.         prep_text = "The first two boxes to be completed are those belonging to the start node \""+visited_list[0].name+"\". The distance to this node will be 0 since it is the \nfirst node in the route. (NB: from left-to-right the boxes signify: the node's name, the order of which the node was reached by the algorithm, \nthe value of the shortest “distance” to that node from the start node. The bottom box contains working values, more about that later.)"
  21.         core.gui.ui.explanation_text.config(text=prep_text)
  22.         self.change_colour(visited_list[0].name, "#FF6600")
  23.         self.cont = 0 #Sets the program to pause. The first of my ideas.
  24.        
  25.     while len(unvisited_list) > 0:
  26.         current_node = visited_list[-1] #The focussed on node is always the last item in the list of visited nodes.
  27.         adjacent_list = self.return_adjacent(current_node.name) #Returns a list of nodes adjacent to the current node in question.
  28.         self.change_colour(current_node.name, "#99CC00") #Uses a function to change the colour of the boxes on the UI.
  29.         for item in adjacent_list:
  30.             #Sum the value of the current node and the distance to visit adjacent nodes
  31.             sum_values = current_node.valeur + Decimal(distance_matrix[self.index_func(current_node.name, "index_matrix")][self.index_func(item.name, "index_matrix")])
  32.             if len(item.working_values) == 0: #If the adjacent node has not been looked at previously...
  33.                 item.working_values.append(sum_values) #Add the previously calculated sum to its working values.
  34.                 text_prep = item.working_values[0]
  35.  
  36.                 #Update the working values visually
  37.                 coords_map = core.gui.ui.canvas.coords(core.core_dict[item.name].working_values_box) #.coords() method returns map in some versions of Python
  38.                 coords = map_to_list(coords_map)
  39.                 core.core_dict[item.name].working_values = core.gui.ui.canvas.create_text((coords[0]+coords[2])/2, (coords[1]+coords[3])/2, text=text_prep)
  40.  
  41.                 time.sleep(2) #Testing sleep in general to see its effect.
  42.                 if step == 1:
  43.                     prep_text = "We go through and add the working values by looking at the most recent node that we included (highlighted in green) and adding \nthe value of that node with the \"distance\" required to travel to its neighbouring nodes that have not get been visited (in turquoise)."
  44.                     core.gui.ui.explanation_text.config(text=prep_text)
  45.                     self.change_colour(item.name, "#3AB1B1")
  46.                    
  47.             elif sum_values < item.working_values[-1]: #Else if it has been looked at previously...
  48.                 item.working_values.append(sum_values) #Only add to working values if it is smaller than the last.
  49.                 multi_text_prep = "" #Translating list into string for UI appearance.
  50.                 for value in item.working_values:
  51.                     multi_text_prep += " "+str(value)
  52.                 core.gui.ui.canvas.itemconfig(core.core_dict[item.name].working_values, text=multi_text_prep)
  53.         lowest_value = 4000 #Setting an unattractive value (this is obviously very technical).
  54.         lowest_value_node = None #This variable will contain the node object which will become the next node to focus on.
  55.         for item in unvisited_list: #Going through the list of unvisited nodes...
  56.             if len(item.working_values) != 0: #Making sure that they are relevant
  57.                 if item.working_values[-1] < lowest_value: #If the smallest working value is less than the current lowest value...
  58.                     lowest_value = item.working_values[-1] #Set the lowest value to that working value
  59.                     lowest_value_node = item #Therefore the lowest value node is the associated node.
  60.         self.increment_id() #Increment the ID number.
  61.         current_id += 1
  62.         lowest_value_node.update_ID(current_id) #Set the next node's ID.
  63.  
  64.         #update the order visually
  65.         id_coords_map = core.gui.ui.canvas.coords(core.core_dict[lowest_value_node.name].order_box)
  66.         id_coords = map_to_list(id_coords_map)
  67.         core.core_dict[lowest_value_node.name].order = core.gui.ui.canvas.create_text((id_coords[0]+id_coords[2])/2, (id_coords[1]+id_coords[3])/2, text=current_id)
  68.  
  69.         lowest_value_node.update_valeur(lowest_value)
  70.        
  71.         #update the value visually
  72.         value_coords_map = core.gui.ui.canvas.coords(core.core_dict[lowest_value_node.name].value_box)
  73.         value_coords = map_to_list(value_coords_map)
  74.         core.core_dict[lowest_value_node.name].value = core.gui.ui.canvas.create_text((value_coords[0]+value_coords[2])/2, (value_coords[1]+value_coords[3])/2, text=lowest_value)
  75.        
  76.         unvisited_list.pop(self.index_func(lowest_value_node.name, "index_unvisited")) #Remove the node from the list of unvisited nodes...
  77.         visited_list.append(lowest_value_node) #And add it to the list of visited nodes.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement