示例#1
0
def main(args):
    if args.verbose:
        logging.basicConfig(level=logging.INFO)
    elif args.debug:
        logging.basicConfig(level=logging.DEBUG)
    # initializations
    gridworld = GridWorld(args.size, args.interval, args.obstacles, args.vision, args.phase)
    logging.info("Generated grid world!")
    logging.info("Visuals created")
    mc = MonteCarlo(gridworld, mode=args.method)
    logging.info("Initialized Monte Carlo method")

    mc.run()
示例#2
0
def burgers():
    x_range = [-2.0, 3.0]
    nx = 200 
    nu = 150 
    C = .3
    tmax = 0.6 
    num_realizations = 800 
    debug = False
    savefilename = 'burgers0' + str(num_realizations) + '.npy'
    
    MC = MonteCarlo(num_realizations=num_realizations, x_range=x_range, tmax=tmax, debug=debug, savefilename=savefilename, nx=nx, C=C)
    MC.multiSolve("gaussians")

    MCprocess = MCprocessing(savefilename)
    #MCprocess.buildHist(40)
    MCprocess.buildKDE(nu, plot=True)
示例#3
0
 def play_game(board_size, fnet, mcts_sims):
     """
     Generate a single game and batch
     """
     game_batch = []
     try:
         eprint ("instantiating sim")
         simulator = MonteCarlo(board_size, fnet, mcts_sims) # Create a MCTS simulator
         game_batch = simulator.play_game()
     except:
         tb = traceback.format_exc()
     else:
         tb = "No error"
     finally:
         eprint(tb)
         return game_batch
示例#4
0
 def play_game(nrows, ncols, inarow, fnet, mcts_sims):
     """
     Generate a single game and batch
     """
     game_batch = []
     try:
         eprint("Instantiating Sim")
         simulator = MonteCarlo(nrows, ncols, inarow, fnet,
                                mcts_sims)  # Create a MCTS simulator
         game_batch = simulator.play_game()
     except:
         tb = traceback.format_exc()
         raise
     # else:
     #     tb = "No error"
     # finally:
     #     eprint(tb)
     return game_batch
示例#5
0
    def chooseParentUsingFitness(self, choices):
        """
            Chooses a parent from a set of choices, using fitness to 
            effect the probability of choosing that parent.
        """
        # This should probably use something more like laplace smoothing.
        # Right now, it only checks to ensure that the total probability is not
        # zero.  But we should probably be using Laplace smoothing to ensure that
        # zero fitness scores have a (small) chance of being selected.  This is
        # only really important for the first few generations, whether there
        # are genes with zero fitness.
        totalFitness = sum([self.fitness[i] for i in choices])
        if totalFitness > 0:
            probOfChoices = [
                float(self.fitness[i]) / float(totalFitness) for i in choices
            ]
        else:
            probOfChoices = [1 / float(len(choices)) for i in choices]

        mc = MonteCarlo(choices, probOfChoices)
        return mc.getWeightedSelection()
示例#6
0
        (2. * a**2 * c) /
        (4. * alpha**2 * c * eta**2 + 2. * c * sigma**2 + 1.)))
    right_denom = np.sqrt(4. * alpha**2 * c * eta**2 + 2. * c * sigma**2 + 1.)
    return left_nom / left_denom - right_nom / right_denom


from covariances import ExpCov, MaternCov, GaussCov
from data import ToyInverseProblem
from montecarlo import MonteCarlo
from pointsets import Random, Mesh1d

cov_fct = MaternCov()
IP = ToyInverseProblem(0.1)
dim = 1
num_pts_mc = 10000
monte_carlo = MonteCarlo(num_pts_mc, dim)

#pt_x = Random(1,1)
# pt_z = Random(1,1)
# print(pt_x.points, pt_z.points)

print(IP.true_observations)
print(IP.locations.points)


def integration(pt_x, cov_fct, IP, monte_carlo):
    def integrand(pt_z, pt_x=pt_x, cov_fct=cov_fct, IP=IP):
        eta = cov_fct.assemble_entry_cov_mtrx(pt_x, pt_x)
        alpha = cov_fct.assemble_entry_cov_mtrx(pt_z, pt_x) / eta
        sigma = cov_fct.assemble_entry_cov_mtrx(
            pt_z, pt_z) - alpha * cov_fct.assemble_entry_cov_mtrx(pt_x, pt_z)
示例#7
0
文件: sarsa.py 项目: captn3m0/easy21
    def update(self, alpha, delta):
        for s in self.q:
            for a in [Action.HIT, Action.STICK]:
                self.q[s][a] += alpha * delta * self.E[s][a]
                self.E[s][a] = Sarsa.GAMMA * self._lambda * self.E[s][a]


if __name__ == "__main__":
    g = graph.graphxy(width=30,
                      x=graph.axis.linear(min=100, max=1000),
                      y=graph.axis.linear(),
                      key=graph.key.key(pos="bl"))
    plots = []
    """ Re-calculate V* """
    m = MonteCarlo()
    for i in range(1, 50000):
        m.run()

    for _l in [e / 10.0 for e in range(0, 11, 1)]:
        print("Training Sarsa(%s)" % _l)
        s = Sarsa(_l)
        c1 = []
        c2 = []
        for j in range(1, 1001):
            s.run()
            if j % 100 == 0:
                c1.append(j)
                e = s.mean_squared_error(m.q)
                c2.append(e)
                if j % 100 == 0:
示例#8
0
                    stats[1] += 1
                    break
                if len(strategy1.moves) == 42: break

            stats[2 + turn % 2] += time.perf_counter() - start
            if n == 1: strategy1.print()

            turn += 1

        strategy1.print()
        print()
        print(stats[0], stats[1], "{:.6f}".format(stats[2]),
              "{:.6f}".format(stats[3]))


#fight(10, MonteCarlo(1), MonteCarlo(1))
#fight(10, SimpleRandom(),SimpleRandom())
fight(10, MonteCarlo(20), Minimax(5))
#fight(1, Human(), Minimax(6))

# r = SimpleRandom()
# r.load([0,6,1,5,2,4,3])
# assert r.winning_move() == True
# r.reset(1)
# r.load([0,6,1,5,2,4,3])
# assert r.winning_move() == False

#fight(1000, Random(), Random())
#fight(100, MonteCarlo(0.00001), Random()) # Vinner 99-100 av 100, oberoende av tid?
#fight(1, MonteCarlo(0.1),MonteCarlo(0.1)) # Vinner 99-100 av 100, oberoende av tid?
示例#9
0
       ['b', 'b', 'w', 'w', 'w', 'b', 'b', 'b', 'w', 'w', 'w', 'b']]

# Cria o mapa de probabilidades
# Quantidade de linhas e colunas do mapa
x = len(map)
y = len(map[0])
p = 1.0 / (x * y)

p_map = [[p for j in range(y)] for i in range(x)]

# Inicia a simulação por um tempo para o robô e os sensores inicializarem
start_angle = 90
robot.step(1000)

# Instancia da localização (passa o mapa com as características)
localize = MonteCarlo(map)

while robot.step(timestep) != -1:

    # Mede a posição atual
    z = robotGetColor()

    # Atualiza o mapa de probabilidades com a medição
    p_map = localize.measure(p_map, z)

    print_map(p_map)

    # Printa a posição atual mais provável
    pos, prob = localize.where(p_map)
    print("- O robô tem %.1f%% de chance de estar na posição %d - %d" %
          (prob, pos[0], pos[1]))
示例#10
0
    y2 = y * y
    L2 = L * L
    xL2 = (x - L) * (x - L)
    yL2 = (y - L) * (y - L)

    c1 = x2 + y2 <= L2
    c2 = xL2 + y2 <= L2
    c3 = x2 + yL2 <= L2
    c4 = xL2 + yL2 <= L2

    tc = c1 and c2 and c3 and c4

    return 1 if tc else 0


mc = MonteCarlo(seed, samples, target_function_L, 2)
print(mc.run())


def target_function(random_vector):
    '''
    Function receives a uniform random vector in [0]
    '''
    x = random_vector[0]
    y = random_vector[1]
    c1 = x + y < 1
    return 1 if c1 else 0


samples = 10000
mc = MonteCarlo(seed, samples, target_function, 2)