def __init__( self, a_dim, s_dim, a_bound, ): self.a_dim, self.s_dim, self.a_bound = a_dim, s_dim, a_bound, #self.sess = tf.Session() self.P_online = Actor(s_dim, a_dim) self.P_target = Actor(s_dim, a_dim) self.P_target.load_state_dict(self.P_online.state_dict()) self.Q_online = Critic(s_dim, a_dim) self.Q_target = Critic(s_dim, a_dim) self.Q_target.load_state_dict(self.Q_online.state_dict()) self.q_optimizer = torch.optim.Adam(self.Q_online.parameters(), lr=LR_C) self.p_optimizer = torch.optim.Adam(self.P_online.parameters(), lr=LR_A) self.loss_td = nn.MSELoss() self.replay_buffer = ReplayBuffer() self.batch_size = 32 self.discrete = False self.ep_step = 0 # noise self.noise = Noise(DELTA, SIGMA, OU_A, OU_MU) # Initialize noise self.ou_level = 0. self.action_low = -2 self.action_high = 2
def __init__(self, task): self.task=task self.state_size=task.state_size self.action_size=task.action_size self.action_low=task.action_low self.action_high=task.action_high self.actor_local=Actor(self.state_size, self.action_size, self.action_low, self.action_high) self.actor_target=Actor(self.state_size, self.action_size, self.action_low, self.action_high) self.critic_local=Critic(self.state_size, self.action_size) self.critic_target=Critic(self.state_size, self.action_size) self.critic_target.model.set_weights(self.critic_local.model.get_weights()) self.actor_target.model.set_weights(self.actor_local.model.get_weights()) self.mu=0 self.theta=0.2 self.sigma=0.005 # random noise self.noise=Noise(self.action_size, self.mu, self.theta, self.sigma) self.gamma=0.9 self.tau=0.1 self.best_score=-np.inf self.score=0 self.buffer_size=100000 self.batch_size=64 self.memory=ReplayBuffer(self.buffer_size, self.batch_size)
def _draw_circle(self, radius, thickness, center_x=0, center_y=0, a=1, b=1, turb_size=0, turb_power=0): """ :param radius: :param thickness: :param center_x: :param center_y: :param a: float, optional, ellipse: radius on x axis :param b: float, optional, ellipse: radius on y axis :return: """ noise = Noise(self.width, self.height) a = 1 / (a * a) b = 1 / (b * b) for x in xrange(self.width): for y in xrange(self.height): xv = (x - self.width / 2.0) / self.width yv = (y - self.height / 2.0) / self.height dist = math.sqrt((xv - center_x) * (xv - center_x) * a + (yv - center_y) * (yv - center_y) * b) dist += turb_power * noise.turbulence(x, y, turb_size) if ((radius - thickness) <= dist) and \ (dist <= (radius + thickness)): theta = np.interp(dist, [radius - thickness, radius + thickness], [0, math.pi]) multiplier = random.uniform(0.2, 0.25) value = math.sin(theta) * multiplier self.data[x, y] -= value
def __init__(self, width, height): self.noise = Noise(256) self.cache = dict() self.width = width self.height = height self.screen = pygame.display.set_mode((width, height)) pygame.display.set_caption('Perlin Noise Demo') pygame.font.init() self.font = pygame.font.SysFont('Arial', 30) self.background = (2, 2, 2)
def __init__(self, width, height, prob_deviation=0): """ :param width: int :param height: int :param prob_deviation: float, optional, probability of some deviation """ self.width = width self.height = height self._noise = Noise(width, height, low=0.2, high=0.7) self.data = self._get_background() self._prob_deviation = prob_deviation self._prob_deviation_internal = 0.3
def __init__(self, sess, params): self.sess = sess self.__dict__.update(params) # create placeholders self.create_input_placeholders() # create actor/critic models self.actor = Actor(self.sess, self.inputs, **self.actor_params) self.critic = Critic(self.sess, self.inputs, **self.critic_params) self.noise_params = {k: np.array(list(map(float, v.split(",")))) for k, v in self.noise_params.items()} self.noise = Noise(**self.noise_params) self.ou_level = np.zeros(self.dimensions["u"]) self.memory = Memory(self.n_mem_objects, self.memory_size)
def main(_): with tf.Session() as sess: env = gym.make(ENV_NAME) np.random.seed(RANDOM_SEED) tf.set_random_seed(RANDOM_SEED) env.seed(RANDOM_SEED) print(env.observation_space) print(env.action_space) state_dim = env.observation_space.shape[0] try: action_dim = env.action_space.shape[0] action_bound = env.action_space.high # Ensure action bound is symmetric assert (env.action_space.high == -env.action_space.low) discrete = False print('Continuous Action Space') except AttributeError: action_dim = env.action_space.n action_bound = 1 discrete = True print('Discrete Action Space') actor = ActorNetwork(sess, state_dim, action_dim, action_bound, ACTOR_LEARNING_RATE, TAU) critic = CriticNetwork(sess, state_dim, action_dim, CRITIC_LEARNING_RATE, TAU, actor.get_num_trainable_vars()) noise = Noise(DELTA, SIGMA, OU_A, OU_MU) reward = Reward(REWARD_FACTOR, GAMMA)
def __init__(self, sess, scale_u, params): self.sess = sess self.scale_u = scale_u self.__dict__.update(params) # CREATE INPUT PLACEHOLDERS self.create_input_placeholders() # INITIALIZE ACTOR & CRITIC MODELS self.agents = [ Actor(self.sess, self.inputs, i, **self.actor_params) for i in [1, 2, 3] ] self.critic = Critic(self.sess, self.inputs, **self.critic_params) # INITIALIZE EXPLORATION MODEL self.noise_params = { k: np.fromstring(v, sep=",", dtype="f") for k, v in self.noise_params.items() } self.noise = [Noise(**self.noise_params) for _ in range(3)] # INITIALIZE REPLAY BUFFER self.memory = Memory(self.memory_size) # AVERAGE AGENT POLICIES avg_pi = [ tf.reduce_mean(i, axis=0) for i in zip(*[x.pi.net_params for x in self.agents]) ] self.avg_op = [ tf.assign(i, j) for x in self.agents for i, j in zip(x.pi.net_params, avg_pi) ]
def main(_): with tf.compat.v1.Session() as sess: env = StageWorld(LASER_BEAM, map_type) np.random.seed(RANDOM_SEED) tf.compat.v1.set_random_seed(RANDOM_SEED) state_dim = LASER_BEAM * LASER_HIST + SPEED + TARGET action_dim = ACTION #action_bound = [0.25, np.pi/6] #bounded acceleration action_bound = [0.5, np.pi / 3] #bounded velocity switch_dim = SWITCH discrete = False print('Continuous Action Space') actor = ActorNetwork(sess, state_dim, action_dim, action_bound, ACTOR_LEARNING_RATE, TAU) critic = CriticNetwork(sess, state_dim, action_dim, switch_dim, CRITIC_LEARNING_RATE, TAU, actor.get_num_trainable_vars()) noise = Noise(DELTA, SIGMA, OU_A, OU_MU) reward = Reward(REWARD_FACTOR, GAMMA) try: train(sess, env, actor, critic, noise, reward, discrete, action_bound) except KeyboardInterrupt: pass
def __init__(self, task): # Task (environment) information self.task = task self.state_size = task.state_size self.action_size = task.action_size self.action_low = task.action_low self.action_high = task.action_high self.action_range = self.action_high - self.action_low self.w = np.random.normal( size=( self.state_size, self.action_size ), # weights for simple linear policy: state_space x action_space scale=(self.action_range / (2 * self.state_size) )) # start producing actions in a decent range self.actor = Actor(self.state_size, self.action_size, self.action_low, self.action_high) self.critic = Critic(self.state_size, self.action_size) self.actor_target = Actor(self.state_size, self.action_size, self.action_low, self.action_high) self.critic_target = Critic(self.state_size, self.action_size) self.gamma = 0.95 self.tau = 0.001 self.best_w = None self.best_score = -np.inf self.exploration_mu = 0.5 self.exploration_theta = 0.2 self.exploration_sigma = 0.4 self.noise = Noise(self.action_size, self.exploration_mu, self.exploration_theta, self.exploration_sigma) self.buffer_size = 100000 self.batch_size = 32 self.memory = ReplayBuffer(self.buffer_size, self.batch_size) self.best_score = -np.inf self.num_steps = 0 # Episode variables self.reset_episode()
def __init__(self, labels, pubkey, curveID=409, fingerprint="fingerprint"): # global _C # self._C = _C self.num = len(labels) self.curveID = curveID self.pubkey = pubkey # print self.pubkey.export().encode("hex") self.lab = {} # Store the group we work in # precompute tables and the generator self.ecgroup = EcGroup(curveID) self.gen = self.ecgroup.generator() self.order = self.ecgroup.order() # self.ecgroup = _C.EC_GROUP_new_by_curve_name(curveID) # if not _C.EC_GROUP_have_precompute_mult(self.ecgroup): # _C.EC_GROUP_precompute_mult(self.ecgroup, _FFI.NULL); # self.gen = _C.EC_GROUP_get0_generator(self.ecgroup) # This is where we store the ECEG ciphertexts self.buf = [] # This DC's weight for noise calculation twbw, p_exit, num_of_dc, sum_of_sq = prob_exit(consensus, fingerprint) for label in labels: #Make session key s_priv = self.order.random() s_pub = s_priv * self.gen alpha = s_pub beta = self.pubkey beta.pt_mul_inplace(s_priv) # Adding noise and setting the resolution res_noise = int(Noise(sigma, sum_of_sq, p_exit) * resolution) n = Bn(res_noise) kappa = self.gen kappa = kappa.pt_mul(n) beta.pt_add_inplace(kappa) del (kappa) del (n) # Save the ECEG ciphertext c = (alpha, beta) self.lab[label] = c self.buf += [c] # Save the resolution resolute = Bn(resolution) self.resolution = self.gen self.resolution = self.resolution.pt_mul(resolute) del (resolute)
def __init__(self, test=False): # device if torch.cuda.is_available(): self.device = torch.device('cuda') else: self.device = torch.device('cpu') ######################################### """ Some hand tune config(for developing) """ self.discrete = False self.action_dim = 1 self.state_dim = 3 self.batch_size = 100 self.action_low = -2 self.action_high = 2 ########################################## self.P_online = Actor(state_dim=self.state_dim, action_size=self.action_dim).to(self.device) self.P_target = Actor(state_dim=self.state_dim, action_size=self.action_dim).to(self.device) self.P_target.load_state_dict(self.P_online.state_dict()) self.Q_online = Critic(state_size=self.state_dim, action_size=self.action_dim).to(self.device) self.Q_target = Critic(state_size=self.state_dim, action_size=self.action_dim).to(self.device) self.Q_target.load_state_dict(self.Q_online.state_dict()) # discounted reward self.gamma = 0.99 self.eps = 0.25 # optimizer self.q_optimizer = torch.optim.Adam(self.Q_online.parameters(), lr=1e-3) self.p_optimizer = torch.optim.Adam(self.P_online.parameters(), lr=1e-3) # saved rewards and actions self.replay_buffer = ReplayBuffer() # noise self.noise = Noise(DELTA, SIGMA, OU_A, OU_MU) # Initialize noise self.ou_level = 0. self.ep_step = 0
def main(_): with tf.Session() as sess: env = gym.make(ENV_NAME) np.random.seed(RANDOM_SEED) tf.set_random_seed(RANDOM_SEED) env.seed(RANDOM_SEED) print(env.observation_space) print(env.action_space) state_dim = env.observation_space.shape[0] try: action_dim = env.action_space.shape[0] action_bound = env.action_space.high # Ensure action bound is symmetric assert (env.action_space.high == -env.action_space.low) discrete = False print('Continuous Action Space') except: #原来的对象抛出处理不了这里的异常,此处更换为全部处理 action_dim = env.action_space.n action_bound = 1 discrete = True print('Discrete Action Space') actor = ActorNetwork(sess, state_dim, action_dim, action_bound, ACTOR_LEARNING_RATE, TAU) critic = CriticNetwork(sess, state_dim, action_dim, CRITIC_LEARNING_RATE, TAU, actor.get_num_trainable_vars()) noise = Noise(DELTA, SIGMA, OU_A, OU_MU) reward = Reward(REWARD_FACTOR, GAMMA) if GYM_MONITOR_EN: if not RENDER_ENV: gym.wrappers.Monitor(env, MONITOR_DIR, video_callable=False, force=True) #此处更换为新版本 # env.monitor.start(MONITOR_DIR, video_callable=False, force=True) else: gym.wrappers.Monitor(env, force=True) #此处更换为新版本 # env.monitor.start(MONITOR_DIR, force=True) try: train(sess, env, actor, critic, noise, reward, discrete) except KeyboardInterrupt: pass if GYM_MONITOR_EN: env.monitor.close()
def __init__(self, state_size, action_size, seed, add_noise=True): """ Initialize an Agent instance. Params ====== state_size (int): Dimension of each state action_size (int): Dimension of each action seed (int): Random seed add_noise (bool): Toggle for using the stochastic process """ # Set the parameters. self.state_size = state_size self.action_size = action_size self.seed = random.seed(seed) # Setting the Actor network (with the Target Network). self.actor_local = Actor(state_size, action_size, seed).to(device) self.actor_target = Actor(state_size, action_size, seed).to(device) # Optimize the Actor using Adam. self.actor_optimizer = optim.Adam(self.actor_local.parameters(), lr=LR_ACTOR) # Setting the Critic network (with the Target Network). self.critic_local = Critic(state_size, action_size, seed).to(device) self.critic_target = Critic(state_size, action_size, seed).to(device) # Optimize the Critic using Adam. self.critic_optimizer = optim.Adam(self.critic_local.parameters(), lr=LR_CRITIC, weight_decay=WEIGHT_DECAY) # Set up noise processing. if add_noise: self.noise = Noise((20, action_size), seed) # Use the Replay memory buffer (once per class). self.memory = ReplayBuffer(action_size, BUFFER_SIZE, BATCH_SIZE, seed, device)
def train(model, criterion, optimizer, n_epochs, train_loader, test_loader=None, scheduler=None, noise_rate=0.0): train_noise_generator = Noise(train_loader, noise_rate=noise_rate) test_noise_generator = Noise(test_loader, noise_rate=noise_rate) if test_loader is not None else None train_loss_per_epoch = [] test_loss_per_epoch = [] correct_per_epoch = [] incorrect_per_epoch = [] memorized_per_epoch = [] for _ in tqdm(range(n_epochs)): # activate train mode model.train() train_loss = 0 for batch_idx, (inputs, targets) in enumerate(train_loader): targets_with_noise = train_noise_generator.symmetric_noise(targets, batch_idx) # to(device) copies data from CPU to GPU inputs, targets = inputs.to(device), targets_with_noise.to(device) optimizer.zero_grad() outputs = model(inputs) loss = criterion(outputs, targets) loss.backward() optimizer.step() train_loss += loss.item() * targets.size(0) train_loss_per_epoch.append(train_loss / len(train_loader.dataset)) if test_loader is not None: model.eval() test_loss = 0 with torch.no_grad(): correct, incorrect, memorized, total = 0, 0, 0, 0 with torch.no_grad(): for batch_idx, (inputs, targets) in enumerate(test_loader): original_targets = targets.to(device) targets_with_noise = test_noise_generator.symmetric_noise(targets, batch_idx) inputs, targets = inputs.to(device), targets_with_noise.to(device) outputs = model(inputs) loss = criterion(outputs, targets) _, predicted = outputs.max(1) total += targets.size(0) correct_idx = predicted.eq(original_targets) memorized_idx = ((predicted != original_targets) & (predicted == targets)) incorrect_idx = ((predicted != original_targets) & (predicted != targets)) correct += correct_idx.sum().item() memorized += memorized_idx.sum().item() incorrect += incorrect_idx.sum().item() test_loss += loss.item() * targets.size(0) test_loss_per_epoch.append(test_loss / total) correct_per_epoch.append(correct / total) memorized_per_epoch.append(memorized / total) incorrect_per_epoch.append(incorrect / total) # anneal learning rate scheduler.step() return (train_loss_per_epoch, test_loss_per_epoch, correct_per_epoch, memorized_per_epoch, incorrect_per_epoch,)
def gift_wrap(self, points): np = len(points) if np <= 2: # too few points to wrap if np == 2: self.lines += [tuple(points)] return points hull_pt = min(points, key=lambda x: x[0]) # leftmost point toR = [] # the points we're wrapping on endpoint = None # the final point while endpoint is None or endpoint != toR[0]: toR.append(hull_pt) endpoint = points[0] for j in points[1:]: if endpoint == hull_pt or self.on_left((hull_pt, endpoint), j): endpoint = j hull_pt = endpoint n = Noise("butts") self.squiggles += [n.noisy_line(c1, c2) for c1, c2 in self.polypoints(toR)] self.polygons.append(toR) return toR
def select_model(): net_options = list('to ' + net_name + ' press ' + str(i) for i, net_name in enumerate(MY_NETS)) message = 'Please select the requested net:\n' net_name = get_input(message, net_options, MY_NETS) if net_name == DEBLUR: deblur = Deblur() blur = Blur() return deblur, blur, net_name else: denoise = Denoise() noise = Noise() return denoise, noise, net_name
def main(_): with tf.Session() as sess: env = gym.make(ENV_NAME) np.random.seed(RANDOM_SEED) tf.set_random_seed(RANDOM_SEED) env.seed(RANDOM_SEED) print env.observation_space print env.action_space state_dim = env.observation_space.shape[0] try: action_dim = env.action_space.shape[0] action_bound = env.action_space.high # Ensure action bound is symmetric assert (env.action_space.high == -env.action_space.low) discrete = False print "Continuous Action Space" except AttributeError: action_dim = env.action_space.n action_bound = 1 discrete = True print "Discrete Action Space" actor = ActorNetwork(sess, state_dim, action_dim, action_bound, ACTOR_LEARNING_RATE, TAU) critic = CriticNetwork(sess, state_dim, action_dim, CRITIC_LEARNING_RATE, TAU, actor.get_num_trainable_vars()) noise = Noise(DELTA, SIGMA, OU_A, OU_MU) reward = Reward(REWARD_FACTOR, GAMMA) if GYM_MONITOR_EN: if not RENDER_ENV: env = wrappers.Monitor(env, MONITOR_DIR, force=True) else: env = wrappers.Monitor(env, MONITOR_DIR, force=True) try: train(sess, env, actor, critic, noise, reward, discrete) except KeyboardInterrupt: pass #if GYM_MONITOR_EN: #env.monitor.close() env.close() gym.upload(MONITOR_DIR, api_key="sk_JObiOSHpRjw48FpWvI1GA")
def __init__(self, width, height, seed, changes=None, min_height=128, max_height=-128): self.width = width self.height = height self.ground_height = [0 for g in range(self.width) ] # небольшая оптимизация self.tree_map = [[0 for g in range(self.width)] for i in range(self.height)] self.sub_tree_map = [0 for g in range(self.width)] self.max_ground = 25 self.chunk = [[0 for g in range(self.width)] for i in range(self.height)] self.seed = seed self.cave_generator = TwoDisNoise(self.seed) self.generator = Noise(self.seed) self.min_height = min_height self.max_height = max_height self.changes = changes if changes is not None else {}
def __init__(self, q, labels, authorities, fingerprint): self.data = {} self.q = q for l in labels: self.data[l] = 0 self.keys = [os.urandom(20) for _ in authorities] self.keys = dict([(PRF(K, "KEYID"), K) for K in self.keys]) twbw, p_exit, number_exits, sum_of_sq = prob_exit(consensus, fingerprint) for _, K in self.keys.iteritems(): shares = keys_from_labels(labels, K, True, q) for (l, s0) in shares: noise = Noise(sigma,fingerprint,sum_of_sq,p_exit) self.data[l] = (self.data[l] + int((s0+noise)/resolution)) % q
def transmit(self, data): noise_values = Noise(self.noise_level).get_noises(len(data)) noisy_data = data[:] for i in range(len(noisy_data)): n = noisy_data[i] + noise_values[i] # Keep voltage levels within [0.0, 1.0] if n >= 1.0: n = .999999999 elif n < 0.0: n = 0.0 noisy_data[i] = n assert (noisy_data[i] >= 0.0 and noisy_data[i] <= 1.0) # double-check return noisy_data
def __init__(self, detail_return): # Filters parameters self.whitening = 0.2 self.size_filter_gaussian = (9, 9) self.type_gaussian = 0 self.size_median = 3 self.thresh_threshold = 25 self.maxvalue_threshold = 255 self.kernel_size_morphology = ((5, 5), (7, 7), (10, 10), (12, 12), (15, 15), (17, 17)) self.color_circle = (255, 255, 0) self.thickness_circle = 3 self.position_text = (120, 30) self.font_text = cv2.FONT_HERSHEY_DUPLEX self.font_scale = 0.2 self.min_area = 50000 self.ellipse = Ellipse() self.noise = Noise(self.min_area)
def main(_): t1 = time.time() # Training the model with tf.Session() as sess: env = PowerSystem() # System Info state_dim = 20 # We only consider the Current of all line as state at this moment action_dim = 4 # The number of generators action_bound = np.array([[-1, 1], [-0.675, 0.675]]) actor = ActorNetwork(sess, state_dim, action_dim, action_bound, ACTOR_LEARNING_RATE, TAU) critic = CriticNetwork(sess, state_dim, action_dim, CRITIC_LEARNING_RATE, TAU, actor.get_num_trainable_vars()) saver = tf.train.Saver() noise = Noise(DELTA, SIGMA, OU_A, OU_MU) # Training the model train(sess, env, actor, critic, noise, action_bound) # # save the variables save_path = saver.save(sess, model_path) # print("[+] Model saved in file: %s" % save_path) # # Testing the model # with tf.Session() as sess: # # env = PowerSystem() # # System Info # state_dim = 11 # We only consider the Current of all line as state at this moment # action_dim = 2 # The number of generators # action_bound = np.array([[-1, 1], [-0.675, 0.675]]) # # actor = ActorNetwork(sess, state_dim, action_dim, action_bound, ACTOR_LEARNING_RATE, TAU) # saver = tf.train.Saver() # load_path = saver.restore(sess, model_path) # test(env, actor) print('Running time: {} minutes.'.format((time.time() - t1) / 60))
def __init__(self, q, labels, authorities, fingerprint, consensus): self.data = {} self.q = q for l in labels: self.data[l] = 0 twbw, p_exit, num_exits, sum_of_sq = prob_exit(consensus, fingerprint) self.keys = [os.urandom(20) for _ in authorities] self.keys = dict([(PRF(K, "KEYID"), K) for K in self.keys]) for _, K in self.keys.iteritems(): shares = keys_from_labels(labels, K, True, q) for (l, s0) in shares: # noise = Noise(sigma, fingerprint, sum_of_sq, p_exit) # self.data[l] = (self.data[l] + int((s0+noise)/resolution)) % self.q self.data[l] = (self.data[l] + int(s0 / resolution)) % self.q # Add noise for each website independently for label in self.data: noise = Noise(sigma, fingerprint, sum_of_sq, p_exit) self.data[label] = (self.data[label] + int(noise / resolution)) % self.q
class HeightAndColourMap: def __init__(self): self.noise = Noise() self.cache = PagedDataCache(self._get_height_and_colour_non_cached, IsoCraftConstants.VOLUME_DEPTH, IsoCraftConstants.VOLUME_DEPTH) def get_height_and_colour(self, x, y): # Cached version of below return self.cache.get(x, y) def _get_height_and_colour_non_cached(self, x, y): # Uses noise to determine the height of a given point. # Return a tuple of this height and the colour to render noise = HeightAndColourMap._normalise_noise( self.noise.get_with_octaves(x * IsoCraftConstants.NOISE_STEP, y * IsoCraftConstants.NOISE_STEP, IsoCraftConstants.OCTAVES)) colour = HeightAndColourMap._colour_from_normalised_noise(noise) # Scale height = int(noise * IsoCraftConstants.VOLUME_HEIGHT) return height, colour @staticmethod def _normalise_noise(n): # Noise will typically be in the range of -Sqrt(N/4) to + Sqrt(N/4) where N is the number of dimension # In our case we are using 2D noise - approximately -0.7 to 0.7 # We want to discount any noise <= Water_Level, then normalise the remaining portion. water_level = max(-0.7, IsoCraftConstants.WATER_LEVEL) return max(n - water_level, 0.0) / (0.7 - water_level) @staticmethod def _colour_from_normalised_noise(noise): return IsoCraftConstants.COLOUR_WATER if noise <= IsoCraftConstants.THRESHOLD_WATER \ else IsoCraftConstants.COLOUR_BEACH if noise <= IsoCraftConstants.THRESHOLD_BEACH \ else IsoCraftConstants.COLOUR_GRASS if noise <= IsoCraftConstants.THRESHOLD_GRASS \ else IsoCraftConstants.COLOUR_ROCK if noise <= IsoCraftConstants.THRESHOLD_ROCK \ else IsoCraftConstants.COLOUR_SNOW
def main(_): # gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.3) # with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess: with tf.Session() as sess: env = StageWorld(LASER_BEAM) np.random.seed(RANDOM_SEED) tf.set_random_seed(RANDOM_SEED) state_dim = LASER_BEAM * LASER_HIST + SPEED + TARGET action_dim = ACTION action_bound = [0.5, np.pi / 3] switch_dim = SWITCH discrete = False print('Continuous Action Space') with tf.name_scope("Actor"): actor = ActorNetwork(sess, state_dim, action_dim, action_bound, ACTOR_LEARNING_RATE, TAU) with tf.name_scope("Critic"): critic = CriticNetwork(sess, state_dim, action_dim, switch_dim, CRITIC_LEARNING_RATE, TAU, actor.get_num_trainable_vars(), baseline_rate=10., control_variance_flag=CONTROL_VARIANCE) noise = Noise(DELTA, SIGMA, OU_A, OU_MU) reward = Reward(REWARD_FACTOR, GAMMA) try: train(sess, env, actor, critic, noise, reward, discrete, action_bound) except KeyboardInterrupt: pass
import time import logging from cnu import app from noise import Noise from db import Database logger = app.logger max_time_update = 60 * 30 * 1000 max_time_feedback = 60 * 60 * 1000 client = Database.get_client() db = client.cnu updates = db.updates feedback = db.feedback noise = Noise(db.updates, logger) cursor = updates.find() for record in cursor: updated = record.get('time') uuid = record.get('id') current = int(round(time.time() * 1000)) if (current - updated) > max_time_update or uuid.startswith('SERVER'): updates.remove({"id": uuid}) for x in set(['Regattas', 'Commons', 'Einsteins']): noise.createNoise(x) cursor = feedback.find() for record in cursor: updated = record.get('time')
format='%(asctime)s: %(message)s', level='INFO', datefmt='%m/%d/%Y %I:%M:%S %p') parser = argparse.ArgumentParser() parser.add_argument( '--data', type=str, choices=['mnist', 'cifar10'], default='mnist') parser.add_argument('--test_eval', type=bool, default=False) args = parser.parse_args() noises = [ 'vert_shrink25', 'horiz_shrink25', 'both_shrink25', 'light_tint', 'gradient', 'checkerboard', 'pos_noise', 'mid_noise', 'neg_noise' ] # Load data X_train, Y_train, X_test, Y_test = load_data(args.data) # Flatten image matrices num_train, num_rows, num_cols, num_channels = X_train.shape num_test, _, _, _ = X_test.shape _, num_classes = Y_train.shape direct_noise = Noise() path = f'../data/{args.data}/noise' os.makedirs(path, exist_ok=True) for noise in noises: X_train_noise = direct_noise.apply_noise(X_train, noise) np.save(f'{path}/{noise}.npy', X_train_noise) logger.info(f"Saved noise {noise}")
def create_field(out_id, seed, scale_noise, base_dir): #int randseed=1; #int seedoffset=321; #if (argc>1) #randseed=atoi(argv[1]); #srand(randseed+seedoffset); seed = randomseed N = 512 #powf(2,ceil(log2(ny))) noise = Noise(N, 0.025, seed) n_array = noise.get_address() sigma = 50 amp = 1.50 omega = 0.01 speed = 1 if DEBUG: tmax = 2 else: tmax = timeSteps width = 1.0 / (2.0 * sigma) xrange = xmax - xmin yrange = ymax - ymin switch_time = 0 offset = 20 x = (xmin + offset ) + (xrange - offset) * np.random.random() #(rand()/(RAND_MAX+1.0))) y = (ymin + offset ) + (yrange - offset) * np.random.random() #(rand()/(RAND_MAX+1.0))) nmax = 0.0 for i in range(N): if nmax < n_array[0, i]: nmax = n_array[0, i] level = '{0:.0e}'.format(scale_noise).replace("+", "").replace("-", "n") out_dir = base_dir + out_id + '-' + level + '/' try: os.makedirs(out_dir) except OSError: print('Warning: ' + out_dir + ' directory exists') pass for t in range(tmax): if t == switch_time: nextx = (xmin + offset) + (xrange - offset) * np.random.random( ) # (rand()/(RAND_MAX+1.0))) nexty = (ymin + offset) + (yrange - offset) * np.random.random( ) #(rand()/(RAND_MAX+1.0))) switch_time = t + 1 + int( np.sqrt((x - nextx)**2 + (y - nexty)**2) / speed) heading = np.arctan2(nexty - y, nextx - x) f = open(out_dir + 't' + str(t) + '.csv', 'w') x = x + speed * np.cos(heading) y = y + speed * np.sin(heading) nav = 0.0 nc = 0 for i in range(xmin, xmax): for j in range(ymin, ymax): nav = nav + n_array[j, i] nc = nc + 1 nav = nav / float(nmax * nc) for i in range(xmin, xmax): out = [] for j in range(ymin, ymax): x_d = i - x y_d = j - y grey = 1.0 - amp * np.exp( -np.sqrt(x_d**2 + y_d**2) * width) + scale_noise * (n_array[j, i] / float(nmax) - nav) if grey > 1: grey = 1.0 if grey < 0: grey = 0.0 #if ((mask.read(i,j)/256.0>0.5) # grey=0.0 out += ["{0:0.2f}".format(grey)] f.write(','.join(out) + '\n') f.close() noise.advance_timestep()
def main(): # You can play a game with two players: one players is represented by a node pair consisting of a transmitter and receiver # You have to specify both players' coordinator_id , transmitter's node id, receiver's node id # Optional you can specify at which frequency to measure(2420Mhz default) , to or not to save the results, and how long should the generator transmit a signal (recommended to be at least 5 seconds, default value=10) listIndex = [] listpTransmitted1 = [] listpTransmitted2 = [] # VESNA power generating list. This must be sorted. Powers are in dBm available_generating_powers = [ 0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16, -17, -18, -19, -20, -21, -22, -23, -24, -25, -26, -27, -28, -29, -30, ] # PLAYER 1 Transmitter1 = 51 Receiver1 = 52 # PLAYER 2 Transmitter2 = 54 Receiver2 = 58 # Desired increase of the players' lower SINR # IncreaseOfSINR=2 # IncreaseOfSINR=1.1 IncreaseOfSINR = 1.1 # IncreaseOfSINR=4 # for demonstrating that it is possible po increase the SINR for higher factors # IncreaseOfSINR=2.5 # TYPE OF USE: when real transmission power levels of VESNA are used or not # 1 - measuring gains only once at the beginning and setting transmission power at the end according to "available_generating_powers" # 2 - gains are continuously measured and discrete values for p1 and p2 set during the game. NOTICE: only used, when the gains are measured in real time, and not in advance (or set manually) TypeOfUse = 1 # now not in use # TRANSMISSION POWER for gains calculation pTransmittedGainCalcdBm = -15 # pTransmittedGainCalcdBm=0 pTransmitGainCalculation1dBm = pTransmittedGainCalcdBm pTransmitGainCalculation2dBm = pTransmittedGainCalcdBm # REQUIRED UTILITY TO END ITERATION # TargetUtility=-1.0e-020 TargetUtility = -1.0e-013 # TargetUtility=-1.0e-012 # SAVE RESULTS saveresults = True # saveresults= False # INITIAL TRANSMISSION POWER (reasonable to be set at the same level as for gains calculation) pTransmitteddBm = pTransmittedGainCalcdBm # pTransmitteddBm=-30 pTransmitted1dBm = pTransmitteddBm pTransmitted2dBm = pTransmitteddBm # Maximum number of iterations in the game to prevent that, in the case that the game does not converge, it is not played infinite time # MaxNrOfIterations=100 MaxNrOfIterations = 1000 # MaxNrOfIterations=5 # Counting the number of iterations during the game index = 1 listIndex.append(index) listpTransmitted1.append(pTransmitted1dBm) listpTransmitted2.append(pTransmitted2dBm) # EXPECTED NOISE # Noise1=3.38672324669e-12 # Noise1=2.2512786538e-10 # to demonstrate the increase with IncreaseOfSINR=4 # Noise2=3.35732111544e-12 # Noise2=1.37506518321e-11 # to demonstrate the increase with IncreaseOfSINR=4 # MEASURING NOISE IN THE OFFICE Noise1 = Noise.getInstantNoise(9501, Receiver1) Noise2 = Noise.getInstantNoise(9501, Receiver2) # MEASURING GAINS IN THE JSI OFFICE # h11 h11 = GainCalculations.calculateInstantGainForSINR( 9501, Transmitter1, Receiver1, pTransmitGainCalculation1dBm, measuring_freq=2422e6, saveresults=True, transmitting_duration=5, ) # h11 = GainCalculations.calculateInstantGainForSINR(9501, 51, 52, 0, measuring_freq=2422e6, saveresults=True, transmitting_duration=5) # h11 =0.000457079604928 # to demonstrate the increase with IncreaseOfSINR=4 # h11 =0.000269415326193 # h11 =1.17914835642e-06 # for testing the required convergence condition # h21 h21 = GainCalculations.calculateInstantGainForSINR( 9501, Transmitter2, Receiver1, pTransmitGainCalculation2dBm, measuring_freq=2422e6, saveresults=True, transmitting_duration=5, ) # h21 = GainCalculations.calculateInstantGainForSINR(9501, 54, 52, 0, measuring_freq=2422e6, saveresults=True, transmitting_duration=5) # h21 =1.17914835642e-06 # to demonstrate the increase with IncreaseOfSINR=4 # h21 =1.4646903564e-05 # h21 =0.000457079604928 # for testing the required convergence condition # h22 h22 = GainCalculations.calculateInstantGainForSINR( 9501, Transmitter2, Receiver2, pTransmitGainCalculation2dBm, measuring_freq=2422e6, saveresults=True, transmitting_duration=5, ) # h22 = GainCalculations.calculateInstantGainForSINR(9501, 54, 58, 0, measuring_freq=2422e6, saveresults=True, transmitting_duration=5) # h22 =1.30015521864e-04 # to demonstrate the increase with IncreaseOfSINR=4 # h22 =4.83003296862e-06 # h22 =2.91873033877e-06 # for testing the required convergence condition # h12 h12 = GainCalculations.calculateInstantGainForSINR( 9501, Transmitter1, Receiver2, pTransmitGainCalculation1dBm, measuring_freq=2422e6, saveresults=True, transmitting_duration=5, ) # h12 = GainCalculations.calculateInstantGainForSINR(9501, 51, 58, 0, measuring_freq=2422e6, saveresults=True, transmitting_duration=5) # h12 =2.91873033877e-06 # to demonstrate the increase with IncreaseOfSINR=4 # h12 =5.08980966629e-07 # h12 =1.30015521864e-04 # for testing the required convergence condition print Noise1 print "Noise1: %.3f dBm" % (10.00 * math.log10(Noise1 / 0.001)) print Noise2 print "Noise2: %.3f dBm" % (10.00 * math.log10(Noise2 / 0.001)) # returned gain is in linear scale. print h11 print "h11: %.3f dB" % (10.00 * math.log10(h11)) print h21 print "h21: %.3f dB" % (10.00 * math.log10(h21)) print h22 print "h22: %.3f dB" % (10.00 * math.log10(h22)) print h12 print "h12: %.3f dB" % (10.00 * math.log10(h12)) # Checking if received signals are higher than interference if not ((h11 > h21) and (h22 > h12)): print "THE REQUIRED CONVERGENCE CONDITION IS NOT SATISFIED" print "The game is not played as it can not converge." return # Transmission powers of both players pTransmitted1 = math.pow(10, pTransmitted1dBm / 10.00) * 0.001 pTransmitted2 = math.pow(10, pTransmitted2dBm / 10.00) * 0.001 # Signal-to-noise ratio for both players SINR1 = (pTransmitted1 * h11) / (pTransmitted2 * h21 + Noise1) SINR2 = (pTransmitted2 * h22) / (pTransmitted1 * h12 + Noise2) print SINR1 print "SINR1: %.3f dB" % (10.00 * math.log10(SINR1)) print SINR2 print "SINR2: %.3f dB" % (10.00 * math.log10(SINR2)) # Interference for both players I1 = pTransmitted2 * h21 I2 = pTransmitted1 * h12 # Defininf the rule of the game: we want to increas the SINR of the player with the lower SINR by factor of parameter TargetUtility if SINR1 > SINR2: SINR2Required = SINR2 * IncreaseOfSINR SINR1Required = SINR1 if SINR2 >= SINR1: SINR1Required = SINR1 * IncreaseOfSINR SINR2Required = SINR2 print SINR1Required print "SINR1Required: %.3f dB" % (10.00 * math.log10(SINR1Required)) print SINR2Required print "SINR2Required: %.3f dB" % (10.00 * math.log10(SINR2Required)) # Required received powers for desired SINRs for both players pReceivedrequired1 = (I1 + Noise1) * SINR1Required pReceivedrequired2 = (I2 + Noise2) * SINR2Required # Required transmission powers for desired SINRs for both players pTransmittedrequired1 = pReceivedrequired1 / h11 pTransmittedrequired2 = pReceivedrequired2 / h22 print pTransmittedrequired1 print "pTransmittedrequired1: %.3f dBm" % (10.00 * math.log10(pTransmittedrequired1 / 0.001)) print pTransmittedrequired2 print "pTransmittedrequired2: %.3f dBm" % (10.00 * math.log10(pTransmittedrequired2 / 0.001)) # Utilities of both players Utility1 = -math.pow((pTransmittedrequired1 - pTransmitted1), 2) Utility2 = -math.pow((pTransmittedrequired2 - pTransmitted2), 2) print Utility1 print "Utility1: %.f" % (Utility1) print Utility2 print "Utility2: %.f" % (Utility2) index = index + 1 # For TypeOfUse is equal 2 if TypeOfUse == 2: if saveresults: results_list = [ pTransmitGainCalculation1dBm, 10.00 * math.log10(h11), 10.00 * math.log10(h21), datetime.datetime.now(), ] printResultsInAFileSINR(results_list, Transmitter1) if saveresults: results_list = [ pTransmitGainCalculation2dBm, 10.00 * math.log10(h22), 10.00 * math.log10(h12), datetime.datetime.now(), ] printResultsInAFileSINR(results_list, Transmitter2) min_diferrence = float("inf") nearest_power = None for i in range(0, len(available_generating_powers)): if ( math.fabs(10.00 * math.log10(pTransmittedrequired1 / 0.001) - available_generating_powers[i]) < min_diferrence ): min_diferrence = math.fabs( 10.00 * math.log10(pTransmittedrequired1 / 0.001) - available_generating_powers[i] ) nearest_power = available_generating_powers[i] print nearest_power print "pTransmittedrequired1: %.3f dBm" % (nearest_power) pTransmittedGainCalcdBm1 = nearest_power pTransmittedrequired1 = math.pow(10, nearest_power / 10.00) * 0.001 print "pTransmittedrequired1: %.3f W" % (pTransmittedrequired1) min_diferrence = float("inf") nearest_power = None for i in range(0, len(available_generating_powers)): if ( math.fabs(10.00 * math.log10(pTransmittedrequired2 / 0.001) - available_generating_powers[i]) < min_diferrence ): min_diferrence = math.fabs( 10.00 * math.log10(pTransmittedrequired2 / 0.001) - available_generating_powers[i] ) nearest_power = available_generating_powers[i] print nearest_power print "pTransmittedrequired2: %.3f dBm" % (nearest_power) pTransmittedGainCalcdBm2 = nearest_power pTransmittedrequired2 == math.pow(10, nearest_power / 10.00) * 0.001 print "pTransmittedrequired2: %.3f W" % (pTransmittedrequired2) listIndex.append(index) listpTransmitted1.append(pTransmittedGainCalcdBm1) listpTransmitted2.append(pTransmittedGainCalcdBm2) pTransmitted1 = pTransmittedrequired1 pTransmitted2 = pTransmittedrequired2 # For TypeOfUse is equal 1 if TypeOfUse == 1: listIndex.append(index) listpTransmitted1.append(10.00 * math.log10(pTransmitted1 / 0.001)) listpTransmitted2.append(10.00 * math.log10(pTransmitted2 / 0.001)) # Iterations in while loop to set the transmission powers according to the desired SINRs # The procedure in the while loop is similar as at the first setting of transmission powers according to the desired SINRS (see above) while Utility1 < TargetUtility or Utility2 < TargetUtility: # For TypeOfUse is equal 2, we continuously measure gains if TypeOfUse == 2: h11 = GainCalculations.calculateInstantGainForSINR( 9501, Transmitter1, Receiver1, pTransmittedGainCalcdBm1, measuring_freq=2422e6, saveresults=True, transmitting_duration=5, ) h21 = GainCalculations.calculateInstantGainForSINR( 9501, Transmitter2, Receiver1, pTransmittedGainCalcdBm2, measuring_freq=2422e6, saveresults=True, transmitting_duration=5, ) h22 = GainCalculations.calculateInstantGainForSINR( 9501, Transmitter2, Receiver2, pTransmittedGainCalcdBm2, measuring_freq=2422e6, saveresults=True, transmitting_duration=5, ) h12 = GainCalculations.calculateInstantGainForSINR( 9501, Transmitter1, Receiver2, pTransmittedGainCalcdBm1, measuring_freq=2422e6, saveresults=True, transmitting_duration=5, ) if not ((h11 > h21) and (h22 > h12)): print "THE REQUIRED CONVERGENCE CONDITION IS NOT SATISFIED" print "The game is stopped as it can not converge." return SINR1 = (pTransmitted1 * h11) / (pTransmitted2 * h21 + Noise1) SINR2 = (pTransmitted2 * h22) / (pTransmitted1 * h12 + Noise2) I1 = pTransmitted2 * h21 I2 = pTransmitted1 * h12 pReceivedrequired1 = (I1 + Noise1) * SINR1Required pReceivedrequired2 = (I2 + Noise2) * SINR2Required pTransmittedrequired1 = pReceivedrequired1 / h11 pTransmittedrequired2 = pReceivedrequired2 / h22 Utility1 = -math.pow((pTransmittedrequired1 - pTransmitted1), 2) Utility2 = -math.pow((pTransmittedrequired2 - pTransmitted2), 2) index = index + 1 if index >= MaxNrOfIterations: Utility1 = 0 Utility2 = 0 if TypeOfUse == 2: if saveresults: results_list = [ pTransmittedGainCalcdBm1, 10.00 * math.log10(h11), 10.00 * math.log10(h21), datetime.datetime.now(), ] printResultsInAFileSINR(results_list, Transmitter1) if saveresults: results_list = [ pTransmittedGainCalcdBm2, 10.00 * math.log10(h22), 10.00 * math.log10(h12), datetime.datetime.now(), ] printResultsInAFileSINR(results_list, Transmitter2) min_diferrence = float("inf") nearest_power = None for i in range(0, len(available_generating_powers)): if ( math.fabs(10.00 * math.log10(pTransmittedrequired1 / 0.001) - available_generating_powers[i]) < min_diferrence ): min_diferrence = math.fabs( 10.00 * math.log10(pTransmittedrequired1 / 0.001) - available_generating_powers[i] ) nearest_power = available_generating_powers[i] print nearest_power print "pTransmittedrequired1: %.3f dBm" % (nearest_power) pTransmittedGainCalcdBm1 = nearest_power pTransmittedrequired1 = math.pow(10, nearest_power / 10.00) * 0.001 print "pTransmittedrequired1: %.3f W" % (pTransmittedrequired1) min_diferrence = float("inf") nearest_power = None for i in range(0, len(available_generating_powers)): if ( math.fabs(10.00 * math.log10(pTransmittedrequired2 / 0.001) - available_generating_powers[i]) < min_diferrence ): min_diferrence = math.fabs( 10.00 * math.log10(pTransmittedrequired2 / 0.001) - available_generating_powers[i] ) nearest_power = available_generating_powers[i] print nearest_power print "pTransmittedrequired2: %.3f dBm" % (nearest_power) pTransmittedGainCalcdBm2 = nearest_power pTransmittedrequired2 == math.pow(10, nearest_power / 10.00) * 0.001 print "pTransmittedrequired2: %.3f W" % (pTransmittedrequired2) listIndex.append(index) listpTransmitted1.append(pTransmittedGainCalcdBm1) listpTransmitted2.append(pTransmittedGainCalcdBm2) pTransmitted1 = pTransmittedrequired1 pTransmitted2 = pTransmittedrequired2 if TypeOfUse == 1: listIndex.append(index) listpTransmitted1.append(10.00 * math.log10(pTransmitted1 / 0.001)) listpTransmitted2.append(10.00 * math.log10(pTransmitted2 / 0.001)) # END OF THE GAME # Displaying some parameters at the end of the game if TypeOfUse == 2: if saveresults: results_list = [ pTransmittedGainCalcdBm1, 10.00 * math.log10(h11), 10.00 * math.log10(h21), datetime.datetime.now(), ] printResultsInAFileSINR(results_list, Transmitter1) if saveresults: results_list = [ pTransmittedGainCalcdBm2, 10.00 * math.log10(h22), 10.00 * math.log10(h12), datetime.datetime.now(), ] printResultsInAFileSINR(results_list, Transmitter2) print "listIndex:" print listIndex print "listpTransmitted1:" print listpTransmitted1 print "listpTransmitted2:" print listpTransmitted2 print Utility1 print "Utility1: %.f" % (Utility1) print Utility2 print "Utility2: %.f" % (Utility2) print pTransmitted1 print "pTransmitted1: %.3f dBm" % (10.00 * math.log10(pTransmitted1 / 0.001)) print pTransmitted2 print "pTransmitted2: %.3f dBm" % (10.00 * math.log10(pTransmitted2 / 0.001)) SINR1 = (pTransmitted1 * h11) / (pTransmitted2 * h21 + Noise1) SINR2 = (pTransmitted2 * h22) / (pTransmitted1 * h12 + Noise2) print SINR1 print "SINR1: %.3f dB" % (10.00 * math.log10(SINR1)) print SINR2 print "SINR2: %.3f dB" % (10.00 * math.log10(SINR2)) print "NrOfIterations: %.f" % (index) min_diferrence = float("inf") nearest_power = None for i in range(0, len(available_generating_powers)): if math.fabs(10.00 * math.log10(pTransmitted1 / 0.001) - available_generating_powers[i]) < min_diferrence: min_diferrence = math.fabs(10.00 * math.log10(pTransmitted1 / 0.001) - available_generating_powers[i]) nearest_power = available_generating_powers[i] pTransmitteddBm1 = nearest_power print pTransmitteddBm1 print "pTransmitted1: %.3f dBm" % (pTransmitteddBm1) min_diferrence = float("inf") nearest_power = None for i in range(0, len(available_generating_powers)): if math.fabs(10.00 * math.log10(pTransmitted2 / 0.001) - available_generating_powers[i]) < min_diferrence: min_diferrence = math.fabs(10.00 * math.log10(pTransmitted2 / 0.001) - available_generating_powers[i]) nearest_power = available_generating_powers[i] pTransmitteddBm2 = nearest_power print pTransmitteddBm2 print "pTransmitted2: %.3f dBm" % (pTransmitteddBm2) pTransmitted1 = math.pow(10, pTransmitteddBm1 / 10.00) * 0.001 pTransmitted2 = math.pow(10, pTransmitteddBm2 / 10.00) * 0.001 SINR1 = (pTransmitted1 * h11) / (pTransmitted2 * h21 + Noise1) SINR2 = (pTransmitted2 * h22) / (pTransmitted1 * h12 + Noise2) print SINR1 print "SINR1: %.3f dB" % (10.00 * math.log10(SINR1)) print SINR2 print "SINR2: %.3f dB" % (10.00 * math.log10(SINR2)) # Plotting results of the game (attained through iterations) min_y_list = min(available_generating_powers) max_y_list = max(available_generating_powers) plot.figure(1) plot.grid() plot.title("Player 1") plot.plot(listIndex, listpTransmitted1) plot.axis([0, len(listpTransmitted1), min_y_list - 2, max_y_list + 2]) plot.xlabel("Iteration") plot.ylabel("Transmission Power (dBm)") plot.figure(2) plot.grid() plot.title("Player 2") plot.plot(listIndex, listpTransmitted2) plot.axis([0, len(listpTransmitted2), min_y_list - 2, max_y_list + 2]) plot.xlabel("Iteration") plot.ylabel("Transmission Power (dBm)") plot.figure(3) plot.grid() plot.title("Player 1 and Player 2") plot.plot(listIndex, listpTransmitted1) plot.plot(listIndex, listpTransmitted2) plot.axis([0, len(listpTransmitted2), min_y_list - 2, max_y_list + 2]) plot.xlabel("Iteration") plot.ylabel("Transmission Power (dBm)") plot.show()
def main(args): # Set path to save result gym_dir = './' + args['env'] + '_' + args['variation'] + '/gym' # Set random seed for reproducibility np.random.seed(int(args['seed'])) tf.set_random_seed(int(args['seed'])) with tf.Session() as sess: # Load environment env = gym.make(args['env']) env.seed(int(args['seed'])) # get size of action and state (i.e. output and input for the agent) obs = env.reset() observation_dim = obs['observation'].shape[0] achieved_goal_dim = obs['achieved_goal'].shape[0] desired_goal_dim = obs['desired_goal'].shape[0] assert achieved_goal_dim == desired_goal_dim # state size = observation size + goal size state_dim = observation_dim + desired_goal_dim action_dim = env.action_space.shape[0] action_highbound = env.action_space.high # print out parameters print('Parameters:') print('Observation Size=', observation_dim) print('Goal Size=', desired_goal_dim) print('State Size =', state_dim) print('Action Size =', action_dim) print('Action Upper Boundary =', action_highbound) # save to monitor if render if args['render']: env = gym.wrappers.Monitor(env, gym_dir, force=True) else: env = gym.wrappers.Monitor(env, gym_dir, video_callable=False, force=True) # create actor actor = Actor(sess, state_dim, action_dim, action_highbound, float(args['actor_lr']), float(args['tau']), int(args['batch_size']), int(args['hidden_size'])) # create critic critic = Critic(sess, state_dim, action_dim, float(args['critic_lr']), float(args['tau']), float(args['gamma']), actor.n_actor_vars, int(args['hidden_size'])) # noise actor_noise = Noise(mu=np.zeros(action_dim)) # train the network if not args['test']: train(sess, env, args, actor, critic, actor_noise, desired_goal_dim, achieved_goal_dim, observation_dim) else: test(sess, env, args, actor, critic, desired_goal_dim, achieved_goal_dim, observation_dim) # close gym env.close() # close session sess.close()
class Agent(): def __init__(self, test=False): # device if torch.cuda.is_available(): self.device = torch.device('cuda') else: self.device = torch.device('cpu') ######################################### """ Some hand tune config(for developing) """ self.discrete = False self.action_dim = 1 self.state_dim = 3 self.batch_size = 100 self.action_low = -2 self.action_high = 2 ########################################## self.P_online = Actor(state_dim=self.state_dim, action_size=self.action_dim).to(self.device) self.P_target = Actor(state_dim=self.state_dim, action_size=self.action_dim).to(self.device) self.P_target.load_state_dict(self.P_online.state_dict()) self.Q_online = Critic(state_size=self.state_dim, action_size=self.action_dim).to(self.device) self.Q_target = Critic(state_size=self.state_dim, action_size=self.action_dim).to(self.device) self.Q_target.load_state_dict(self.Q_online.state_dict()) # discounted reward self.gamma = 0.99 self.eps = 0.25 # optimizer self.q_optimizer = torch.optim.Adam(self.Q_online.parameters(), lr=1e-3) self.p_optimizer = torch.optim.Adam(self.P_online.parameters(), lr=1e-3) # saved rewards and actions self.replay_buffer = ReplayBuffer() # noise self.noise = Noise(DELTA, SIGMA, OU_A, OU_MU) # Initialize noise self.ou_level = 0. self.ep_step = 0 def act(self, state, test=False): if not test: with torch.no_grad(): # boring type casting state = ((torch.from_numpy(state)).unsqueeze(0)).float().to( self.device) action = self.P_online(state) # continuous output a = action.data.cpu().numpy() # if self.ep_step < 200: # self.ou_level = self.noise.ornstein_uhlenbeck_level(self.ou_level) # a = a + self.ou_level if self.discrete: action = np.argmax(a) return a, action else: if self.ep_step < 200: self.ou_level = self.noise.ornstein_uhlenbeck_level( self.ou_level) action = np.clip(a + self.ou_level, self.action_low, self.action_high) return action, action def collect_data(self, state, action, reward, next_state, done): self.replay_buffer.push( torch.from_numpy(state).float().unsqueeze(0), torch.from_numpy(action).float(), torch.tensor([reward]).float().unsqueeze(0), torch.from_numpy(next_state).float().unsqueeze(0), torch.tensor([done]).float().unsqueeze(0)) def clear_data(self): raise NotImplementedError("Circular Queue don't need this function") def update(self): if len(self.replay_buffer) < self.batch_size: return states, actions, rewards, next_states, dones = self.replay_buffer.sample( batch_size=self.batch_size, device=self.device) # discounted rewards # rewards = torch.from_numpy(discount((rewards.view(rewards.shape[0])).cpu().numpy())).float().to(self.device) ### debug shape : ok #===============================Critic Update=============================== self.Q_online.train() Q = self.Q_online((states, actions)) with torch.no_grad(): # don't need backprop for target value self.Q_target.eval() self.P_target.eval() target = rewards + self.gamma * (1 - dones) * self.Q_target( (next_states, self.P_target(next_states))) critic_loss_fn = torch.nn.MSELoss() critic_loss = critic_loss_fn(Q, target).mean() # update self.q_optimizer.zero_grad() critic_loss.backward() self.q_optimizer.step() # print("critic loss", critic_loss.item()) #===============================Actor Update=============================== # fix online_critic , update online_actor self.Q_online.eval() for p in self.Q_online.parameters(): p.requires_grad = False for p in self.P_online.parameters(): p.requires_grad = True policy_loss = -self.Q_online((states, self.P_online(states))) policy_loss = policy_loss.mean() self.p_optimizer.zero_grad() policy_loss.backward() self.p_optimizer.step() # print("policy loss", policy_loss.item()) for p in self.Q_online.parameters(): p.requires_grad = True #===============================Target Update=============================== soft_update(self.Q_target, self.Q_online, tau=1e-3) soft_update(self.P_target, self.P_online, tau=1e-3) self.eps -= EPSILON_DECAY if self.eps <= 0: self.eps = 0
def omsim(settings): # cd to the directory containing the configuration file os.chdir(settings.directory) # process input cmaps = import_input(settings) print('Imported ' + str(sum(cmaps[iname].count() for iname in cmaps)) + ' nicks in ' + str(sum(cmaps[iname].seq_len() for iname in cmaps)) + 'bp.') cmaps = KMP(settings, cmaps) # write processed input write_processed_input(settings, cmaps) # filter input for enzymes / files we need seqs, seq_lens, fns = filter_input(settings, cmaps) prev = 0 cum_seq_lens = [] for seq_len in seq_lens: curr = prev + seq_len cum_seq_lens += [curr] prev = curr print('Using ' + str(sum(len(f) for f in fns)) + ' nicks in ' + str(sum(seq_lens)) + 'bp.') #compute reverse nicking sites rns = get_rns(settings, fns, seq_lens) #estimate number of chips based on expected coverage if settings.chips == 0: temp = int(sum(seq_lens) * settings.coverage / (settings.scans_per_chip * settings.get_scan_size())) settings.chips = temp if temp > 1 else 1 #estimate coverage settings.estimated_coverage = int(settings.get_scan_size() * settings.scans_per_chip * settings.chips / float(sum(seq_lens))) print('Generating reads on ' + str(settings.chips) + ' chip' + ('' if settings.chips == 1 else 's') + ', estimated coverage: ' + str(settings.estimated_coverage) + 'x.') noise = Noise(settings) bnx = BNX(settings, noise) # generate reads for chip in range(1, settings.chips + 1): chip_settings = {'size': 0, 'scans': 0, 'chip_id': '20249,11843,07/17/2014,840014289', 'run_id': str(chip), 'flowcell': 1, 'molecule_count': 0, 'bpp': 425, 'stretch_factor': noise.chip_stretch_factor()} chip_settings['bpp'] /= chip_settings['stretch_factor'] molecules = {} for label in settings.labels: molecules[label] = [] # generate reads moleculeID = 0 relative_stretch = [] for scan in range(1, settings.scans_per_chip + 1): chip_settings['scans'] += 1 scan_stretch = noise.scan_stretch_factor(chip_settings['stretch_factor']) for l, m, meta in noise.generate_scan(seq_lens, cum_seq_lens, fns, rns): moleculeID += 1 molecule = {} for label in settings.labels: molecule[label] = [] for nick in m: molecule[nick[1]['label']].append(nick[0]) for label in settings.labels: if settings.min_nicks <= len(molecule[label]): molecules[label].append((l, molecule[label], chip_settings['scans'], meta)) relative_stretch.append(noise.mol_stretch_factor(scan_stretch) / chip_settings['stretch_factor']) chip_settings['molecule_count'] += 1 chip_settings['size'] += l # write output for label in settings.labels: moleculeID = 0 ofile = open(settings.prefix + '.' + label + '.' + str(chip) + '.bnx', 'w') #bedfile = open(settings.prefix + '.' + label + '.' + str(chip) + '.bed', 'w') bnx.write_bnx_header(ofile, label, chip_settings) for l, m, s, meta in molecules[label]: moleculeID += 1 bnx.write_bnx_entry((moleculeID, l, s), m, ofile, chip_settings, relative_stretch[moleculeID - 1]) #for idx, mol in enumerate(meta): #bedfile.write(seqs[mol[0]] + '\t' + str(mol[1]) + '\t' + str(mol[1] + l) + '\t' + str(moleculeID) + ('.' + str(idx) if len(meta) > 1 else '') + '\n') ofile.close() #bedfile.close() print('Finished chip ' + str(chip) + '/' + str(settings.chips)) print('Finished processing ' + settings.name + '.\n')
class LiposomeBasic(object): def __init__(self, width, height, prob_deviation=0): """ :param width: int :param height: int :param prob_deviation: float, optional, probability of some deviation """ self.width = width self.height = height self._noise = Noise(width, height, low=0.2, high=0.7) self.data = self._get_background() self._prob_deviation = prob_deviation self._prob_deviation_internal = 0.3 def _get_background(self): """ Makes background of microscope image :return: numpy 2d array of shape(self.width, self.height) """ data = np.zeros((self.width, self.height)) turbulence_size = 4 for x in xrange(self.width): for y in xrange(self.height): data[x, y] = self._noise.turbulence(x, y, turbulence_size) return data @abstractmethod def _draw(self): pass #@abstractmethod def _deviate(self): if random.random() > 0.4: self._deviation_spot() if random.random() > 0.97: self._deviation_patch() if random.random() < self._prob_deviation_internal: self._deviation_overlay_internal(self._center_x, self._center_y, self._radius) if random.random() > 0.5: self._deviation_arc_unilameral() if random.random() > 0.9: self._deviation_arc_multilamellar() def _draw_circle(self, radius, thickness, center_x=0, center_y=0, a=1, b=1, turb_size=0, turb_power=0): """ :param radius: :param thickness: :param center_x: :param center_y: :param a: float, optional, ellipse: radius on x axis :param b: float, optional, ellipse: radius on y axis :return: """ noise = Noise(self.width, self.height) a = 1 / (a * a) b = 1 / (b * b) for x in xrange(self.width): for y in xrange(self.height): xv = (x - self.width / 2.0) / self.width yv = (y - self.height / 2.0) / self.height dist = math.sqrt((xv - center_x) * (xv - center_x) * a + (yv - center_y) * (yv - center_y) * b) dist += turb_power * noise.turbulence(x, y, turb_size) if ((radius - thickness) <= dist) and \ (dist <= (radius + thickness)): theta = np.interp(dist, [radius - thickness, radius + thickness], [0, math.pi]) multiplier = random.uniform(0.2, 0.25) value = math.sin(theta) * multiplier self.data[x, y] -= value def make(self): self._draw() if random.random() < self._prob_deviation: self._deviate() def _deviation_spot(self): # add a spot like on image 544825 p = random.random() n = 1 if p > 0.5: n = 2 if p > 0.75: n = 3 for _ in xrange(n): x = random.uniform(-0.3, 0.3) y = random.uniform(-0.3, 0.3) radius = random.uniform(0.01, 0.03) self._draw_circle(radius, 0.05, center_x=x, center_y=y) def _deviation_patch(self): # a patch like on image 545302 x = random.uniform(-0.3, 0.3) y = random.uniform(-0.3, 0.3) radius = 0.05 thickness = 0.6 turb_size = 4 turb_power = 0.1 self._draw_circle(radius, thickness, center_x=x, center_y=y, turb_size=turb_size, turb_power=turb_power) def _deviation_overlay_internal(self, center_x, center_y, center_radius, n=None, radius=None): """ Overlay of liposomes like on image 539202 :param n: int, optional, number of inscribed liposomes, randomized if None :return: nothing """ if n is None: n = random.randint(1, 2) for _ in xrange(n): if radius is None: radius = random.uniform(0.2, 0.3) thickness = random.uniform(0.04, 0.05) theta = random.randint(1, 360) x = center_x + (center_radius - radius) * math.cos(theta) y = center_y + (center_radius - radius) * math.sin(theta) a = random.uniform(0.7, 1.3) b = random.uniform(0.7, 1.3) self._draw_circle(radius, thickness, center_y=y, center_x=x, turb_size=32, turb_power=0.1, a=a, b=b) def _deviation_arc_unilameral(self, n=None): """ Draw an arc(s) as on image 545216, 545413, 547997 :param n: int, optional, number of arcs, randomized between 1 and 5 if None :return: """ if n is None: p = random.random() n = 1 if p > 0.4: n = 2 if p > 0.6: n = 3 if p > 0.9: n = 4 if p > 0.97: n = 5 thetas = np.linspace(0, 360, 6) thetas = random.sample(thetas, n) for theta in thetas: radius = random.uniform(0.4, 0.7) x = radius * math.cos(theta) y = radius * math.sin(theta) thickness = random.uniform(0.04, 0.05) a = random.uniform(0.8, 1.2) b = random.uniform(0.8, 1.2) radius = random.uniform(0.2, 0.4) self._draw_circle(radius, thickness, center_y=y, center_x=x, a=a, b=b) def _deviation_arc_multilamellar(self, n=None): """ Deviation like on image 546292 :param n: int, optional, number of arcs, randomized between 2 and 3 if None :return: """ if n is None: n = 1 if random.random() > 0.5: n = 2 thetas = np.linspace(0, 360, num=6) thetas = random.sample(thetas, n) for theta in thetas: circles = 2 if random.random() > 0.5: circles = 3 radius = random.uniform(0.5, 0.7) x = radius * math.cos(theta) y = radius * math.sin(theta) thickness = random.uniform(0.03, 0.05) a = random.uniform(0.8, 1.2) b = random.uniform(0.8, 1.2) radius = random.uniform(0.4, 0.6) for _ in xrange(circles): self._draw_circle(radius, thickness, center_y=y, center_x=x, a=a, b=b) radius -= random.uniform(0.08, 0.11)