#NEWLINE# def ring_topology( random , population , args ) : #NEWLINE# #INDENT# 'Returns the neighbors using a ring topology.\n    \n    This function sets all particles in a specified sized neighborhood\n    as neighbors for a given particle. This is known as a ring \n    topology. The resulting list of lists of neighbors is returned.\n    \n    .. Arguments:\n       random -- the random number generator object\n       population -- the population of particles\n       args -- a dictionary of keyword arguments\n\n    Optional keyword arguments in args:\n    \n    - *neighborhood_size* -- the width of the neighborhood around a \n      particle which determines the size of the neighborhood\n      (default 3)\n    \n    ' #NEWLINE# neighborhood_size = args . setdefault ( 'neighborhood_size' , 3 ) #NEWLINE# half_hood = ( neighborhood_size // 2 ) #NEWLINE# neighbor_index_start = [ ] #NEWLINE# for index in range ( len ( neighbor_index_start ) ) : #NEWLINE# #INDENT# if ( index < half_hood ) : #NEWLINE# #INDENT# neighbor_index_start . append ( ( ( len ( population ) - half_hood ) + index ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# neighbor_index_start . append ( ( index - half_hood ) ) #NEWLINE# #UNINDENT# #UNINDENT# neighbors = [ ] #NEWLINE# for start in neighbor_index_start : #NEWLINE# #INDENT# n = [ ] #NEWLINE# for i in range ( 0 , neighborhood_size ) : #NEWLINE# #INDENT# n . append ( population [ ( ( start + i ) % len ( population ) ) ] ) #NEWLINE# #UNINDENT# ( yield n )