def check_ai(self): if self.game_roles.get_current_ai() == 1: self.canvas_blocked = 1 self.revertButton.config(state=tkinter.DISABLED) if self.first == 0: self.add_piece(random.randint(7, 9), random.randint(7, 9)) else: MCTS = montecarlo.MonteCarlo(self.state, self.game_roles) move = MCTS.best_next_move() x, y = move[0], move[1] self.add_piece(x, y)
def st(n): id_igre, igra = igre.nova_igra(n) if bottle.request.get_cookie("nacin_igre") == "pvb": igra.bot = random.choice([model.BELI, model.CRNI]) # Barvo izberemo naključno mc = montecarlo.MonteCarlo(igra) simulacije[id_igre] = mc cookie = str(n) id_igre = str(id_igre) if bottle.request.get_cookie("velikost") == None: bottle.response.set_cookie("velikost", cookie, path="/") if bottle.request.get_cookie("id_igre") == None: bottle.response.set_cookie("id_igre", id_igre, path="/") bottle.redirect("/igra/")
def pvb(): """Vmesnik za igro proti računalniku.""" velikost = izberi_velikost() barva = izberi_barvo() igra = model.Go(velikost) mc = montecarlo.MonteCarlo(igra) while not igra.konec: print(izpis_igre(igra)) if igra.na_potezi() == barva: while True: poteza = tuhtaj_dokler_igralec_ne_vnese_poteze(mc) try: igra.igraj(poteza) mc.stanje = mc.potomec_poteza(poteza) break except AssertionError as e: print(e) print('') else: poteza = mc.najboljsa_poteza() igra.igraj(poteza) print(izpis_koncnega_rezultata(igra))
import options import montecarlo import bmgenerator import time #================================================================== #Call option in Black Scholes without variance reduction print('\nCall option in Black Scholes without variance reduction') bm_gen_normal = bm_gen = bmgenerator.BMGenerator(method = 'normal') #method can be set to 'normal', 'a' antithetic, 'mm' (moment mathcing), 'sobol', sobolbb (Brownian Bridge) and 'sobolpca' bs_model = models.BlackScholesModel(spot=100, rate=0, vol=0.2, bm_generator = bm_gen_normal) #Define model call_option = options.CallOption(strike = 110, maturity = 1) #Define option mc_pricer = montecarlo.MonteCarlo(model= bs_model, option = call_option, n_sim = 2**15) #Define MC-pricer t0 = time.time() price = mc_pricer.estimate_price(confidence = False) #Esimates price t1 = time.time() print('Estimated price:',price,'Time taken:',t1-t0) mc_pricer.plot_mc_price() #Plot convergence #================================================================== #Ordinary Call option in Black Scholes with use of Sobol numbers and Brownian Bridge construction print('\nCall option in Black Scholes with use of Sobol numbers and Brownian Bridge construction') bm_gen_sobolbb = bm_gen = bmgenerator.BMGenerator(method = 'sobolbb')
neel = wavefunction.ProductState(conf_init, directors_d, jastrow_init) return neel else: print('No valid state selected') exit() def create_hamiltonian(lattice_in): neighbor_pairs = list( set(map(tuple, map(sorted, lattice_in.get_neighbor_pairs( 0))))) # remove duplicate neighbor pairs H = local_operator.Hamiltonian() H.add_term('J', 1.0, neighbor_pairs, interaction_type=local_operator.HeisenbergExchange) return H if __name__ == '__main__': lattice_run = create_lattice() wavefunction_run = create_wavefunction(lattice_run) ham_run = create_hamiltonian(lattice_run) mc = montecarlo.MonteCarlo(wavefunction_run, lattice_run.get_neighbor_pairs(0), su3=False, measures=1000, throwaway=500) mc.add_observable(ham_run) results, per_site, measurements = mc.run() print(per_site)
import gamelogic import montecarlo import random random.seed("prump") numberOfGames = 0 board = montecarlo.Board() initialPhase2 = board.init([]) mc = montecarlo.MonteCarlo(board, initialPhase2, 5, 100, 1) winners = [0, 0, 0] while numberOfGames < 100: phase1 = gamelogic.game() playerCards = [phase1[0].cards, phase1[1].cards, phase1[2].cards] initialPhase2 = board.init(playerCards) mc.reset(initialPhase2) previousState = initialPhase2 numberOfGames += 1 while True: aimove = mc.get_play() previousState = board.next_state(previousState, [ aimove,
jastrow_init) return afm120 else: print('No valid state selected') exit() def create_hamiltonian(lattice_in): neighbor_pairs = list( set(map(tuple, map(sorted, lattice_in.get_neighbor_pairs( 0))))) # remove duplicate neighbor pairs hermitian_conj_rings = [(sites[0], sites[2], sites[1]) for sites in lattice_in.get_ring_exchange_list()] H = local_operator.Hamiltonian() H.add_term('J', 1.0, neighbor_pairs) # K = 0.0 # H.add_term('K', K, lattice_in.get_ring_exchange_list(), interaction_type=local_operator.ThreeRingExchange) # H.add_term('K_prime', K, hermitian_conj_rings, interaction_type=local_operator.ThreeRingExchange) return H if __name__ == '__main__': lattice_run = create_lattice() wavefunction_run = create_wavefunction(lattice_run, 'afq120') ham_run = create_hamiltonian(lattice_run) mc = montecarlo.MonteCarlo(wavefunction_run, lattice_run.get_neighbor_pairs(0), measures=1000) mc.add_observable(ham_run) results, per_site, measurements = mc.run() print(per_site)
def main(): monte = montecarlo.MonteCarlo() minim = minimax.MiniMax()
# ==== Covariance Frobenius Norm ===== # ==================================== norm_exact = np.linalg.norm(mcov, 'fro') # ==================================== # ===== Portfolio Hedging Inits ====== # ==================================== # Used for estimating hedged portfolio variance, involing inverse of covariance matrix. p = putils.Portfolio(np.array([1, 0, 0, 0])) p_var = putils.portfolio_var(p, mcov) hdg = putils.mvp_hedge(p, mcov) mvp = p + hdg mvp_var = putils.portfolio_var(mvp, mcov) truevals = {'mvp.var': mvp_var, 'cov.relnorm': norm_exact, 'p.var': p_var} # ==================================== # =========== Monte Carlo ============ # ==================================== obs_gen = stats.multivariate_normal(cov=mcov) T_grid = pmc.init_sparse_grid(lbound=p_dim, max_sample_size=64) mc = mc.MonteCarlo(rndgen=lambda: obs_gen.rvs(T_grid[-1]), nsim=1000, evalfunc=lambda welfords, path: pmc.mc_path_eval( T_grid, p, mcov, welfords, path), welfords=pmc.estimations(len(T_grid))) est = mc.run() # ===================== # ===== Plotting ====== # ===================== present.result(T_grid, est, truevals, alpha_conf=0.01)
import lattice import montecarlo as mc import numpy as np lat = lattice.Lattice(lat_type='triangle', lx=2, ly=2) print(lat.basis) lat = lattice.Lattice(lat_type='triangle', lx=2, ly=3, unit_cell_mult=3) print(lat.basis) print(lat.a1) print(lat.a2) print(lat.coordinates) print(lat.distances) print(lat.neighbor_table[0]) print(lat.neighbor_pbc[0]) print(lat.get_neighbor_pairs(0)) conf_init = {'size': lat.N, 'S2': 2, 'num_each': (6, 6, 6)} MC = mc.MonteCarlo(conf_init, {2: lat.get_neighbor_pairs(0)}) print(MC.neighbor_list) print(MC.propose_move())