def _initialize_x0_randomly( rng: np.random.RandomState, search_space: Dict[str, BaseDistribution]) -> Dict[str, float]: x0 = {} for name, distribution in search_space.items(): if isinstance( distribution, ( optuna.distributions.UniformDistribution, optuna.distributions.DiscreteUniformDistribution, optuna.distributions.IntUniformDistribution, ), ): x0[name] = distribution.low + rng.rand() * (distribution.high - distribution.low) elif isinstance( distribution, ( optuna.distributions.IntLogUniformDistribution, optuna.distributions.LogUniformDistribution, ), ): log_high = math.log(distribution.high) log_low = math.log(distribution.low) x0[name] = log_low + rng.rand() * (log_high - log_low) else: raise NotImplementedError( "The distribution {} is not implemented.".format(distribution)) return x0
def circ_uniform(n: int, r_outer: float, r_inner: float = 0, rng: np.random.RandomState = None): """Generate n points uniform distributed on an annular region. The outputs is given in polar coordinates. Parameters ---------- n : int, number of points. r_outer : float, outer radius of the annular region. r_inner : float, inner radius of the annular region. Returns ------- rho : np.ndarray, distance of each point from center of the annular region. phi : np.ndarray, azimuth angle of each point. """ if rng is None: rho = np.sqrt((r_outer ** 2 - r_inner ** 2) * np.random.rand(n, 1) + r_inner ** 2) phi = 2 * np.pi * np.random.rand(n, 1) else: rho = np.sqrt((r_outer ** 2 - r_inner ** 2) * rng.rand(n, 1) + r_inner ** 2) phi = 2 * np.pi * rng.rand(n, 1) return rho, phi
def sample(self, rs: np.random.RandomState, size=None) -> Union[int, np.ndarray]: if size is None: return self._sample(self.wmax * rs.rand()) else: return np.array( [self._sample(r) for r in self.wmax * rs.rand(size)])
def get_random_points_in_cube_local( n: int, rng: np.random.RandomState, ) -> List[np.ndarray]: x_positions = rng.rand(n) - 0.5 y_positions = rng.rand(n) - 0.5 z_positions = rng.rand(n) - 0.5 points = np.array([x_positions, y_positions, z_positions]) vectors = points.T return vectors
def sample(self, rs: np.random.RandomState, size=None) -> Union[int, np.ndarray]: if size is None: r = rs.rand() * self.N return self._sample(r) else: r = rs.rand(size) * self.N i = np.floor(r).astype(np.int64) p = r - i ret = np.where(p < self.ptable[i], i, self.itable[i]) return ret
def _alias_draw(probabilities: List[float], alias: List[float], random_state: np.random.RandomState): """ Draw sample from a non-uniform discrete distribution using alias sampling. """ number_of_outcomes = len(probabilities) random_index = int(np.floor(random_state.rand() * number_of_outcomes)) if random_state.rand() < alias[random_index]: return random_index else: return probabilities[random_index]
def crossover( self, parents_params: np.ndarray, rng: np.random.RandomState, study: Study, search_space_bounds: np.ndarray, ) -> np.ndarray: # https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.422.952&rep=rep1&type=pdf # Section 3.2 Crossover Schemes (vSBX) if self._eta is None: eta = 20.0 if study._is_multi_objective() else 2.0 else: eta = self._eta us = rng.rand(len(search_space_bounds)) beta_1 = np.power(1 / 2 * us, 1 / (eta + 1)) beta_2 = np.power(1 / 2 * (1 - us), 1 / (eta + 1)) mask = us > 0.5 c1 = 0.5 * ((1 + beta_1) * parents_params[0] + (1 - beta_1) * parents_params[1]) c1[mask] = (0.5 * ((1 - beta_1) * parents_params[0] + (1 + beta_1) * parents_params[1])[mask]) c2 = 0.5 * ((3 - beta_2) * parents_params[0] - (1 - beta_2) * parents_params[1]) c2[mask] = (0.5 * (-(1 - beta_2) * parents_params[0] + (3 - beta_2) * parents_params[1])[mask]) # vSBX applies crossover with establishment 0.5, and with probability 0.5, # the gene of the parent individual is the gene of the child individual. # The original SBX creates two child individuals, # but optuna's implementation creates only one child individual. # Therefore, when there is no crossover, # the gene is selected with equal probability from the parent individuals x1 and x2. child_params_list = [] for c1_i, c2_i, x1_i, x2_i in zip(c1, c2, parents_params[0], parents_params[1]): if rng.rand() < 0.5: if rng.rand() < 0.5: child_params_list.append(c1_i) else: child_params_list.append(c2_i) else: if rng.rand() < 0.5: child_params_list.append(x1_i) else: child_params_list.append(x2_i) child_params = np.array(child_params_list) return child_params
def gen_data(line: Line, rand: np.random.RandomState = None, size0: int = 30, zero_one_ratio: float = 1): if rand is None: rand = np.random.RandomState(seed=0) size1 = int(size0 * zero_one_ratio) X, Y = np.empty((size0 + size1, 2)), np.empty(size0 + size1) X[:size0, :] = reflect(rand.rand(size0, 2), line, True) Y[:size0] = 0 X[size0:, :] = reflect(rand.rand(size1, 2), line, False) Y[size0:] = 1 ind = np.arange(size0 + size1) np.random.shuffle(ind) return X[ind], Y[ind]
def crossover( self, parents_params: np.ndarray, rng: np.random.RandomState, study: Study, search_space_bounds: np.ndarray, ) -> np.ndarray: # https://www.researchgate.net/publication/2388486_Progress_Toward_Linkage_Learning_in_Real-Coded_GAs_with_Simplex_Crossover # Section 2 A Brief Review of SPX n = self.n_parents - 1 G = np.mean(parents_params, axis=0) # Equation (1). rs = np.power(rng.rand(n), 1 / (np.arange(n) + 1)) # Equation (2). epsilon = np.sqrt(len(search_space_bounds) + 2) if self._epsilon is None else self._epsilon xks = [G + epsilon * (pk - G) for pk in parents_params] # Equation (3). ck = 0 # Equation (4). for k in range(1, self.n_parents): ck = rs[k - 1] * (xks[k - 1] - xks[k] + ck) child_params = xks[-1] + ck # Equation (5). return child_params
def random_index_based_on_weights(weights: list, random_state: np.random.RandomState): """Generate a random index, based on index weights and a random number generator. Parameters ---------- weights The weights of the centroid's indexes. random_state Random number generator instance. Returns ------- int The generated index. """ prob_sum = np.sum(weights) val = random_state.rand() * prob_sum index = 0 sum_value = 0.0 while (sum_value <= val) & (index < len(weights)): sum_value += weights[index] index += 1 return index - 1
def play_move(board: numpy.ndarray, side: int, state: numpy.random.RandomState) -> int: """ Play a move :param board: board :param side: side (0, 1, 2, 3) (down, right, up, left) :param state: numpy random state :return score """ score = 0 size = board.shape[0] direction = side % 2 # 0 down, 1 right sens = (direction == 0) * (side - 1) + (direction == 1) * (side - 2) # -1 down/right, 1 up/down for i in (range(size - 1, -1, -1) if sens == -1 else range(size)): for j in range(size): a = (i, j) if direction == 0 else (j, i) b = (i + sens, j) if direction == 0 else (j, i + sens) while 0 <= b[0] < size > b[1] >= 0 <= a[0] < size > a[1] >= 0 and (board[a] == board[b] or board[a] == 0): board[a] += board[b] score += board[b] board[b] = 0 a = (a[0] - sens, a[1]) if direction == 0 else (a[0], a[1] - sens) b = (b[0] - sens, b[1]) if direction == 0 else (b[0], b[1] - sens) if score != 0: x = state.randint(0, size ** 2) while board[x // size, x % size] != 0: x = (x + 1) % size ** 2 board[x // size, x % size] = 2 + 2 * (state.rand() > 0.8) return score
def _sample_from_categorical_dist(rng: np.random.RandomState, probabilities: np.ndarray) -> np.ndarray: n_samples = probabilities.shape[0] rnd_quantile = rng.rand(n_samples) cum_probs = np.cumsum(probabilities, axis=1) return np.sum(cum_probs < rnd_quantile[..., None], axis=1)
def get_random_points_in_geometry_local( n: int, geometry: 'pg.Geometry', rng: np.random.RandomState, ) -> List[np.ndarray]: points = [] for _ in range(n): is_inside = False while not is_inside: # All Geometry instances are centered around (0, 0, 0) # with maximum width, height, depth of 1, hence offset -0.5: x = rng.rand() - 0.5 y = rng.rand() - 0.5 z = rng.rand() - 0.5 is_inside = geometry.is_inside_geometry( np.array((x, y, z))) if is_inside: points.append(np.array((x, y, z))) return points
def simulate_reward( slate: List[Document], prng: np.random.RandomState # pyre-ignore[11] ): reward = 0 position = 0 n = len(slate) if not n: return 0 # Bail if slate is empty comparison = slate[position].tap roll = prng.rand() done = comparison < roll while not done: reward += slate[position].quality comparison = 1 - slate[position].abandon roll = prng.rand() position += 1 done = (comparison < roll) or (position >= n) return reward
def _vsbx( x1: np.ndarray, x2: np.ndarray, rng: np.random.RandomState, eta: float, ) -> np.ndarray: # https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.422.952&rep=rep1&type=pdf # Section 3.2 Crossover Schemes (vSBX) assert x1.shape == x2.shape assert x1.ndim == 1 us = rng.uniform(0, 1, size=len(x1)) beta_1 = np.power(1 / 2 * us, 1 / (eta + 1)) beta_2 = np.power(1 / 2 * (1 - us), 1 / (eta + 1)) mask = us > 0.5 c1 = 0.5 * ((1 + beta_1) * x1 + (1 - beta_1) * x2) c1[mask] = 0.5 * ((1 - beta_1) * x1 + (1 + beta_1) * x2)[mask] c2 = 0.5 * ((3 - beta_2) * x1 - (1 - beta_2) * x2) c2[mask] = 0.5 * (-(1 - beta_2) * x1 + (3 - beta_2) * x2)[mask] # vSBX applies crossover with establishment 0.5, and with probability 0.5, # the gene of the parent individual is the gene of the child individual. # The original SBX creates two child individuals, # but optuna's implementation creates only one child individual. # Therefore, when there is no crossover, # the gene is selected with equal probability from the parent individuals x1 and x2. child_params_list = [] for c1_i, c2_i, x1_i, x2_i in zip(c1, c2, x1, x2): if rng.rand() < 0.5: if rng.rand() < 0.5: child_params_list.append(c1_i) else: child_params_list.append(c2_i) else: if rng.rand() < 0.5: child_params_list.append(x1_i) else: child_params_list.append(x2_i) child_params_array = np.array(child_params_list) return child_params_array
def random_potential(n_states: int, rng: np.random.RandomState = np.random) -> np.ndarray: r"""Generates a random potential function. Args: n_states: The number of states. rng: Random number generator. Returns: A one-dimensional potential $$\phi$$. """ return rng.rand(n_states)
def slice_sampler_step_in(lower_bound: float, upper_bound: float, log_pivot: float, sliced_log_density: Callable[[float], float], random_state: np.random.RandomState) -> float: """Find the right amount of movement along with a random_direction""" for _ in range(MAX_STEP_LOOP): movement = (upper_bound - lower_bound) * random_state.rand() + lower_bound if movement == 0.0: raise SliceException("The interval for slice sampling has reduced to zero in step in") if sliced_log_density(movement) > log_pivot: return movement else: lower_bound = movement if movement < 0.0 else lower_bound upper_bound = movement if movement > 0.0 else upper_bound raise SliceException("Reach maximum iteration ({}) while stepping in".format(MAX_STEP_LOOP))
def _inlined_categorical_uniform_crossover( parent_params: np.ndarray, rng: np.random.RandomState, swapping_prob: float, search_space: Dict[str, BaseDistribution], ) -> np.ndarray: # We can't use uniform crossover implementation of `BaseCrossover` for # parameters from `CategoricalDistribution`, since categorical params are # passed to crossover untransformed, which is not what `BaseCrossover` # implementations expect. n_categorical_params = len(search_space) masks = (rng.rand(n_categorical_params) >= swapping_prob).astype(int) return parent_params[masks, range(n_categorical_params)]
def _uniform(x1: np.ndarray, x2: np.ndarray, rng: np.random.RandomState, swapping_prob: float) -> np.ndarray: # https://www.researchgate.net/publication/201976488_Uniform_Crossover_in_Genetic_Algorithms # Section 1 Introduction assert x1.shape == x2.shape assert x1.ndim == 1 child_params_list = [] for x1_i, x2_i in zip(x1, x2): param = x1_i if rng.rand() < swapping_prob else x2_i child_params_list.append(param) child_params_array = np.array(child_params_list) return child_params_array
def random_reward(n_states: int, n_actions: int, rng: np.random.RandomState = np.random) -> np.ndarray: """Generates a random reward matrix. Args: n_states: The number of states. n_actions: The number of actions. rng: Random number generator. Returns: A three-dimensional array R, where R[s,a,s'] is the reward starting at state s, taking action a, and transitioning to state s'. """ return rng.rand(n_states, n_actions, n_states)
def crossover( self, parents_params: np.ndarray, rng: np.random.RandomState, study: Study, search_space_bounds: np.ndarray, ) -> np.ndarray: # https://www.researchgate.net/publication/201976488_Uniform_Crossover_in_Genetic_Algorithms # Section 1 Introduction n_params = len(search_space_bounds) masks = (rng.rand(n_params) >= self._swapping_prob).astype(int) child_params = parents_params[masks, range(n_params)] return child_params
def _generate_sample(self, rng_sample: np.random.RandomState): idx = random_index_based_on_weights(self.centroid_weights, rng_sample) current_centroid = self.centroids[idx] att_vals = dict() magnitude = 0.0 for i in range(self.n_features): att_vals[i] = (rng_sample.rand() * 2.0) - 1.0 magnitude += att_vals[i] * att_vals[i] magnitude = np.sqrt(magnitude) desired_mag = rng_sample.normal() * current_centroid.std_dev scale = desired_mag / magnitude x = { i: current_centroid.centre[i] + att_vals[i] * scale for i in range(self.n_features) } y = current_centroid.class_label return x, y
def random_state_only_reward( n_states: int, n_actions: int, rng: np.random.RandomState = np.random) -> np.ndarray: """Generates a random reward matrix, differing only in first axis. Args: n_states: The number of states. n_actions: The number of actions. rng: Random number generator. Returns: A three-dimensional array R, where R[s,a,s'] is the reward starting at state s, taking action a, and transitioning to state s'. """ rew = rng.rand(n_states, 1, 1) return np.tile(rew, (1, n_actions, n_states))
def slice_sampler_step_out(log_pivot: float, scale: float, sliced_log_density: Callable[[float], float], random_state: np.random.RandomState) -> Tuple[float, float]: r = random_state.rand() lower_bound = -r * scale upper_bound = lower_bound + scale def bound_step_out(bound, direction): """direction -1 for lower bound, +1 for upper bound""" for _ in range(MAX_STEP_OUT): if sliced_log_density(bound) <= log_pivot: return bound else: bound += direction * scale raise SliceException("Reach maximum iteration ({}) while stepping out for bound ({})".format(MAX_STEP_OUT, direction)) lower_bound = bound_step_out(lower_bound, -1.) upper_bound = bound_step_out(upper_bound, 1.) return lower_bound, upper_bound
def crossover( self, parents_params: np.ndarray, rng: np.random.RandomState, study: Study, search_space_bounds: np.ndarray, ) -> np.ndarray: # http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.465.6900&rep=rep1&type=pdf # Section 2 Crossover Operators for RCGA 2.1 Blend Crossover parents_min = parents_params.min(axis=0) parents_max = parents_params.max(axis=0) diff = self._alpha * (parents_max - parents_min) # Equation (1). low = parents_min - diff # Equation (1). high = parents_max + diff # Equation (1). r = rng.rand(len(search_space_bounds)) child_params = (high - low) * r + low return child_params
def _select_gene_indices_for_mutation( self, mutation_rate: float, rng: np.random.RandomState) -> np.ndarray: """Selects the gene indices for mutations 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 selecting the indices. Returns ---------- np.ndarray indices of the genes selected for mutation. """ selected_gene_indices = np.nonzero( rng.rand(len(self.dna)) < mutation_rate)[0] return selected_gene_indices
def random_cartpole(random: np.random.RandomState = None, env=None): env = CartPoleEnv() if env is None else env feature_size = len(CartPoleEnv.params) feature_min = np.asarray([0.75, 0.075, 0.75, 7.5]) feature_max = np.asarray([1.25, 0.125, 1.25, 12.5]) feature_min_abs = np.asarray([0.1, 0.01, 0.1, 1.]) if isinstance(random, np.random.RandomState): features = random.randn(feature_size) elif isinstance(random, np.ndarray): features = random elif isinstance(random, (int, float)): random = np.random.RandomState(random) features = random.rand(feature_size) features = np.clip(features, feature_min, feature_max) features = np.where( np.abs(features) < feature_min_abs, np.sign(features) * feature_min_abs, features) params = {k: v for k, v in zip(env.params, features)} env.set_parameters(**params) return env
def random_lunarlander(random: np.random.RandomState=None, env=None): env = LunarLanderEnv() if env is None else env feature_size = len(LunarLanderEnv.params) feature_min = np.asarray([10., 0.5]) feature_max = np.asarray([16., 0.7]) feature_min_abs = np.asarray([10., 0.5]) if isinstance(random, np.random.RandomState): features = random.randn(feature_size) elif isinstance(random, np.ndarray): features = random elif isinstance(random, (int, float)): random = np.random.RandomState(random) features = random.rand(feature_size) features = np.clip(features, feature_min, feature_max) features = np.where(np.abs(features) < feature_min_abs, np.sign(features) * feature_min_abs, features) params = {k:v for k, v in zip(env.params, features)} env.set_parameters(**params) return env
def generate_sample_histogram( smoothed_data: np.ndarray, num_samples: int, random_state: np.random.RandomState, ): """Sample from the smoothed data as if it were a probability distribution""" # calculate the cdf values (making sure to normalize) cdf = smoothed_data.cumsum() cdf /= cdf[-1] # note: all elements of cdf are in [0,1] # evaluate the inverse cdf `num_samples` times by linearly interpolating points = np.linspace(0, len(cdf), endpoint=True) values = np.interp( x=random_state.rand(num_samples), xp=cdf, fp=points[:-1], ) # be lazy and get numpy to make a histogram for us return np.histogram( a=values, bins=points, )[0]
def generate_pos_neg_label_crop_centers( spatial_size: Union[Sequence[int], int], num_samples: int, pos_ratio: float, label_spatial_shape: Sequence[int], fg_indices: np.ndarray, bg_indices: np.ndarray, rand_state: np.random.RandomState = np.random, ) -> List[List[np.ndarray]]: """ Generate valid sample locations based on the label with option for specifying foreground ratio Valid: samples sitting entirely within image, expected input shape: [C, H, W, D] or [C, H, W] Args: spatial_size: spatial size of the ROIs to be sampled. num_samples: total sample centers to be generated. pos_ratio: ratio of total locations generated that have center being foreground. label_spatial_shape: spatial shape of the original label data to unravel selected centers. fg_indices: pre-computed foreground indices in 1 dimension. bg_indices: pre-computed background indices in 1 dimension. rand_state: numpy randomState object to align with other modules. Raises: ValueError: When the proposed roi is larger than the image. ValueError: When the foreground and background indices lengths are 0. """ spatial_size = fall_back_tuple(spatial_size, default=label_spatial_shape) if not (np.subtract(label_spatial_shape, spatial_size) >= 0).all(): raise ValueError("The size of the proposed random crop ROI is larger than the image size.") # Select subregion to assure valid roi valid_start = np.floor_divide(spatial_size, 2) # add 1 for random valid_end = np.subtract(label_spatial_shape + np.array(1), spatial_size / np.array(2)).astype(np.uint16) # int generation to have full range on upper side, but subtract unfloored size/2 to prevent rounded range # from being too high for i in range(len(valid_start)): # need this because np.random.randint does not work with same start and end if valid_start[i] == valid_end[i]: valid_end[i] += 1 def _correct_centers( center_ori: List[np.ndarray], valid_start: np.ndarray, valid_end: np.ndarray ) -> List[np.ndarray]: for i, c in enumerate(center_ori): center_i = c if c < valid_start[i]: center_i = valid_start[i] if c >= valid_end[i]: center_i = valid_end[i] - 1 center_ori[i] = center_i return center_ori centers = [] fg_indices, bg_indices = np.asarray(fg_indices), np.asarray(bg_indices) if fg_indices.size == 0 and bg_indices.size == 0: raise ValueError("No sampling location available.") if fg_indices.size == 0 or bg_indices.size == 0: warnings.warn( f"N foreground {len(fg_indices)}, N background {len(bg_indices)}," "unable to generate class balanced samples." ) pos_ratio = 0 if fg_indices.size == 0 else 1 for _ in range(num_samples): indices_to_use = fg_indices if rand_state.rand() < pos_ratio else bg_indices random_int = rand_state.randint(len(indices_to_use)) center = np.unravel_index(indices_to_use[random_int], label_spatial_shape) # shift center to range of valid centers center_ori = list(center) centers.append(_correct_centers(center_ori, valid_start, valid_end)) return centers