11from enum import Enum , auto
22
3- import mesa
3+ from mesa . discrete_space import FixedAgent
44
55
66class State (Enum ):
@@ -9,28 +9,34 @@ class State(Enum):
99 EATING = auto ()
1010
1111
12- class ForkAgent (mesa . Agent ):
13- def __init__ (self , model , position ):
12+ class ForkAgent (FixedAgent ):
13+ def __init__ (self , model ):
1414 super ().__init__ (model )
15- self .position = position
1615 self .is_used = False
1716 self .owner = None
1817
18+ @property
19+ def position (self ):
20+ return self .cell .coordinate
21+
1922 def __repr__ (self ):
2023 owner = None if self .owner is None else getattr (self .owner , "position" , None )
2124 return f"Fork-{ self .position } (used={ self .is_used } , owner={ owner } )"
2225
2326
24- class PhilosopherAgent (mesa . Agent ):
25- def __init__ (self , model , position ):
27+ class PhilosopherAgent (FixedAgent ):
28+ def __init__ (self , model ):
2629 super ().__init__ (model )
27- self .position = position
2830 self .state = State .THINKING
2931 self .total_eaten = 0
3032 self .ticks_since_state_change = 0
3133 self .total_wait_time = 0
3234 self .eating_count = 0
3335
36+ @property
37+ def position (self ):
38+ return self .cell .coordinate
39+
3440 def _start_eating (self ):
3541 self .total_wait_time += self .ticks_since_state_change
3642 self .eating_count += 1
@@ -59,8 +65,7 @@ def step(self):
5965 self .ticks_since_state_change = 0
6066
6167 def put_down_forks (self ):
62- neighbors = self .model .grid .get_neighbors (self .pos , include_center = False )
63- my_forks = [a for a in neighbors if isinstance (a , ForkAgent )]
68+ my_forks = self ._get_neighbor_forks ()
6469
6570 for fork in my_forks :
6671 if fork .owner == self :
@@ -78,8 +83,7 @@ def try_to_eat(self):
7883 self .eat_strategy_cooperative ()
7984
8085 def eat_strategy_naive (self ):
81- neighbors = self .model .grid .get_neighbors (self .pos , include_center = False )
82- my_forks = [a for a in neighbors if isinstance (a , ForkAgent )]
86+ my_forks = self ._get_neighbor_forks ()
8387
8488 left_pos = (self .position - 1 ) % self .model .num_nodes
8589 right_pos = (self .position + 1 ) % self .model .num_nodes
@@ -100,8 +104,7 @@ def eat_strategy_naive(self):
100104 pass
101105
102106 def eat_strategy_atomic (self ):
103- neighbors = self .model .grid .get_neighbors (self .pos , include_center = False )
104- my_forks = [a for a in neighbors if isinstance (a , ForkAgent )]
107+ my_forks = self ._get_neighbor_forks ()
105108
106109 if all (not fork .is_used for fork in my_forks ):
107110 for fork in my_forks :
@@ -110,8 +113,7 @@ def eat_strategy_atomic(self):
110113 self ._start_eating ()
111114
112115 def eat_strategy_cooperative (self ):
113- neighbors = self .model .grid .get_neighbors (self .pos , include_center = False )
114- my_forks = [a for a in neighbors if isinstance (a , ForkAgent )]
116+ my_forks = self ._get_neighbor_forks ()
115117
116118 # If any fork is used, we can't eat anyway
117119 if any (fork .is_used for fork in my_forks ):
@@ -123,7 +125,7 @@ def eat_strategy_cooperative(self):
123125
124126 neighbors_p = [
125127 p
126- for p in self .model .philosophers
128+ for p in self .model .agents_by_type [ PhilosopherAgent ]
127129 if p .position in (left_p_pos , right_p_pos )
128130 ]
129131
@@ -151,3 +153,10 @@ def eat_strategy_cooperative(self):
151153
152154 def __repr__ (self ):
153155 return f"Phil-{ self .position } ({ self .state .name } )"
156+
157+ def _get_neighbor_forks (self ):
158+ return [
159+ agent
160+ for agent in self .cell .neighborhood .agents
161+ if isinstance (agent , ForkAgent )
162+ ]
0 commit comments