def naive_with_rc(p, t): occur_for = naive(p, t) occur_rev = naive(reverseComplement(p), t) occurrences = [] for i in occur_for: if i not in occurrences: occurrences.append(i) for i in occur_rev: if i not in occurrences: occurrences.append(i) return (occurrences)
def naive_with_rc(p,t): occur_for = naive(p,t) occur_rev = naive(reverseComplement(p),t) occurrences = [] for i in occur_for: if i not in occurrences: occurrences.append(i) for i in occur_rev: if i not in occurrences: occurrences.append(i) return(occurrences)
def pollard_rho(n): x = 2 y = 2 d = 1 c = 2 k = 0 if n == 2 or 4: return 2 while d == 1: count = 1 while count <= c and d <= 1: x = (f(x) + k) % n while x == y: k += 1 x = (f(x) + k) % n d = gcd(abs(x - y), n) count += 1 c *= 2 y = x if naive(d): return d else: return (pollard_rho(d))
'Checks all permutations of labels when computing accuracy (useful for clustering problems' ) parser.add_argument('-v', action='store_true', help='Verbose mode (Styles Matrices + probZ)') args = parser.parse_args() data = init_from_file(args.train_data, 0, not args.r, args.t or args.n) # Uncomment to confirm correctness of Q and gradQ # check_gradient(data) # Run the naive (baseline) algorithm if args.n: print naive(data) # Train our model else: start = time() EM(data) elapsed = time() - start print "Completed training in %d minutes and %d seconds\n" % ( elapsed / 60, elapsed % 60) if args.v: data.outputResults() if args.t: if args.p: acc, ce = data.permutedAcc() else: acc = data.std_percent_correct()
alphabet = "a b c" types = alphabet.split() numCharacters = len(types) accuracies = [] cross_entropies = [] for sim in range(100): # Run 100 simulations data = init_from_file("Tests/RareClass/data/%d.txt" % sim, 0, not args.r, True) if args.m: acc = MV(data) print "Simulation %d: %.2f" % (sim, acc) elif args.n: acc = naive(data) print "Simulation %d: %.2f" % (sim, acc) else: EM(data) acc = data.best_percent_correct() ce = data.best_cross_entropy() print "Simulation %d: %.2f % | %.2f CE" % (sim, acc, ce) cross_entropies.append(ce) accuracies.append(acc) average_acc = sum(accuracies) / len(accuracies) print"---" if args.m or args.n:
def routing(customers, depot, customer_count, vehicle_count, vehicle_capacity, times): # Create the routing index manager. manager = pywrapcp.RoutingIndexManager(len(customers), vehicle_count, 0) # Create Routing Model. routing = pywrapcp.RoutingModel(manager) # Create and register a transit callback. def distance_callback(from_index, to_index): """Returns the distance between the two nodes.""" # Convert from routing variable Index to distance matrix NodeIndex. from_node = manager.IndexToNode(from_index) to_node = manager.IndexToNode(to_index) return length(customers[from_node], customers[to_node]) transit_callback_index = routing.RegisterTransitCallback(distance_callback) # Define cost of each arc. routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index) # Add Capacity constraint. def demand_callback(from_index): """Returns the demand of the node.""" # Convert from routing variable Index to demands NodeIndex. from_node = manager.IndexToNode(from_index) return customers[from_node].demand demand_callback_index = routing.RegisterUnaryTransitCallback( demand_callback) routing.AddDimensionWithVehicleCapacity( demand_callback_index, 0, # null capacity slack [vehicle_capacity] * vehicle_count, # vehicle maximum capacities True, # start cumul to zero 'Capacity') # Setting first solution heuristic. search_parameters = pywrapcp.DefaultRoutingSearchParameters() search_parameters.first_solution_strategy = ( routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC) search_parameters.local_search_metaheuristic = ( routing_enums_pb2.LocalSearchMetaheuristic.GUIDED_LOCAL_SEARCH) search_parameters.time_limit.seconds = times # Solve the problem. solution = routing.SolveWithParameters(search_parameters) # Print solution on console. if solution: all_routes = [] total_distance = 0 total_load = 0 for vehicle_id in range(vehicle_count): index = routing.Start(vehicle_id) plan_output = 'Route for vehicle {}:\n'.format(vehicle_id) route = [] route_distance = 0 route_load = 0 while not routing.IsEnd(index): node_index = manager.IndexToNode(index) route.append(customers[node_index]) route_load += customers[node_index].demand plan_output += '{0} Load({1}) -> '.format( node_index, route_load) previous_index = index index = solution.Value(routing.NextVar(index)) route_distance += routing.GetArcCostForVehicle( previous_index, index, vehicle_id) plan_output += ' {0} Load({1})\n'.format( manager.IndexToNode(index), route_load) plan_output += 'Distance of the route: {}m\n'.format( route_distance) plan_output += 'Load of the route: {}\n'.format(route_load) #print(plan_output) total_distance += route_distance total_load += route_load route.append(customers[0]) all_routes.append(route[1:-1]) #print('Total distance of all routes: {}m'.format(total_distance)) #print('Total load of all routes: {}'.format(total_load)) #print(all_routes) return all_routes else: return naive(customers, depot, customer_count, vehicle_count, vehicle_capacity)