def fingerprint(self, input, n_total): """ Returns `n_total` results from a list of inputs Takes in: - input: - Type: list | tuple (as specified by self.formatter) - What: Input to select random choices from - n_total: - Type: int - What: Number of total answers to return EG: ``` males = [ ["male"], ["Carter"], ["John"], ["Jose"], ["Luke"], ["Adam"], ["Ahmed"] ] x=x_rand() globals().update(x.fingerprint(males, n_total=2)) print(male_00, male_01) ``` """ input=self.formatter(input) choices=random.sample(input, n_total) return self.shuffle_and_stack_dicts_numerically(choices)
def sample_if(self, input, variable, string, sample_size): """ Returns `sample_size` items from a subset of `input` items where a `string` is contained in a given `variable` Takes in: - input: - Type: list | tuple (as specified by self.formatter) - What: Input to select random choices from - variable: - Type: string - What: Variable name to check for existence of `string` - string: - Type: string - What: String to check if exists in `variable` and create a subset to sample from - sample_size: - Type:int - What: The number of results to return """ return random.sample([input[i] for i in range(len(input)) if (string in input[i][variable])], sample_size)
def solve(self): start_time = time.time() elapsed_time = 0 iteration = 0 best_distance = 0 best_route = [] best_distances = [] while iteration < self.iterations: num_cities = len(self.distance_matrix) print(round(elapsed_time), 'sec') initial_route = [0] + random2.sample(range(1, num_cities), num_cities - 1) tsp = Solver(self.distance_matrix, initial_route) new_route, new_distance, distances = tsp.two_opt() if iteration == 0: best_distance = new_distance best_route = new_route else: pass if new_distance < best_distance: best_distance = new_distance best_route = new_route best_distances = distances elapsed_time = time.time() - start_time iteration += 1 if self.writer_flag: self.writer(best_route, best_distance, self.cities_names) if self.cities_names: best_route = [self.cities_names[i] for i in best_route] return best_distance, best_route else: return best_distance, best_route
positions = ['GK', 'M', 'A', 'D', ...] heights = [191, 184, 185, 180, ...] Each element in the lists corresponds to a player. The first list, positions, contains strings representing each player's position. The possible positions are: 'GK' (goalkeeper), 'M' (midfield), 'A' (attack) and 'D' (defense). The second list, heights, contains integers representing the height of the player in cm. The first player in the lists is a goalkeeper and is pretty tall (191 cm). You're fairly confident that the median height of goalkeepers is higher than that of other players on the soccer field. Some of your friends don't believe you, so you are determined to show them using the data you received from FIFA and your newly acquired Python skills. """ import numpy as np import random2 positions = random2.sample((['GK', 'M', 'A', 'D'] * 1250), 5000) # Generates a random list height = np.round(np.random.normal(1.65, 0.20, 5000), 2) # Convert heights and positions, which are regular lists, to numpy arrays. Call them np_heights and np_positions. np_positions = np.array(positions) np_heights = np.array(height) # Extract all the heights of the goalkeepers. You can use a little trick here: use np_positions == 'GK' as an index # for np_heights. Assign the result to gk_heights gk_heights = np_heights[np_positions == 'GK'] print(gk_heights) # Extract all the heights of all the other players. This time use np_positions != 'GK' as an index for np_heights. # Assign the result to other_heights. other_heights = np_heights[np_positions != 'GK'] print(other_heights)
def mutate(seq): idx = range(len(seq)) i1, i2 = random2.sample(idx, 2) seq[i1], seq[i2] = seq[i2], seq[i1]
import random2 pool = [] population = [[0] + random2.sample(range(1, 10), 9), [0] + random2.sample(range(1, 10), 9), [0] + random2.sample(range(1, 10), 9)] fitness = [] def calculateDistance(route): total_cost = 0 for i in range(1, len(route)): total_cost += abs(route[i] - route[i - 1]) return total_cost def mutate(seq): idx = range(len(seq)) i1, i2 = random2.sample(idx, 2) seq[i1], seq[i2] = seq[i2], seq[i1] recordDistance = float('inf') best_ever = [] d = 0 while d < recordDistance: for i in range(10): for generation in population: d = calculateDistance(generation) if d < recordDistance: