def BuildMst(cities, paths): # Treat a random node as the start, # Make it a forest with one node. randomstart = random.choice(cities) randomstart.isconnected = True while True: # Collect all Nodes that are part of the forest. connected_cities = pcc.create(ConnectedCity, cities) # If all nodes are part of the forest exit loop. if len(connected_cities) == len(cities): break # Find all paths that are have not been chosen. disconnected_paths = pcc.create(DisconnectedPath, paths) # Find all the paths that are not chosen # but can be connected to the existing forest. all_nearby_discon_paths = pcc.create(NearByDisconnectedPath, disconnected_paths, params=(connected_cities, )) if len(all_nearby_discon_paths) != 0: # Choose the path with the least weight. best_choice_path = sorted(all_nearby_discon_paths, key=lambda x: x.distance)[0] # Join it to the forest. best_choice_path.isconnected = True best_choice_path.city1.isconnected = True best_choice_path.city2.isconnected = True
def BuildMst(cities, paths): # Treat a random node as the start, # Make it a forest with one node. randomstart = random.choice(cities) randomstart.isconnected = True while True: # Collect all Nodes that are part of the forest. connected_cities = pcc.create(ConnectedCity, cities) # If all nodes are part of the forest exit loop. if len(connected_cities) == len(cities): break # Find all paths that are have not been chosen. disconnected_paths = pcc.create(DisconnectedPath, paths) # Find all the paths that are not chosen # but can be connected to the existing forest. all_nearby_discon_paths = pcc.create(NearByDisconnectedPath, disconnected_paths, params=(connected_cities,)) if len(all_nearby_discon_paths) != 0: # Choose the path with the least weight. best_choice_path = sorted(all_nearby_discon_paths, key=lambda x: x.distance)[0] # Join it to the forest. best_choice_path.isconnected = True best_choice_path.city1.isconnected = True best_choice_path.city2.isconnected = True
def main(): trainingSet = [] testSet = [] split = 0.67 loadDataset("iris.data", split, trainingSet, testSet) print 'Train set: ' + repr(len(trainingSet)) print 'Test set: ' + repr(len(testSet)) predictions = [] k = 3 for one_test in testSet: knns = pcc.create(knn, trainingSet, params = (one_test, k)) one_test.predicted_type = getResponse(knns) print('> predicted=' + repr(one_test.predicted_type) + ', actual=' + repr(one_test.fl_type)) accuracy = getAccuracy(testSet) print('Accuracy: ' + repr(accuracy) + '%')
def main(): trainingSet = [] testSet = [] split = 0.67 loadDataset("iris.data", split, trainingSet, testSet) print 'Train set: ' + repr(len(trainingSet)) print 'Test set: ' + repr(len(testSet)) predictions = [] k = 3 for one_test in testSet: knns = pcc.create(knn, trainingSet, params=(one_test, k)) one_test.predicted_type = getResponse(knns) print('> predicted=' + repr(one_test.predicted_type) + ', actual=' + repr(one_test.fl_type)) accuracy = getAccuracy(testSet) print('Accuracy: ' + repr(accuracy) + '%')
def PrintTSPPath(cities, paths): # Step 1: Building a Minimun spanning tree using Prim's Algorithm BuildMst(cities, paths) MST = pcc.create(ConnectedPath, paths) # Step 2: Find all cities in the MST that have odd degree (O) O = pcc.create(CityWithOddDegree, cities, params=(MST,)) # Step 3: Find the induced subgraph given by the vertices from O not_MST_paths = pcc.create(DisconnectedPath, paths) subgraph = pcc.create(PathsWithGivenCities, not_MST_paths, params=(O,)) # Step 4: Find the Minimum weight perfect matching M in the subgraph M = pcc.create(min_weight_perfect_match, subgraph) # Step 5: Combining the edges of M and T to form a connected multigraph H # in which each vertex has even degree H = pcc.create(multigraph, M, MST) # Step 6: Form an Eulerian circuit in H. eulertour = pcc.create(EulerTour, cities, params=(H,)) # Step 7: Making the circuit found in previous step into a Hamiltonian # circuit by skipping repeated vertices (shortcutting). finaltour = pcc.create(HamiltonianTour, eulertour) # Printing out the resulting tour. PrintConnections(finaltour)
def PrintTSPPath(cities, paths): # Step 1: Building a Minimun spanning tree using Prim's Algorithm BuildMst(cities, paths) MST = pcc.create(ConnectedPath, paths) # Step 2: Find all cities in the MST that have odd degree (O) O = pcc.create(CityWithOddDegree, cities, params=(MST, )) # Step 3: Find the induced subgraph given by the vertices from O not_MST_paths = pcc.create(DisconnectedPath, paths) subgraph = pcc.create(PathsWithGivenCities, not_MST_paths, params=(O, )) # Step 4: Find the Minimum weight perfect matching M in the subgraph M = pcc.create(min_weight_perfect_match, subgraph) # Step 5: Combining the edges of M and T to form a connected multigraph H # in which each vertex has even degree H = pcc.create(multigraph, M, MST) # Step 6: Form an Eulerian circuit in H. eulertour = pcc.create(EulerTour, cities, params=(H, )) # Step 7: Making the circuit found in previous step into a Hamiltonian # circuit by skipping repeated vertices (shortcutting). finaltour = pcc.create(HamiltonianTour, eulertour) # Printing out the resulting tour. PrintConnections(finaltour)