コード例 #1
0
def train(self, core_id, config_data, start_date):
    start = time.time()
    cfg = Helpers.prepare_config(config_data)

    walls, seeker, hiding, width, height = Helpers.prepare_map(cfg)

    env, step_img_path, fps_batch, render_mode, wins_l = Helpers.create_env(
        config=cfg,
        width=width,
        height=height,
        walls=walls,
        seeker=seeker,
        hiding=hiding,
        start_date=start_date,
        core_id=core_id,
    )

    AGENTS = 2
    algorithm = Helpers.pick_algorithm(cfg, env=env, agents=AGENTS)
    algorithm.prepare_model()

    for i in range(cfg['game']['episodes']):
        algorithm.before_episode()
        metadata = Helpers.update_celery_metadata(
            core_id=core_id,
            curr=i + 1,
            total=cfg['game']['episodes'],
            ep_iter=cfg['game']['duration'],
            fps=None,
            itera=0,
            iter_perc=0,
            time_elap=round(time.time() - start),
            img_path=step_img_path,
            eta=None,
            rewards=[0, 0],
            wins=[0, 0],
            wins_moving=[0, 0],
        )
        self.update_state(state='PROGRESS', meta=metadata)

        obs_n, reward_n, rewards_ep, done, fps_episode = Helpers.new_ep(env)

        while True:
            rewards_ep = [
                rewards_ep[0] + reward_n[0], rewards_ep[1] + reward_n[1]
            ]
            metadata['status'] = Helpers.update_metadata_status(
                fps=env.clock.get_fps(),
                itera=int(cfg['game']['duration']) - env.duration,
                iter_perc=round(
                    ((int(cfg['game']['duration']) - env.duration) /
                     int(cfg['game']['duration'])) * 100, 2),
                time_elap=round(time.time() - start),
                eta=round((env.duration / env.clock.get_fps()) +
                          int(cfg['game']['duration']) / env.clock.get_fps() *
                          (int(cfg['game']['episodes']) - i))
                if env.clock.get_fps() else None,
                img_path=step_img_path[8:],
                rewards=rewards_ep,
                wins=[sum(w) for w in wins_l],
                wins_moving=[sum(w[-10:]) for w in wins_l],
            )

            fps_episode.append(env.clock.get_fps())

            algorithm.before_action(obs_n=obs_n)

            action_n = algorithm.take_action(obs_n=obs_n)
            obs_old_n = copy.deepcopy(obs_n)

            algorithm.before_step(action_n=action_n)
            obs_n, reward_n, done, _ = env.step(action_n)
            algorithm.after_step(reward_n=reward_n,
                                 obs_old_n=obs_old_n,
                                 obs_n=obs_n,
                                 done=done)

            Helpers.update_img_status(env, cfg['video']['monitoring'],
                                      step_img_path, render_mode)
            self.update_state(state='PROGRESS', meta=metadata)

            if done[0]:
                algorithm.handle_gameover(
                    obs_n=obs_n,
                    reward_n=reward_n,
                    ep_length=int(cfg['game']['duration']) - env.duration,
                )
                Helpers.handle_gameover(done[1], wins_l)
                break

        algorithm.after_episode()

        fps_batch.append(statistics.fmean(fps_episode))

    algorithm.before_cleanup()
    Helpers.cleanup(env, core_id)

    metadata = {'step': 'read meta file'}
    self.update_state(state='PLOTTING', meta=metadata)
    path_to_file = '/opt/app/monitor/' + \
        start_date + "/core-" + str(core_id) + '/'
    raw_stats = read_json(path_to_file)

    metadata = {'step': 'parse meta file'}
    self.update_state(state='PLOTTING', meta=metadata)
    parsed_stats, _ = parse_json_2_dataframe(raw_stats)

    metadata = {
        'step': 'plot seeker',
    }
    self.update_state(state='PLOTTING', meta=metadata)
    seeker_plot = plot_rewards(parsed_stats, 'SEEKER', path_to_file)

    metadata = {
        'step': 'plot hiding',
    }
    self.update_state(state='PLOTTING', meta=metadata)
    hiding_plot = plot_rewards(parsed_stats, 'HIDING', path_to_file)

    return Helpers.get_celery_success(
        core_id=core_id,
        time_elap=round(time.time() - start, 4),
        fps_batch=fps_batch,
        wins=[sum(w) for w in wins_l],
        seeker_plot=seeker_plot,
        hiding_plot=hiding_plot,
    )
コード例 #2
0
        plugin.notify(_(30100).encode('utf-8'))
        plugin.open_settings()
    except exceptions.RequestError:
        plugin.notify(_(30103).encode('utf-8'))
        plugin.log.error(sys.stdout.getvalue())
    except exceptions.ResourceNotFoundError:
        plugin.notify(_(30104).encode('utf-8'))
        plugin.log.error(sys.stdout.getvalue())
    except exceptions.ResponseError:
        plugin.notify(_(30105).encode('utf-8'))
        plugin.log.error(sys.stdout.getvalue())
    except exceptions.StreamingRightsError:
        plugin.notify(_(30106).encode('utf-8'))
        plugin.log.error(sys.stdout.getvalue())
    except IOError as e:
        plugin.log.error(sys.stdout.getvalue())
        traceback.print_exc()
        if e.errno == 2 and e.message.endswith('.tmp'):
            pass
    except Exception as e:
        plugin.log.error(sys.stdout.getvalue())
        traceback.print_exc()
        plugin.notify(e)
    finally:
        helpers.cleanup()

        stdout = sys.stdout.getvalue()
        if len(stdout):
            for line in stdout.split('\n'):
                plugin.log.info(line.replace('\r', ''))