Esempio n. 1
0
def _test():
    """
    Nothing important here.
    """
    state.init()
    out = _convert_and_save_model('../notebooks/saved_mlp_model_with_w.h5')
    print(out)
Esempio n. 2
0
def think(dt, events, kpress, mpos):
	global alpha, playing
	playing = bool(hud.dialoguebox)
	if playing:
		alpha = min(1 * dt + alpha, 1)
	else:
		alpha -= 1 * dt
		if alpha <= 0:
			state.init()
			scene.swap(scenes.game)
			return
	sound.setvolume(alpha)

	if kpress[K_ESCAPE]:
		playing = False

	for event in events:
		if event.type == MOUSEBUTTONDOWN:
			if event.button == 1:
				hud.dialoguebox.settext(lines.pop(0) if lines else None)
		if event.type == KEYDOWN and event.key == K_SPACE:
			hud.dialoguebox.settext(lines.pop(0) if lines else None)
		if event.type == KEYDOWN and event.key == K_ESCAPE:
			hud.dialoguebox.settext()
		if event.type == KEYDOWN and event.key == K_F12:
			graphics.screenshot()

	dx, dy, dr, dA = -0.02 * dt, -0.005 * dt, 0, 0
	camera.move(1 * dx, 1 * dy, 0.7 * dr, 0.7 * dA)
	camera.think(dt)

	hud.think(dt)
Esempio n. 3
0
def init(targets):
    if not os.environ.get('REDO'):
        # toplevel call to redo
        if len(targets) == 0:
            targets.append('all')
        exenames = [os.path.abspath(sys.argv[0]),
                    os.path.realpath(sys.argv[0])]
        dirnames = [os.path.dirname(p) for p in exenames]
        trynames = ([os.path.abspath(p+'/../lib/redo') for p in dirnames] +
                    [p+'/redo-sh' for p in dirnames] +
                    dirnames)
        seen = {}
        dirs = []
        for k in trynames:
            if not seen.get(k):
                seen[k] = 1
                dirs.append(k)
        os.environ['PATH'] = ':'.join(dirs) + ':' + os.environ['PATH']
        os.environ['REDO'] = os.path.abspath(sys.argv[0])

    if not os.environ.get('REDO_BASE'):
        base = os.path.commonprefix([os.path.abspath(os.path.dirname(t))
                                     for t in targets] + [os.getcwd()])
        bsplit = base.split('/')
        for i in range(len(bsplit), 0, -1):
            newbase = '/'.join(bsplit[:i])
            if os.path.exists(newbase + '/.redo'):
                base = newbase
                break
        os.environ['REDO_BASE'] = base
        os.environ['REDO_STARTDIR'] = os.getcwd()

        import state
        state.init()
Esempio n. 4
0
def init(targets):
    if not os.environ.get('REDO'):
        # toplevel call to redo
        exenames = [os.path.abspath(sys.argv[0]),
                    os.path.realpath(sys.argv[0])]
        if exenames[0] == exenames[1]:
            exenames = [exenames[0]]
        dirnames = [os.path.dirname(p) for p in exenames]
        os.environ['PATH'] = ':'.join(dirnames) + ':' + os.environ['PATH']
        os.environ['REDO'] = os.path.abspath(sys.argv[0])

    if not os.environ.get('REDO_BASE'):
        base = os.path.commonprefix([os.path.abspath(os.path.dirname(t))
                                     for t in targets] + [os.getcwd()])
        bsplit = base.split('/')
        for i in range(len(bsplit)-1, 0, -1):
            newbase = '/'.join(bsplit[:i])
            if os.path.exists(newbase + '/.redo'):
                base = newbase
                break
        os.environ['REDO_BASE'] = base
        os.environ['REDO_STARTDIR'] = os.getcwd()

        import state
        state.init()
Esempio n. 5
0
def init(targets):
    if not os.environ.get('REDO'):
        # toplevel call to redo
        if len(targets) == 0:
            targets.append('all')
        exenames = [os.path.abspath(sys.argv[0]),
                    os.path.realpath(sys.argv[0])]
        dirnames = [os.path.dirname(p) for p in exenames]
        trynames = ([os.path.abspath(p+'/../lib/redo') for p in dirnames] +
                    [p+'/redo-sh' for p in dirnames] +
                    dirnames)
        seen = {}
        dirs = []
        for k in trynames:
            if not seen.get(k):
                seen[k] = 1
                dirs.append(k)
        os.environ['PATH'] = ':'.join(dirs) + ':' + os.environ['PATH']
        os.environ['REDO'] = os.path.abspath(sys.argv[0])

    if not os.environ.get('REDO_BASE'):
        base = os.path.expanduser('~')
        atoms = os.getcwd().split('/')
        for i in range(len(atoms), 0, -1):
            possible_base = '/'.join(atoms[:i])
            if os.path.exists(possible_base + '/.redo-base'):
                base = possible_base
        os.environ['REDO_BASE'] = base
        os.environ['REDO_STARTDIR'] = os.getcwd()

        import state
        state.init()

    os.environ['REDO_TARGETS_SEEN'] = os.environ.get('REDO_TARGETS_SEEN', '')
Esempio n. 6
0
def main():
	pygame.init()
	pygame.mixer.init()
	sound.setvolume(0)
	pygame.font.init()
	graphics.setmode()
	graphics.init()
	if settings.playback:
		state0, record = cPickle.load(open(data.filepath("record.pkl"), "rb"))
		state.loadobj(state0)
		scene.push(scenes.game if settings.skipmenu else scenes.menu)
	elif settings.restart:
		state.init()
		scene.push(scenes.game if settings.skipmenu else scenes.menu)
	else:
		scene.push(scenes.menu)
		if state.load():
			scene.push(scenes.game)
	clock = pygame.time.Clock()

	if settings.playback:
		for rstate, dt, events, kpress, mpos in record:
			clock.tick(settings.fps)
			s = scene.top()
			random.setstate(rstate)
			if settings.vidcap:
				vidcap._mpos = mpos
			s.think(dt, events, kpress, mpos)
			s.draw(mpos)
			pygame.display.flip()
		return

	if settings.getrecord:
		record = []
		state0 = cPickle.loads(cPickle.dumps(state.saveobj()))
	while scene.top():
		dt = min(0.001 * clock.tick(settings.fps), 0.5)
		if settings.fixfps:
			dt = 1.0 / settings.fps
		s = scene.top()
		while dt > 0:
			tdt = min(dt, 0.1)
			events = map(EventProxy, pygame.event.get())
			kpress = pygame.key.get_pressed()
			mpos = pygame.mouse.get_pos()
			if settings.getrecord:
				record.append((random.getstate(), tdt, events, kpress, mpos))

			s.think(tdt, events, kpress, mpos)
			dt -= tdt
		s.draw(mpos)
		pygame.display.flip()

	graphics.quit()
	
	if settings.getrecord:
		cPickle.dump([state0, record], open(data.filepath("record.pkl"), "wb"))
Esempio n. 7
0
def main():
    state_module.init()
    
    clock = pygame.time.Clock()
    while True:
        if not state_module.state.handle_input(): break
        state_module.state.draw()
        pygame.display.flip()
        
        clock.tick(30)
    pygame.quit()
Esempio n. 8
0
def main():
    state_module.init()

    clock = pygame.time.Clock()
    while True:
        if not state_module.state.handle_input(): break
        state_module.state.draw()
        pygame.display.flip()

        clock.tick(30)
    pygame.quit()
Esempio n. 9
0
def init():
	glEnable(GL_COLOR_MATERIAL)
	glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)
	glEnable(GL_NORMALIZE)

	lighting.init()
	state.init()
	hud.init()
	hud.menumode()
	camera.menuinit()
	global playing, alpha, selected
	playing = True
	alpha = 0
	selected = None

	sound.playmusic(info.music["menu"])
	
	state.R = 40
Esempio n. 10
0
def initialise_tasks():
  # Initialise state storage (state.db)
  state.init(os.path.join(state_dir_path, 'state'))

  # Ush V3 link initialisation
  yield ush_v3.get_link().initialise_tasks()

  # Datachannel directory initialisation
  yield capture_api.get_directory().initialise()
  register_task(capture_api.DatachannelDirectoryRefreshTask(task_id="dcdir-refresh", first_delay=30), start=True)

  # Initialise pull/push routine for each of the events/channels
  for (event_id, event) in ush_v3.get_link().events.iteritems():
    logger.info("Starting pull/push routine for event=%s (%s)" % (event_id, event.display_name))
    t = pull_push.create_themes_pull_task(event,
      chunk_size=theme_pull_chunk_size,
      period=theme_pull_period, first_delay=(0,15))
    register_task(t, start=True)
Esempio n. 11
0
def init(targets):
    if not os.environ.get('REDO'):
        # toplevel call to redo
        if len(targets) == 0:
            targets.append('all')
        exenames = [
            os.path.abspath(sys.argv[0]),
            os.path.realpath(sys.argv[0])
        ]
        dirnames = [os.path.dirname(p) for p in exenames]
        trynames = ([os.path.abspath(p + '/../lib/redo')
                     for p in dirnames] + [p + '/redo-sh'
                                           for p in dirnames] + dirnames)
        seen = {}
        dirs = []
        for k in trynames:
            if not seen.get(k):
                seen[k] = 1
                dirs.append(k)
        os.environ['PATH'] = ':'.join(dirs) + ':' + os.environ['PATH']
        os.environ['REDO'] = os.path.abspath(sys.argv[0])

    if not os.environ.get('REDO_BASE'):
        base = os.path.commonprefix(
            [os.path.abspath(os.path.dirname(t))
             for t in targets] + [os.getcwd()])
        bsplit = base.split('/')
        for i in range(len(bsplit) - 1, 0, -1):
            newbase = '/'.join(bsplit[:i])
            if os.path.exists(newbase + '/.redo'):
                base = newbase
                break
        os.environ['REDO_BASE'] = base
        os.environ['REDO_STARTDIR'] = os.getcwd()

        import state
        state.init()

    os.environ['REDO_LOCKS'] = os.environ.get('REDO_LOCKS', '')
Esempio n. 12
0
def start():
    state.init()

    storage = TagStorage(get_store_path())
    state.set_storage(storage)

    def nfcLoaded():
        nfc_reader.onTagRead += on_tag_read

    nfc_reader.onLoad += nfcLoaded
    nfc_reader.start_nfc_thread()

    web_interface.init()

    player = MediaPlayer()
    state.set_player(player)

    # TODO: Startup Sound
    #player.play_ding()

    # Blocks, keep last.
    web_interface.run_wait()
Esempio n. 13
0
    print("Posted file: {}".format(request.files['file']))


# def check_timeout_condition():
#     """
#     TO BE IMPLEMENTED.
#     """
#     TIMEOUT_DELTA_IN_MINS = 10
#     time_now = time.time()
#     if time_now > TIMEOUT_DELTA_IN_MINS * 60:
#         # Need to trigger the event of broadcasting to all nodes.
#         # The nodes to drop everything they were doing.
#         pass

if __name__ == '__main__':

    log.startLogging(sys.stdout)

    factory = CloudNodeFactory()
    factory.protocol = CloudNodeProtocol
    wsResource = WebSocketResource(factory)

    wsgiResource = WSGIResource(reactor, reactor.getThreadPool(), app)
    rootResource = WSGIRootResource(wsgiResource, {b'': wsResource})
    site = Site(rootResource)

    state.init()

    reactor.listenTCP(8999, site)
    reactor.run()
Esempio n. 14
0
EVALUATE_EVERY_STEP = 100
EVALUATION_ROUNDS = 20
STEPS_BEFORE_TAU_0 = 10

device = torch.device("cpu")

path = os.getcwd()
model_path = os.path.join(path, f"model4")
net = torch.load(model_path)
mcts = mcts.MCTS()

while True:

    won = None
    cur_player = s.get_random_player()
    cur_state = s.init()

    print(s.decode(cur_state))

    while won is None:

        print(f"Player: {cur_player}")

        if cur_player == 1:
            mcts.search_batch(20,
                              16,
                              cur_state,
                              cur_player,
                              net,
                              device=device)
            probs, _ = mcts.get_policy_value(cur_state, tau=0)
Esempio n. 15
0
	def __init__(self):
		state.init()
		state.start()
		self.t = 0
Esempio n. 16
0
def play_game(mcts_stores,
              replay_buffer,
              net1,
              net2,
              steps_before_tau_0,
              mcts_searches,
              mcts_batch_size,
              net1_plays_first=None,
              device="cpu"):
    """
    Play one single game, memorizing transitions into the replay buffer
    :param mcts_stores: could be None or single MCTS or two MCTSes for individual net
    :param replay_buffer: queue with (state, probs, values), if None, nothing is stored
    :param net1: player1
    :param net2: player2
    :return: value for the game in respect to player1 (+1 if p1 won, -1 if lost, 0 if draw)
    """

    if mcts_stores is None:
        mcts_stores = [mcts.MCTS(), mcts.MCTS()]
    elif isinstance(mcts_stores, mcts.MCTS):
        mcts_stores = [mcts_stores, mcts_stores]

    state = s.init()
    nets = [net1, net2]
    if net1_plays_first is None:
        cur_player = s.get_random_player()
    else:
        cur_player = 1 if net1_plays_first else 2
    step = 0
    tau = 1 if steps_before_tau_0 > 0 else 0
    game_history = []

    result = None
    net1_result = None

    while result is None:
        mcts_stores[cur_player - 1].search_batch(mcts_searches,
                                                 mcts_batch_size,
                                                 state,
                                                 cur_player,
                                                 nets[cur_player - 1],
                                                 device=device)
        probs, _ = mcts_stores[cur_player - 1].get_policy_value(state, tau=tau)
        game_history.append((state, cur_player, probs))
        action = np.random.choice(9, p=probs)
        if action not in s.get_action_space(state):
            print("Impossible action selected")
        state, won = s.move(state, action, cur_player)
        if won:
            result = 1
            net1_result = 1 if cur_player == 1 else -1
            break
        cur_player = s.next_player(cur_player)
        # check the draw case
        if len(s.get_action_space(state)) == 0:
            result = 0
            net1_result = 0
            break
        step += 1
        if step >= steps_before_tau_0:
            tau = 0

    if replay_buffer is not None:
        for state, cur_player, probs in reversed(game_history):
            replay_buffer.append((state, cur_player, probs, result))
            result = -result

    return net1_result, step