Пример #1
0
def runMethods(idx_state, methods, Environment, heuristicFn_nnet, args, data):
    idx, state = idx_state
    stateStr = " ".join([str(x) for x in state])
    # print(stateStr)
    for method in methods:
        start_time = time.time()
        if method == "kociemba":
            soln = Kociemba.solve(state)
            nodesGenerated_num = 0

            elapsedTime = time.time() - start_time
        elif method == "nnet":
            BestFS_solve = search_utils.BestFS_solve([state], heuristicFn_nnet, Environment, bfs=args.bfs)
            isSolved, solveSteps, nodesGenerated_num = BestFS_solve.run(numParallel=args.nnet_parallel,
                                                                        depthPenalty=args.depth_penalty,
                                                                        verbose=args.verbose)
            BestFS_solve = []
            del BestFS_solve
            gc.collect()

            soln = solveSteps[0]
            nodesGenerated_num = nodesGenerated_num[0]

            elapsedTime = time.time() - start_time
        else:
            continue

        data["times"][method][idx] = elapsedTime
        data["nodesGenerated_num"][method][idx] = nodesGenerated_num

        assert (validSoln(state, soln, Environment))

        data["solutions"][method][idx] = soln
Пример #2
0
    def solveNnet(self):
        startTime = time.time()

        BestFS_solve = search_utils.BestFS_solve(self.state,self.heuristicFn,self.env,bfs=0)
        isSolved, solveSteps, nodesGenerated_num = BestFS_solve.run(numParallel=100,depthPenalty=0.1,verbose=True)

        ### Make move
        moves = solveSteps[0]
        print("Neural network found solution of length %i (%s)" % (len(moves),time.time()-startTime))
        for move in moves:
            self.state = self.env.next_state(self.state,move)
            self._updatePlot()
            time.sleep(0.5)
Пример #3
0
def runMethods(state):
    stateStr = " ".join([str(x) for x in state])
    # print(stateStr)
    start_time = time.time()
    runType = "python"
        # if (args.env.upper() in ['CUBE3','PUZZLE15','PUZZLE24','PUZZLE35']) or ('LIGHTSOUT' in args.env.upper()):
        #    runType = "cpp"
        # else:
    #    runType = "python"
    if runType == "cpp":
        popen = Popen(
            ['./ml_utils/parallel_weighted_astar', stateStr, str(0.2), str(100),
             socketName, 'cube3'], stdout=PIPE, stderr=PIPE, bufsize=1, universal_newlines=True)
        lines = []
        for stdout_line in iter(popen.stdout.readline, ""):
            stdout_line = stdout_line.strip('\n')
            lines.append(stdout_line)
            # if args.verbose:
            #     sys.stdout.write("%s\n" % (stdout_line))
            #     sys.stdout.flush()

        moves = [int(x) for x in lines[-3].split(" ")[:-1]]
        soln = [Environment.legalPlays[x] for x in moves][::-1]
        nodesGenerated_num = int(lines[-1])
    else:
        BestFS_solve = search_utils.BestFS_solve([state], heuristicFn_nnet, Environment, 0)
        isSolved, solveSteps, nodesGenerated_num = BestFS_solve.run(numParallel=100,
                                                                    depthPenalty=0.2,
                                                                    verbose=False)
        BestFS_solve = []
        del BestFS_solve
        gc.collect()

        soln = solveSteps[0]
        nodesGenerated_num = nodesGenerated_num[0]

    elapsedTime = time.time() - start_time
    assert (validSoln(state, soln, Environment))
    return [state, soln]
Пример #4
0
                                                  Environment,
                                                  maxTurns=maxTurns,
                                                  searchDepth=searchDepth,
                                                  numRollouts=numRollouts,
                                                  searchMethod=solveMethod,
                                                  verbose=verbose)

        percentSolved = 100 * float(sum(isSolved)) / float(len(isSolved))
        avgSolveSteps = 0.0
        if percentSolved > 0.0:
            avgSolveSteps = np.mean(solveSteps[isSolved])

    elif solveMethod == "BESTFS":
        stateVals = heuristicFn(np.stack(testStates_cube, axis=0))

        BestFS_solve = search_utils.BestFS_solve(testStates_cube, heuristicFn,
                                                 Environment)
        isSolved, solveSteps = BestFS_solve.run(
            numParallel=numRollouts,
            depthPenalty=args.depth_penalty,
            verbose=verbose)

        percentSolved = 100 * float(sum(isSolved)) / float(len(isSolved))
        avgSolveSteps = 0.0
        if percentSolved > 0.0:
            solveSteps_isSolved = [
                x for x, y in zip(solveSteps, isSolved) if y == True
            ]
            avgSolveSteps = np.mean([len(x) for x in solveSteps_isSolved])

    solve_elapsed_time = time.time() - solve_start_time
    print(
def runMethods(idx_state):
    idx, state = idx_state
    stateStr = " ".join([str(x) for x in state])
    #print(stateStr)
    for method in methods:
        start_time = time.time()
        if method == "kociemba":
            soln = Kociemba.solve(state)
            nodesGenerated_num = 0

            elapsedTime = time.time() - start_time
        elif method == "nnet":
            runType = "python"
            #if (args.env.upper() in ['CUBE3','PUZZLE15','PUZZLE24','PUZZLE35']) or ('LIGHTSOUT' in args.env.upper()):
            #    runType = "cpp"
            #else:
            #    runType = "python"
            if runType == "cpp":
                popen = Popen([
                    './ml_utils/parallel_weighted_astar', stateStr,
                    str(args.depth_penalty),
                    str(args.nnet_parallel), socketName, args.env
                ],
                              stdout=PIPE,
                              stderr=PIPE,
                              bufsize=1,
                              universal_newlines=True)
                lines = []
                for stdout_line in iter(popen.stdout.readline, ""):
                    stdout_line = stdout_line.strip('\n')
                    lines.append(stdout_line)
                    if args.verbose:
                        sys.stdout.write("%s\n" % (stdout_line))
                        sys.stdout.flush()

                moves = [int(x) for x in lines[-3].split(" ")[:-1]]
                soln = [Environment.legalPlays[x] for x in moves][::-1]
                nodesGenerated_num = int(lines[-1])
            else:
                BestFS_solve = search_utils.BestFS_solve([state],
                                                         heuristicFn_nnet,
                                                         Environment,
                                                         bfs=args.bfs)
                isSolved, solveSteps, nodesGenerated_num = BestFS_solve.run(
                    numParallel=args.nnet_parallel,
                    depthPenalty=args.depth_penalty,
                    verbose=args.verbose)
                BestFS_solve = []
                del BestFS_solve
                gc.collect()

                soln = solveSteps[0]
                nodesGenerated_num = nodesGenerated_num[0]

            elapsedTime = time.time() - start_time
        elif method == "optimal":
            soln, nodesGenerated_num, elapsedTime = Optimal.solve(state)
        else:
            continue

        data["times"][method][idx] = elapsedTime
        data["nodesGenerated_num"][method][idx] = nodesGenerated_num

        assert (validSoln(state, soln, Environment))

        data["solutions"][method][idx] = soln
Пример #6
0
def getResult(states):
    nnet_parallel = 100
    depth_penalty = 0.2
    bfs = 0
    startIdx = 0
    endIdx = -1
    verbose = False
    useGPU = False

    if endIdx == -1:
        endIdx = len(states)

    states = states[startIdx:endIdx]
    print('state:', states)

    ### Load nnet
    from ml_utils import nnet_utils
    from ml_utils import search_utils

    if len(os.environ['CUDA_VISIBLE_DEVICES']) > 1:
        gpuNums = [
            int(x) for x in os.environ['CUDA_VISIBLE_DEVICES'].split(",")
        ]
    else:
        gpuNums = [None]
    numParallel = len(gpuNums)

    ### Initialize files
    dataQueues = []
    resQueues = []
    for num in range(numParallel):
        dataQueues.append(Queue(1))
        resQueues.append(Queue(1))

        dataListenerProc = Process(target=dataListener,
                                   args=(
                                       dataQueues[num],
                                       resQueues[num],
                                       gpuNums[num],
                                   ))
        dataListenerProc.daemon = True
        dataListenerProc.start()

    def heuristicFn_nnet(x):
        ### Write data
        parallelNums = range(min(numParallel, x.shape[0]))
        splitIdxs = np.array_split(np.arange(x.shape[0]), len(parallelNums))
        for num in parallelNums:
            dataQueues[num].put(x[splitIdxs[num]])

        ### Check until all data is obtaied
        results = [None] * len(parallelNums)
        for num in parallelNums:
            results[num] = resQueues[num].get()

        results = np.concatenate(results)

        return (results)

    ### Get solutions
    BestFS_solve = search_utils.BestFS_solve([states],
                                             heuristicFn_nnet,
                                             Environment,
                                             bfs=bfs)
    isSolved, solveSteps, nodesGenerated_num = BestFS_solve.run(
        numParallel=nnet_parallel, depthPenalty=depth_penalty, verbose=verbose)
    BestFS_solve = []

    del BestFS_solve
    gc.collect()

    soln = solveSteps[0]
    return soln