Example #1
0
 def move(self, board, currentPlayer):
     #blockPrint()j
     print(f"mctsPlayer is searching for the best move")
     montecarlo.simulate(50)
     #enablePrint()
     chosen_child_node = montecarlo.make_choice()
     return (chosen_child_node.state.get_previous_move(self.root.state))
Example #2
0
def parallel(domain, func, iters, pool_size=None):
    ''' Run simulation in parallel using multiprocessing. '''
    if pool_size is None:
        return mc.simulate(domain, func, iters)

    log.info('using pool of %d processes', pool_size)
    pool = multiprocessing.Pool(pool_size)
    results = []
    for _ in range(pool_size):
        args = [domain, func, iters / pool_size]
        # NOTE: args must be serializable (via cPickle),
        # since mc.simulate will run in another Python process.
        res = pool.apply_async(mc.simulate, args)
        results.append(res)
    results = [res.get() for res in results]

    return sum(results) / len(results)
 def test_zero(self):
     res = mc.simulate([], func=lambda: 0, iters=1)
     self.assertEquals(0, res)
Example #4
0
 def init_board(state, currentPlayer):
     self.board = board.Board(state, currentPlayer)
     self.root = Node(board.Board(board.getState(), currentPlayer))
     santoriniSim.player_number = 2
     montecarlo = MonteCarlo(root)
     montecarlo.simulate(self.num_sims)
def index():
    plot = ""
    errors = ""

    for fname in os.listdir(
            out_dir):  # delete previously generated plot images
        path = out_dir + fname
        os.remove(path)

    if request.method == "GET":
        return render_template("display_plot.html")

    if request.method == "POST":
        pv = None
        pmt = None
        t = None
        r = None
        sd = None
        N = None
        inflation = 0

        # define dictionary of values: (interest, stddev). Key is percentage in equities.
        interest_sd_dict = {
            '100': (6.6, 10.6),
            '80': (5.9, 8.7),
            '60': (5.2, 6.8),
            '40': (4.6, 5.3),
            '20': (3.9, 4.1),
            '0': (3.2, 3.9)
        }

        # validate input ----------------------------------------------------------------------------
        try:
            pv = float(request.form["pv"])
        except:
            errors += "PV {} is invalid.\n".format(request.form["pv"])
        try:
            pmt = float(request.form["pmt"])
        except:
            errors += "Payment {} is invalid.\n".format(request.form["pmt"])
        try:
            t = int(request.form["t"])
            if t <= 0:
                errors += "# years must be greater than 0.\n"
        except:
            errors += "# years {} is not a whole number.\n".format(
                request.form["t"])
        try:
            asset_mix = request.form['asset_mix']
        except:
            errors += "You must select an asset mix to determine the expected annual rate of return."
        try:
            N = int(request.form["num_sims"])
        except:
            errors += "You must select the number of simulations to run."
        # if errors occurred
        if errors:
            return render_template("error.html", errors=errors)
        # -------------------------------------------------------------------------------------------

        # define variables and simulate
        inflation = request.form.get('inflation')
        inflation = float(inflation) if inflation else 0
        r, sd = interest_sd_dict[
            asset_mix]  # get value from radio button to extract r, sd
        r -= inflation
        median, plot_fname = simulate(pv, pmt, t, r, sd, N=N)
        plot = "/static/images/{}".format(plot_fname)

        return render_template("display_plot.html", plot=plot)