def generate(cant_buyers: int, cant_sellers: int, offset_sellers: float = 0, offset_buyers: float = 0, r: np.random.RandomState = None, eps: float = 1e-4): """ Generates random bids. All the volumes and reservation prices are sampled independently from a uniform distribution. For sellers, the reservation price is shifted `offset_seller` while for the buyers is shifter `offset_buyers`. If there are two sellers or two buyers with the same price, the reservation price of one of them is resampled until in both side of the market, all players have different values. Parameters ---------- cant_buyers: int Number of buyers to generate. Has to be positiv cant_sellers: int Number of sellers to generate. Has to be positive. offset_sellers Quantity to shift the reservation price of sellers offset_buyers Quantity to shift the reservation price of buyers r : optional RandomState used to generate the data eps : optional Minimum precision of the prices Returns ------- bids List of tuples of all the bids generated Examples --------- >>> r = np.random.RandomState(420) >>> generate(2, 3, 1, 2, r, 0.1) [(0.5, 2.8, 0, True, 0, True), (0.7000000000000001, 2.5, 1, True, 0, True), (0.6000000000000001, 1.2, 2, False, 0, True), (0.1, 1.7000000000000002, 3, False, 0, True), (0.2, 1.3, 4, False, 0, True)] """ if r is None: r = np.random.RandomState() offset = [offset_buyers, offset_sellers] quantities = [cant_buyers, cant_sellers] bids = [] user = 0 for i, (o_, q_) in enumerate(zip(offset, quantities)): range_ = np.arange(0, 1, eps) qs = r.choice(range_, q_, replace=False) vs = r.choice(range_ + o_, q_, replace=False) for j in range(q_): bid = (qs[j], vs[j], user, bool(1 - i), 0, True) bids.append(bid) user += 1 return bids
def sample(cls, rng: np.random.RandomState, p_num_drops: typing.List[int]): assert len(cls.DROP_IN_GROUPS) >= len(p_num_drops) > 0 p_num_drops = np.array(p_num_drops) / sum(p_num_drops) # Sample the number of symptom groups to drop-in num_drops = rng.choice(list(range(1, len(p_num_drops) + 1)), p=p_num_drops) # Sample that many symptom groups dropin_groups = rng.choice(cls.DROP_IN_GROUPS, size=num_drops, replace=False).tolist() return dropin_groups
def make_nshot_dataset( samples: List[Dict[str, Any]], shots: int, rng: np.random.RandomState, repeat_samples: int = 1, separator: str = " ", max_examples: Optional[int] = None, ) -> List[Dict[str, Any]]: """Formats samples into n-shot formatting. Args: samples: List of dictionaries with samples. shots: number of shots to format. rng: random number generator repeat_samples: Number of times the original samples are used as queries (with different prefixes). max_examples: maximum number of examples to return, None = no limit Returns: Formatted dictionary of samples. Raises: ValueError: If not enough examples for the number of shots. """ samples = copy.deepcopy(samples) if len(samples) < shots + 1: raise ValueError("Do not have enough examples for the number of shots") if repeat_samples != 1: samples = samples * repeat_samples fewshot_samples = [] if max_examples is not None: query_samples = rng.choice(samples, max_examples, replace=False) else: query_samples = samples for sample in query_samples: validation_example = copy.deepcopy(sample) valid_samples = [x for x in samples if x != sample] shot_examples = list(rng.choice(valid_samples, shots, replace=False)) context = separator.join( [ example["input"] + rng.choice(example["target"]) for example in shot_examples ] ) validation_example["input"] = context + separator + validation_example["input"] fewshot_samples.append(validation_example) return fewshot_samples
def reorder(self, rng: np.random.RandomState) -> None: """Reorder the genome Shuffle node ordering of internal (hidden) nodes in genome without changing the phenotype. (Goldman 2015, DOI: 10.1109/TEVC.2014.2324539) During reordering, inactive genes, e.g., address genes of nodes with arity zero, are not taken into account and can hence have invalid values after reordering. These invalid values are replaced by random values for the respective gene after reordering. Parameters ---------- rng : numpy.RandomState Random number generator instance. Returns ---------- None """ if (self._n_rows != 1) or (self._levels_back != self._n_columns): raise ValueError( "Genome reordering is only implemented for n_rows=1" " and levels_back=n_columns") dna = self._dna.copy() node_dependencies: Dict[ int, Set[int]] = self._determine_node_dependencies() addable_nodes: Set[int] = self._get_addable_nodes(node_dependencies) new_node_idx: int = self._n_inputs # First position to be placed is after inputs used_node_indices: List[int] = [] old_to_new_parameter_names_to_values: Dict[Tuple[str, str], float] = {} while len(addable_nodes) > 0: old_node_idx = rng.choice(list(addable_nodes)) dna = self._copy_dna_segment(dna, old_node_idx=old_node_idx, new_node_idx=new_node_idx) old_to_new_parameter_names_to_values.update( self._convert_parameter_names(old_node_idx, new_node_idx)) for dependencies in node_dependencies.values(): dependencies.discard(old_node_idx) used_node_indices.append(old_node_idx) addable_nodes = self._get_addable_nodes(node_dependencies, used_node_indices) new_node_idx += 1 self._update_address_genes(dna, used_node_indices) self._replace_invalid_address_alleles(dna, rng) self._update_parameters_names_to_values( old_to_new_parameter_names_to_values) self.dna = dna
def get_random_forgeries_from_dev(dev_set: Tuple[np.ndarray, np.ndarray, np.ndarray], num_forg_from_dev: int, rng: np.random.RandomState): """ Obtain a set of random forgeries form a development set (to be used as negative samples) Parameters ---------- dev_set: tuple of np.ndarray (x, y, yforg) The development dataset num_forg_from_dev: int The number of random forgeries (signatures) from each user in the development set to be considered rng: np.random.RandomState The random number generator (for reproducibility) Returns ------- np.ndarray (N x M) The N negative samples (M is the dimensionality of the feature set) """ x, y, yforg = dev_set users = np.unique(y) random_forgeries = [] for user in users: idx = np.flatnonzero((y == user) & (yforg == False)) chosen_idx = rng.choice(idx, num_forg_from_dev, replace=False) random_forgeries.append(x[chosen_idx]) return np.concatenate(random_forgeries)
def _get_disk_sample(coords: np.ndarray, center: Tuple[float, float], center_radius: float, rnd_func: np.random.RandomState, sample_count: int): """ Subfunction for RasterEquidistantMetricSpace. Calculates the indexes of a subsample in a disk "center sample". Same parameters as in the class. """ # First index: preselect samples in a disk of certain radius dist_center = np.sqrt((coords[:, 0] - center[0])**2 + (coords[:, 1] - center[1])**2) idx1 = dist_center < center_radius count = np.count_nonzero(idx1) indices1 = np.argwhere(idx1) # Second index: randomly select half of the valid pixels, # so that the other half can be used by the equidist # sample for low distances indices2 = rnd_func.choice(count, size=min(count, sample_count), replace=False) if count != 1: return indices1[indices2].squeeze() else: return indices1[indices2][0]
def get_patch_kdtree(kdtree: spatial.cKDTree, rng: np.random.RandomState, query_point, patch_radius, points_per_patch, n_jobs): if patch_radius <= 0.0: pts_dists_ms, patch_pts_ids = kdtree.query(x=query_point, k=points_per_patch, n_jobs=n_jobs) else: patch_pts_ids = kdtree.query_ball_point(x=query_point, r=patch_radius, n_jobs=n_jobs) patch_pts_ids = np.array(patch_pts_ids, dtype=np.int32) point_count = patch_pts_ids.shape[0] # if there are too many neighbors, pick a random subset if point_count > points_per_patch: patch_pts_ids = patch_pts_ids[rng.choice(np.arange(point_count), points_per_patch, replace=False)] # pad with zeros if point_count < points_per_patch: missing_points = points_per_patch - point_count padding = np.full((missing_points), -1, dtype=np.int32) if point_count == 0: patch_pts_ids = padding else: patch_pts_ids = np.concatenate((patch_pts_ids, padding), axis=0) return patch_pts_ids
def passive_sampling( classifier: Any, X: Union[np.ndarray, list], n_instances: int = 1, rng: np.random.RandomState = None) -> Tuple[list, np.ndarray]: """ Passive sampling strategy for AL that picks samples uniformly at random. :param classifier: not used Messy way of keeping with the query function abstraction :param X: np.ndarray, list Data pool to pick queries from :param n_instances: int, default 1 Number of instances to pick :param rng: np.random.RandomState, optional Random number generator to use for seeding :return: tuple (list, np.ndarray) Tuple with indices of queried samples and the samples themselves """ if rng is not None: query_ids = rng.choice(X.shape[0], size=n_instances, replace=False) else: query_ids = np.random.choice(X.shape[0], size=n_instances, replace=False) return query_ids, X[query_ids]
def compose_random_img(allowed_values: Union[np.ndarray, List[Union[int, str]]], dataset: Dict[str, torch.Tensor], writer: Union[int, None] = None, rand: np.random.RandomState = np.random.RandomState( seed=1234), **kwargs) -> Tuple[torch.Tensor, Union[int, str]]: """Compose one random image, from any of the list of allowed values. Args: allowed_values: Possible values be converted to an image. This is input as a string so that you can choose to have a leading zero or not, but it must be a string of numbers. dataset: A pre-sorted lookup of a given dataset, where dataset[key] is a tensor containing all instances of that class of image specified by the key label. writer: The MNIST writer whose handwriting we will use. An integer value from 0 to 5421. `None` will pick at random. rand: For reproducibility, pass a seeded numpy random number generator object. Returns: img: An image of the hand-written value. """ value = rand.choice(allowed_values, size=1, replace=False).item() return (value_to_img(str(value), dataset=dataset, writer=writer, rand=rand, **kwargs), value)
def _get_clip_to_track(self, random_state: np.random.RandomState): # Randomly select a starting point. index = random_state.choice( len(self._possible_starts), p=self._start_probabilities) clip_index, start_step = self._possible_starts[index] self._current_clip_index = clip_index clip_id = self._dataset.ids[self._current_clip_index] if self._all_clips[self._current_clip_index] is None: # fetch selected trajectory logging.info('Loading clip %s', clip_id) self._all_clips[self._current_clip_index] = self._loader.get_trajectory( clip_id, start_step=self._dataset.start_steps[self._current_clip_index], end_step=self._dataset.end_steps[self._current_clip_index], zero_out_velocities=False) self._current_clip = self._all_clips[self._current_clip_index] self._clip_reference_features = self._current_clip.as_dict() self._clip_reference_features = _strip_reference_prefix( self._clip_reference_features, 'walker/') # The reference features are already restricted to # clip_start_step:clip_end_step. However start_step is in # [clip_start_step:clip_end_step]. Hence we subtract clip_start_step to # obtain a valid index for the reference features. self._time_step = start_step - self._dataset.start_steps[ self._current_clip_index] self._current_start_time = (start_step - self._dataset.start_steps[ self._current_clip_index]) * self._current_clip.dt self._last_step = len( self._clip_reference_features['joints']) - self._max_ref_step - 1 logging.info('Mocap %s at step %d with remaining length %d.', clip_id, start_step, self._last_step - start_step)
def _random_center_initializer( X: np.ndarray, n_clusters: int, random_state: np.random.RandomState, **kwargs ) -> np.ndarray: """Compute initial centroids using random method. This works by assigning each point randomly to a cluster. Then the average of the cluster is taken to get the centers. Parameters ---------- X : np.ndarray (3d array of shape (n_instances,n_dimensions,series_length)) Time series instances to cluster. n_clusters: int, defaults = 8 The number of clusters to form as well as the number of centroids to generate. random_state: np.random.RandomState Determines random number generation for centroid initialization. Returns ------- np.ndarray (3d array of shape (n_clusters, n_dimensions, series_length)) Indexes of the cluster centers. """ new_centres = np.zeros((n_clusters, X.shape[1], X.shape[2])) selected = random_state.choice(n_clusters, X.shape[0], replace=True) for i in range(n_clusters): curr_indexes = np.where(selected == i)[0] result = mean_average(X[curr_indexes]) if result.shape[0] > 0: new_centres[i, :] = result return new_centres
def _randomly_unspecify_column(self, ndarray: np.ndarray, column_slice: slice, random_state: np.random.RandomState, rows_kept: np.ndarray, p: float = 0.5) -> np.ndarray: column_contents = slice(column_slice.start, column_slice.stop - 1) column_empty = column_slice.stop - 1 mask = np.zeros(ndarray.shape[0], dtype=np.bool_) mask[random_state.choice(ndarray.shape[0], int(ndarray.shape[0] * p))] = True mask[rows_kept] = False ndarray[mask, column_contents] = 0 ndarray[mask, column_empty] = 1 return ndarray
def create_training_set_for_user(user: int, exp_train: Tuple[np.ndarray, np.ndarray, np.ndarray], num_forg_from_exp: int, other_negatives: np.ndarray, rng: np.random.RandomState) -> Tuple[np.ndarray, np.ndarray]: """ Creates a training set for training a WD classifier for a user Parameters ---------- user: int The user for which a dataset will be created exp_train: tuple of np.ndarray (x, y, yforg) The training set split of the exploitation dataset num_forg_from_exp: int The number of random forgeries from each user in the exploitation set (other than "user") that will be used as negatives other_negatives: np.ndarray A collection of other negative samples (e.g. from a development set) rng: np.random.RandomState The random number generator (for reproducibility) Returns ------- np.ndarray (N), np.ndarray (N) The dataset for the user (x, y), where N is the number of signatures (genuine + random forgeries) """ exp_x, exp_y, exp_yforg = exp_train positive_samples = exp_x[(exp_y == user) & (exp_yforg == 0)] if num_forg_from_exp > 0: users = np.unique(exp_y) other_users = list(set(users).difference({user})) negative_samples_from_exp = [] for other_user in other_users: idx = np.flatnonzero((exp_y == other_user) & (exp_yforg == False)) chosen_idx = rng.choice(idx, num_forg_from_exp, replace=False) negative_samples_from_exp.append(exp_x[chosen_idx]) negative_samples_from_exp = np.concatenate(negative_samples_from_exp) else: negative_samples_from_exp = [] if len(other_negatives) > 0 and len(negative_samples_from_exp) > 0: negative_samples = np.concatenate((negative_samples_from_exp, other_negatives)) elif len(other_negatives) > 0: negative_samples = other_negatives elif len(negative_samples_from_exp) > 0: negative_samples = negative_samples_from_exp else: raise ValueError('Either random forgeries from exploitation or from development sets must be used') train_x = np.concatenate((positive_samples, negative_samples)) train_y = np.concatenate((np.full(len(positive_samples), 1), np.full(len(negative_samples), -1))) return train_x, train_y
def _random_clifford(num_angles: int, random_state: np.random.RandomState) -> np.ndarray: """Returns an array of Clifford angles chosen uniformly at random. Args: num_angles: Number of Clifford angles to return in array. random_state: Random state for sampling. """ return np.array( [random_state.choice(_CLIFFORD_ANGLES) for _ in range(num_angles)])
def _randomize_allele_group( self, alleles: np.ndarray, recoded: np.ndarray, hidden: np.ndarray, MOIs: np.ndarray, id_index: int, locus_index: int, max_MOI: int, alleles_definitions_RR: List[pd.DataFrame], rand: np.random.RandomState): ''' TODO: Cut down on the parameter list (combine last few elements in tuple?) NOTE: Depends on assuming alleles, recoded, and hidden will be modified in-place (i.e. that they're passed by reference) ''' i = id_index j = locus_index # TODO: Start/end of what? The portion of the row w/ this locus information? start = max_MOI * j end = max_MOI * (j + 1) # Find all the non-zero length alleles (meaning we've observed them) num_alleles = np.count_nonzero(alleles[i, start:end]) num_missing = MOIs[i] - num_alleles # TODO: Eliminate code duplication if possible? missing_alleles_indices = np.arange(start, end)[ np.where(alleles[i, start: start + MOIs[i]] == 0) ] present_alleles_indices = np.arange(start, end)[ np.where(alleles[i, start: start + MOIs[i]] != 0) ] # Sample to randomly initialize the missing alleles/hidden variables # (no need to sample for observed alleles w/ known lengths) if num_alleles > 0: hidden[i, present_alleles_indices] = HiddenAlleleType.OBSERVED.value if num_missing == 0: return new_hidden_alleles = rand.choice( # Select from first row (count of how many probabilities they are) np.arange(0, int(self.frequencies_RR.lengths[j])), size=num_missing, replace=True, p=self.frequencies_RR.matrix[j, 0: int(self.frequencies_RR.lengths[j])] ) # Choose random initial data for missing alleles recoded[i, missing_alleles_indices] = new_hidden_alleles # calculate row means (mean allele lengths) alleles[i, missing_alleles_indices] = np.mean(alleles_definitions_RR[j], axis=1)[new_hidden_alleles] hidden[i, missing_alleles_indices] = HiddenAlleleType.MISSING.value
def make_game_by_iteration(g: nx.Graph, candidates, distance, r: np.random.RandomState = None, max_iter=100): candidates = list(candidates) if r is None: r = np.random.RandomState() a = r.choice(candidates) b = a d0 = 0 for _ in range(max_iter): c, d = r.choice(candidates, 2, replace=False) d1 = nx.shortest_path_length(g, c, d) if 2 * distance >= d1 > d0: a, b = c, d d0 = d1 if d0 == 2 * distance: break return a, b
def _create_random_output_region( self, rng: np.random.RandomState, permissible_addresses: List[int]) -> List[int]: region = [] region.append(self._id_output_node) region.append(rng.choice(permissible_addresses)) # output nodes have only one address, other genes are hence non-coding region += [self._id_unused_gene] * (self._primitives.max_arity - 1) return region
def _create_random_hidden_region( self, rng: np.random.RandomState, permissible_addresses: List[int]) -> List[int]: region = [] node_id = self._primitives.sample_allele(rng) region.append(node_id) region += list( rng.choice(permissible_addresses, self._primitives.max_arity)) return region
def sample_subtasks(rng: np.random.RandomState, pool: List[str], minimum_size: int, maximum_size: Optional[int] = None, replace: bool = False) -> List[str]: if maximum_size is not None: assert maximum_size <= len(pool), 'Invalid maximum_size.' maximum_size = maximum_size or len(pool) random_size = rng.randint(minimum_size, maximum_size + 1) sampled_subtasks = rng.choice(pool, size=random_size, replace=replace) return list(sampled_subtasks)
def sample_query(p_weights, dim_names: Sequence[str], dim_unique_values: Sequence[Sequence], r: np.random.RandomState) -> Dict: n_dims = len(p_weights) query_filter = dict() for dim_idx in range(n_dims): if r.uniform() < p_weights[dim_idx]: # filter on dimension cur_dim_value = r.choice(dim_unique_values[dim_idx]) query_filter[dim_names[dim_idx]] = cur_dim_value return query_filter
def perform_measurement(self, qubits: Sequence['cirq.Qid'], prng: np.random.RandomState, collapse_state_vector=True) -> List[int]: """Performs a measurement over one or more qubits. Args: qubits: The sequence of qids to measure, in that order. prng: A random number generator, used to simulate measurements. collapse_state_vector: A Boolean specifying whether we should mutate the state after the measurement. Raises: ValueError: If the probabilities for the measurements differ too much from one for the tolerance specified in simulation options. """ results: List[int] = [] if collapse_state_vector: state = self else: state = self.copy() for qubit in qubits: n = state.qubit_map[qubit] # Trace out other qubits M = state.partial_trace(keep_qubits={qubit}) probs = np.diag(M).real sum_probs = sum(probs) # Because the computation is approximate, the probabilities do not # necessarily add up to 1.0, and thus we re-normalize them. if abs(sum_probs - 1.0) > self.simulation_options.sum_prob_atol: raise ValueError( f'Sum of probabilities exceeds tolerance: {sum_probs}') norm_probs = [x / sum_probs for x in probs] d = qubit.dimension result: int = int(prng.choice(d, p=norm_probs)) collapser = np.zeros((d, d)) collapser[result][result] = 1.0 / math.sqrt(probs[result]) old_n = state.i_str(n) new_n = 'new_' + old_n collapser = qtn.Tensor(collapser, inds=(new_n, old_n)) state.M[n] = (collapser @ state.M[n]).reindex({new_n: old_n}) results.append(result) return results
def sample_ast(self, rng: np.random.RandomState, n_object: int) -> AST: objects: Dict[int, AST] = {} for i, t in enumerate(rng.choice(self.leaf_candidates, n_object)): if t == "Circle": objects[i] = Circle(rng.choice(self.size_candidates)) elif t == "Rectangle": objects[i] = Rectangle(rng.choice(self.size_candidates), rng.choice(self.size_candidates)) else: raise Exception(f"Invalid type: {t}") ops = {} for i, t in enumerate(rng.choice(self.branch_candidates, n_object - 1)): ops[i] = t n_node = rng.randint(0, len(ops) + 2) n = len(ops) for i, t in enumerate(rng.choice(self.node_candidates, n_node)): ops[i + n] = t while len(objects) > 1 and len(ops) != 0: op_key = rng.choice(list(ops.keys())) op = ops.pop(op_key) obj0_key = rng.choice(list(objects.keys())) obj0 = objects.pop(obj0_key) if op == "Translation": objects[obj0_key] = Translation( rng.choice(self.length_candidates), rng.choice(self.length_candidates), obj0) elif op == "Rotation": objects[obj0_key] = Rotation( rng.choice(self.degree_candidates), obj0) else: obj1_key = rng.choice(list(objects.keys())) obj1 = objects.pop(obj1_key) if op == "Union": objects[obj0_key] = Union(obj0, obj1) elif op == "Difference": objects[obj0_key] = Difference(obj0, obj1) else: raise Exception(f"Invalid type: {t}") return list(objects.values())[0]
def mutate(self, mutation_rate: float, rng: np.random.RandomState): """Mutate the genome. Parameters ---------- mutation_rate : float Probability of a gene to be mutated, between 0 (excluded) and 1 (included). rng : numpy.random.RandomState Random number generator instance to use for mutating. Returns ---------- bool True if only inactive regions of the genome were mutated, False otherwise. """ graph = CartesianGraph(self) active_regions = graph.determine_active_regions() dna = list(self._dna) only_silent_mutations = True selected_gene_indices = self._select_gene_indices_for_mutation( mutation_rate, rng) for (gene_idx, allele) in zip(selected_gene_indices, np.array(dna)[selected_gene_indices]): region_idx = self._get_region_idx(gene_idx) permissible_values = self._permissible_values[gene_idx] permissible_alternative_values = permissible_values[ permissible_values != allele] if len(permissible_alternative_values) > 0: dna[gene_idx] = rng.choice(permissible_alternative_values) modified_parameter_value: bool if self._is_function_gene(gene_idx): region_idx = self._get_region_idx(gene_idx) region = self._get_region(region_idx, dna) modified_parameter_value = self._initialize_parameter_values( region_idx, region, reinitialize=True) else: modified_parameter_value = False silent = (region_idx not in active_regions) and ( not modified_parameter_value) only_silent_mutations = only_silent_mutations and silent self.dna = dna return only_silent_mutations
def random_subspace(all_features: list, k: int, rng: np.random.RandomState): """Utility function to generate a random feature subspace of length k Parameters ---------- all_features List of possible features to select from. k Subspace length. rng Random number generator (initialized). """ return rng.choice(all_features, k, replace=False)
def shuffle(cls, xsource: np.ndarray, others: list, rds: np.random.RandomState) -> tuple: ndarr = np.stack(others, axis=1) sample_space = cls.__get_sample_space(ndarr) str_arr = np.array([''.join(row.astype(str)) for row in ndarr]) positions = [str_arr == sample for sample in sample_space] xsource_shuffle = np.zeros(xsource.shape) for p in positions: xsource_shuffle[p] = rds.choice(xsource[p], p.sum()) xsource_shuffle = xsource_shuffle.astype(int) return xsource_shuffle, others
def _gen_soln(Z: np.ndarray, rng: np.random.RandomState): """ Generates feasible integer solutions using a probabilistic sampling heuristic """ Z_int = np.zeros(shape=Z.shape, dtype=np.int32) C, L = Z.shape # Go through each label, and sample proportional the "probability # distribution" defined by each row for i in range(C): group = rng.choice(a=np.arange(L), size=1, p=Z[i, :]) Z_int[i, group] = 1 return Z_int
def _select_parent( study: Study, parent_population: Sequence[FrozenTrial], rng: np.random.RandomState, dominates: Callable[[FrozenTrial, FrozenTrial, Sequence[StudyDirection]], bool], ) -> FrozenTrial: population_size = len(parent_population) candidate0 = parent_population[rng.choice(population_size)] candidate1 = parent_population[rng.choice(population_size)] # TODO(ohta): Consider crowding distance. if dominates(candidate0, candidate1, study.directions): return candidate0 else: return candidate1
def random_max(x:np.ndarray, rng:np.random.RandomState=None): """ Returns a randomly selected index from a 1D array where that index's value equals the array maximum Args: x (np.ndarray): 1D array rng (np.random.RandomState): RNG instance Returns: [type]: [description] """ if rng is None: rng = np.random return rng.choice(np.where(x == x.max())[0])
def _replace_invalid_address_alleles(self, dna: List[int], rng: np.random.RandomState) -> None: """Replace invalid alleles for unused address genes of all nodes by random permissible values. WARNING: Works only if self.n_rows==1. """ assert self._n_rows == 1 for gene_idx, gene_value in enumerate(dna): region_idx = self._get_region_idx(gene_idx) if self._is_hidden_address_gene( gene_idx, region_idx) and gene_value > region_idx: permissible_values = self.determine_permissible_values_per_gene( gene_idx) gene_value = rng.choice(permissible_values) dna[gene_idx] = gene_value
def sample(self, rng: np.random.RandomState, size: int) -> Dict[str, np.ndarray]: multivariate_samples = {} active = rng.choice(len(self._weights), size, p=self._weights) for param_name, dist in self._search_space.items(): if isinstance(dist, distributions.CategoricalDistribution): categorical_weights = self._categorical_weights[param_name] assert categorical_weights is not None weights = categorical_weights[active, :] samples = _MultivariateParzenEstimator._sample_from_categorical_dist( rng, weights) else: # We restore parameters of parzen estimators. low = self._low[param_name] high = self._high[param_name] mus = self._mus[param_name] sigmas = self._sigmas[param_name] assert low is not None assert high is not None assert mus is not None assert sigmas is not None # We sample from truncnorm. trunc_low = (low - mus[active]) / sigmas[active] trunc_high = (high - mus[active]) / sigmas[active] samples = np.full((), fill_value=high + 1.0, dtype=np.float64) while (samples >= high).any(): samples = np.where( samples < high, samples, truncnorm.rvs( trunc_low, trunc_high, size=size, loc=mus[active], scale=sigmas[active], random_state=rng, ), ) multivariate_samples[param_name] = samples multivariate_samples = self._transform_from_uniform( multivariate_samples) return multivariate_samples