I am currently trying to fix my pathfinding system in my game. The A pathfinding python code is super slow, considering it has to calculate thousands of nodes each time for my grid. My grid is stored in a dictionary that has the positions of all of the walls and obstacles. Are there any ways I could speed this up signifigantly?
Here is my algorithm:
def findpath_subgrid(self, start, end):
self.subgrid_cache.clear()
start_subgrid = (start[0] // self.subgrid_size, start[1] // self.subgrid_size)
end_subgrid = (end[0] // self.subgrid_size, end[1] // self.subgrid_size)
if start_subgrid == end_subgrid:
return self.find_path(start, end)
else:
with self.lock:
if start_subgrid in self.subgrid_cache:
return self.subgrid_cache[start_subgrid]
else:
path = self.find_path(start, end)
self.subgrid_cache[start_subgrid] = path
return path
def heuristic(self, a, b):
return abs(a[0] - b[0]) + abs(a[1] - b[1])
def find_path(self, start, end):
queue = []
heapq.heappush(queue, (0, start))
came_from = {}
cost_so_far = {}
came_from[start] = None
cost_so_far[start] = 0
while queue:
current = heapq.heappop(queue)[1]
if current == end:
break
for next in self.adjacent_cells(current):
new_cost = cost_so_far[current] + self.cost(current, next)
if next not in cost_so_far or new_cost < cost_so_far[next]:
cost_so_far[next] = new_cost
priority = new_cost + self.heuristic(end, next)
heapq.heappush(queue, (priority, next))
came_from[next] = current
return self.reconstruct_path(came_from, start, end)
def adjacent_cells(self, pos):
x, y = pos
results = [(x+1, y), (x-1, y), (x, y+1), (x, y-1)]
results = filter(self.in_bounds, results)
results = filter(self.passable, results)
return results
def in_bounds(self, pos):
x, y = pos
return 0 <= x < 2000 and 0 <= y < 2000
def passable(self, pos):
return self.grid.get(pos) != 1 # check if the cell is not an obstacle using the new grid dictionary
def cost(self, current, next):
if self.grid.get(next) == 2:
return 1000 # high cost for cells with enemies
else:
return 1 # otherwise, the cost is 1
def heuristic(self, a, b):
return abs(a[0] - b[0]) + abs(a[1] - b[1])
def reconstruct_path(self, came_from, start, goal):
current = goal
path = []
while current != start:
path.append(current)
current = came_from[current]
path.append(start)
path.reverse()
return path
I've tried subgrids, cache's but its still very very slow.
If a position is extracted from the queue, you do not need to consider them again in your search (this statement is true because you are working on a grid and using manhattan distance as heuristic function). I added a variable expanded_list to function find_path and adjacent_cells (my Python writing is not the best so you can write them better later). Also it would be better if you use a 2D array for holding your grid instead of a dictionary.
def find_path(self, start, end):
queue = []
heapq.heappush(queue, (0, start))
came_from = {}
cost_so_far = {}
came_from[start] = None
cost_so_far[start] = 0
expanded_list = {}
expanded_list[current] = False
while queue:
current = heapq.heappop(queue)[1]
expanded_list[current] = True
if current == end:
break
for next in self.adjacent_cells(current):
expanded_list[next] = False
new_cost = cost_so_far[current] + self.cost(current, next)
if next not in cost_so_far or new_cost < cost_so_far[next]:
cost_so_far[next] = new_cost
priority = new_cost + self.heuristic(end, next)
heapq.heappush(queue, (priority, next))
came_from[next] = current
return self.reconstruct_path(came_from, start, end)
def adjacent_cells(self, pos, expanded_list):
x, y = pos
results = [(x+1, y), (x-1, y), (x, y+1), (x, y-1)]
results = filter(self.in_bounds, results)
results = filter(self.passable, results)
results = filter(lambda pos:not(pos in expanded_list and expanded_list[pos]==True), results)
return results
Related
I try to optimize a simple python algorithm I made that approximately solve the Traveling Salesman Problem :
import math
import random
import matplotlib.pyplot as plt
import datetime
#Distance between two point
def distance(point1, point2):
return math.sqrt((point2[0]-point1[0])**2+(point2[1]-point1[1])**2)
#TSP TimeTraveler Algorithm
def TSP_TimeTraveler(Set_Points):
print("Solving TSP")
#For calculating execution time
time_start = datetime.datetime.now()
#Copy the set points
points = Set_Points.copy()
route = []
#Take 3 points at random
route.append(points.pop(random.randint(0,len(points)-1)))
route.insert(0,points.pop(random.randint(0,len(points)-1)))
route.insert(1,points.pop(random.randint(0,len(points)-1)))
#Calulating the initial route length
Length = distance(route[0],route[1]) + distance(route[1],route[-1]) + distance(route[-1],route[0])
#Time Traveler Algorithm
while len(points)>0 :
print("Points left : ", len(points),' ', end="\r")
#Take a random point from the Set
point = points.pop(random.randint(0,len(points)-1))
###############################################################################################################
#### Finding the closest route segment by calculation all lengths posibilities and finding the minimum one ####
###############################################################################################################
Set_Lengths = []
for i in range(1,len(route)):
#Set of Lengths when the point is on each route segment except the last one
L = Length - distance(route[i-1],route[i]) + distance(route[i-1],point) + distance(point, route[i])
Set_Lengths.append((i,L))
#Adding the last length when the point is on the last segement
L = Length - distance(route[-1],route[0]) + distance(route[-1],point) + distance(point, route[0])
Set_Lengths.append((0,L))
###############################################################################################################
###############################################################################################################
#Sorting the set of lengths
Set_Lengths.sort(key=lambda k: k[1])
#Inserting the point on the minimum length segment
route.insert(Set_Lengths[0][0], point)
#Updating the new route length
Length = Set_Lengths[0][1]
#Connecting the start point with the finish point
route.append(route[0])
#For calculating execution time
time_end = datetime.datetime.now()
delta = (time_end-time_start).total_seconds()
print("Points left : ", len(points),' Done ',)
print("Execution time : ", delta, "secs")
return route
#######################
#Testing the Algorithm#
#######################
#Size of the set
size = 2520
#Generating a set of random 2D points
points = []
for i in range(size):
points.append([random.uniform(0, 100),random.uniform(0, 100)])
#Solve TSP
route = TSP_TimeTraveler(points)
#Plot the solution
plt.scatter(*zip(*points),s=5)
plt.plot(*zip(*route))
plt.axis('scaled')
plt.show()
The algorithm operate in 3 simple steps :
1/ First step I take 3 points at random from the points set and connect them as the initial route.
2/ Then each next step, I take a point at random from the set of points left. And try to find the closest segment of the route i have and connect it to it.
3/ I keep repeating step 2/ until the set of points left is empty.
Here is a gif of how the algorithm solve a set of 120 points : TimeTravelerAlgorithm.gif
I give it the name "Time Traveler" because it's operate like a greedy salesman algorithm. But instead traveling to the closest new city in the present, the greedy salesman time travel to the past to the closest city he had already visited and go visit that new city then continue his normal route.
The time traveler start a route of 3 cities, and the traveler add a new city each step in his past, until he reach a present where he visited all the cities and returned to his home city.
The algorithm give decent solutions fast for small set of points. Here is the execution time for each number of sets, all are made on a 2.6GHz dual-core Intel Core i5 processor Macbook :
120 points in around 0.03 secs
360 points in around 0.23 secs
2520 points in around 10 secs
10 000 points in around 3 mins
100 000 points in around 5 hours (Solution Map)
The algorithm is far from being optimized, because in some cases it gives cross routes which is suboptimal. And It's all made in pure python. Maybe using numpy or some advance library or even GPU can speed up the program.
I want your review and help on how to optimize it. I try to approximately solve without cross routes for set of points that can be extremely large (from 1 million to 100 billions points).
PS: My algorithm and codes are open. People from internet, feel free to use it in any project or any research you have.
Thanks for the comments. I re-implemented the algorithm using Objects, Sets and Linked list. I also removed the square root from distance function . Now the code look more clean :
import math
import random
import datetime
import matplotlib.pyplot as plt
#Distance between two point
def distance(point1, point2):
return (point2[0]-point1[0])**2 + (point2[1]-point1[1])**2
#Distance between two point
class Node:
def __init__(self, dataval=None):
self.dataval = dataval
self.nextval = None
class TSP_TimeTraveler():
def __init__(self, dataval=None):
self.count = 0
self.position = None
self.length = 0
def get_position():
return self.position
def next_city():
self.position = self.position.nextval
return self.position
#adding a city to the current route with Time Traveler Algorithm :
def add_city(self, point):
node = Node(point)
if self.count <=0 :
self.position = node
elif self.count == 1 :
node.nextval = self.position
self.position.nextval = node
self.length = 2*distance(self.position.dataval,node.dataval)
else :
#Creating the traveler
traveler = self.position
c = traveler.dataval #current position
n = traveler.nextval.dataval #next position
#Calculating the length of adding the city to the path
Min_L = self.length-distance(c,n)+distance(c,node.dataval)+distance(node.dataval,n)
Min_Node = traveler
traveler = traveler.nextval
while traveler != self.position :
c = traveler.dataval #current position
n = traveler.nextval.dataval #next position
#Calculating the length of adding the city to the path
L = self.length-distance(c,n)+distance(c,node.dataval)+distance(node.dataval,n)
#Searching the path to the of city with minimum length
if L < Min_L :
Min_L = L
Min_Node = traveler
traveler = traveler.nextval
#Adding the city to the minimum path
node.nextval = Min_Node.nextval
Min_Node.nextval = node
self.length = Min_L
#Incrementing the number of city in the route
self.count = self.count + 1
#Get the list of the route
def getRoute(self):
result = []
traveler = self.position
result.append(traveler.dataval)
traveler = traveler.nextval
while traveler != self.position :
result.append(traveler.dataval)
traveler = traveler.nextval
result.append(traveler.dataval)
return result
def Solve(self, Set_points):
print("Solving TSP")
#For calculating execution time
time_start = datetime.datetime.now()
#Copy the set points list
points = Set_points.copy()
#Transform the list into set
points = set(tuple(i) for i in points)
#Add
while len(points)>0 :
print("Points left : ", len(points),' ', end="\r")
point = points.pop()
self.add_city(point)
result = self.getRoute()
#For calculating execution time
time_end = datetime.datetime.now()
delta = (time_end-time_start).total_seconds()
print("Points left : ", len(points),' Done ',)
print("Execution time : ", delta, "secs")
return result
#######################
#Testing the Algorithm#
#######################
#Size of the set
size = 120
#Generating a set of random 2D points
points = []
for i in range(size):
points.append((random.uniform(0, 100),random.uniform(0, 100)))
#Solve TSP
TSP = TSP_TimeTraveler()
route = TSP.Solve(points)
#Plot the solution
plt.scatter(*zip(*points),s=5)
plt.plot(*zip(*route))
plt.axis('scaled')
plt.show()
And using PyPy instead of normal python it runs alot faster :
120 in around 0.03sec
360 in around 0.05sec
2520 in around 0.22sec
10 000 in around 2sec
100 000 in around 7min
The 100 000 case that took before 5 hours, now it's solved in 7min.
Next, I will try to implement a 2-opt with double linked list and KD-tree. So it can solve for large sets without crosses.
I improved the algorithm by adding double linked list and 2-opt at each insertion :
import math
import random
import datetime
import matplotlib.pyplot as plt
#Distance between two point
def distance(point1, point2):
return (point2[0]-point1[0])**2 + (point2[1]-point1[1])**2
#Intersection between two segments
def intersects(p1, q1, p2, q2):
def on_segment(p, q, r):
if r[0] <= max(p[0], q[0]) and r[0] >= min(p[0], q[0]) and r[1] <= max(p[1], q[1]) and r[1] >= min(p[1], q[1]):
return True
return False
def orientation(p, q, r):
val = ((q[1] - p[1]) * (r[0] - q[0])) - ((q[0] - p[0]) * (r[1] - q[1]))
if val == 0 : return 0
return 1 if val > 0 else -1
o1 = orientation(p1, q1, p2)
o2 = orientation(p1, q1, q2)
o3 = orientation(p2, q2, p1)
o4 = orientation(p2, q2, q1)
if o1 != o2 and o3 != o4:
return True
if o1 == 0 and on_segment(p1, q1, p2) : return True
if o2 == 0 and on_segment(p1, q1, q2) : return True
if o3 == 0 and on_segment(p2, q2, p1) : return True
if o4 == 0 and on_segment(p2, q2, q1) : return True
return False
#Distance Double Linked Node
class Node:
def __init__(self, dataval=None):
self.dataval = dataval
self.prevval = None
self.nextval = None
class TSP_TimeTraveler():
def __init__(self):
self.count = 0
self.position = None
self.length = 0
self.traveler = None
self.travelert_past = None
self.is_2opt = True
def get_position():
return self.position
def traveler_init(self):
self.traveler = self.position
self.travelert_past = self.position.prevval
return self.traveler
def traveler_next(self):
if self.traveler.nextval != self.travelert_past:
self.travelert_past = self.traveler
self.traveler = self.traveler.nextval
return self.traveler, False
else :
self.travelert_past = self.traveler
self.traveler = self.traveler.prevval
return self.traveler, True
#adding a city to the current route with Time Traveler Algorithm :
def add_city(self, point):
node = Node(point)
if self.count <=0 :
self.position = node
elif self.count == 1 :
node.nextval = self.position
node.prevval = node
self.position.nextval = node
self.position.prevval = self.position
self.length = 2*distance(self.position.dataval,node.dataval)
elif self.count == 2 :
node.nextval = self.position.nextval
node.prevval = self.position
self.position.nextval.prevval = node
self.position.nextval = node
self.length = 2*distance(self.position.dataval,node.dataval)
else :
#Creating the traveler
traveler = self.traveler_init()
c = traveler #current position
prev = False #inverse link
n, prev = self.traveler_next()
#Calculating the length of adding the city to the path
Min_prev = prev
Min_L = self.length-distance(c.dataval,n.dataval)+distance(c.dataval,node.dataval)+distance(node.dataval,n.dataval)
Min_Node = c
traveler = n
while traveler != self.position :
c = n #current position
n, prev = self.traveler_next()
#Calculating the length of adding the city to the path
L = self.length-distance(c.dataval,n.dataval)+distance(c.dataval,node.dataval)+distance(node.dataval,n.dataval)
#Searching the path to the of city with minimum length
if L < Min_L :
Min_prev = prev
Min_L = L
Min_Node = c
traveler = n
if Min_prev :
Min_Next_Node = Min_Node.prevval
else :
Min_Next_Node = Min_Node.nextval
node.nextval = Min_Next_Node
node.prevval = Min_Node
if Min_prev :
Min_Node.prevval = node
else :
Min_Node.nextval = node
if Min_Next_Node.nextval == Min_Node:
Min_Next_Node.nextval = node
else :
Min_Next_Node.prevval = node
self.length = Min_L
#2-OP
if self.is_2opt == True :
self._2opt(Min_Node, node, Min_Next_Node)
#Incrementing the number of city in the route
self.count = self.count + 1
#apply the 2opt to a-b-c
def _2opt(self, a, b, c):
traveler = self.traveler_init()
c1 = a
c2 = b
n1 = b
n2 = c
c = traveler #current position
t_prev = False
n, t_prev = self.traveler_next()
traveler = n
while traveler != self.position :
cross = False
if (c.dataval != c1.dataval and c.dataval != c2.dataval and n.dataval != c1.dataval and n.dataval != c2.dataval) and intersects(c.dataval, n.dataval, c1.dataval, c2.dataval):
self._2optswap(c,n,c1,c2)
cross = True
a = n
n = c1
c2 = a
if (c.dataval != n1.dataval and c.dataval != n2.dataval and n.dataval != n1.dataval and n.dataval != n2.dataval) and intersects(c.dataval, n.dataval, n1.dataval, n2.dataval):
self._2optswap(c,n,n1,n2)
cross = True
a = n
n = n1
n2 = a
if cross:
return
c = n #current position
n, t_prev = self.traveler_next()
traveler = n
#swap between the crossed segment a-b and c-d
def _2optswap(self, a, b, c, d):
if a.nextval == b :
a.nextval = c
else :
a.prevval = c
if b.prevval == a :
b.prevval = d
else :
b.nextval = d
if c.nextval == d :
c.nextval = a
else :
c.prevval = a
if d.prevval == c :
d.prevval = b
else :
d.nextval = b
self.length = self.length - distance(a.dataval,b.dataval) - distance(c.dataval,d.dataval) + distance(a.dataval,c.dataval) + distance(b.dataval,d.dataval)
#Get the list of the route
def getRoute(self):
result = []
traveler = self.traveler_init()
result.append(traveler.dataval)
traveler, prev = self.traveler_next()
while traveler != self.position :
result.append(traveler.dataval)
traveler, prev = self.traveler_next()
result.append(traveler.dataval)
return result
def Solve(self, Set_points, with_2opt = True):
print("Solving TSP")
#For calculating execution time
time_start = datetime.datetime.now()
#Copy the set points list
points = Set_points.copy()
#Transform the list into set
points = set(tuple(i) for i in points)
#Add
while len(points)>0 :
print("Points left : ", len(points),' ', end="\r")
point = points.pop()
self.add_city(point)
result = self.getRoute()
#For calculating execution time
time_end = datetime.datetime.now()
delta = (time_end-time_start).total_seconds()
L=0
for i in range(len(result)-1):
L = L + math.sqrt((result[i-1][0]-result[i][0])**2 + (result[i-1][1]-result[i][1])**2)
print("Points left : ", len(points),' Done ',)
print("Execution time : ", delta, "secs")
print("Average time per point : ", 1000*delta/len(Set_points), "msecs")
print("Length : ", L)
return result
#######################
#Testing the Algorithm#
#######################
#Size of the set
size = 1000
#Generating a set of random 2D points
points = []
for i in range(size):
points.append((random.uniform(0, 100),random.uniform(0, 100)))
#Solve TSP
TSP = TSP_TimeTraveler()
route = TSP.Solve(points, with_2opt = True)
plt.scatter(*zip(*route), s=5)
plt.plot(*zip(*route))
plt.axis('scaled')
plt.show()
Now the solution give fast results with no cross routes.
With PyPy it solves 100,000 points with no cross routes in 30 min.
Now Im working on implementing the KD-tree to solve for large sets.
I am currently trying to solve: https://open.kattis.com/problems/shortestpath1 which is a shortest path problem with positive weights, and I'm pretty new to Python.
I have implemented the algorithm and the main function. It works fine for their first test-case, but exceeds the time-limit of >3s for the second case, resulting in my code not being accepted as an answer, and I believe there's an error in my code(and not really an optimilization error as I believe 3 seconds should be enough time).
What can I do to improve my code, such that it passes the second test-case?
from collections import deque, namedtuple
inf = float('inf')
Edge = namedtuple('Edge', 'start, end, cost')
def make_edge(start, end, cost=1):
return Edge(start, end, cost)
class Graph:
def __init__(self, edges):
self.edges = [make_edge(*edge) for edge in edges]
#property
def vertices(self):
return set(
sum(
([edge.start, edge.end] for edge in self.edges), []
)
)
def get_node_pairs(self, n1, n2, both_ends=True):
if both_ends:
node_pairs = [[n1, n2], [n2, n1]]
else:
node_pairs = [[n1, n2]]
return node_pairs
def remove_edge(self, n1, n2, both_ends=True):
node_pairs = self.get_node_pairs(n1, n2, both_ends)
edges = self.edges[:]
for edge in edges:
if [edge.start, edge.end] in node_pairs:
self.edges.remove(edge)
def add_edge(self, n1, n2, cost=1, both_ends=True):
node_pairs = self.get_node_pairs(n1, n2, both_ends)
#for edge in self.edges:
#if [edge.start, edge.end] in node_pairs:
#return ValueError('Edge {} {} already exists'.format(n1, n2))
self.edges.append(Edge(start=n1, end=n2, cost=cost))
if both_ends:
self.edges.append(Edge(start=n2, end=n1, cost=cost))
#property
def neighbours(self):
neighbours = {vertex: set() for vertex in self.vertices}
for edge in self.edges:
neighbours[edge.start].add((edge.end, edge.cost))
return neighbours
def dijkstra(self, source, dest):
#assert source in self.vertices, 'Such source node doesn\'t exist'
distances = {vertex: inf for vertex in self.vertices}
previous_vertices = {
vertex: None for vertex in self.vertices
}
distances[source] = 0
vertices = self.vertices.copy()
while vertices:
current_vertex = min(
vertices, key=lambda vertex: distances[vertex])
vertices.remove(current_vertex)
if distances[current_vertex] == inf:
break
for neighbour, cost in self.neighbours[current_vertex]:
alternative_route = distances[current_vertex] + cost
if alternative_route < distances[neighbour]:
distances[neighbour] = alternative_route
previous_vertices[neighbour] = current_vertex
#Deque er som list men med O(1) over O(N) for list.pop()
path, current_vertex = deque(), dest
while previous_vertices[current_vertex] is not None:
path.appendleft(current_vertex)
current_vertex = previous_vertices[current_vertex]
if path:
path.appendleft(current_vertex)
#return path here to get the deque with path as list of strings
#need the distance:
d = 0
for index in range(len(path)):
for x in self.edges:
if x.start == path[index-1] and x.end == path[index]:
d += x.cost
return d
if __name__ == "__main__":
#edge1 = Edge("0", "1", 2)
#edge2 = Edge("1", "2", 2)
#testGraph2()
#while dette får input..
n, m, q, s = [0, 0, 0, 0]
u, v, w = [0, 0, 0]
while True:
edges = []
[n, m, q, s] = input().split()
#print("n: ", n, "m: ", m, "q", q, "s: ", s)
if n == "0" and m == "0" and q == "0" and s == "0":
break
for i in range(int(m)):
[u, v, w] = input().split()
edges.append(Edge(u, v, int(w)))
graph = Graph(edges)
for j in range(int(q)):
q = input()
distance = graph.dijkstra(s, q)
if distance == 0 and s != q:
print("Impossible")
else:
print(distance)
print("\n")
If I have a list class that can be initialized with a variable number of dimensions, how do I set an entry at the lowest level of the list with an element? (Also would like to know if my get method should work in theory)
I'm trying to simulate board games that use multiple dimensions (Can you even imagine 5-th dimensional chess? How about 17th?)
class Board():
DIMENSIONS = [8, 8]
#board and pieces have their respective rules.
def __init__(self, D=[8,8]):
if len(D) <= 0:
board = [None for i in range(D)]
else:
board = [None for i in range(D[0])]
for j in range(1,len(D)):
board = [board for i in range(D[j])]
def get(self, location):
try:
for coordinate in location:
result = board[coordinate]
return result
except:
print('Error: Cannot get coordinate')
return None
def set(self, location, piece):
try:
for coordinate in location:
result = self.board[coordinate]
result = piece
except:
print('Error: Cannot set coordinate')
def move(self, start, end):
x = self.get(start)
if x is not None:
for m, r in x.moves, x.rules:
if self.get(is_legitimate(self, start, m, r)) == end:
= x
pass
#Check alignment and position, if it's transformable react according to board rules.
#returns false if not, but returns location if legit.
def is_legitimate(self, start, move, rule):
location = start
forwardback = True
while move != 1:
primes = [2]
while move % primes[-1] == 0:
if forwardback:
location[len(primes) // 2]+=1
else:
location[len(primes) // 2]-=1
move = move % primes[-1]
if not self.bounds(location):
return False
primes.append(next_prime(primes))
forwardback = not forwardback
def bounds(self, location):
for coordinate, d in location, self.DIMENSIONS:
if coordinate < 0 or coordinate > d:
return False
return True
#using prime numbers?
def next_prime(primes):
if len(primes) == 0:
return 2
prev_result = 1
result = 2
while prev_result != result:
prev_result = result
for x in primes:
if result == x or result % x == 0:
result += 1
break
return result
Code is mostly rough draft, don't play just look.
I keep getting an
IndexError: list assignment index out of range.
The error on line 78
This code is written to find motif DNA to bioinformatics
How we can solve this error or the problem ?
Here is my code:
from math import log
class MotifMedianFinding(object):
def __init__(self, input_file):
super(MotifMedianFinding, self).__init__()
self.input_lines = open("C:\\Users\\A.Khassawneh\\Desktop\\fasta.txt")
def output(self):
#main method to call both functions
sequences = {}
for line in self.input_lines:
if '>' in line:
sequences[line] = self.input_lines.next()
for label, seq in sequences.iteritems():
print "DNA:" + seq + "\n\n\n\n\n"
median = self.median_string(seq, 5,5, len(seq))
self.motif(seq, median,5,len(seq))
def median_string(self, dna, t, n, l):
#bound and search method of calulating median string
start_pos = start_pos = [1,1,1,1,1]
best_dist = 1000000000
i = 1
while i > 0:
if i < l:
prefix = str(start_pos)
opt_dist = self.hamming_score(prefix, dna)
if opt_dist > best_dist:
s,i = self.bypass(start_pos,i,l,4)
else:
s,i = self.next_vertex(start_pos,i,l,4)
else:
word = str(s)
if self.hamming_score(word, dna) < best_dist:
best_dist = self.hamming_score(word, dna)
bestword = word
s,i = self.next_vertex(start_pos,i,l,4)
print "Best Word: %s (tot_dis = %s)" % (bestword,best_dist)
return bestword
def motif(self, dna, t, n, l):
#bound and search method of calculating motif
start_pos = [1,1,1,1,1]
best_score = 0
i = 1
while 1 > 0:
if i < t:
opt_score = Score(s, i, dna) + (t-1) * l
if opt_score < best_score:
start_pos, i = self.bypass(start_pos, i, t, n-l+1)
else:
start_pos, i = self.next_vertex(start_pos, i, t, n-l+1)
else:
if self.score(start_pos, dna) > best_score:
best_score = self.score(start_pos)
best_motif = str(s)
start_pos, i = self.next_vertex(start_pos, i, t, n-l+1)
print "motif consensus string: %s (consensus_score = %s) " % (best_motif, best_score)
print "motif positions/string s=(s1..st): %s" % ', '.join(start_pos)
return best_motif
def bypass(vertex, level, l, k):
#skip uncessary calculations in the tree
j = level
for ind in xrange(j,1,-1):
if a[j] < k:
a[j] = a[j] + 1
return vertex, j
return vertex, 0
def next_vertex(self, vertex, level, L, k):
#transverse the tree of a strand of genes
if level <L:
vertex[level+1] = 1
return vertex,level+1
else:
j = L
for ind in xrange(j,1,-1):
if vertex[ind] < k:
vertex[j] = vertex[j] + 1
return vertex, j
return vertex, 0
def score(start_pos):
# biggest score of motif
total = 0
for i in start_pos:
total += i
return total
def hamming_score(self, s, dna):
pass
motif_median = MotifMedianFinding('HMP-part.fa')
motif_median.output()
xrange(x,y) goes from x to y-1 (x, x+1.... y-1). In your code, it would have been fine to do xrange(1,j), because that wouldn't have included j. But if you swap it to xrange(j,1,-1), you go (j, j-1.... 2).
Basically, you probably need to change it to xrange(j-1,0,-1) depending on your intended range.
I am trying to implement the A* algorithm in python but have hit a problem when trying to find the path of this map:
X X X X X X X S = Start
0 0 0 X 0 0 0 E = End
0 S 0 X 0 E 0 X = Wall
0 0 0 X 0 0 0
0 0 0 0 0 0 0
I am using the Manhattan method. My implementation does find a path, but not the shortest one. The error starts on it's second move -- after moving right. At this point it can move up and the heuristic cost would be four (three right, one down) or down (three right, one up). Is there a way so it chooses down to get the shortest path?
Code:
class Node:
def __init__(self, (x, y), g, h, parent):
self.x = x
self.y = y
self.g = g
self.h = h
self.f = g+h
self.parent = parent
def __eq__(self, other):
if other != None:
return self.x == other.x and self.y == other.y
return False
def __lt__(self, other):
if other != None:
return self.f < other.f
return False
def __gt__(self, other):
if other != None:
return self.f > other.f
return True
def __str__(self):
return "(" + str(self.x) + "," + str(self.y) + ") " + str(self.f)
def find_path(start, end, open_list, closed_list, map, no_diag=True, i=1):
closed_list.append(start)
if start == end or start == None:
return closed_list
new_open_list = []
for x, y in [(-1,1),(-1,-1),(1,-1),(1,1),(0,-1),(0,1),(-1,0),(1,0)]:
full_x = start.x + x
full_y = start.y + y
g = 0
if x != 0 and y != 0:
if no_diag:
continue
g = 14
else:
g = 10
h = 10 * (abs(full_x - end.x) + abs(full_y - end.y))
n = Node((full_x,full_y),g,h,start)
if 0 <= full_y < len(map) and 0 <= full_x < len(map[0]) and map[full_y][full_x] != 1 and n not in closed_list:
if n in open_list:
if open_list[open_list.index(n)].g > n.g:
new_open_list.append(n)
else:
new_open_list.append(open_list[open_list.index(n)])
else:
new_open_list.append(n)
if new_open_list == None or len(new_open_list) == 0:
return find_path(start.parent, end, open_list, closed_list, map, no_diag, i-1)
new_open_list.sort()
return find_path(new_open_list[0], end, new_open_list, closed_list, map, no_diag)
You seem to be constructing a new open list for each node, which contains only that node's neighbors. This essentially makes your search a form of a depth-first search, while A* should be a best-first search.
You need to use one open list which will be updated with each node's neighbors as you visit that node. The old nodes in the open list must remain there until they are traversed and moved to the closed list.
Regarding what you said in your question, It's OK for the search to try moving up before down (since according to the heuristic, they are the same distance from the goal). What matters is that in the end, the path chosen will be the shortest.