Python Monte Carlo Simulation Loop - python

I am working on a simple Monte-Carlo simulation script, which I will later on extend for a larger project. The script is a basic crawler trying to get from point A to point B in a grid. The coordinates of point A is (1,1) (this is top left corner), and the coordinates of point B is (n,n) (this is bottom right corner, n is the size of the grid).
Once the crawler starts moving, there are four options, it can go left, right, up or down (no diagonal movement allowed). If any of these four options satisfy the following:
The new point should still be within the boundries of the n x n grid
The new point should not be visited previously
the new point will be selected randomly among the remaining valid options (as far as I know Python uses the Mersenne Twister algorithm for picking random numbers).
I would like to run the simulation for 1,000,000 times (the code below runs for 100 only), and each iteration should be terminated either:
The crawler gets stuck (no valid options for movement)
The crawler gets to the final destination (n,n) on the grid.
I thought I implemented the algorithm correctly, but obviously something is wrong. No matter how many times I run the simulations (100 or 1,000,000), I only get 1 successful event wehere the crawler manages to get to the end, and rest of the attempts (99, or 999,999) is unsuccessful.
I bet there is something simple I am missing out, but cannot see it for some reason. Any ideas?
Thanks a bunch!
EDIT: Some typos in the text were corrected.
import random
i = 1 # initial coordinate top left corner
j = 1 # initial coordinate top left corner
k = 0 # counter for number of simulations
n = 3 # Grid size
foundRoute = 0 # counter for number of cases where the final point is reached
gotStuck = 0 # counter for number of cases where no valid options found
coordList = [[i, j]]
while k < 100:
while True:
validOptions = []
opt1 = [i - 1, j]
opt2 = [i, j + 1]
opt3 = [i + 1, j]
opt4 = [i, j - 1]
# Check 4 possible options out of bound and re-visited coordinates are
# discarded:
if opt1[0] != 0 and opt1[0] <= n and opt1[1] != 0 and opt1[1] <= n:
if not opt1 in coordList:
validOptions.append(opt1)
if opt2[0] != 0 and opt2[0] <= n and opt2[1] != 0 and opt2[1] <= n:
if not opt2 in coordList:
validOptions.append(opt2)
if opt3[0] != 0 and opt3[0] <= n and opt3[1] != 0 and opt3[1] <= n:
if not opt3 in coordList:
validOptions.append(opt3)
if opt4[0] != 0 and opt4[0] <= n and opt4[1] != 0 and opt4[1] <= n:
if not opt4 in coordList:
validOptions.append(opt4)
# Break loop if there are no valid options
if len(validOptions) == 0:
gotStuck = gotStuck + 1
break
# Get random coordinate among current valid options
newCoord = random.choice(validOptions)
# Append new coordinate to the list of grid points visited (to be used
# for checks)
coordList.append(newCoord)
# Break loop if lower right corner of the grid is reached
if newCoord == [n, n]:
foundRoute = foundRoute + 1
break
# If the loop is not broken, assign new coordinates
i = newCoord[0]
j = newCoord[1]
k = k + 1
print 'Route found %i times' % foundRoute
print 'Route not found %i times' % gotStuck

Your problem is that you're never clearing out your visited locations. Change your block that breaks out of the the inner while loop to look something like this:
if len(validOptions) == 0:
gotStuck = gotStuck + 1
coordList = [[1,1]]
i,j = (1,1)
break
You'll also need to change your block where you succeed:
if newCoord == [n, n]:
foundRoute = foundRoute + 1
coordList = [[1,1]]
i,j = (1,1)
break
Alternatively, you could simply place this code right before your inner while loop. The start of your code would then look like:
k = 0 # counter for number of simulations
n = 3 # Grid size
foundRoute = 0 # counter for number of cases where the final point is reached
gotStuck = 0 # counter for number of cases where no valid options found
while k < 100:
i,j = (1,1)
coordList = [[i,j]]
while True:
#Everything else

Related

Efficient implemetation of BFS on python

There is a following problem:
Given set of n points with x, y coordinates. Distance between them is L1 : sum of abs of differences of corresponding coordinates. There is an edge between two points if distance between them is no more than given k. Given different start and end points I need to find the number of edges in minimum path between them, and minimum is counted by number of edges, not overall length. If there is no path between given nodes, I should print -1
It seemed to me that this problem is solved by BFS. But since we have to have list of edges to use BFS, which is O(n^2), I decided to merge edge processing with BFS. So my code looks like that:
n = int(input())
cities = []
def read_two_numbers():
s = input()
s = s.split(' ')
return list(map(int,s))
for i in range(n):
cities.append(read_two_numbers())
def distance(city1, city2):
return abs(cities[city1][0] - cities[city2][0]) + abs(cities[city1][1] - cities[city2][1])
k = int(input())
start, end = read_two_numbers()
start -= 1
end -= 1
def find_minimum_path():
reached = [start]
not_reached = [i for i in range(n) if i != start]
level = 1
while len(not_reached) > 0:
new_reached = []
# print(f'reached is {reached}')
# print(f'not_reached is {not_reached}')
for city_from in reached:
new_not_reached = []
for city_to in not_reached:
if distance(city_from, city_to) <= k:
if city_to == end:
print(level)
return
new_reached.append(city_to)
else:
new_not_reached.append(city_to)
not_reached = new_not_reached
if len(not_reached) == 0:
break
reached = new_reached
level += 1
print(-1)
return
find_minimum_path()
The problem is that this solution exceeds time limit. Is there a way to solve this task more efficiently? Or it is connected with low speed of Python itself?

Function to find winning line with NxN board and M pieces in a row in Python 3

I am trying to create a function that finds if a move is winning on an NxN board where the winning condition is M pieces in a row in Python 3.
I am pretty new to programming and in my specific case I am creating a Gomoku game (15x15 board with 5 pieces in a row to win). To get it working I created 6 for loops to check vertical, horizontal and 4 diagonals. See the code below for examples on the 2 options for left to right digonals. This takes way too long though when I need to loop through it many times (8) for computer to find if I can win or if it has a winning move.
end_row = 15
for j in range(11):
end_row -= 1
counter = 0
for i in range(end_row):
if board[i+j][i] == board[i+1+j][i+1] and board[i+j][i] != ' ':
counter += 1
if counter == 4:
winning_line = [(i+j-3, i-3), (i+j-2, i-2), (i+j-1, i-1), (i+j, i), (i+1+j, i+1)]
winner = True
break
else:
counter = 0
# Top left to bottom right, lower side
end_row = 15
for j in range(11):
end_row -= 1
counter = 0
for i in range(end_row):
if board[i][i+j] == board[i+1][i+1+j] and board[i][i+j] != ' ':
counter += 1
if counter == 4:
winning_line = [(i-3, i+j-3), (i-2, i+j-2), (i-1, i+j-1), (i, i+j), (i+1, i+1+j)]
winner = True
break
else:
counter = 0
# What I want to do instead, where x and y are coordinates of last move:
# Horizontal
counter = 0
for i = x - (n - 1) to x + (n - 1):
if board[i][y] == board[x][y] :
counter++
else :
counter = 0
if counter == n:
return true
The problem with the lower part of the code is that if I place a piece on e.g. position (0, 0) the program will complain when trying to reach board[-4][0] in the first looping. I will have to place lots of if statements when I get close to the edge, which is not an elegant solution.
I thought of making a 3*15 x 3*15 board instead, where the actual board is the inner 15x15 part and the rest just contains placeholders:
15x15 || 15x15 || 15x15
15x15 || board || 15x15
15x15 || 15x15 || 15x15
This to avoid getting outside of my list of lists when looping through. Not an elegant solution either, but takes less space in the code.
Any suggestions on how to solve this problem? Thank you in advance from a beginner programmer!
As #MePsyDuck mentioned in comments, you can use min and max functions to limit the range to only reference valid squares in the board matrix.
Furthermore, you could make a generic function that does the count-job on any given list of values. Then you can call that generic function four times: once for every direction (horizontal, vertical, diagonal \ and diagonal /)
Here is how that could work:
def is_win(board, n, x, y):
end_row = len(board)
color = board[x][y]
def check(values):
counter = 0
for value in values:
if value == color:
counter += 1
else:
counter = 0
if counter == n:
return True
return False
return (check([board[i][y] for i in range(max(0, x - n + 1), min(end_row, x + n))])
or check([board[x][i] for i in range(max(0, y - n + 1), min(end_row, y + n))])
or check([board[x+i][y+i] for i in range(max(-x, -y, 1 - n), min(end_row - x, end_row - y, n))])
or check([board[x+i][y-i] for i in range(max(-x, y - end_row + 1, 1 - n), min(end_row - x, y + 1, n))]))
Instead of looping from 0 to 14, just loop from 0 to (board_size - winning_length).
Here's an example for a 1-dimensional board:
BOARD_SIZE = 15
WINNING_LENGTH = 5
for x in range(BOARD_SIZE - WINNING_LENGTH):
players_here = set()
for pos in range(x, x + WINNING_LENGTH):
players_here.add(board[pos])
if len(players_here) == 1:
# Exactly 1 player occupies every position in this line, so they win

how to optimize the python code of staircase problem to work with larger values?

I had to create a program for a staircase problem where i had to design staircase with n number of bricks. The complexity is that the number of bricks in every step must be unique, and less than the previous step.
for example, using 6 bricks, i can make a staircase with step height (5,1) , (4,2) and (3,2,1) but not (3,3) or (1,2,3) or (2,4) or any other permutation.
I have designed the code but the problem is that it runs fine till n as nearly 100 or 120, but freezes if the inputs are larger than these values. I am a beginner in Python programming and learning concepts on the go.
I tried memoization, but to no avail. I need to know if there is something else i can do to make my code more optimized to run over n as 200-250?
import cProfile
def solution(n):
memory = {0: [], 1: [1], 2: [2]}
def rec(max_val, i):
t = []
r = []
for j in range(1,i):
y = i - j
if y < max_val:
if y > j:
t = [y, j]
r.append(t)
if n / 2 >= j >= 3 and j in memory:
mem = memory[j]
[r.append([y, item]) for item in mem]
else:
if y >= 3 and n / 2 >= j >= 3 and j in memory:
mem = memory[j]
for item in mem:
if y > item[0]:
r.append([y, item])
else:
v = rec(y, j)
if v:
for item in v:
t = [y, item]
r.append(t)
if r:
if i in memory:
if len(memory[i]) < len(r):
memory[i] = r
else:
memory[i] = r
return r
def main_func(n):
stair = []
max_val = 201
total = 0
for i in range (1,n):
x = n - i
if x > i:
s = [x, i]
total += 1
if i >= 3:
u = rec(max_val, i)
total += len(u)
elif x == i and i >= 3:
u = rec(max_val, i)
total += len(u)
elif x < i and i >= 3:
u = rec(x, i)
total += len(u)
return total
stairs = main_func(n)
return (stairs)
print(solution(100))
You can approach the problem recursively from the perspective of the base of the stairs. The strategy is to add up the pattern counts of the next step levels for each base size.
For example, with 6 bricks, the first call would go through base sizes n = 5,4,3,2 and make a recursive calls to know how many combinations are possible for the next step levels using the remaining bricks and with a maximum base of n-1. The sum of next levels counts will constitute the total count of possible stair patterns.
At the top of the stairs, you need at least 3 bricks to add more than one level so you can stop the recursion with a count of 1 when there are fewer than 3 bricks left. This will roll up the recursive calls to form larger totals and produce the right answer once the original call completes.
In order to optimize this process, you can use memoization and you can also short circuit the calculation using the base size provided at each recursion.
For a given base, the largest number of bricks that can be used will be the sum of numbers from 1 to that base. This can be computed using Gauss's formula: base*(base+1)/2. If you have more bricks than the maximum brick count for the base, then you can stop the recursion and return a count of zero (since you have too many remaining bricks and will not be able to fit them all over the previous level's base)
Another way to optimize the calculation is to loop through the base sizes in decreasing order. This will allow you to stop the loop as soon as you get a count of zero for the next levels, which means that there are too many remaining bricks for that base size (or any smaller base size)
Here's an example (using lru_cache for memoization):
from functools import lru_cache
#lru_cache(None)
def stairCount(N,base=None):
base = min(base or N-1,N)
if N > base*(base+1)//2: return 0
if N < 3: return 1
count = 0
while True:
nextLevels = stairCount(N-base,base-1)
if nextLevels == 0: break
count += nextLevels
base = base - 1
return count
With these optimizations, the function will respond in less than a second for up to 600 bricks (depending on the speed of your computer).
With Python's list comprehensions, you could write this function more concisely (though it would lose the decreasing base order optimization ≈ 10%):
#lru_cache(None)
def stairCount(N,base=None):
base = min(base or N-1,N)
if N > base*(base+1)//2: return 0
if N < 3: return 1
return sum(stairCount(N-b,b-1) for b in range(2,base+1))
EDIT Here's a version with a "manual" memoization (i.e. without using functools):
def stairCount(N,base=None,memo=dict()):
memoKey = (N,base)
if memoKey in memo: return memo[memoKey]
base = min(base or N-1,N)
if N > base*(base+1)//2: return 0
if N < 3: return 1
count = 0
while True:
nextLevels = stairCount(N-base,base-1,memo)
if nextLevels == 0: break
count += nextLevels
base = base - 1
memo[memoKey] = count
return count

Check indexes in 3D numpy array

I'm trying to write code that will play a dice game called Pig through the command line with a person against the computer
For the computer's player, I am using a 3D numpy array to store a game strategy based on the information current score AI_1_current, banked score AI_1_bank and opponent's banked score human_bank.
The idea is, each turn, the computer will check the strategy array, if the value at the index [AI_1_current, AI_1_bank, human_bank] is equal to 0, it will keep rolling, if it's equal to 1, it will bank it's current score.
Now, the problem I'm having, is the function that takes the computer's turn is not reading the array properly, and not banking when it should.
I have this code to set up the array:
AI_1_strategy = numpy.zeroes((100,100,100))
for i in range(10, 100):
for j in range(10, 100):
for k in range(10, 100):
AI_1_strategy[i, j, k] = 1
Which ideally should mean, if i, j or k are greater than or equal to 10, the computer will bank every turn
Then later I have this code to check the array:
if AI_1_strategy[AI_1_bank, AI_1_current, human_bank] == 1:
AI_1_bank += AI_1_current # add current points to the bank
AI_1_current = 0
time.sleep(3) # 3 second delay
if AI_1_bank >= 100: # win condition
print "AI_1 WINS!"
else:
AI_current = 0 # sets current points to 0
print "AI has banked"
pig_human() # moves to the player's turn
else:
time.sleep(3) # 3 second delay
print "AI_1 chose to keep rolling"
pig_AI_1() # AI takes another roll
Despite this code, the computer will fail to bank consistently.
The problem also occurs if I instead do:
AI_1_strategy = numpy.ones((100,100,100))
for i in range(10, 100):
for j in range(10, 100):
for k in range(10, 100):
AI_1_strategy[i, j, k] = 0
Except in that case, it will bank every turn regardless of the scores, when it should really stop banking once i, j or k reaches 10.
If anybody could help me out I'd greatly appreciated, I have no idea what I'm doing wrong.
Thanks.
It looks like you're making a small logic error.
I have this code to set up the array:
AI_1_strategy = numpy.zeroes((100,100,100))
for i in range(10, 100):
for j in range(10, 100):
for k in range(10, 100):
AI_1_strategy[i, j, k] = 1
Which ideally should mean, if i, j or k are greater than or equal to 10, the computer will bank every turn
This is incorrect: Each of i, j AND k must be >=10 to read a 1 from this array.

Using Python for quasi randomization

Here's the problem: I try to randomize n times a choice between two elements (let's say [0,1] -> 0 or 1), and my final list will have n/2 [0] + n/2 [1]. I tend to have this kind of result: [0 1 0 0 0 1 0 1 1 1 1 1 1 0 0, until n]: the problem is that I don't want to have serially 4 or 5 times the same number so often. I know that I could use a quasi randomisation procedure, but I don't know how to do so (I'm using Python).
To guarantee that there will be the same number of zeros and ones you can generate a list containing n/2 zeros and n/2 ones and shuffle it with random.shuffle.
For small n, if you aren't happy that the result passes your acceptance criteria (e.g. not too many consecutive equal numbers), shuffle again. Be aware that doing this reduces the randomness of the result, not increases it.
For larger n it will take too long to find a result that passes your criteria using this method (because most results will fail). Instead you could generate elements one at a time with these rules:
If you already generated 4 ones in a row the next number must be zero and vice versa.
Otherwise, if you need to generate x more ones and y more zeros, the chance of the next number being one is x/(x+y).
You can use random.shuffle to randomize a list.
import random
n = 100
seq = [0]*(n/2) + [1]*(n-n/2)
random.shuffle(seq)
Now you can run through the list and whenever you see a run that's too long, swap an element to break up the sequence. I don't have any code for that part yet.
Having 6 1's in a row isn't particularly improbable -- are you sure you're not getting what you want?
There's a simple Python interface for a uniformly distributed random number, is that what you're looking for?
Here's my take on it. The first two functions are the actual implementation and the last function is for testing it.
The key is the first function which looks at the last N elements of the list where N+1 is the limit of how many times you want a number to appear in a row. It counts the number of ones that occur and then returns 1 with (1 - N/n) probability where n is the amount of ones already present. Note that this probability is 0 in the case of N consecutive ones and 1 in the case of N consecutive zeros.
Like a true random selection, there is no guarantee that the ratio of ones and zeros will be the 1 but averaged out over thousands of runs, it does produce as many ones as zeros.
For longer lists, this will be better than repeatedly calling shuffle and checking that it satisfies your requirements.
import random
def next_value(selected):
# Mathematically, this isn't necessary but it accounts for
# potential problems with floating point numbers.
if selected.count(0) == 0:
return 0
elif selected.count(1) == 0:
return 1
N = len(selected)
selector = float(selected.count(1)) / N
if random.uniform(0, 1) > selector:
return 1
else:
return 0
def get_sequence(N, max_run):
lim = min(N, max_run - 1)
seq = [random.choice((1, 0)) for _ in xrange(lim)]
for _ in xrange(N - lim):
seq.append(next_value(seq[-max_run+1:]))
return seq
def test(N, max_run, test_count):
ones = 0.0
zeros = 0.0
for _ in xrange(test_count):
seq = get_sequence(N, max_run)
# Keep track of how many ones and zeros we're generating
zeros += seq.count(0)
ones += seq.count(1)
# Make sure that the max_run isn't violated.
counts = [0, 0]
for i in seq:
counts[i] += 1
counts[not i] = 0
if max_run in counts:
print seq
return
# Print the ratio of zeros to ones. This should be around 1.
print zeros/ones
test(200, 5, 10000)
Probably not the smartest way, but it works for "no sequential runs", while not generating the same number of 0s and 1s. See below for version that fits all requirements.
from random import choice
CHOICES = (1, 0)
def quasirandom(n, longest=3):
serial = 0
latest = 0
result = []
rappend = result.append
for i in xrange(n):
val = choice(CHOICES)
if latest == val:
serial += 1
else:
serial = 0
if serial >= longest:
val = CHOICES[val]
rappend(val)
latest = val
return result
print quasirandom(10)
print quasirandom(100)
This one below corrects the filtering shuffle idea and works correctly AFAICT, with the caveat that the very last numbers might form a run. Pass debug=True to check that the requirements are met.
from random import random
from itertools import groupby # For testing the result
try: xrange
except: xrange = range
def generate_quasirandom(values, n, longest=3, debug=False):
# Sanity check
if len(values) < 2 or longest < 1:
raise ValueError
# Create a list with n * [val]
source = []
sourcelen = len(values) * n
for val in values:
source += [val] * n
# For breaking runs
serial = 0
latest = None
for i in xrange(sourcelen):
# Pick something from source[:i]
j = int(random() * (sourcelen - i)) + i
if source[j] == latest:
serial += 1
if serial >= longest:
serial = 0
guard = 0
# We got a serial run, break it
while source[j] == latest:
j = int(random() * (sourcelen - i)) + i
guard += 1
# We just hit an infinit loop: there is no way to avoid a serial run
if guard > 10:
print("Unable to avoid serial run, disabling asserts.")
debug = False
break
else:
serial = 0
latest = source[j]
# Move the picked value to source[i:]
source[i], source[j] = source[j], source[i]
# More sanity checks
check_quasirandom(source, values, n, longest, debug)
return source
def check_quasirandom(shuffled, values, n, longest, debug):
counts = []
# We skip the last entries because breaking runs in them get too hairy
for val, count in groupby(shuffled):
counts.append(len(list(count)))
highest = max(counts)
print('Longest run: %d\nMax run lenght:%d' % (highest, longest))
# Invariants
assert len(shuffled) == len(values) * n
for val in values:
assert shuffled.count(val) == n
if debug:
# Only checked if we were able to avoid a sequential run >= longest
assert highest <= longest
for x in xrange(10, 1000):
generate_quasirandom((0, 1, 2, 3), 1000, x//10, debug=True)

Categories

Resources