Ejemplo n.º 1
0
def main():
    name, game = gamesman.load_game_from_args('Solve games using MPI.')

    def maybePrimitive(pos):
        value = game.primitive(pos)
        if value is gamesman.UNDECIDED:
            recurseDown(pos, None)
        else:
            solved(pos, (value, 0))

    @keyReduce
    def recurseDown(pos, old, new):
        if old is None:
            # We have a new position. Expand one layer of the tree.
            moves = game.generateMoves(pos)
            children = set([game.doMove(pos, move) for move in moves])
            solving(pos, ChildValAccumulator(len(children)))
            for child in children:
                childToParents(child, ('parent', pos))
                maybePrimitive(child)
            # Record that we've already visited this subtree.
        return True

    @keyReduce
    def solved(pos, old, new):
        # We should only get here multiple times for primitives.
        assert old is None or old == new
        if old is None and new[0] != gamesman.DRAW:
            childToParents(pos, ('value', new))
        return new

    @keyReduce
    def childToParents(pos, old, new):
        if old is None:
            old = (None, [])
        old_val, old_parents = old
        kind, val = new
        if kind == 'value':
            assert old_val is None
            for parent in old_parents:
                # We already have some parents, so send them the new value.
                solving(parent, val)
            return (val, old_parents)
        else:
            assert kind == 'parent'
            parent = val
            # We have a new parent.
            old_parents.append(parent)
            if old_val is not None:
                # We already received the value, so send it to the new parent.
                solving(parent, old_val)
            return old

    @keyReduce
    def solving(pos, old, new):
        if old is None:
            return new
        else:
            old.update(new)
            if old.done():
                solved(pos, old.finish())
            return old

    if COMM_RANK == 0:
        # Only one process needs to inject the initialPosition.
        maybePrimitive(game.initialPosition)

    KeyReducer.router.run_until_done()
    #KeyReducer.router.print_all()

    @solving.foreach
    def markDraw(pos, acc):
        if not acc.done():
            if acc.children_observed != 0:
                print('pos', repr(pos), 'best', acc.best_value, acc.children_observed, '/', acc.child_count)
                print('pos', repr(pos), 'children', [game.doMove(pos, m) for m in game.generateMoves(pos)])
            solved(pos, (gamesman.DRAW, gamesman.DRAW_REMOTENESS))

    solved.save('results/{}/solved'.format(name))
    childToParents.save('results/{}/childToParents'.format(name))
    solving.save('results/{}/solving'.format(name))
Ejemplo n.º 2
0
def main():
    name, game = gamesman.load_game_from_args('Naively solve games.')
    print(solve(game, game.initialPosition))
Ejemplo n.º 3
0
def main():
    name, game = gamesman.load_game_from_args('Solve games locally.')
    print(Solver(game).solve())
Ejemplo n.º 4
0
def main():
    name, game = gamesman.load_game_from_args('Solve games locally.')
    print(Solver(game).solve())
Ejemplo n.º 5
0
def main():
    name, game = gamesman.load_game_from_args('Naively solve games.')
    print(solve(game, game.initialPosition))