def run_exercise(exercise): for i in range(exercise.repetition): for side in exercise.sides: player.pause() Coach.say(exercise.sentence(side=side)) player.play() countdown(exercise.default_time)
def __init__(self, key=False, secret=False, timeout=None, coach=True, jsonNums=False, proxies=None): """ key = str api key supplied by Poloniex secret = str secret hash supplied by Poloniex timeout = int time in sec to wait for an api response (otherwise 'requests.exceptions.Timeout' is raised) coach = bool to indicate if the api coach should be used jsonNums = datatype to use when parsing json ints and floats # Time Placeholders: (MONTH == 30*DAYS) self.MINUTE, self.HOUR, self.DAY, self.WEEK, self.MONTH, self.YEAR """ # set logger, coach, and proxies self.logger = logger self.coach = coach self.proxies = proxies if self.coach is True: self.coach = Coach() # create nonce self._nonce = int("{:.6f}".format(time()).replace('.', '')) # json number datatypes self.jsonNums = jsonNums # grab keys, set timeout self.key, self.secret, self.timeout = key, secret, timeout # set time labels self.MINUTE, self.HOUR, self.DAY = 60, 60 * 60, 60 * 60 * 24 self.WEEK, self.MONTH = self.DAY * 7, self.DAY * 30 self.YEAR = self.DAY * 365
def login_coach(): coach = Coach() email = request.get_json()["email"] password = request.get_json()["password"] if len(password) < 8: return jsonify({"response": "short"}) if not pyvalidator.email(email): return jsonify({"response": "invalid_email"}) return coach.login_coach(email, password)
def add_player(): coach = Coach() coach_username = request.form["coach_mail"] name = request.form["name"] dom_hand = request.form["dom_hand"] if 'image' not in request.files: return jsonify({"response": "no_photo"}) image = request.files["image"] return coach.register_player_for_coach(name, dom_hand, coach_username, image, app)
def run_workout(workout, debug=False): player.next() for e in workout.workout_exercises: e.created_date = datetime.now() e.time_per_set = e.exercise.default_time e.repetition = e.exercise.repetition if not debug: run_exercise(e.exercise) else: logging.info("QA MODE: Running exercise: %s" % e.exercise) time.sleep(1) Coach.say('Workout finished')
class Team(): "Team" def __init__(self, city, nickname,human=False,id=None): if id: self.id = id else: self.id = self.get_next_team_id() self.city = city self.nickname = nickname self.human_control = human self.skills = {'qb': float(randint(60,90)), 'rb': float(randint(60,90)), 'wr': float(randint(60,90)), 'ol': float(randint(60,90)), 'dl': float(randint(60,90)), 'lb': float(randint(60,90)), 'cb': float(randint(60,90)), 's': float(randint(60,90)), 'p': float(randint(60,90)), 'k': float(randint(60,90)), 'sp': float(randint(60,90))} self.home_field_advantage = float(randint(1,3)) self.playbook = Playbook() self.stats = StatBook() self.coach = Coach() self.coach.practice_plays(self.playbook,self.skills) self.primary_color = (randint(0,255),randint(0,255),randint(0,255)) self.secondary_color = (randint(0,255),randint(0,255),randint(0,255)) self.league_stats = { 'overall' : self._new_record_stats(), 'home' : self._new_record_stats(), 'away' : self._new_record_stats(), 'win_opp' : [], 'loss_opp' : [] } def _new_record_stats(self): return {'wins': 0, 'losses' : 0, 'ties' : 0, 'pct' : 0.0, 'points' : 0, 'opp' : 0} def get_next_team_id(self): global team_id team_id += 1 return team_id
def __init__(self): self.objectList = [] self.performance = 0 ode.World.__init__(self) self.coach = Coach() self.camera = Camera(0.0, 14.0, 19.5, -30.0, 0.0) self.objectList.append(self.camera) self.setGravity((0.0, -9.81, 0.0)) self.contactgroup = ode.JointGroup() self.space = ode.Space() self.accum = 0.0 self.time0 = 0 self.startTime = 0
def self_play(ctx): """Train by self-play, retraining from self-played frames and changing best player when new trained player beats currently best player. Args: ctx (click.core.Context): context object. Parameters for training: * 'game' (string) : game name (Default: tictactoe) * 'max_iter' (int) : number of train process iterations (Default: -1) * 'min_examples' (int) : minimum number of examples to start training nn, if -1 then no threshold. (Default: -1) * 'policy_warmup' (int) : how many stochastic warmup steps should take deterministic policy (Default: 12) * 'n_self_plays' (int) : number of self played episodes (Default: 100) * 'n_tournaments' (int) : number of tournament episodes (Default: 20) * 'save_checkpoint_folder' (string) : folder to save best models (Default: "checkpoints") * 'save_checkpoint_filename' (string) : filename of best model (Default: "best") * 'save_self_play_log_path' (string) : where to save self-play logs. (Default: "./logs/self-play.log") * 'save_tournament_log_path' (string) : where to save tournament logs. (Default: "./logs/tournament.log") * 'update_threshold' (float): : required threshold to be new best player (Default: 0.55) """ cfg = ctx.obj coach = Coach(cfg) # Create TensorBoard logger tb_logger = TensorBoardLogger( utils.create_tensorboard_log_dir(cfg.logging['tensorboard_log_folder'], 'score')) iteration = coach.global_epoch // cfg.training['epochs'] while cfg.self_play[ "max_iter"] == -1 or iteration < cfg.self_play["max_iter"]: iter_counter_str = "{:03d}/{:03d}".format(iteration + 1, cfg.self_play["max_iter"]) \ if cfg.self_play["max_iter"] > 0 else "{:03d}/inf".format(iteration + 1) coach.play("Self-play " + iter_counter_str) # Proceed to training only if threshold is fulfilled if len(coach.storage.big_bag) <= cfg.self_play["min_examples"]: log.warning("Skip training, gather minimum %d training examples!", cfg.self_play["min_examples"]) continue coach.train() coach.evaluate("Tournament " + iter_counter_str, tournament_mode=True) # Log current player's score tb_logger.log_scalar("Best score", coach.best_score, iteration) # Increment iterator iteration += 1
def store_all_cards(cls): client = gspread.authorize(creds) ws = client.open_by_key(cls.MASTERSHEET_ID) try: sheet = ws.worksheet(cls.MASTER_NAME) except gspread.exceptions.WorksheetNotFound: sheet = ws.add_worksheet(title=cls.MASTER_NAME, rows=100, cols=15) sheet.clear() cards = [] cards.append(cls.MASTER_LIST_HEADER) for coach in Coach.all(): for card in coach.collection: card["Coach"] = coach.name cards.append(card) cards_amount, keys_amount = len(cards), len(cls.MASTER_LIST_HEADER) cell_list = sheet.range( f"A1:{gspread.utils.rowcol_to_a1(cards_amount, keys_amount)}") for cell in cell_list: if cell.row == 1: cell.value = cards[cell.row - 1][cell.col - 1] else: cell.value = cards[cell.row - 1][cls.MASTER_LIST_HEADER[cell.col - 1]] sheet.update_cells(cell_list)
async def __run_newcoach(self): name = str(self.message.author) if Coach.exists(name): msg = LongMessage(self.client, self.message.channel) msg.add( f"**{self.message.author.mention}** account exists already\n") await msg.send() else: coach = Coach.load_coach(str(self.message.author)) coach.store_coach() # transaction is ok and coach is saved msg = LongMessage(self.client, self.message.channel) msg.add(f"**{self.message.author.mention}** account created\n") msg.add(f"**Bank:** {coach.account.cash} coins") msg.add(f"**Rules**: <{RULES_LINK}>") await msg.send()
def add_coach(self,name: str,age: int,style: str) -> None: """ Creates a new Coach instance with input. Team gets the coach instance assigned as their new coach. """ coach = Coach(style,name,age) self.__coach = coach
def __init__(self, name, window): self._name = name self._dis = Display(window) self._co = Coach() self._co.register(self, self._dis) self._dis.register(self, self._co) self._co.create_exercises() #TODO.WN091101 replace by storing Exerc.s self._calcs = None #pop !
def human_play(ctx, model_path, n_games): """Play `n_games` with trained model. Args: model_path: (string): Path to trained model. """ cfg = ctx.obj coach = Coach(cfg, model_path) coach.current_mind.players[1] = HumanPlayer(cfg.mdp) coach.eval_callbacks.append(BoardRender(cfg.env, render=True, fancy=True)) coach.scoreboard = Tournament() coach.evaluate(desc="Test models: Human vs. {}".format( model_path.split("/")[-1]), n_games=n_games) log.info("Human vs. %s results: %s", model_path.split("/")[-1], coach.scoreboard.results)
def register_coach(): coach = Coach() password = request.form["password"] email = request.form["email"] city = request.form["city"] name = request.form["name"] dom_hand = request.form["dom_hand"] club = request.form["club"] typ = request.form["type"] if 'image' not in request.files: return jsonify({"response": "no_photo"}) image = request.files["image"] if len(password) < 8: return jsonify({"response": "password_short"}) if not pyvalidator.email(email): return jsonify({"response": "invalid_email"}) return coach.register_coach(password, name, email, city, dom_hand, club, image, typ, app)
def update(team_member_id): """ Update a team member from the Team Roster by ID """ content = request.json try: if content["type"] == 'Player': member = Player(content['first_name'], content['last_name'], content['member_num'], content['annual_salary'], content['contract_years_length'], content['last_team'], 'Player', content['jersey_num'], content['position']) # roster_mgr.update(player) if content["type"] == 'Coach': member = Coach(content['first_name'], content['last_name'], content['member_num'], content['annual_salary'], content['contract_years_length'], content['last_team'], 'Coach', content['specialization'], content['is_former_player']) member.id = team_member_id roster_mgr.update(member) response = app.response_class(status=200) return response except KeyError as err: response = app.response_class( response= 'Team member object is invalid. Team member with ID: %d has type: \'%s\'. %s must be inputted.' % (team_member_id, roster_mgr.get(team_member_id).type, err), status=400) return response except ValueError as err: response = app.response_class(response=str(err), status=400) return response except (AttributeError, UnboundLocalError) as err: response = app.response_class( response='Team member with an ID: %d does not exist. %s' % (team_member_id, err), status=404) return response
def clash(ctx, first_model_path, second_model_path, render, n_games): """Test two models. Play `n_games` between themselves. Args: first_model_path: (string): Path to player one model. second_model_path (string): Path to player two model. """ cfg = ctx.obj coach = Coach(cfg, current_ckpt=first_model_path, best_ckpt=second_model_path) coach.scoreboard = Tournament() coach.evaluate(desc="Test models: {} vs {}".format( first_model_path.split("/")[-1], second_model_path.split("/")[-1]), render_mode=render, n_games=n_games) log.info("%s vs %s results: %s", first_model_path.split("/")[-1], second_model_path.split("/")[-1], coach.scoreboard.results)
def __init__(self, game, name, s_size, a_size, trainer, model_path, global_episodes): self.name = "worker_" + str(name) self.number = name self.model_path = model_path self.trainer = trainer self.global_episodes = global_episodes self.increment = self.global_episodes.assign_add(1) self.episode_rewards = [] self.episode_lengths = [] self.episode_mean_values = [] self.summary_writer = tf.summary.FileWriter("train_" + str(self.number)) self.coach = Coach() #Create the local copy of the network and the tensorflow op to copy global paramters to local network self.local_AC = AC_Network(s_size, a_size, self.name, trainer) self.update_local_ops = update_target_graph('global', self.name) self.actions = np.identity(a_size, dtype=bool).tolist() self.car = game
class Session: """ for stand-alone usage by the ox-user. ?for collaborative sessions see CoSession """ print("DEL import Session") def __init__(self, name, window): self._name = name self._dis = Display(window) self._co = Coach() self._co.register(self, self._dis) self._dis.register(self, self._co) self._co.create_exercises() #TODO.WN091101 replace by storing Exerc.s self._calcs = None #pop ! def run(self): """as long as user does exercises""" #print("in Session.run") self._co.request_exercise() self._dis.init_calc() #TODOWN091101 take Exercise as argument def notify(self, (msg, data)): '''called by the observed objects''' #print('in Session.notify: msg=,data=', msg, data) if msg == 'setting-done': # from Coach self._ex = data self._calcs = data._generate_calcs() self._key = data.get_topic() (self._calcs).reverse() _calc = (self._calcs).pop() #print('in Session.notify: calc=', _calc) _lines, self._input = data.format(_calc) self._dis.display_calc(_lines) self._curr_in = self._input.pop() #need _curr_in in notify self._dis.create_entryline(self._curr_in) # create_entryline sets the callback from gtk to Display if msg == 'digit-done': # from Display #print('in Session.notify, digit-done: _input=', self._input) (lino, pos, dig, proterr, protok, li) = self._curr_in self._dis.create_entryline((lino, -1, dig, proterr, protok, li)) if len(self._input) > 0: self._curr_in = self._input.pop() self._dis.create_entryline(self._curr_in) else: # start new calc self._dis.show_progress() if len(self._calcs) > 0: _calc = (self._calcs).pop() print('in Session.notify: calc=', _calc) _lines, self._input = self._ex.format(_calc) self._dis.display_calc(_lines) self._curr_in = self._input.pop() #need _curr_in in notify self._dis.create_entryline(self._curr_in) # create_entryline sets the callback from gtk to Display else: self._dis.finish_calc()
async def __run_list(self): coach = Coach.load_coach(str(self.message.author)) order = False if "bydate" in self.message.content else True msg = LongMessage(self.client, self.message.author) msg.add(f"**Bank:** {coach.account.cash} coins\n") msg.add("**Collection**:\n") msg.add("-" * 65 + "") msg.add( f"{self.__class__.format_pack(coach.collection_with_count(),order)}" ) msg.add("-" * 65 + "\n") await msg.send() await self.client.send_message(self.message.channel, "Collection sent to PM")
def getAllCoaches(suppressOutput=True, min_year_active=2004): coaches = dict() glsoup = getSoupFromURL('https://www.basketball-reference.com/coaches/', suppressOutput) all_rows = glsoup.find("table", id="coaches").find("tbody").find_all("tr") for row in all_rows: coach = row.find("th", attrs={"data-stat": "coach", "scope": "row"}) if coach is None: continue coach = coach.find("a") name = coach.get_text() last_year_active_soup = row.find("td", attrs={"data-stat": "year_max"}) last_year_active = int(last_year_active_soup.get_text()) try: if last_year_active >= min_year_active: coaches[name] = Coach(name, 'https://www.basketball-reference.com' + coach.attrs['href']) except Exception as e: print("ERROR:", e) sleep(1) # sleeping to be kind for requests return coaches
async def __run_genpack(self): if self.__class__.check_gen_command(self.cmd): ptype = self.args[1] if ptype == "player": team = self.args[2] pack = Pack(ptype, team=team) pack.generate() elif ptype == "training": pack = Pack(ptype) pack.generate() elif ptype == "booster": ptype = "booster_budget" if len( self.args) < 3 else f"booster_{self.args[2]}" pack = Pack(ptype) pack.generate() # add error handling eventually coach = Coach.load_coach(str(self.message.author)) t = Transaction(pack, pack.price) try: coach.make_transaction(t) except TransactionError as e: await self.transaction_error(e) return else: # transaction is ok and coach is saved msg = LongMessage(self.client, self.message.channel) msg.add( f"**{pack.description()}** for **{self.message.author}** - **{pack.price}** coins:\n" ) msg.add(f"{self.__class__.format_pack(pack.cards)}\n") msg.add(f"**Bank:** {coach.account.cash} coins") await msg.send() # export ImperiumSheet.store_all_cards() else: await self.client.send_message(self.message.channel, self.__class__.gen_help())
def train(ctx, checkpoint, save_dir, tensorboard): """Train NN from passed configuration.""" cfg = ctx.obj coach = Coach(cfg, checkpoint) # Create TensorBoard logging callback if enabled if tensorboard: coach.train_callbacks = [ TensorBoard(log_dir=utils.create_tensorboard_log_dir( cfg.logging['tensorboard_log_folder'], 'train')) ] else: coach.train_callbacks = [] coach.train() # Save model checkpoint if path passed if save_dir: save_fname = utils.create_checkpoint_file_name('train', cfg.self_play["game"], coach.global_epoch, coach.best_score) coach.current_nn.save_checkpoint(save_dir, save_fname)
def main(): log.info('Loading %s...', Game.__name__) g = Game(4, 9, 4) log.info('Loading %s...', nn.__name__) nnet = nn(g) if args.load_model: log.info('Loading checkpoint "%s/%s"...', args.load_folder_file[0], args.load_folder_file[1]) nnet.load_checkpoint(args.load_folder_file[0], args.load_folder_file[1]) else: log.warning('Not loading a checkpoint!') log.info('Loading the Coach...') c = Coach(g, nnet, args) if args.load_model: log.info("Loading 'trainExamples' from file...") c.loadTrainExamples() log.info('Starting the learning process 🎉') c.learn()
class GameWorld(ode.World): def __init__(self): self.objectList = [] self.performance = 0 ode.World.__init__(self) self.coach = Coach() self.camera = Camera(0.0, 14.0, 19.5, -30.0, 0.0) self.objectList.append(self.camera) self.setGravity((0.0, -9.81, 0.0)) self.contactgroup = ode.JointGroup() self.space = ode.Space() self.accum = 0.0 self.time0 = 0 self.startTime = 0 def launchClock(self): self.time0 = CLOCK() def applyInputs(self): glfwDisable(GLFW_STICKY_MOUSE_BUTTONS) if not self.coach.gameOver: for object in self.objectList: object.applyInputs(self.performance) if (gameglobals.gameState == 1 or gameglobals.gameState == 2) and glfwGetKey(GLFW_KEY_ESC): gameglobals.gameState = 0 def draw(self): glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glMatrixMode(GL_MODELVIEW) glLoadIdentity() self.camera.draw() glLightfv(GL_LIGHT0, GL_POSITION, (0.0, 17.0, 10.0, 1.0)) for part in self.objectList: glPushMatrix() part.draw() glPopMatrix() self.coach.draw(self.performance) def addObject(self, object): if 'Player' in object.geom.name: self.coach.register(object) if 'Ball' in object.geom.name: self.camera.setTarget(object) self.coach.ball = object self.objectList.append(object) def idle(self): gameWorld = self newTime = CLOCK() deltaTime = newTime - gameWorld.time0 self.accum += deltaTime p = deltaTime while self.accum >= 1.0/60.0: gameWorld.performance = 1.0/60.0 #deltaTime gameWorld.applyInputs() c = gameWorld.coach c.objectList = [] gameWorld.space.collide((gameWorld, gameWorld.contactgroup), near_callback) c.manageGame() gameWorld.step(1.0/60.0) self.accum -= 1.0/60.0 gameWorld.performance = p gameWorld.draw() gameWorld.contactgroup.empty() glfwSwapBuffers() gameWorld.time0 = newTime framerate()
def countdown(exc_time): for sec in range(exc_time): logging.info("%d seconds until the next exercise" % (exc_time - sec)) if exc_time - sec == 10: Coach.say("10 Seconds left") time.sleep(1)
def add_coach(raw_coach_query_list): """Takes in a list of split data by space""" stripped_list = [element.strip() for element in raw_coach_query_list] raw_coach_query_list = stripped_list # validation for coach_id coach_id = raw_coach_query_list[1] letters = sum(char.isalpha() for char in coach_id) if letters > 7: print('invalid number of characters in coach_id. MAX is 7') return numbers = sum(char.isdigit() for char in coach_id) if numbers > 2: print('invalid number of numbers in coach_id. MAX is 2') return # validation for year. season_year = raw_coach_query_list[2] numbers = sum(char.isdigit() for char in season_year) if numbers != 4: print('invalid number of numbers in coach_id. Must be 4 digit number') return # processing of first name -> removing any + characters first_name = raw_coach_query_list[3] first_name = first_name.replace('+', ' ') # processing of last name -> removing any + characters last_name = raw_coach_query_list[4] last_name = last_name.replace('+', ' ') # processing of the rest of data wins/losses try: season_win = int(raw_coach_query_list[5]) except: print("You must pass in a valid integer to season win!") return finally: if season_win < 0: print('Invalid. Season win must be >= 0') try: season_loss = int(raw_coach_query_list[6]) except: print("You must pass in a valid integer to season loss.") finally: if season_loss < 0: print('Invalid. Season loss must be >= 0') try: playoff_win = int(raw_coach_query_list[7]) except: print("You must pass in a valid integer to playoff win!") finally: if playoff_win < 0: print('Invalid. Playoff win must be >= 0') try: playoff_loss = int(raw_coach_query_list[8]) except: print("You must pass in a valid integer to playoff loss!") finally: if playoff_loss < 0: print('Invalid. Playoff loss must be >= 0') # processing of team team = raw_coach_query_list[9] # append the added coach to the table (list) of coaches) coach = Coach(coach_id, season_year, first_name, last_name, season_win, season_loss, playoff_win, playoff_loss, team) coach_list.append(coach)
from coach import Coach from controllers import Agent_Controller from agents import RandomAgent import torch import gym import boardgame2 import torch import numpy N_AGENTS = 1 N_EPISODES = 10 N_EPOCHS = 1 coach = Coach(env="CartPole-v0", loss_fn=Coach.reinforce, optim=torch.optim.Adam, n_agents=N_AGENTS ) target_agent = RandomAgent(actions=coach.actions) agent = Agent_Controller( n_agents=N_AGENTS, agent=target_agent ) for epoch in range(N_EPOCHS): for episode in range(N_EPISODES): returns = coach.run_episode(agent, return_tau=True, render=True) print(returns)
async def __run_admin(self): # if not started from admin-channel if not self.__class__.is_private_admin_channel(self.message.channel): emsg = "Insuficient rights" logger.error(emsg) await self.client.send_message(self.message.channel, emsg) return #adminlist cmd if self.message.content.startswith('!adminlist'): # require username argument if len(self.args) == 1: emsg = "Username missing" await self.client.send_message(self.message.channel, emsg) return coaches = Coach.find_by_name(self.args[1]) msg = LongMessage(self.client, self.message.channel) if len(coaches) == 0: msg.add("No coaches found") for coach in coaches: msg.add(f"Coach **{coach.name}**\n") msg.add(f"**Bank:** {coach.account.cash} coins\n") msg.add("**Collection**:") msg.add("-" * 65 + "") msg.add( f"{self.__class__.format_pack(coach.collection_with_count())}" ) msg.add("-" * 65 + "\n") await msg.send() return if self.message.content.startswith('!adminbank'): # require username argument if len(self.args) < 4: emsg = "Not enough arguments!!!\n" await self.client.send_message(self.message.channel, emsg) await self.client.send_message(self.message.channel, self.__class__.adminbank_help()) return # amount must be int if not RepresentsInt(self.args[1]): emsg = "<amount> is not whole number!!!\n" await self.client.send_message(self.message.channel, emsg) return # find coach coaches = Coach.find_by_name(self.args[2]) if len(coaches) == 0: emsg = f"Coach __{self.args[2]}__ not found!!!\n" await self.client.send_message(self.message.channel, emsg) return if len(coaches) > 1: emsg = f"Coach __{self.args[2]}__ not **unique**!!!\n" emsg += "Select one: " for coach in coaches: emsg += coach.name emsg += " " await self.client.send_message(self.message.channel, emsg) return amount = int(self.args[1]) coach = coaches[0] reason = ' '.join( str(x) for x in self.message.content.split(" ") [3:]) + " - updated by " + str(self.message.author) t = Transaction(reason, -1 * amount) try: coach.make_transaction(t) except TransactionError as e: await self.transaction_error(e) return else: msg = LongMessage(self.client, self.message.channel) msg.add( f"Bank for {coach.name} updated to **{coach.account.cash}** coins:\n" ) msg.add(f"Note: {reason}\n") msg.add(f"Change: {amount} coins") await msg.send()
def show_player(): coach = Coach() coach_username = request.get_json()["coach_mail"] return coach.show_players(coach_username)
class Poloniex(object): """The Poloniex Object!""" def __init__(self, key=False, secret=False, timeout=None, coach=True, jsonNums=False, proxies=None): """ key = str api key supplied by Poloniex secret = str secret hash supplied by Poloniex timeout = int time in sec to wait for an api response (otherwise 'requests.exceptions.Timeout' is raised) coach = bool to indicate if the api coach should be used jsonNums = datatype to use when parsing json ints and floats # Time Placeholders: (MONTH == 30*DAYS) self.MINUTE, self.HOUR, self.DAY, self.WEEK, self.MONTH, self.YEAR """ # set logger, coach, and proxies self.logger = logger self.coach = coach self.proxies = proxies if self.coach is True: self.coach = Coach() # create nonce self._nonce = int("{:.6f}".format(time()).replace('.', '')) # json number datatypes self.jsonNums = jsonNums # grab keys, set timeout self.key, self.secret, self.timeout = key, secret, timeout # set time labels self.MINUTE, self.HOUR, self.DAY = 60, 60 * 60, 60 * 60 * 24 self.WEEK, self.MONTH = self.DAY * 7, self.DAY * 30 self.YEAR = self.DAY * 365 # -----------------Meat and Potatos--------------------------------------- def _retry(func): """ retry decorator """ @_wraps(func) def retrying(*args, **kwargs): problems = [] for delay in _chain(retryDelays, [None]): try: # attempt call return func(*args, **kwargs) # we need to try again except RequestException as problem: problems.append(problem) if delay is None: logger.debug(problems) raise RetryException('retryDelays exhausted ' + str(problem)) else: # log exception and wait logger.debug(problem) logger.info("-- delaying for %ds", delay) sleep(delay) return retrying @_retry def __call__(self, command, args={}): """ Main Api Function - encodes and sends <command> with optional [args] to Poloniex api - raises 'poloniex.PoloniexError' if an api key or secret is missing (and the command is 'private'), if the <command> is not valid, or if an error is returned from poloniex.com - returns decoded json api message """ # get command type cmdType = self._checkCmd(command) # pass the command args['command'] = command payload = {} # add timeout payload['timeout'] = self.timeout # private? if cmdType == 'Private': payload['url'] = 'https://poloniex.com/tradingApi' # wait for coach if self.coach: self.coach.wait() # set nonce print('nonce: ' + str(self.nonce)) args['nonce'] = self.nonce # add args to payload payload['data'] = args # sign data with our Secret sign = _new(self.secret.encode('utf-8'), _urlencode(args).encode('utf-8'), _sha512) # add headers to payload payload['headers'] = {'Sign': sign.hexdigest(), 'Key': self.key} # add proxies if needed if self.proxies: payload['proxies'] = self.proxies # send the call print(payload) ret = _post(**payload) # return data return self._handleReturned(ret.text) # public? if cmdType == 'Public': # encode url payload['url'] = 'https://poloniex.com/public?' + _urlencode(args) # wait for coach if self.coach: self.coach.wait() # add proxies if needed if self.proxies: payload['proxies'] = self.proxies # send the call ret = _get(**payload) # return data return self._handleReturned(ret.text) @property def nonce(self): """ Increments the nonce""" print('a kuku: ' + str(self._nonce)) self._nonce += 42 return self._nonce def _checkCmd(self, command): """ Returns if the command is private of public, raises PoloniexError if command is not found """ global PUBLIC_COMMANDS, PRIVATE_COMMANDS if command in PRIVATE_COMMANDS: # check for keys if not self.key or not self.secret: raise PoloniexError("An Api Key and Secret needed!") return 'Private' if command in PUBLIC_COMMANDS: return 'Public' raise PoloniexError("Invalid Command!: %s" % command) def _handleReturned(self, data): """ Handles returned data from poloniex""" try: if not self.jsonNums: out = _loads(data, parse_float=str) else: out = _loads(data, parse_float=self.jsonNums, parse_int=self.jsonNums) except: self.logger.error(data) raise PoloniexError('Invalid json response returned') # check if poloniex returned an error if 'error' in out: # update nonce if we fell behind if "Nonce must be greater" in out['error']: self._nonce = int(out['error'].split('.')[0].split()[-1]) # raise RequestException so we try again raise RequestException('PoloniexError ' + out['error']) # conncetion timeout from poloniex if "please try again" in out['error'].lower(): # raise RequestException so we try again raise RequestException('PoloniexError ' + out['error']) # raise other poloniex errors, ending retry loop else: raise PoloniexError(out['error']) return out # --PUBLIC COMMANDS------------------------------------------------------- def returnTicker(self): """ Returns the ticker for all markets. """ return self.__call__('returnTicker') def return24hVolume(self): """ Returns the 24-hour volume for all markets, plus totals for primary currencies. """ return self.__call__('return24hVolume') def returnOrderBook(self, currencyPair='all', depth=20): """ Returns the order book for a given market as well as a sequence number for use with the Push API and an indicator specifying whether the market is frozen. (defaults to 'all' markets, at a 'depth' of 20 orders) """ return self.__call__('returnOrderBook', { 'currencyPair': str(currencyPair).upper(), 'depth': str(depth) }) @_retry def marketTradeHist(self, currencyPair, start=False, end=False): """ Returns the past 200 trades for a given market, or up to 50,000 trades between a range specified in UNIX timestamps by the "start" and "end" parameters. """ if self.coach: self.coach.wait() args = { 'command': 'returnTradeHistory', 'currencyPair': str(currencyPair).upper() } if start: args['start'] = start if end: args['end'] = end ret = _get('https://poloniex.com/public?' + _urlencode(args), timeout=self.timeout) # decode json return self._handleReturned(ret.text) def returnChartData(self, currencyPair, period=False, start=False, end=False): """ Returns candlestick chart data. Parameters are "currencyPair", "period" (candlestick period in seconds; valid values are 300, 900, 1800, 7200, 14400, and 86400), "start", and "end". "Start" and "end" are given in UNIX timestamp format and used to specify the date range for the data returned (default date range is start='1 day ago' to end='now') """ if period not in [300, 900, 1800, 7200, 14400, 86400]: raise PoloniexError("%s invalid candle period" % str(period)) if not start: start = time() - self.DAY if not end: end = time() return self.__call__( 'returnChartData', { 'currencyPair': str(currencyPair).upper(), 'period': str(period), 'start': str(start), 'end': str(end) }) def returnCurrencies(self): """ Returns information about all currencies. """ return self.__call__('returnCurrencies') def returnLoanOrders(self, currency): """ Returns the list of loan offers and demands for a given currency, specified by the "currency" parameter """ return self.__call__('returnLoanOrders', {'currency': str(currency).upper()}) # --PRIVATE COMMANDS------------------------------------------------------ def returnBalances(self): """ Returns all of your available balances.""" return self.__call__('returnBalances') def returnCompleteBalances(self, account='all'): """ Returns all of your balances, including available balance, balance on orders, and the estimated BTC value of your balance. By default, this call is limited to your exchange account; set the "account" parameter to "all" to include your margin and lending accounts. """ return self.__call__('returnCompleteBalances', {'account': str(account)}) def returnDepositAddresses(self): """ Returns all of your deposit addresses. """ return self.__call__('returnDepositAddresses') def generateNewAddress(self, currency): """ Generates a new deposit address for the currency specified by the "currency" parameter. """ return self.__call__('generateNewAddress', {'currency': currency}) def returnDepositsWithdrawals(self, start=False, end=False): """ Returns your deposit and withdrawal history within a range, specified by the "start" and "end" parameters, both of which should be given as UNIX timestamps. (defaults to 1 month)""" if not start: start = time() - self.MONTH if not end: end = time() args = {'start': str(start), 'end': str(end)} return self.__call__('returnDepositsWithdrawals', args) def returnOpenOrders(self, currencyPair='all'): """ Returns your open orders for a given market, specified by the "currencyPair" parameter, e.g. "BTC_XCP". Set "currencyPair" to "all" to return open orders for all markets. """ return self.__call__('returnOpenOrders', {'currencyPair': str(currencyPair).upper()}) def returnTradeHistory(self, currencyPair='all', start=False, end=False, limit=None): """ Returns your trade history for a given market, specified by the "currencyPair" POST parameter. You may specify "all" as the currencyPair to receive your trade history for all markets. You may optionally specify a range via "start" and/or "end" POST parameters, given in UNIX timestamp format; if you do not specify a range, it will be limited to one day. You may optionally limit the number of entries returned using the "limit" parameter, up to a maximum of 10,000. If the "limit" parameter is not specified, no more than 500 entries will be returned. """ args = {'currencyPair': str(currencyPair).upper()} if start: args['start'] = start if end: args['end'] = end if limit: args['limit'] = limit return self.__call__('returnTradeHistory', args) def returnOrderTrades(self, orderNumber): """ Returns all trades involving a given order, specified by the "orderNumber" parameter. If no trades for the order have occurred or you specify an order that does not belong to you, you will receive an error. """ return self.__call__('returnOrderTrades', {'orderNumber': str(orderNumber)}) def buy(self, currencyPair, rate, amount, orderType=False): """ Places a limit buy order in a given market. Required parameters are "currencyPair", "rate", and "amount". You may optionally set "orderType" to "fillOrKill", "immediateOrCancel" or "postOnly". A fill-or-kill order will either fill in its entirety or be completely aborted. An immediate-or-cancel order can be partially or completely filled, but any portion of the order that cannot be filled immediately will be canceled rather than left on the order book. A post-only order will only be placed if no portion of it fills immediately; this guarantees you will never pay the taker fee on any part of the order that fills. If successful, the method will return the order number. """ args = { 'currencyPair': str(currencyPair).upper(), 'rate': str(rate), 'amount': str(amount), } # order type specified? if orderType: possTypes = ['fillOrKill', 'immediateOrCancel', 'postOnly'] # check type if not orderType in possTypes: raise PoloniexError('Invalid orderType') args[orderType] = 1 return self.__call__('buy', args) def sell(self, currencyPair, rate, amount, orderType=False): """ Places a sell order in a given market. Parameters and output are the same as for the buy method. """ args = { 'currencyPair': str(currencyPair).upper(), 'rate': str(rate), 'amount': str(amount), } # order type specified? if orderType: possTypes = ['fillOrKill', 'immediateOrCancel', 'postOnly'] # check type if not orderType in possTypes: raise PoloniexError('Invalid orderType') args[orderType] = 1 return self.__call__('sell', args) def cancelOrder(self, orderNumber): """ Cancels an order you have placed in a given market. Required parameter is "orderNumber". """ return self.__call__('cancelOrder', {'orderNumber': str(orderNumber)}) def moveOrder(self, orderNumber, rate, amount=False, orderType=False): """ Cancels an order and places a new one of the same type in a single atomic transaction, meaning either both operations will succeed or both will fail. Required parameters are "orderNumber" and "rate"; you may optionally specify "amount" if you wish to change the amount of the new order. "postOnly" or "immediateOrCancel" may be specified as the "orderType" param for exchange orders, but will have no effect on margin orders. """ args = {'orderNumber': str(orderNumber), 'rate': str(rate)} if amount: args['amount'] = str(amount) # order type specified? if orderType: possTypes = ['immediateOrCancel', 'postOnly'] # check type if not orderType in possTypes: raise PoloniexError('Invalid orderType: %s' % str(orderType)) args[orderType] = 1 return self.__call__('moveOrder', args) def withdraw(self, currency, amount, address, paymentId=False): """ Immediately places a withdrawal for a given currency, with no email confirmation. In order to use this method, the withdrawal privilege must be enabled for your API key. Required parameters are "currency", "amount", and "address". For XMR withdrawals, you may optionally specify "paymentId". """ args = { 'currency': str(currency).upper(), 'amount': str(amount), 'address': str(address) } if paymentId: args['paymentId'] = str(paymentId) return self.__call__('withdraw', args) def returnFeeInfo(self): """ If you are enrolled in the maker-taker fee schedule, returns your current trading fees and trailing 30-day volume in BTC. This information is updated once every 24 hours. """ return self.__call__('returnFeeInfo') def returnAvailableAccountBalances(self, account=False): """ Returns your balances sorted by account. You may optionally specify the "account" parameter if you wish to fetch only the balances of one account. Please note that balances in your margin account may not be accessible if you have any open margin positions or orders. """ if account: return self.__call__('returnAvailableAccountBalances', {'account': account}) return self.__call__('returnAvailableAccountBalances') def returnTradableBalances(self): """ Returns your current tradable balances for each currency in each market for which margin trading is enabled. Please note that these balances may vary continually with market conditions. """ return self.__call__('returnTradableBalances') def transferBalance(self, currency, amount, fromAccount, toAccount, confirmed=False): """ Transfers funds from one account to another (e.g. from your exchange account to your margin account). Required parameters are "currency", "amount", "fromAccount", and "toAccount" """ args = { 'currency': str(currency).upper(), 'amount': str(amount), 'fromAccount': str(fromAccount), 'toAccount': str(toAccount) } if confirmed: args['confirmed'] = 1 return self.__call__('transferBalance', args) def returnMarginAccountSummary(self): """ Returns a summary of your entire margin account. This is the same information you will find in the Margin Account section of the Margin Trading page, under the Markets list """ return self.__call__('returnMarginAccountSummary') def marginBuy(self, currencyPair, rate, amount, lendingRate=2): """ Places a margin buy order in a given market. Required parameters are "currencyPair", "rate", and "amount". You may optionally specify a maximum lending rate using the "lendingRate" parameter (defaults to 2). If successful, the method will return the order number and any trades immediately resulting from your order. """ return self.__call__( 'marginBuy', { 'currencyPair': str(currencyPair).upper(), 'rate': str(rate), 'amount': str(amount), 'lendingRate': str(lendingRate) }) def marginSell(self, currencyPair, rate, amount, lendingRate=2): """ Places a margin sell order in a given market. Parameters and output are the same as for the marginBuy method. """ return self.__call__( 'marginSell', { 'currencyPair': str(currencyPair).upper(), 'rate': str(rate), 'amount': str(amount), 'lendingRate': str(lendingRate) }) def getMarginPosition(self, currencyPair='all'): """ Returns information about your margin position in a given market, specified by the "currencyPair" parameter. You may set "currencyPair" to "all" if you wish to fetch all of your margin positions at once. If you have no margin position in the specified market, "type" will be set to "none". "liquidationPrice" is an estimate, and does not necessarily represent the price at which an actual forced liquidation will occur. If you have no liquidation price, the value will be -1. (defaults to 'all')""" return self.__call__('getMarginPosition', {'currencyPair': str(currencyPair).upper()}) def closeMarginPosition(self, currencyPair): """ Closes your margin position in a given market (specified by the "currencyPair" parameter) using a market order. This call will also return success if you do not have an open position in the specified market. """ return self.__call__('closeMarginPosition', {'currencyPair': str(currencyPair).upper()}) def createLoanOffer(self, currency, amount, lendingRate, autoRenew=0, duration=2): """ Creates a loan offer for a given currency. Required parameters are "currency", "amount", "lendingRate", "duration" (num of days, defaults to 2), "autoRenew" (0 or 1, defaults to 0 'off'). """ return self.__call__( 'createLoanOffer', { 'currency': str(currency).upper(), 'amount': str(amount), 'duration': str(duration), 'autoRenew': str(autoRenew), 'lendingRate': str(lendingRate) }) def cancelLoanOffer(self, orderNumber): """ Cancels a loan offer specified by the "orderNumber" parameter. """ return self.__call__('cancelLoanOffer', {'orderNumber': str(orderNumber)}) def returnOpenLoanOffers(self): """ Returns your open loan offers for each currency. """ return self.__call__('returnOpenLoanOffers') def returnActiveLoans(self): """ Returns your active loans for each currency.""" return self.__call__('returnActiveLoans') def returnLendingHistory(self, start=False, end=False, limit=False): """ Returns your lending history within a time range specified by the "start" and "end" parameters as UNIX timestamps. "limit" may also be specified to limit the number of rows returned. (defaults to the last months history)""" if not start: start = time() - self.MONTH if not end: end = time() args = {'start': str(start), 'end': str(end)} if limit: args['limit'] = str(limit) return self.__call__('returnLendingHistory', args) def toggleAutoRenew(self, orderNumber): """ Toggles the autoRenew setting on an active loan, specified by the "orderNumber" parameter. If successful, "message" will indicate the new autoRenew setting. """ return self.__call__('toggleAutoRenew', {'orderNumber': str(orderNumber)})
'--load_model', dest='load_model', action='store_true') parser.add_argument('-loadf', '--load_folder_file', dest='load_folder_file', type=str) parser.add_argument('-iterexamp', '--num_iters_example', dest='numItersForTrainExamplesHistory', type=int, default=20) args = parser.parse_args() fh = open(os.path.join("..", "data", "puzzle1.txt")) fcontent = fh.read() fh.close() sys.setrecursionlimit(10000) g = game(fcontent) nnet = nn(g, args) if args.load_model: nnet.load_checkpoint(args.load_folder_file) c = Coach(g, nnet, args) if args.load_model: print("Load trainExamples from file") c.loadTrainExamples() c.learn()
import torch import gym import boardgame2 import torch import numpy discount = 0.9 batch_size = 512 n_agents = 1 update_interval = 25 games = 20000 rate = 0.001 coach = Coach(env="CartPole-v0", loss_fn=Coach.reinforce, optim=torch.optim.RMSprop, n_agents=n_agents, flatten=True, ) agent = Softmax_Agent(model= torch.nn.Sequential(torch.nn.Linear(8,32), torch.nn.Dropout(0.06), torch.nn.Tanh(), torch.nn.Linear(32,32), torch.nn.Dropout(0.06), torch.nn.Tanh(), torch.nn.Linear(32, coach.actions), ), batch=batch_size, n_agents=n_agents )
game = Checkers() dqn = DQN() NUM_EPOCHS = 80 NUM_TRAIN_GAMES = 200 NUM_EVAL_GAMES = 20 HALF = int(NUM_EVAL_GAMES/2) TRAIN_EPSILON = 0.4 GAMMA = 0.8 EVAL_EPSILON = 0.01 LR = 0.0001 PATH = "models/test.pt" coach = Coach( game=game, nnet=dqn, lr=LR) num_improvements = 0 for _ in range(NUM_EPOCHS): ## Saving a copy of the current network weights to pnet torch.save(coach.nnet.state_dict(), PATH) coach.pnet.load_state_dict(torch.load(PATH)) for _ in tqdm(range(NUM_TRAIN_GAMES), desc="Self play"): data = coach.execute_episode( epsilon=TRAIN_EPSILON, gamma=GAMMA) data = torch.stack(data) coach.learn(data)
from RULEngine.Framework import Framework from coach import Coach from config.config_service import ConfigService __author__ = 'RoboCupULaval' def set_arg_parser(): # TODO add mode debug, redirect, pathfinder! prog_desc = "Module de l'intelligence artificielle. L'option est de charger un fichier de configuration." arg_parser = argparse.ArgumentParser(prog="RobocupULaval's Team ULtron AI", description=prog_desc) arg_parser.add_argument('config_file', nargs='?', help="load a configuration file(.ini/cfg style)", default="config/sim_kalman_redirect.cfg") return arg_parser if __name__ == '__main__': # parser for command line arguments parser = set_arg_parser() args = parser.parse_args() config_service = ConfigService().load_file(args.config_file) # ai init ai_coach = Coach() # RULEngine init framework = Framework() # Starting point framework.start_game(ai_coach.main_loop, ai_coach.set_reference)