Example #1
0
 def to_prims(self):
     return {
         'order':
         self.order,
         'entity_iden':
         self.entity_iden,
         'event_name':
         self.event_name,
         'args':
         ser.serialize_embeddable(self.args),
         'prevals':
         tuple(ser.serialize_embeddable(prev) for prev in self.prevals)
     }
 def to_prims(self):
     return {
         'train_bot': self.train_bot,
         'adver_bot': self.adver_bot,
         'bot_folder': self.bot_folder,
         'train_seq': list(ser.serialize_embeddable(ses) for ses in self.train_seq),
         'cur_ind': self.cur_ind
     }
def _run(args):
    settings: TrainSettings = None
    if not os.path.exists(args.settings):
        os.makedirs(os.path.dirname(args.settings), exist_ok=True)
        settings = TrainSettings.defaults(args.bot)
        with open(args.settings, 'w') as outfile:
            json.dump(ser.serialize_embeddable(settings), outfile)
    else:
        with open(args.settings, 'r') as infile:
            rawsettings = json.load(infile)
            settings = ser.deserialize_embeddable(rawsettings)

    os.makedirs(settings.bot_folder, exist_ok=True)
    executable = 'python3' if args.py3 else 'python'
    port = args.port
    create_flags = 0 if args.headless else subprocess.CREATE_NEW_CONSOLE
    nthreads = args.numthreads
    spec = not args.headless

    ncores = psutil.cpu_count(logical=False)
    if args.aggressive and nthreads > (ncores // 3):
        print(f'auto-reducing simultaneous servers to {ncores // 3} since there are {ncores} '
              + 'cores and we need 3 cores per server')
        nthreads = ncores // 3

    if os.path.exists(settings.replay_folder):
        rb.FileWritableReplayBuffer(settings.replay_folder, exist_ok=True).close()

    while settings.cur_ind < len(settings.train_seq):
        print('--starting session--')
        print(settings.current_session)
        _get_experiences_async(settings, executable, port, port + 10*nthreads, create_flags,
                               args.aggressive, spec, nthreads)
        _train_experiences(settings, executable)
        if args.debug:
            break
        _cleanup_session(settings)
        settings.cur_ind += 1
        with open(args.settings, 'w') as outfile:
            json.dump(ser.serialize_embeddable(settings), outfile)

    print('--finished--')
Example #4
0
 def to_prims(self):
     return {
         'order':
         self.order,
         'attacker_iden':
         self.attacker_iden,
         'defender_iden':
         self.defender_iden,
         'og_damage':
         self.og_damage,
         'tags':
         tuple(self.tags),
         'attack_prevals':
         tuple(
             ser.serialize_embeddable(prev)
             for prev in self.attack_prevals),
         'defend_prevals':
         tuple(
             ser.serialize_embeddable(prev)
             for prev in self.defend_prevals),
     }
def _get_experiences_async(settings: TrainSettings, executable: str, port_min: int, port_max: int,
                           create_flags: int, aggressive: bool, spec: bool, nthreads: int):
    num_ticks_to_do = settings.current_session.tar_ticks
    if os.path.exists(settings.replay_folder):
        replay = rb.FileReadableReplayBuffer(settings.replay_folder)
        num_ticks_to_do -= len(replay)
        replay.close()

        if num_ticks_to_do <= 0:
            print(f'get_experiences_async nothing to do (already at {settings.replay_folder})')
            return

    replay_paths = [os.path.join(settings.bot_folder, f'replay_{i}') for i in range(nthreads)]
    setting_paths = [os.path.join(settings.bot_folder, f'settings_{i}.json')
                     for i in range(nthreads)]
    workers = []
    serd_settings = ser.serialize_embeddable(settings)
    ports_per = (port_max - port_min) // nthreads
    if ports_per < 3:
        raise ValueError('not enough ports assigned '
                         + f'({nthreads} threads, {port_max-port_min} ports)')
    ticks_per = int(math.ceil(num_ticks_to_do / nthreads))
    for worker in range(nthreads):
        proc = Process(target=_get_experiences_target,
                       args=(serd_settings, executable, port_min + worker*ports_per,
                             port_min + (worker+1)*ports_per, create_flags, aggressive, spec,
                             replay_paths[worker], setting_paths[worker], ticks_per))
        proc.start()
        workers.append(proc)
        time.sleep(1)

    for proc in workers:
        proc.join()

    print(f'get_experiences_async finished, storing in {settings.replay_folder}')
    if os.path.exists(settings.replay_folder):
        filetools.deldir(settings.replay_folder)

    if os.path.exists(settings.replay_folder):
        tmp_replay_folder = settings.replay_folder + '_tmp'
        os.rename(settings.replay_folder, tmp_replay_folder)
        replay_paths.append(tmp_replay_folder)

    if os.path.exists(HOLDOVER_DIR):
        replay_paths.append(HOLDOVER_DIR)

    rb.merge_buffers(replay_paths, settings.replay_folder)

    for path in replay_paths:
        filetools.deldir(path)
Example #6
0
 def to_prims(self):
     return {
         'iden':
         self.iden,
         'x':
         self.x,
         'y':
         self.y,
         'depth':
         self.depth,
         'health':
         self.health,
         'base_max_health':
         self.base_max_health,
         'base_damage':
         self.base_damage,
         'base_armor':
         self.base_armor,
         'modifiers':
         [ser.serialize_embeddable(mod) for mod in self.modifiers],
         'items':
         dict((str(key), ser.serialize_embeddable(val))
              for key, val in self.items),
     }
Example #7
0
 def to_prims(self):
     return {
         'attacker_iden': self.attacker_iden,
         'attack_result': ser.serialize_embeddable(self.attack_result)
     }
Example #8
0
 def to_prims(self):
     return {
         'order': self.order,
         'depth': self.depth,
         'dungeon': ser.serialize_embeddable(self.dungeon)
     }
Example #9
0
 def to_prims(self):
     return {
         'order': self.order,
         'entity_iden': self.entity_iden,
         'modifier': ser.serialize_embeddable(self.modifier)
     }
Example #10
0
 def to_prims(self) -> typing.Any:
     return {
         'order': self.order,
         'entity': ser.serialize_embeddable(self.entity)
     }
Example #11
0
 def to_prims(self):
     return {'thing': ser.serialize_embeddable(self.thing)}
Example #12
0
 def to_prims(self):
     return {'dgen': ser.serialize_embeddable(self.dgen)}
Example #13
0
 def to_prims(self):
     return {
         'dgen': ser.serialize_embeddable(self.dgen),
         'p1_depth': self.p1_depth,
         'p2_depth': self.p2_depth
     }
Example #14
0
 def to_prims(self):
     return {
         'game_state': ser.serialize_embeddable(self.game_state),
         'player_iden': self.player_iden
     }
Example #15
0
 def to_prims(self):
     return {'update': ser.serialize_embeddable(self.update)}