def gen(cls, pp: RegevPublicParameters, seed: bytes = None): """ Generate a key for batched/packed Regev encryption """ seed = seed or secrets.token_bytes(32) rng = SeededRNG(seed) sec = uniform(pp.cipher_mod, rng, (pp.n, pp.bs)) A = uniform(pp.cipher_mod, rng, (pp.m, pp.n)) return RegevKey(seed, A, sec)
def init_parameters(self): """ Wieghts Initialization """ torch.nn.init.xavier_uniform_(self.weight_matrix) uniform(self.out_channels, self.bias)
def random(starts, N_new, problem, **kwargs): if len(starts) == 0: print('empty starts list!', flush=True) return list() num_random_steps = 10 if 'num_random_steps' not in kwargs else kwargs[ 'num_random_steps'] total_runs = 1000 if 'total_runs' not in kwargs else kwargs['total_runs'] while len(starts) < total_runs: start = sample(starts) problem.reset_to_state(start) for t in range(num_random_steps): action_t = uniform(low=problem.action_space.low, high=problem.action_space.high) state_t, _, _, _ = problem.step(action_t, ret_state=True) if (problem.env_name == 'PlanarQuad-v0' and problem.env.unwrapped._in_obst(state_t)): break starts.append(state_t) new_starts = sample(starts, size=N_new) return new_starts
def wait_check_fight(call, tmin, tmax, cond=0): global first_fight sleep(uniform(tmin, tmax)) if useful['infight'] and cond != -1: fight(call, first_fight) first_fight = False print("out")
def createSingleSim(_): sim, xml, drop_name = contacts.sample_settled(asset_path, num_objects, polygons, settle_bounds) # print(drop_name) #RV: Drop name is a string ending with _number logger = Logger(xml, sim, steps=num_images_per_scene + 1, img_dim=args.img_dim) ## drop all objects except [ drop_name ] logger.settle_sim(drop_name, args.settle_steps_min, args.settle_steps_max) #logger.log(0) ## filter scenes in which objects are intersecting ## because it makes the physics unpredictable overlapping = True while overlapping: ## get position for drop block if random.uniform(0, 1) < 0.5 and len(xml.meshes) > 1: ## some hard-coded messiness ## to drop a block directly on top ## of an existnig block half of the time mesh = random.choice(xml.meshes) pos = [float(p) for p in mesh['pos'].split(' ')] pos[2] += random.uniform(.4, .8) else: ## drop on random position pos = utils.uniform(*drop_bounds['pos']) ## get orientation for drop block if 'horizontal' in drop_name: axangle = [1, 0, 0, 0] else: axis = [0, 0, 1] axangle = utils.random_axangle(axis=axis) ## position and orient the block logger.position_body(drop_name, pos, axangle) ## check whether there are any block intersections overlapping = contacts.is_overlapping(sim, drop_name) # print(logger.get_state()[drop_name]) action_vec = action_to_vector(logger.get_state()[drop_name]) #RV addition # print(args.render_freq, args.drop_steps_max) #logger.log(0) for i in range(args.drop_steps_max + 1): ## log every [ render_freq ] steps if i % args.render_freq == 0: ## print(i, (i // args.render_freq) + 1) logger.log((i // args.render_freq)) ## simulate one timestep sim.step() #logger.log(1) data, images, masks = logger.get_logs() return images, action_vec
def teleport(text): print("teleport", text) try: call, prev = wrapper(teleport, text), useful['mapid'] app.send_keystrokes('h') if cond_wait(1.5, 2.5, ('mapid', prev, 5, call)): return print("enter haven bag", useful['mapid']) click(173, offx=-10, offy=-10) sleep(uniform(2.5, 3.5)) click(134, offy=-10) sleep(uniform(2, 2.5)) app.send_keystrokes(text + '{ENTER}', with_spaces=True) if cond_wait(3, 5, ('mapid', 162793472, 5, call)): return except RuntimeError: sleep(uniform(3, 5)) teleport(text) print("done teleport")
def pack(self, pp: RegevPublicParameters, seed: bytes = None): """ More densely encodes a batched Regev ciphertext 'self' """ seed = seed or secrets.token_bytes(32) rng = SeededRNG(seed) while True: r = uniform(pp.cipher_mod, rng) if PackedRegevCiphertext._near_mes((r+self.c2) % pp.cipher_mod, pp.bound, pp.cipher_mod, self.mes_mod): break w = mround((((self.c2 + r) % pp.cipher_mod) * self.mes_mod) / pp.cipher_mod) % self.mes_mod return PackedRegevCiphertext(self.c1, w, r, self.mes_mod)
def _get_a(self, s: int) -> int: if self._store_matrix: return utils.p_choice(p=self._policy_matrix[s, :]) else: # could also jit this if needed if utils.uniform() > self.epsilon: return self.greedy_policy[s] else: # flat fairly slow at 9ms flat = np.flatnonzero( self._environment.s_a_compatibility[s, :]) i = utils.n_choice(flat.shape[0]) return flat[i]
def encrypt_raw(cls, pp: RegevPublicParameters, k: RegevKey, mes: np.ndarray, mes_mod: int = 2, seed=None): rng = SeededRNG(seed or secrets.token_bytes(32)) if mes.ndim != 1: raise MessageWrongDimensions() if mes.shape[0] != pp.bs: raise MessageWrongSize( f"Expected message size {pp.bs}, got {mes.shape[0]}") mes = mes % mes_mod r = uniform(2, rng, lbound=-1, shape=(1, pp.m)) # print(r.tolist()) c1 = r @ k.A % pp.cipher_mod b = (c1 @ k.sec + gaussian(pp.bound, rng, shape=(pp.m, pp.bs))) % pp.cipher_mod c2 = (b + mround(pp.cipher_mod / mes_mod) * mes) % pp.cipher_mod return BatchedRegevCiphertext(c1, c2, mes_mod)
def _draw_action(self, state: State) -> Action: """" :param state: starting state :return: action drawn from probability distribution pi(state, action; theta) """ self._environment.build_possible_actions(state, build_array=False) possible_actions: list[ Action] = self._environment.possible_actions_list action_values: np.ndarray = self._state_action_function.get_action_values( state, possible_actions) # could also jit this if needed if utils.uniform() > self.epsilon: max_index: int = int(np.argmax(action_values)) return possible_actions[max_index] else: i = utils.n_choice(len(possible_actions)) return possible_actions[i]
def change_request(self, position: common.XY, velocity: common.XY, acceleration: common.XY)\ -> tuple[common.XY, common.XY]: u: float = utils.uniform() if u > self.skid_probability: # not skidding new_velocity = common.XY( x=velocity.x + acceleration.x, y=velocity.y + acceleration.y ) else: # skid new_velocity = velocity new_position: common.XY = common.XY( x=position.x + new_velocity.x, y=position.y + new_velocity.y ) # project back to grid if outside # new_position: common.XY = self._project_back_to_grid(expected_position) return new_position, new_velocity
def reset_parameters(self): uniform(self.in_channels, self.weight) uniform(self.in_channels, self.bias)
def reset_parameters(self): torch.nn.init.xavier_uniform_(self.weight) if self.bias.numel() > 1: uniform(self.bias.numel(), self.bias)
def reset_parameters(self): size = self.n_bases * self.in_h uniform(size, self.basis) uniform(size, self.att) uniform(size, self.root) uniform(size, self.bias)
import timeit import time import numpy as np import utils rng: np.random.Generator = np.random.default_rng() v = np.random.uniform() print(v) v = rng.uniform() print(v) v = utils.uniform() print(v) def get_time_ns(stmt: str) -> float: iterations = 1_000_000 total_times = timeit.repeat(setup=SETUP_CODE, stmt=stmt, timer=time.process_time_ns, number=iterations) single_time_: float = min(total_times) / iterations return single_time_ SETUP_CODE = ''' import timeit import time
def reset_parameters(self): reset(self.nn) uniform(self.in_channels, self.root) uniform(self.in_channels, self.bias)
def reset_parameters(self): size = self.num_bases * self.in_channels uniform(size, self.basis) uniform(size, self.att) uniform(size, self.root) uniform(size, self.bias)
def reset_parameters(self): uniform(self.in_channels, self.weight) self.lin.reset_parameters()
def reset_parameters(self): uniform(self.hidden_dim, self.weight_z_t) uniform(self.hidden_dim, self.weight_z_f) uniform(self.hidden_dim, self.weight_z_cf) uniform(self.hidden_dim, self.weight_z_ct)
def reset_parameters(self): size = self.in_channels * self.weight.size(0) uniform(size, self.weight) uniform(size, self.bias)
## filter scenes in which objects are intersecting ## because it makes the physics unpredictable overlapping = True while overlapping: ## get position for drop block if random.uniform(0, 1) < 0.5 and len(xml.meshes) > 1: ## some hard-coded messiness ## to drop a block directly on top ## of an existnig block half of the time mesh = random.choice(xml.meshes) pos = [float(p) for p in mesh['pos'].split(' ')] pos[2] += random.uniform(.4, .8) else: ## drop on random position pos = utils.uniform(*drop_bounds['pos']) ## get orientation for drop block if 'horizontal' in drop_name: axangle = [1,0,0,0] else: axis = [0,0,1] axangle = utils.random_axangle(axis=axis) ## position and orient the block logger.position_body(drop_name, pos, axangle) ## check whether there are any block intersections overlapping = contacts.is_overlapping(sim, drop_name) for i in range(args.drop_steps_max):
def reset_parameters(self): uniform(self.out_channels, self.weight) self.rnn.reset_parameters()
def reset_parameters(self): uniform(self.weight.size(0), self.weight) uniform(self.bias.size(0), self.bias)
def reset_parameters(self): uniform(self.hidden_dim, self.weight)