def set_patches(self, x): rgb = self.patch_colours[str(x)] rgb = tuple(common.clamp(x*255.0, 0, 255) for x in rgb) for p in self.probe_meta.values(): p['patch'].setStyleSheet("QWidget { background-color: rgb(%d, %d, %d) }" % rgb) p['patch'].show() p['patch'].setAutoFillBackground(True)
def tune_steering_slow(atc, iErrf, imaxAA, AM): atc.atPID.P = 1 atc.atPID.I = 0 atc.atPID.D = 0 slowF = (1 + SlowConfig.SlowTorqueF / max(atc.engineF.acceleration, atc.engineF.deceleration)) noise_scale = clamp( abs(50 * (abs(atc.avPID.perror) + abs(atc.error / np.pi)))**0.5, 0.01, 1) if atc.MaxAA >= 1: atc.avPID.P = SlowConfig.avP_HighAA_Scale / slowF * noise_scale atc.avPID.D = clampL( SlowConfig.avD_HighAA_Intersect - SlowConfig.avD_HighAA_Inclination * atc.MaxAA, SlowConfig.avD_HighAA_Max) * noise_scale else: atc.avPID.P = SlowConfig.avP_LowAA_Scale / slowF * noise_scale atc.avPID.D = ( SlowConfig.avD_LowAA_Intersect - SlowConfig.avD_LowAA_Inclination * atc.MaxAA) * noise_scale atc.avPID.I = SlowConfig.avI_Scale * clampH(atc.MaxAA, 1) * noise_scale # avFilter.ratio = clamp(abs(atc.AV)+abs(atc.error/np.pi)*500, 0.01, 1) # AV = avFilter.EWA(atc.AV) AV = atc.AV update_pids(atc, AV)
def eu4(coord: GeoCoord) -> MapCoord: lat, lon = coord.lat, coord.lon # the eu4 map is mostly miller, but: # (1) the poles are trimmed if not radians(-56) < lat < radians(72): return 1, 1 # (2) east siberia is stretched if radians(50) < lat and radians(154) < lon: lat += (lon - radians(154)) / 3 # (3) australia is shunken and moved northward if lat < radians(-11) and radians(112) < lon < radians(154): lat = remap(lat, radians(-39), radians(-11), radians(-35), radians(-11)) lon = remap(lon, radians(112), radians(154), radians(116), radians(151)) # (4) new zealand is moved northward if lat < radians(-33) and radians(165) < lon: lat += radians(8) # (5) greenland and iceland and jan mayen are moved northward if radians(-57) < lon < radians(-8) and radians(59) < lat: lat += radians(5) # (6) the americas are moved northward elif lon < radians(-34): lat += radians(13) # (7) in addition, the bottom of south america is squished if lat < radians(-31): lat = remap(lat, radians(-45), radians(-31), radians(-37), radians(-31)) y, x = miller(clamp(lat, -pi / 2, pi / 2), lon) y = remap(y, -28 / 90, 61 / 90, -1, 1) return MapCoord(x, y)
def tip_over(self): start_roll = self.start_roll end_roll = self.end_roll completion = clamp( (self.mean_altitude() - start_roll) / (end_roll - start_roll)) target_pitch = 90 * (1.0 - completion) if abs(target_pitch - self.ap.target_pitch) > 0.5: self.ap.target_pitch_and_heading(target_pitch, self.target_heading)
def set_patches(self, x): rgb = self.patch_colours[str(x)] rgb = tuple(common.clamp(x * 255.0, 0, 255) for x in rgb) for p in self.probe_meta.values(): p['patch'].setStyleSheet( "QWidget { background-color: rgb(%d, %d, %d) }" % rgb) p['patch'].show() p['patch'].setAutoFillBackground(True)
def compute_targets(self, batch, *args, **kwargs): """Compute the targets for the given batch. Args: batch (tuple): A tuple (states, actions, rewards, next_states, next_states_idx) where each element is a numpy array. next_states does not contain next_states for states where the episode ended. For this reason, next_states_idx contains indices to match next_states with the appropriate elements of states. I.e. if the i-th element of next_states_idx is k, then the k-th element of next_states refers to the k-th element of states. Returns: A Numpy column vector with computed target values on the rows. """ states, actions, rewards, next_states, next_states_idx = batch actions_ts = torch.tensor(actions, dtype=self.dtype) rewards_ts = torch.tensor(rewards, dtype=self.dtype) next_states_ts = torch.tensor(next_states, dtype=self.dtype) next_states_idx_ts = torch.tensor(next_states_idx, dtype=torch.long) with torch.no_grad(): # Sampling and clamping noise noise_shape = (len(next_states_idx_ts), *actions_ts.size()[1:]) noise = torch.randn(noise_shape) * self.target_noise noise = common.clamp(noise, -self.noise_clip, self.noise_clip) # Compute target actions and clamping if a max range is specified. if next_states_idx.shape[0] > 0: next_action = self.dqac_nets.predict_target_actions(next_states_ts) + noise if self.max_action is not None: next_action = common.clamp(next_action, self.min_action, self.max_action) # Compute target values target_values = self.dqac_nets.predict_targets( next_states_ts, next_action, mode='min' ) rewards_ts[next_states_idx_ts] += self.df * target_values return rewards_ts.detach().numpy()
def on_frame(): alt_err = alt - self.dX if self.AS > 0 or self.DS > 0: if alt_err > 0: self.pid.kp = clamp(0.01 * abs(alt_err) / clampL(self.upV, 1), 0.0, d) elif alt_err < 0: self.pid.kp = clamp(self.twr ** 2 / clampL(-self.upV, 1), 0.0, clampH(d * self.twr / 2, d)) else: self.pid.kp = d self.pid.kd = d / clampL(self.dX, 1) if alt_err < 0: alt_err = alt_err / clampL(self.dX, 1) self.maxV = self.pid.update(alt_err) print self.pid, alt_err, clamp(0.01 * abs(alt_err) / clampL(self.upV, 1), 0.0, d), d if self.N > 0: dV = (self.upV - self.dV) / clampL(self.dX, 1) if alt_err < 0: # print '% f %+f = %f' % (dV/clampL(self.dX/50, 1), clampL(alt_err*self.dX/5000*self.twr, self.pid.min*10), dV/clampL(self.dX/50, 1) + clampL(alt_err*self.dX/5000*self.twr, self.pid.min*10)) dV = dV + clampL(alt_err * self.dX / 500 * self.twr, self.pid.min * 10) else: dV = clampL(dV, 0) # print dV self.dVsp.append(dV) self.maxV += dV self.Vsp.append(self.maxV)
def act(self, mean, *args, **kwargs): """Compute the action by sampling from a standard Gaussian with zero mean and epsilon variance. Args: mean (torch.Tensor): The predicted action, that will be used as mean. Returns: A Tensor with the same shape of mean, containing the chosen action. """ noise = torch.randn_like(mean) * np.sqrt( self.epsilon_updater.cur_value) action = mean + noise action = common.clamp(action, self.min_action, self.max_action) self.epsilon_updater.update() return action
def tune_steering_mixed(atc, iErrf, imaxAA, AM): if atc.InstantRatio > 0.3: tune_steering_fast(MixedConfig3plus, atc, iErrf, imaxAA, AM) else: # if atc.MaxAA >= 1: # atc.atPID.P = 20 # atc.atPID.I = 0 # atc.atPID.D = 0 # atc.avPID.P = 1/clampL(abs(AM)/atc.MaxAA**0.2/12, 1) # atc.avPID.I = 0.002 # atc.avPID.D = 1.4/atc.MaxAA**0.3 # else: # atc.atPID.P = 40/clampL(abs(AM)/2, 1) # atc.atPID.I = 0 # atc.atPID.D = 0 # atc.avPID.P = 1/clampL(abs(AM)/12, 1) # atc.avPID.I = 0.001 # atc.avPID.D = 1.4/atc.MaxAA**0.3 # clampH(abs(40*(abs(atc.avPID.perror)+abs(atc.error/np.pi)))**6, 1) noise_scale = clamp((50 * (abs(atc.AV) + atc.errorF))**0.6, 0.001, 1) atc.atPID.P = 1 #clamp(0.5*atc.MaxAA**0.5, 0.0001, 1) atc.atPID.I = 0 atc.atPID.D = 0 atc.avPID.P = ( (MixedConfig.avP_A / (atc.InstantRatio**MixedConfig.avP_D + MixedConfig.avP_B) + MixedConfig.avP_C)) / clampL(abs(AM), 1) / atc.MaxAA * noise_scale atc.avPID.D = ( (MixedConfig.avD_A / (atc.InstantRatio**MixedConfig.avD_D + MixedConfig.avD_B) + MixedConfig.avD_C)) / atc.MaxAA * noise_scale atc.avPID.I = MixedConfig.avI_Scale * clampH(atc.MaxAA, 1) * noise_scale # atc.avPID.P = (1 + 8 / atc.MaxAA) * noise_scale # atc.avPID.D = clampH((clampL(abs(AM) / atc.MaxAA, 1) ** 0.1 - 1), 2) * noise_scale # avFilter.ratio = clamp((abs(atc.avPID.perror)+abs(atc.error/np.pi))/atc.InstantRatio*50, 0.1, 1) # AV = avFilter.EWA(atc.AV) # print avFilter.ratio AV = atc.AV update_pids(atc, AV)
def thrust_controller(vessel, deltaV): ''' This function is somewhat arbitrary in it's working - there's not a 'rule' that I've found on how to feather out throttle towards the end of a burn but given that the chances of overshooting go up with TWR (since we fly in a world with discrete physics frames!) it makes sense to relate the throttle to the TWR for this purpose. ''' if not vessel.available_thrust: vessel.control.activate_next_stage() if not vessel.available_thrust: vessel.control.activate_next_stage() time.sleep(0.1) TWR = vessel.available_thrust / vessel.mass min_throttle = 0.00 throttle = min_throttle + deltaV / TWR / 2 throttle = clamp(throttle) return throttle, TWR * throttle
def tune_steering_slow(atc, iErrf, imaxAA, AM): atc.atPID.P = 1 atc.atPID.I = 0 atc.atPID.D = 0 slowF = (1 + SlowConfig.SlowTorqueF / max(atc.engineF.acceleration, atc.engineF.deceleration)) noise_scale = clamp(abs(50 * (abs(atc.avPID.perror) + abs(atc.error / np.pi))) ** 0.5, 0.01, 1) if atc.MaxAA >= 1: atc.avPID.P = SlowConfig.avP_HighAA_Scale/slowF * noise_scale atc.avPID.D = clampL(SlowConfig.avD_HighAA_Intersect - SlowConfig.avD_HighAA_Inclination * atc.MaxAA, SlowConfig.avD_HighAA_Max) * noise_scale else: atc.avPID.P = SlowConfig.avP_LowAA_Scale/slowF * noise_scale atc.avPID.D = (SlowConfig.avD_LowAA_Intersect - SlowConfig.avD_LowAA_Inclination * atc.MaxAA) * noise_scale atc.avPID.I = SlowConfig.avI_Scale*clampH(atc.MaxAA, 1) * noise_scale # avFilter.ratio = clamp(abs(atc.AV)+abs(atc.error/np.pi)*500, 0.01, 1) # AV = avFilter.EWA(atc.AV) AV = atc.AV update_pids(atc, AV)
def _render_list(self, win, list, row_start, n_rows, col_start, n_cols, selected_i, is_active, centered=False): n_elems = len(list) start_entry_i = common.clamp(selected_i - n_rows/2, 0, max(n_elems-n_rows, 0)) end_entry_i = start_entry_i + n_rows display_list = list[start_entry_i:end_entry_i] for i, entry in enumerate(display_list): text = str(entry) if i == (selected_i-start_entry_i) and is_active: style = uc.A_BOLD | uc.A_STANDOUT else: style = uc.A_NORMAL self._render_text(win, row_start+i, col_start, text, n_cols, style, centered=centered)
async def max(self, ctx, *, arg=''): uid = ctx.author.id if uid not in self.raids: return raid = self.raids[uid] if raid.channel != ctx.channel: return await send_message( ctx, f'You may only adjust this in your active raid channel.', error=True) if not arg: return await send_message(ctx, f'Please enter a number, i.e. `.max 18`', error=True) try: n = int(arg) except ValueError: return await send_message(ctx, f'Please enter a number, i.e. `.max 18`', error=True) n = clamp(n, 3, 30 if raid.mode == QUEUE else 50) size = raid.pool.size() if n < size: return await send_message( ctx, f'You cannot go lower than the current pool size ({size}).', error=True) if raid.max_joins == n: return await send_message(ctx, f'That\'s... already the max.', error=True) verb = 'increased' if raid.max_joins < n else 'decreased' raid.max_joins = n await raid.update_channel_emoji() await ctx.send(f'Max participants {verb} to **{n}**.')
def tune_steering_mixed(atc, iErrf, imaxAA, AM): if atc.InstantRatio > 0.3: tune_steering_fast(MixedConfig3plus, atc, iErrf, imaxAA, AM) else: # if atc.MaxAA >= 1: # atc.atPID.P = 20 # atc.atPID.I = 0 # atc.atPID.D = 0 # atc.avPID.P = 1/clampL(abs(AM)/atc.MaxAA**0.2/12, 1) # atc.avPID.I = 0.002 # atc.avPID.D = 1.4/atc.MaxAA**0.3 # else: # atc.atPID.P = 40/clampL(abs(AM)/2, 1) # atc.atPID.I = 0 # atc.atPID.D = 0 # atc.avPID.P = 1/clampL(abs(AM)/12, 1) # atc.avPID.I = 0.001 # atc.avPID.D = 1.4/atc.MaxAA**0.3 # clampH(abs(40*(abs(atc.avPID.perror)+abs(atc.error/np.pi)))**6, 1) noise_scale = clamp((50 * (abs(atc.AV) + atc.errorF)) ** 0.6, 0.001, 1) atc.atPID.P = 1#clamp(0.5*atc.MaxAA**0.5, 0.0001, 1) atc.atPID.I = 0 atc.atPID.D = 0 atc.avPID.P = ((MixedConfig.avP_A / (atc.InstantRatio ** MixedConfig.avP_D + MixedConfig.avP_B) + MixedConfig.avP_C)) / clampL(abs(AM), 1) / atc.MaxAA * noise_scale atc.avPID.D = ((MixedConfig.avD_A / (atc.InstantRatio ** MixedConfig.avD_D + MixedConfig.avD_B) + MixedConfig.avD_C)) / atc.MaxAA * noise_scale atc.avPID.I = MixedConfig.avI_Scale * clampH(atc.MaxAA, 1) * noise_scale # atc.avPID.P = (1 + 8 / atc.MaxAA) * noise_scale # atc.avPID.D = clampH((clampL(abs(AM) / atc.MaxAA, 1) ** 0.1 - 1), 2) * noise_scale # avFilter.ratio = clamp((abs(atc.avPID.perror)+abs(atc.error/np.pi))/atc.InstantRatio*50, 0.1, 1) # AV = avFilter.EWA(atc.AV) # print avFilter.ratio AV = atc.AV update_pids(atc, AV)
def victoria2(coord: GeoCoord) -> MapCoord: lat, lon = coord.lat, coord.lon # the victoria 2 map is identical to the eu4 map, except AUS and south america are left unchanged. # (1) the poles are trimmed if not radians(-56) < lat < radians(72): return 1, 1 # (2) east siberia is stretched if radians(50) < lat and radians(154) < lon: lat += (lon - radians(154)) / 3 # (3) new zealand is moved northward, but less than eu4 if lat < radians(-33) and radians(165) < lon: lat += radians(4) # (4) greenland and iceland and jan mayen are moved northward if radians(-57) < lon < radians(-8) and radians(59) < lat: lat += radians(5) # (5) the americas are moved northward elif lon < radians(-34): lat += radians(13) y, x = miller(clamp(lat, -pi / 2, pi / 2), lon) y = remap(y, -32 / 90, 61 / 90, -1, 1) return MapCoord(x, y)
def ditherInertia(botId, dith, damp): if not S2R_DITHER: return # dither mass, inertia for j in range(1, 4): dinfo = p.getDynamicsInfo(botId, j) mass = cm.dither(dinfo[0], dith) x = cm.dither(dinfo[2][0], dith**2) y = cm.dither(dinfo[2][1], dith**2) moment = (x, y, y) damping = 0 if 1 == j: damping = cm.clamp(cm.dither(damp, dith), 0, .001) if 3 == j: y = cm.dither(dinfo[2][2], dith) moment = (x, x, y) p.changeDynamics(botId, j, mass=mass, localInertiaDiagonal=moment, jointDamping=damping)
async def send_confirm_prompt(self, ctx, options): async with self.lock: if self.confirmed or self.channel or self.wfr_message: return self.raid_name = options['raid_name'] self.mode = options['mode'] self.max_joins = clamp(options['max_joins'], 3, 30 if options['mode'] == QUEUE else 50) self.private = False self.locked = False self.desc = options.get('desc', None) self.emoji = options.get('emoji', None) # move to confirm and create # if self.mode == FFA and 'ffa' not in self.raid_name.lower(): # self.raid_name = f'''ffa {self.raid_name}''' msg, reactions = self.make_prompt_text() self.wfr_message = await ctx.send(msg) for reaction in reactions: await self.wfr_message.add_reaction(reaction.strip('<>')) self.bot.wfr[self.host_id] = self
def NextThrottle(dV, prev): dt = clamp(dV / 10, 0.5, 2) return clamp((dV / MaxThrust * M - prev * ThrustDecelerationTime) / dt, 0, 1)
def randTraj(action): if np.random.uniform(0, 1) > 0.0: action += cm.sgn() * round(np.random.uniform(0, 1)**3 * 4, 0) action = cm.clamp(action, 0, len(action_table) - 1) return int(action)
def update_pids(atc, AV): atc.atPID.update2(abs(atc.error), -np.sign(atc.error)*AV) avErr = abs(atc.atPID.action) * np.sign(atc.error) - AV atc.avPID.update(avErr) atc.avPID.action = avFilter.EWA(clamp(atc.avPID.action, -1, 1))
def update_pids(cfg, atc, iErrf): # avFilter.ratio = clamp(abs(atc.AV) + abs(atc.error / np.pi), 0.1, 1) # AV = avFilter.EWA(atc.AV) atc.atPID.update2(atc.error, -atc.AV) atc.atPID.action = clamp(atc.atPID.action, -1, 1)
def update_pids(atc, AV): atc.atPID.update2(abs(atc.error), -AV) avErr = abs(atc.atPID.action) * np.sign(atc.error) - AV atc.avPID.update(avErr) atc.avPID.action = avFilter.EWA(clamp(atc.avPID.action, -1, 1))
def update_pids(cfg, atc, iErrf): # avFilter.ratio = clamp(abs(atc.AV) + abs(atc.error / np.pi), 0.1, 1) # AV = avFilter.EWA(atc.AV) atc.atPID.update2(atc.error, -atc.AV) atc.atPID.action = clamp(atc.atPID.action, -1, 1)