Skip to content

Commit

Permalink
hacky GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
elben committed Aug 6, 2009
1 parent 2419e8f commit a625cbe
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 14 deletions.
3 changes: 2 additions & 1 deletion kapal/algo.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import heapq
from state import *
from world import *
import sys

class Algo:
def __init__(self, world, start, goal):
Expand Down Expand Up @@ -55,7 +56,7 @@ def plan_gen(self):
if n.g > s.g + cost:
# s improves n
n.g = s.g + cost
n.h = self.h(n, self.goal)
n.h = self.h(n, goal)
n.bp = s
heapq.heappush(self.open, n)
yield s
Expand Down
7 changes: 4 additions & 3 deletions kapal/state.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import kapal

class State:
pass

Expand All @@ -9,15 +11,14 @@ def __str__(self):
return "(" + str(self.y) + ", " + str(self.x) + ")"

class State2dAStar(State2d):
inf = 1e100 # = float('inf')
def __init__(self, y=0, x=0, g=inf, h=0, bp=None):
def __init__(self, y=0, x=0, g=kapal.inf, h=0, bp=None):
State2d.__init__(self, y, x)
self.g = g
self.h = h
self.bp = bp

def reset(self):
self.g = State2dAStar.inf
self.g = kapal.inf

def __cmp__(self, other):
# TODO: allow any key function?
Expand Down
10 changes: 6 additions & 4 deletions kapal/world.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from state import *
import math

class World:
"""
Expand Down Expand Up @@ -67,9 +66,12 @@ def c(self, s1, s2):
return costs[s2.y][s2.x]

def h(self, s1, s2):
dy = abs(s2.y - s1.y)
dx = abs(s2.x - s1.x)
return math.sqrt(dx**2 + dy**2)
if self.diags:
dy = abs(s2.y - s1.y)
dx = abs(s2.x - s1.x)
return math.sqrt(dx**2 + dy**2)
else:
return abs(s2.y-s1.y) + abs(s2.x-s1.x)

def change_c(self, s1, s2, c):
if not self.in_bounds(s2.y, s2.x):
Expand Down
71 changes: 71 additions & 0 deletions seaship/seaship.pyw
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import sys
import PyQt4
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import kapal
import kapal.algo
import kapal.state
import kapal.world
import kapal.tools
import copy

class SeashipMainWindow(QMainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)

self.setGeometry(100, 100, 400, 400)
self.setWindowTitle('Seaship')
self.painter = QtGui.QPainter()

# A* test
width = 10
self.c = kapal.tools.rand_cost_map(width, width, 1, kapal.inf,
flip=True, flip_chance=.0)
self.c2 = copy.deepcopy(self.c)
w = kapal.world.World2d(self.c, state_type = kapal.state.State2dAStar)

start_y = 2
start_x = 2
goal_y = 8
goal_x = 4
astar = kapal.algo.AStar(w, w.state(start_y,start_x),
w.state(goal_y, goal_x))
num_popped = 0
for s in astar.plan_gen():
self.c2[s.y][s.x] = -1
num_popped += 1
print num_popped
for s in astar.path():
self.c2[s.y][s.x] = -2

def paintEvent(self, event):
self.draw_world2d(self.painter, self.c2)

def draw_world2d(self, painter, world,
x_start=0, y_start=0, x_goal=0, y_goal=0):
for r in range(len(world)):
for c in range(len(world[r])):
color = (0, 80, 255, 255) # blue
if world[r][c] == -1:
color = (255, 0, 0, 255) # red
elif world[r][c] == -2:
color = (0, 255, 0, 255) # green
elif world[r][c] == kapal.inf:
color = (0, 0, 128, 255) # blue
self.draw_square(painter, c, r, color=color)

def draw_square(self, painter, x=0, y=0, color=(0, 0, 0, 0), size=32, brush=None):
painter.begin(self)
if brush is None:
brush = QtGui.QBrush(QtCore.Qt.SolidPattern)
r, g, b, a = color
brush.setColor(QtGui.QColor(r, g, b, a))
painter.setBrush(brush)
painter.drawRect(x*size, y*size, size, size)
painter.end()

app = QtGui.QApplication(sys.argv)
seawin = SeashipMainWindow()
seawin.show()
app.exec_()
12 changes: 6 additions & 6 deletions tests/astar.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from kapal.state import *
import kapal.tools
import time
import matplotlib.pyplot as plt
#import matplotlib.pyplot as plt

def main():
"""
Expand All @@ -23,7 +23,7 @@ def main():
while width < max_width:
print width

c = tools.rand_cost_map(width, width, 1, 3, flip=True)
c = kapal.tools.rand_cost_map(width, width, 1, 3, flip=True)
w = World2d(c, state_type = State2dAStar)

astar = AStar(w, w.state(0,0), w.state(width-1, width-1))
Expand All @@ -38,8 +38,8 @@ def main():
width += 50
print x_axis
print y_axis
plt.plot(x_axis, y_axis)
plt.ylabel('time (s)')
plt.xlabel('world size (width and height)')
plt.show()
#plt.plot(x_axis, y_axis)
#plt.ylabel('time (s)')
#plt.xlabel('world size (width and height)')
#plt.show()
main()

0 comments on commit a625cbe

Please sign in to comment.