本文整理匯總了Python中mesa.space.SingleGrid.is_cell_empty方法的典型用法代碼示例。如果您正苦於以下問題:Python SingleGrid.is_cell_empty方法的具體用法?Python SingleGrid.is_cell_empty怎麽用?Python SingleGrid.is_cell_empty使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類mesa.space.SingleGrid
的用法示例。
在下文中一共展示了SingleGrid.is_cell_empty方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: ShapesModel
# 需要導入模塊: from mesa.space import SingleGrid [as 別名]
# 或者: from mesa.space.SingleGrid import is_cell_empty [as 別名]
class ShapesModel(Model):
def __init__(self, N, width=20, height=10):
self.running = True
self.N = N # num of agents
self.headings = ((1, 0), (0, 1), (-1, 0), (0, -1)) # tuples are fast
self.grid = SingleGrid(width, height, torus=False)
self.schedule = RandomActivation(self)
self.make_walker_agents()
def make_walker_agents(self):
unique_id = 0
while True:
if unique_id == self.N:
break
x = random.randrange(self.grid.width)
y = random.randrange(self.grid.height)
pos = (x, y)
heading = random.choice(self.headings)
# heading = (1, 0)
if self.grid.is_cell_empty(pos):
print("Creating agent {2} at ({0}, {1})"
.format(x, y, unique_id))
a = Walker(unique_id, self, pos, heading)
self.schedule.add(a)
self.grid.place_agent(a, pos)
unique_id += 1
def step(self):
self.schedule.step()
示例2: SeparationBarrierModel
# 需要導入模塊: from mesa.space import SingleGrid [as 別名]
# 或者: from mesa.space.SingleGrid import is_cell_empty [as 別名]
#.........這裏部分代碼省略.........
settler = Settler(self.unique_id, (x, y),
vision=self.settler_vision, model=self, breed="Settler")
self.unique_id += 1
self.grid.position_agent(settler, x,y)
self.schedule.add(settler)
def add_settler(self, pos):
settler = Settler(self.unique_id, pos,
vision=self.settler_vision, model=self, breed="Settler")
self.unique_id += 1
self.grid.position_agent(settler, pos[0], pos[1])
self.schedule.add(settler)
def set_barrier(self,victim_pos, violent_pos):
#print("Set barrier - Greed level", self.greed_level)
visible_spots = self.grid.get_neighborhood(victim_pos,
moore=True, radius=self.greed_level + 1)
furthest_empty = self.find_furthest_empty_or_palestinian(victim_pos, visible_spots)
x,y = furthest_empty
current = self.grid[y][x]
#print ("Set barrier!!", pos, current)
free = True
if (current is not None and current.breed == "Palestinian"):
#print ("Relocating Palestinian")
free = self.relocate_palestinian(current, current.pos)
if (free):
barrier = Barrier(-1, furthest_empty, model=self)
self.grid.position_agent(barrier, x,y)
# Relocate the violent palestinian
#violent_x, violent_y = violent_pos
#if violent_pos != furthest_empty:
# violent_palestinian = self.grid[violent_y][violent_x]
# self.relocate_palestinian(violent_palestinian, furthest_empty)
def relocate_palestinian(self, palestinian, destination):
#print ("Relocating Palestinian in ", palestinian.pos, "To somehwhere near ", destination)
visible_spots = self.grid.get_neighborhood(destination,
moore=True, radius=palestinian.vision)
nearest_empty = self.find_nearest_empty(destination, visible_spots)
#print("First Nearest empty to ", palestinian.pos, " Is ", nearest_empty)
if (nearest_empty):
self.grid.move_agent(palestinian, nearest_empty)
else:
#print ("Moveing to random empty")
if (self.grid.exists_empty_cells()):
self.grid.move_to_empty(palestinian)
else:
return False
return True
def find_nearest_empty(self, pos, neighborhood):
nearest_empty = None
sorted_spots = self.sort_neighborhood_by_distance(pos, neighborhood)
index = 0
while (nearest_empty is None and index < len(sorted_spots)):
if self.grid.is_cell_empty(sorted_spots[index]):
nearest_empty = sorted_spots[index]
index += 1
return nearest_empty
def find_furthest_empty_or_palestinian(self, pos, neighborhood):
furthest_empty = None
sorted_spots = self.sort_neighborhood_by_distance(pos, neighborhood)
sorted_spots.reverse()
index = 0
while (furthest_empty is None and index < len(sorted_spots)):
spot = sorted_spots[index]
if self.grid.is_cell_empty(spot) or self.grid[spot[1]][spot[0]].breed == "Palestinian" :
furthest_empty = sorted_spots[index]
index += 1
return furthest_empty
def sort_neighborhood_by_distance(self, from_pos, neighbor_spots):
from_x, from_y = from_pos
return sorted(neighbor_spots, key = lambda spot: self.eucledean_distance(from_x, spot[0], from_y, spot[1], self.grid.width, self.grid.height))
def eucledean_distance(self, x1,x2,y1,y2,w,h):
# http://stackoverflow.com/questions/2123947/calculate-distance-between-two-x-y-coordinates
return math.sqrt(min(abs(x1 - x2), w - abs(x1 - x2)) ** 2 + min(abs(y1 - y2), h - abs(y1-y2)) ** 2)
def step(self):
"""
Advance the model by one step and collect data.
"""
self.violence_count = 0
# for i in range(100):
self.schedule.step()
self.total_violence += self.violence_count
# average = self.violence_count / 100
#print("Violence average %f " % average)
print("Total Violence: ", self.total_violence)