def compute_strategy(self, state, id_team, id_player): m = MyState(state, id_team, id_player) act = Action(state, id_team, id_player) p = m.ball_position pos = Position(state, id_team, id_player) qui = Qui_a_la_balle(state, id_team, id_player) if qui.j_ai_la_balle: return m.aller(p) + SoccerAction(Vector2D( 3.14, 0), pos.joueur_le_plus_proche(1)) + act.passe_test( state, id_team, id_player) if qui.mon_equipe_a_la_b: return m.en_attente else: return Vector2D(20, 45) - m.my_position
def action(self, env: Environment) -> Action: new_env: Environment = self.env.new_env(env.observation.data, env.agents) key = self.counter_key(new_env) action: int = -1 policy = None action_by_value: int = -1 for tl in range(self.thinking_loop): if tl > 0 and self.logging_thinking: print( f"continue thinking: policy move=({action % 8}, {action // 8}), " f"value move=({action_by_value % 8}, {action_by_value // 8})" ) self.search_moves(env) policy = self.calc_policy(env) action = int(np.random.choice(range(self.labels_n), p=policy)) action_by_value = int( np.argmax(self.var_q[key] + (self.var_n[key] > 0) * 100)) if action == action_by_value or new_env.turn < self.change_tau_turn: break # this is for play_gui, not necessary when training. self.thinking_history[new_env.observation] = HistoryItem( action, policy, list(self.var_q[key]), list(self.var_n[key])) self.moves.append( [new_env.observation.record_encode(), policy.tolist()]) return Action(action)
def compute_strategy(self, state, id_team, id_player): act = Action(state, id_team, id_player) mystate = MyState(state, id_team, id_player) qui = Qui_a_la_balle(state, id_team, id_player) distance = mystate.distance_ball_player #distance entre la balle et le but distance_but = mystate.distance_but_ball if distance > 20: if (mystate.adv() == 2): position = Vector2D( (0.5 / 5) * GAME_WIDTH, state.ball.position.y) - mystate.my_position else: position = Vector2D( (3.5 / 5) * GAME_WIDTH, state.ball.position.y) - mystate.my_position else: position = state.ball.position - mystate.my_position #return SoccerAction(position,V-mystate.my_position) # if qui.mon_equipe_a_la_b(): # return mystate.en_attente # else: return SoccerAction(state.ball.position-state.player_state(id_team,id_player).position,\ Vector2D((2-id_team)*settings.GAME_WIDTH,settings.GAME_HEIGHT/2.)-state.ball.position)
def compute_strategy(self, state, id_team, id_player): qui = Qui_a_la_balle(state, id_team, id_player) act = Action(state, id_team, id_player) #if qui.j_ai_la_balle()==True: #p=act.passe_test(state,id_team,id_player) return SoccerAction(state.ball.position-state.player_state(id_team,id_player).position ,\ Vector2D((2-id_team)*settings.GAME_WIDTH,settings.GAME_HEIGHT/2.)-state.ball.position)
def compute_strategy(self, state, id_team, id_player): qui = Qui_a_la_balle(state, id_team, id_player) m = MyState(state, id_team, id_player) p = Position(state, id_team, id_player) act = Action(state, id_team, id_player) if m.distance_ball_player <= m.PR_BR: return SoccerAction( Vector2D(0, 0), shoot ) #+ SoccerAction(state.ball.position-state.player_state(id_team,id_player).position ,\
def calculateTunnelingRate(distance): print('Calculating transition rate for distance %0.2f' % distance) # calculate potential parameter lambda_ = distanceToParameter(distance) p = Potential(mu, lambda_) a = Action(tau, mass, p) m = Metropolis(N, a, borders=[-10, 10], hbar=hbar, tau=tau) tC = TransitionCounter() for value in m: tC.registerValue(value) return tC.getTransitions()
async def search_my_move(self, env: Environment, is_root_node=False): """ Q, V is value for this Player(always white). P is value for the player of next_player (black or white) :param env: :param is_root_node: :return: """ if env.done: # return env.winner if env.winner == 1: return 1 elif env.winner == 2: return -1 else: return 0 key = self.counter_key(env) while key in self.now_expanding: await asyncio.sleep(self.wait_for_expanding_sleep_sec) # is leaf? if key not in self.expanded: # reach leaf node leaf_v = await self.expand_and_evaluate(env) if env.current_agent_index() == 1: return leaf_v # Value for white else: return -leaf_v # Value for white == -Value for white action_t = self.select_action_q_and_u(env, is_root_node) _, _ = env.step(Action(action_t)) virtual_loss = self.virtual_loss self.var_n[key][action_t] += virtual_loss self.var_w[key][action_t] -= virtual_loss leaf_v = await self.search_my_move(env) # next move # on returning search path # update: N, W, Q, U n = self.var_n[key][ action_t] = self.var_n[key][action_t] - virtual_loss + 1 w = self.var_w[key][ action_t] = self.var_w[key][action_t] + virtual_loss + leaf_v self.var_q[key][action_t] = w / n return leaf_v
def compute_strategy(self, state, id_team, id_player): mystate = MyState(state, id_team, id_player) act = Action(state, id_team, id_player) pos = Position(state, id_team, id_player) angle = versOu(state, id_team, id_player) #distance avec la balle distance = mystate.distance_ball_player #distance entre la balle et le but distance_but = mystate.distance_but_ball V = angle.vers_les_but_adv if distance > 20: if (mystate.adv() == 2): position = V + Vector2D( (0.5 / 5) * GAME_WIDTH, state.ball.position.y) - mystate.my_position else: position = V + Vector2D( (3.5 / 5) * GAME_WIDTH, state.ball.position.y) - mystate.my_position else: position = state.ball.position - mystate.my_position return SoccerAction(position, V - mystate.my_position)
# from motion import * from tools import Action # read key from keyboard_read import * import wiringpi if __name__ == "__main__": # move top_speed = 100 turn_speed = 0.5 sens = 0.3 speed_rate = 0.5 ctrl = Action(top_speed, turn_speed, sens) ctrl.init_motor() while True: key = readkey() if key=='w': ctrl.go_forward(speed_rate) elif key=='s': ctrl.go_backward(speed_rate) elif key=='d': ctrl.turn_right() elif key=='a': ctrl.turn_left() elif key=='p': break # stop print("stop") ctrl.stop()
weights, z=ds, R=sensor_std_err, landmarks=landmarks) ##更新粒子权值 if (timer2 == 10): print("can't find mark and no update weight") if neff(weights) < N / 2: ##判断是否需要重采样 simple_resample(particles, weights) mu, var = estimate(particles, weights) D = max - mu Dis = round(D) xs.append(mu) print("Get the current car position as follows : %s" % mu) if (timer2 != 10): print("到mark%s的" % nowid) print('距离是%s' % Dis) if __name__ == '__main__': top_speed = 100 turn_speed = 0.5 sens = 0.3 speed_rate = 0.5 ctrl = Action(top_speed, turn_speed, sens) ctrl.init_motor() sys.path.append("/home/pi/JingoLan/aruco") from MarkerDetector import MarkerDetector from tools import Action import math run_pf(N=5000)
import numpy as np # number of lattice positions N = 100000 # parameters mass = 0.01 mu = -10 lambda_ = -0.02 step = 0.1 tau = 0.1 hbar = 1 p = Potential(mu, lambda_) a = Action(tau, mass, p) m = Metropolis(N, a, borders=[-10, 10], hbar=hbar, tau=tau) vals = list(m) bins = np.arange(*m.borders, step=step) values = np.bincount(np.digitize(vals, bins), minlength=len(bins)) plt.figure() plt.errorbar(bins, values, yerr=np.sqrt(values), fmt='.') plt.xlim(*m.borders) plt.show()
def action(self, env: Environment) -> Action: return Action(int(input("Please input: ")))