Esempio n. 1
0
def test_with_database():
    gdl_str = get_gdl_for_game("connectFour")

    mapping, info = lookup.by_gdl(gdl_str)

    assert mapping is None
    assert info.game == "connectFour"
    sm = info.get_sm()

    # ensure keeps returning valid statemachines
    for ii in range(10):
        new_mapping, new_info = lookup.by_gdl(gdl_str)
        new_sm = new_info.get_sm()

        assert new_mapping is None
        assert new_info is info
        assert new_sm != sm
        assert id(new_sm) != id(sm)
        assert new_sm.get_initial_state() == sm.get_initial_state()
        interface.dealloc_statemachine(new_sm)

    # finally run rollouts in c++ on the original sm
    log.info("Testing sm %s" % sm)
    msecs_taken, rollouts, _ = interface.depth_charge(sm, 1)
    rollouts_per_second = (rollouts / float(msecs_taken)) * 1000
    log.info("c++ rollouts per second %.2f" % rollouts_per_second)
Esempio n. 2
0
def test_not_in_database():
    some_simple_game = """
  (role white)
  (role black)

  (init o1)

  (legal white a)
  (legal white b)
  (legal black a)

  (<= (next o2) (does white a) (true o1))
  (<= (next o3) (does white b) (true o1))

  (<= (goal white 0) (true o1))
  (<= (goal white 10) (true o2))
  (<= (goal white 90) (true o3))

  (<= (goal black 0) (true o1))
  (<= (goal black 90) (true o2))
  (<= (goal black 10) (true o3))

  (<= terminal (true o2))
  (<= terminal (true o3))
    """

    mapping, info = lookup.by_gdl(some_simple_game)
    assert info.game == "unknown"
    sm = info.get_sm()

    # run rollouts in c++
    msecs_taken, rollouts, _ = interface.depth_charge(sm, 1)
    rollouts_per_second = (rollouts / float(msecs_taken)) * 1000
    log.info("c++ rollouts per second %.2f" % rollouts_per_second)
Esempio n. 3
0
def main_3(game_file, output_file, seconds_to_run):
    # builds without accessing database database
    _, game_info = lookup.by_gdl(open(game_file).read())
    sm = game_info.get_sm()

    if debug:
        log.verbose("GAME_FILE %s" % game_file)
        log.verbose("OUTPUT_FILE %s" % output_file)
        log.verbose("SECONDS_TO_RUN %s" % seconds_to_run)

    # for the result
    f = open(output_file, "w")

    print >> f, "version=%s" % VERSION
    try:
        msecs_taken, rollouts, num_state_changes = go(sm, seconds_to_run)

        # see gdl-perf (XXX do python3 print)
        print >> f, "millisecondsTaken=%s" % msecs_taken
        print >> f, "numStateChanges=%s" % num_state_changes
        print >> f, "numRollouts=%s" % rollouts

    except Exception as exc:
        error_str = "Error %s" % exc
        type, value, tb = sys.exc_info()
        traceback.print_exc()

        print >> f, "errorMessage=%s" % (error_str, )

    f.close()
Esempio n. 4
0
def test_not_in_db():
    some_simple_game = """
  (role white)
  (role black)

  (init o1)

  (legal white a)
  (legal white b)
  (legal black a)

  (<= (next o2) (does white a) (true o1))
  (<= (next o3) (does white b) (true o1))

  (<= (goal white 0) (true o1))
  (<= (goal white 10) (true o2))
  (<= (goal white 90) (true o3))

  (<= (goal black 0) (true o1))
  (<= (goal black 90) (true o2))
  (<= (goal black 10) (true o3))

  (<= terminal (true o2))
  (<= terminal (true o3))
    """

    _, game_info = lookup.by_gdl(some_simple_game)
    gm = GameMaster(game_info)

    # add two python players
    gm.add_player(get.get_player("pyrandom"), "black")
    gm.add_player(get.get_player("ggtest1"), "white")

    gm.start(meta_time=10, move_time=5)
    gm.play_to_end()
Esempio n. 5
0
    def handle_start(self, symbols):
        assert len(symbols) == 6
        match_id = symbols[1]
        role = symbols[2]
        gdl = symbols[3]
        meta_time = int(symbols[4])
        move_time = int(symbols[5])

        if self.current_match is not None:
            log.debug("GOT A START message for %s while already playing match" % match_id)
            return "busy"
        else:
            log.info("Starting new match %s" % match_id)

            # lookup game and create match
            gdl_symbol_mapping, game_info = lookup.by_gdl(gdl)
            self.current_match = match.Match(game_info, match_id, role, meta_time,
                                             move_time, self.player,
                                             cushion_time=CUSHION_TIME,
                                             gdl_symbol_mapping=gdl_symbol_mapping)
            try:
                # start gameserver timeout
                self.update_gameserver_timeout(self.current_match.meta_time)

                self.current_match.do_start()
                return "ready"

            except match.BadGame:
                return "busy"
Esempio n. 6
0
    def __init__(self, gdl_str, verbose=False, fast_reset=False):
        self.verbose = verbose
        self.fast_reset = fast_reset

        # used to convert to base state
        self.symbol_factory = SymbolFactory()

        self.gdl_str = gdl_str
        _, info = lookup.by_gdl(gdl_str)
        self.sm = info.get_sm()
        self.game = info.game
        self.match_id = None

        # store a joint move / basestate internally
        self.joint_move = self.sm.get_joint_move()
        self.next_basestate = self.sm.new_base_state()

        def get_base_tuple(i):
            return tuple(self.symbol_factory.to_symbols(self.sm.get_gdl(i)))[0]

        self.bases = [get_base_tuple(i) for i in range(self.next_basestate.len())]

        self.players = []
        self.players_map = {}

        # updated after game is finished
        self.scores = {}

        if verbose:
            log.info("GAMEMASTER: create a gamemaster for game %s" % self.game)
        self.matches = None
Esempio n. 7
0
    def do_start(self, initial_basestate=None, game_depth=0):
        ''' Optional initial_basestate.  Used mostly for testing. If none will use the initial
            state of state machine (and the game_depth will be zero).  Game depth may not be
            handled by the base player. '''

        enter_time = time.time()
        end_time = enter_time + self.meta_time
        if self.cushion_time > 0:
            end_time -= self.cushion_time

        if self.verbose:
            log.debug("Match.do_start(), time = %.1f" %
                      (end_time - enter_time))

        if self.load_game:
            (self.gdl_symbol_mapping, self.game_info) = lookup.by_gdl(self.gdl)

            self.sm = self.game_info.get_sm()

        self.sm.reset()
        if self.verbose:
            log.debug("Got state machine %s for game '%s' and match_id: %s" %
                      (self.sm, self.game_info.game, self.match_id))

        if initial_basestate:
            # dupe the state - it could be deleted under our feet
            bs = self.sm.new_base_state()
            bs.assign(initial_basestate)
            initial_basestate = bs

            if self.verbose:
                log.debug("The start state is %s" %
                          self.sm.basestate_to_str(initial_basestate))

            # update the statemachine
            self.sm.update_bases(initial_basestate)

            # check it is not actually finished
            assert not self.sm.is_terminal()

        else:
            initial_basestate = self.sm.get_initial_state()

        self.states.append(initial_basestate)

        # store a joint move internally
        self.joint_move = self.sm.get_joint_move()

        # set our role index
        if self.gdl_symbol_mapping:
            our_role = self.gdl_symbol_mapping[self.role]
        else:
            our_role = self.role

        self.our_mapped_role = our_role
        if our_role not in self.sm.get_roles():
            raise BadGame("Our role not found. %s in %s",
                          (our_role, self.sm.get_roles()))

        self.our_role_index = self.sm.get_roles().index(our_role)
        if self.verbose:
            log.info('roles : %s, our_role : %s, role_index : %s' %
                     (self.sm.get_roles(), our_role, self.our_role_index))
        assert self.our_role_index != -1

        # starting point for the game (normally zero)
        self.game_depth = game_depth

        # FINALLY : call the meta gaming stage on the player
        # note: on_meta_gaming must use self.match.get_current_state()
        self.player.reset(self)
        self.player.on_meta_gaming(end_time)