Paths in Python/Sage - python

I've been working on this problem (https://imgur.com/a/nJEMfM9) asking me to plot all lattice paths in a nxn grid for the last week, and I have no idea how to proceed.
This is about as far as I've been able to get
def NE_lattice_paths(x,y):
Vn= vector([0,1])
Ve= vector([1,0])
plot(Vn) + plot(Ve, start=Vn)
I know I have to use vectors, and I have to use the "def" command to make a function, but how would I make a function that can plot every path and know to take a different one each time? What I wrote doesn't really make sense, but I could use some guidance on how to proceed. Thank you!

You can get all the paths with a nested for loop (or list comprehension).
So this will give all the paths.
def NE_lattice_paths(x,y):
paths = []
for i in range(x):
path = []
for j in range(y):
path.append((i,j))
paths.append(path)
return paths
result = NE_lattice_paths(5,3)
print(result)
result
[[(0, 0), (0, 1), (0, 2)], [(1, 0), (1, 1), (1, 2)], [(2, 0), (2, 1), (2, 2)], [(3, 0), (3, 1), (3, 2)], [(4, 0), (4, 1), (4, 2)]]
I will leave it as an excersize for the OP to do the animation...

Related

Find common union groups among tuples in a set

I need help to write a function that:
takes as input set of tuples
returns the number of tuples that has unique numbers
Example 1:
# input:
{(0, 1), (3, 4), (0, 0), (1, 1), (3, 3), (2, 2), (1, 0)}
# expected output: 3
The expected output is 3, because:
(3,4) and (3,3) contain common numbers, so this counts as 1
(0, 1), (0, 0), (1, 1), and (1, 0) all count as 1
(2, 2) counts as 1
So, 1+1+1 = 3
Example 2:
# input:
{(0, 1), (2, 1), (0, 0), (1, 1), (0, 3), (2, 0), (0, 2), (1, 0), (1, 3)}
# expected output: 1
The expected output is 1, because all tuples are related to other tuples by containing numbers in common.
This may not be the most efficient algorithm for it, but it is simple and looks nice.
from functools import reduce
def unisets(iterables):
def merge(fsets, fs):
if not fs: return fsets
unis = set(filter(fs.intersection, fsets))
return {reduce(type(fs).union, unis, fs), *fsets-unis}
return reduce(merge, map(frozenset, iterables), set())
us = unisets({(0,1), (3,4), (0,0), (1,1), (3,3), (2,2), (1,0)})
print(us) # {frozenset({3, 4}), frozenset({0, 1}), frozenset({2})}
print(len(us)) # 3
Features:
Input can be any kind of iterable, whose elements are iterables (any length, mixed types...)
Output is always a well-behaved set of frozensets.
this code works for me
but check it maby there edge cases
how this solution?
def count_groups(marked):
temp = set(marked)
save = set()
for pair in temp:
if pair[1] in save or pair[0] in save:
marked.remove(pair)
else:
save.add(pair[1])
save.add(pair[0])
return len(marked)
image

convert output from dictionary to list with bfs and dfs networkx

I am currently using networkx library for Python with BFS and DFS. I need to get a tree and then explore it to get a path from a start node to an end node.
For the BFS part I am using bfs_successorsand it returns an iterator of successors in breadth-first-search from source.
For the DFS part I am using: dfs_successors and it returns a dictionary of successors in depth-first-search from source.
I need to get a list of nodes from source to end from both the algorithms. Each node is (x, y) and is a cell in a grid.
Do you have any advice about how to do it? Can you help me please?
MWE:
DFS = nx.bfs_successors(mazePRIM,start)
print(dict(BFS))
DFS = nx.dfs_successors(mazePRIM, start)
print(DFS)
and I get this:
{(0, 0): [(0, 1), (1, 0)], (1, 0): [(1, 1)], (1, 1): [(1, 2)], (1, 2): [(0, 2), (1, 3)], (0, 2): [(0, 3)]}
{(0, 0): [(0, 1), (1, 0)], (1, 0): [(1, 1)], (1, 1): [(1, 2)], (1, 2): [(0, 2), (1, 3)], (0, 2): [(0, 3)]}
But I need an output like this:
[(0, 0), (1, 0), (1, 1), (1, 2), (1, 3)]
which is the list of nodes from start to end.
IIUC you're not really interested in finding all successors encourtered with nx.bfs_successors, since you only need the path between a source and a target nodes.
For that you can either find the shortest path (in the case there are multiple):
nx.shortest_path(G, source, target)
Or find all simple paths between them:
nx.all_simple_paths(G, source, target)
Which returns a generator with all simple paths between both nodes.

Python Chess Implementation

I'm beginning work on a chess implementation and before going too far down the rabbit hole, I wanted to get the community's input if you wouldn't mind since I'm already at a dead end ha. I'm struggling to figure out the best way to associate the pieces with the coordinates.
Right now, I have a list of list with the various pieces where each list represents a board.
For the coordinates, I used this list comprehension
coordinates = [[(i,j) for i in range(0,8)] for j in range(0,8)]
which gives me a table like this
[(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (5, 0), (6, 0), (7, 0)]
[(0, 1), (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (7, 1)]
[(0, 2), (1, 2), (2, 2), (3, 2), (4, 2), (5, 2), (6, 2), (7, 2)]
[(0, 3), (1, 3), (2, 3), (3, 3), (4, 3), (5, 3), (6, 3), (7, 3)]
[(0, 4), (1, 4), (2, 4), (3, 4), (4, 4), (5, 4), (6, 4), (7, 4)]
[(0, 5), (1, 5), (2, 5), (3, 5), (4, 5), (5, 5), (6, 5), (7, 5)]
[(0, 6), (1, 6), (2, 6), (3, 6), (4, 6), (5, 6), (6, 6), (7, 6)]
[(0, 7), (1, 7), (2, 7), (3, 7), (4, 7), (5, 7), (6, 7), (7, 7)]
Any strong thoughts on how I can associate the piece with their coordinate to find potential moves? I was thinking dictionary at first, but you have multiple pieces of the same type (eg. two knights) and don't think this would be ideal as the board evolved.
Thanks as always.
Funnily enough, I have just been working on exactly this! Previously, I wrote a chess AI but in javascript, however today I have been converting that code into Python for use with a bigger project so the knowledge is fresh in my mind.
Originally, in the JS version, I stored the board effectively as an 8x8 array of strings for each piece (in reality this was inside an object with other data such as castling but that is not important).
However, this method of using an array (list in Python) led to problems due to the way they are passed by reference. The issue was that passing the board state through the negamax algorithm meant that for each move to be considered, the whole array (in JS) would have to be copied to stop the move being made to the original board state.
I got around this by storing the board states as strings which are immutable in Python. I would advise you to start off using lists though as they are much simpler to access and change values even though they will probably end up leading to slowness (from making copies of them) later down the line when you come to optimising.
The actual trick to storing the board state is to use one character for each piece and use upper and lowercase to represent the white and black sides. I stole this technique from the widely used FEN notation and it turns out to be really useful for both displaying and doing operations on the board state!
To see what I mean, you could initialise the starting state with:
state = ["RNBQKBNR", "PPPPPPPP", " ", " ", " ", " ", "pppppppp", "rnbqkbnr"]
state = [list(r) for r in state]
and then you can easily create a display function with:
def display(state):
for r in reversed(state):
print(''.join(r))
then whenever you want to display a given state, you can call display(state) which gives:
rnbqkbnr
pppppppp
PPPPPPPP
RNBQKBNR
Hopefully this helps you out! You can look at the code for my full implementation of a chess AI on github: in Python and in javascript :)
OK it goes like this:
You got 64 cells, traditionally coordinated with letter and digit.
Name each cell numeric so that cell will coordinated: "a1" will be 11, h5 will be 85 etc.
Now for the moves:
Up: (cell value) + 1, Down: (cell value) - 1, Right: (cell value) +
10, Left: (cell value) - 10,
Diagnose: Up-Left: (cell value) - 9, Up-Right: (cell value) +
11, Down-Left: (cell value) - 11, Down-Right: ((cell value) + 9,
And for the Knight: (cell value) + 21, (cell value) - 21, (cell
value) + 12, (cell value) - 12, (cell value) + 8, (cell value) - 8,
(cell value) + 19, (cell value) – 19.
As you can understand, I recently build one by myself ( based on JS if you mind) ha ha.
Good Luck!
As someone mentioned, the most obvious simple implementation is a list of lists, for example in my implementation this logic creates the board, and then pieces are added to it using the add() method:
https://github.com/akulakov/pychess/blob/7176b168568000af721e79887981bcd6467cfbc0/chess.py#L141

Issue with python recursion

I have the following code written in python 2.7 to find n time Cartesian product of a set (AxAxA...xA)-
prod=[]
def cartesian_product(set1,set2,n):
if n>=1:
for x in set1:
for y in set2:
prod.append('%s,%s'%(x,y))
#prod='[%s]' % ', '.join(map(str, prod))
#print prod
cartesian_product(set1,prod,n-1)
else:
print prod
n=raw_input("Number of times to roll: ")
events=["1","2","3","4","5","6"]
cartesian_product(events,events,1)
This works properly when n=1. But changing the parameter value from cartesian_product(events,events,1) to cartesian_product(events,events,2) doesn't work. Seems there's an infinite loop is running. I can't figure where exactly I'm making a mistake.
When you pass the reference to the global variable prod to the recursive call, you are modifying the list that set2 also references. This means that set2 is growing as you iterate over it, meaning the iterator never reaches the end.
You don't need a global variable here. Return the computed product instead.
def cartesian_product(set1, n):
# Return a set of n-tuples
rv = set()
if n == 0:
# Degenerate case: A^0 == the set containing the empty tuple
rv.add(())
else:
rv = set()
for x in set1:
for y in cartesian_product(set1, n-1):
rv.add((x,) + y)
return rv
If you want to perserve the order of the original argument, use rv = [] and rv.append instead.
def cartesian_product(*X):
if len(X) == 1: #special case, only X1
return [ (x0, ) for x0 in X[0] ]
else:
return [ (x0,)+t1 for x0 in X[0] for t1 in cartesian_product(*X[1:]) ]
n=int(raw_input("Number of times to roll: "))
events=[1,2,3,4,5,6]
prod=[]
for arg in range(n+1):
prod.append(events)
print cartesian_product(*prod)
Output:
Number of times to roll: 1
[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6)]
you can also pass string in your events list but it'll print string in tuple also.
inside the recursive call cartesian_product(set1,prod,n-1) you are passing the list prod, and you are again appending values to it, so it just grows over time and the inner loop never terminates. Perhaps you might need to change your implementation.

A* search with multiple-goals (Python)

I am trying to write a function that completes an A* search with multiple goals. Basically it is searching a grid like structure of the form:
%%%%%%%%%%%%%%%%%%%%
%. ...P .%
%.%%.%%.%%.%%.%% %.%
% %% %..... %.%
%%%%%%%%%%%%%%%%%%%%
for a path from P that goes through all the dots (basically Pacman).
However I have run into a problem with my algorithm (which I attempted to adapt from my A* search for a single goal) as the path it returns does not go through all the dots. This is the path it returns for the above maze:
Path = [(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (1, 10), (1, 11), (1, 12), (1, 13), (1, 14), (1, 15), (1, 16)]
while a print statement shows that the visited variable has a value at return of:
[(1, 16), (1, 15), (2, 16), (1, 17), (1, 14), (3, 16), (1, 18), (1, 13), (3, 15), (2, 18), (1, 12), (2, 13), (3, 18), (3, 14), (1, 11), (3, 13), (3, 12), (1, 10), (1, 9), (3, 11), (2, 10), (1, 8), (3, 10), (1, 7), (3, 9), (1, 6), (3, 8), (2, 7), (1, 5), (3, 7), (1, 4), (3, 6), (2, 4), (1, 3), (3, 4), (1, 2), (1, 1), (2, 1)]
I think that that problem is how I am storing the current path (where each node stores its parent node, and then I return the end node and go backwards recursively to get the path). Does anyone have any advice for what I should change? I attached my current code below. Thanks!
What your algorithm is currently doing is trying to find the goal by expending its area around the starting point and finding the best path for every node its visiting.
In a single-goal situation, it works well and you can get the path to this goal.
However how you have adapted it to a multi-goal purpose is that only the stop condition changes (when all goals as been visited once), meaning that you found the shortest path from the start point to each goal but not a single path visiting all nodes.
In the case, you just want the paths from the start point to each goal, just get the path (via parents) from each goal point.
If you really want to implement a pacman-like search, this is NP-Hard problem (see this answer).
As one of the comment proposes, if you have a small list of goals, you can find a solution with brute-force:
Let's say you have 3 goals: A,B,C (which were dots):
%%%%%%%%%%%%%%%%%%%%
%A P %
% %% %% %% %%C%% % %
% %% % B % %
%%%%%%%%%%%%%%%%%%%%
Using your algorithm, you can find the shortest path from P to A, then A to B then B to C. Do the same for other permutations ((P,A,C,B),(P,B,A,C) ...): see itertools.combinations(goals, len(goals))
You can then use your algorithm to find the path from one point to the other:
def A_multiple_goals(maze, start, goals):
paths = []
for itinerary in itertools.combinations(goals, len(goals)):
path = get_path(A_search_multiple(maze, start, itinerary[0])) # First go to first goal from start
for i in range(1 , len(itinerary)): # Then from each goal, goto the next one
path += get_path(A_search_multiple(maze, itinerary[i-1], itinerary[i]))
paths.append(paths)
return min(paths, key=len)
This is a brute-force approach, if you have a lot of goals, you would need a better algorithm based around the Traveling Salesman Problem.

Categories