Skip to content

Commit 246ecbf

Browse files
author
lsampras
committed
refactor visualization function
1 parent f8df193 commit 246ecbf

File tree

3 files changed

+25
-23
lines changed

3 files changed

+25
-23
lines changed

Visualization/MapVisualizer.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import matplotlib.pyplot as plt
2+
from matplotlib import colors
3+
import matplotlib as mpl
4+
import numpy as np
5+
6+
7+
def saveColorGradedMap(name,map,color='Reds',maxRange=1.0,minRange=0.0,underRange=(0.,0.,0.),overRange=(1.,1.,1.)):
8+
norm = mpl.colors.Normalize(vmin=minRange, vmax=maxRange)
9+
cmap = mpl.cm.get_cmap(color)
10+
cmap.set_under(underRange)
11+
cmap.set_over(overRange)
12+
13+
fig, ax = plt.subplots()
14+
ax.imshow(map, cmap=cmap, norm=norm)
15+
# draw gridlines
16+
ax.grid(which='major', axis='both', linestyle='-', color='k', linewidth=2)
17+
ax.set_xticks(np.arange(-0.5, map.shape[0], 1))
18+
ax.set_yticks(np.arange(-0.5, map.shape[1], 1))
19+
plt.savefig(name + ".png")
20+
plt.close(fig)

pheromone-driver.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import numpy as np
33
import moves
44
import random
5+
from Visualization.MapVisualizer import saveColorGradedMap
6+
57
np.set_printoptions(formatter={'float': lambda x: "{0:0.3f}".format(x)})
68

79
def getObstacleData(size,prob):
@@ -19,15 +21,13 @@ def addEnemy():
1921
for i in range(40):
2022
addEnemy()
2123

22-
# make sure directory exists
24+
2325

2426
for turn in range(10):
2527
if(turn%1 == 0):
26-
# print("\n\n\n")
27-
# print(ph.map)
2828
addEnemy()
29-
ph.save_map("pheromone-sample/{}".format(turn))
29+
# TODO: make sure directory exists
30+
saveColorGradedMap("pheromone-sample/{}".format(turn),ph.map,underRange=(0.,0.,1.))
3031
ph.propogate()
3132

3233

33-
ph.save_map("pheromone-sample/{}".format(turn))

pheromone.py

-18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
import numpy as np
2-
import matplotlib.pyplot as plt
3-
from matplotlib import colors
4-
import matplotlib as mpl
52
from point import Point
63
class pheromone(object):
74
def __init__(self,obstacleData,possible_moves,decayRate = 0.7,dropOff = 0.1):
@@ -13,15 +10,11 @@ def __init__(self,obstacleData,possible_moves,decayRate = 0.7,dropOff = 0.1):
1310
self.moves.append(Point(0,0))
1411
self.multiplier = (1 - decayRate) #normalize
1512
self.temp = np.zeros(shape = self.map.shape,dtype = np.float32)
16-
self.cmap = mpl.cm.Reds
17-
self.cmap.set_under((0.,0.,1.))
18-
self.norm = mpl.colors.Normalize(vmin=0, vmax=1)
1913
return
2014

2115
def addEnemy(self,x,y):
2216
self.map[x][y] = 1.0
2317

24-
2518
def decay(self):
2619
self.map = self.map*self.multiplier
2720

@@ -49,14 +42,3 @@ def propogate_point(self,value,x,y):
4942
delta = (value * self.multiplier)/len(changes)
5043
for pos in changes:
5144
self.temp[pos[0]][pos[1]] +=delta
52-
53-
54-
def save_map(self,name):
55-
fig, ax = plt.subplots()
56-
ax.imshow(self.map, cmap=self.cmap, norm=self.norm)
57-
# draw gridlines
58-
ax.grid(which='major', axis='both', linestyle='-', color='k', linewidth=2)
59-
ax.set_xticks(np.arange(-0.5, self.map.shape[0], 1));
60-
ax.set_yticks(np.arange(-0.5, self.map.shape[1], 1));
61-
plt.savefig(name + ".png")
62-
plt.close(fig)

0 commit comments

Comments
 (0)