I have two parties A=1 and B=0, being used in a model the distribution of votes for each party on a 2d array. In my code Ive set up rules that if the majority of neighbors in an nine cell neighborhood around a central cell ( central cell included) then the central cell will vote with the majority.
This part works fine but the next step im trying to implement is that when this system reaches equilibrium , we must change the rules.
I'm having trouble figuring out how to define equilibrium. Originally I thought it would be when 50% of the cells would be voting for A and the rest for B. But after studying the system i noticed it often had stable states where the distribution was not 50 50.
So now I think that i should measure the rate of change instead? And say that when the rate of change of A is equal to the rate of change of b then the sytem will have reached equilibrium. I seem to be implementing this incorrectly though ( or perhaps this idea wont work at all either ), is there a pre-existing function in python that could give me a rate of change or will I have to build some kind of function to calculate it ?
Edit: I've added my code below, sorry for not putting this up originally I'm not a really a prolific poster on this particular stack exchange so i wasnt sure how much i should put up
def update_world(frameNum, world, N):
# copy grid since we require 8 neighbors for calculation
# and we go line by line
newworld = world.copy()
broken=False
for i in range(N):
for j in range(N):
# compute 8-neghbor sum using toroidal boundary conditions
# x and y wrap around so that the simulation
# takes place on a toroidal surface
Num_neighbours = int((world[i, (j-1)%N] + world[i, (j+1)%N] +
world[(i-1)%N, j] + world[(i+1)%N, j] +
world[(i-1)%N, (j-1)%N] + world[(i-1)%N, (j+1)%N] +
world[(i+1)%N, (j-1)%N] + world[(i+1)%N (j+1)%N]+world[i,j]))
equal= np.sum((world))
if [i, j] == 1:
if Num_neighbours >= 5:
newworld[i, j] = 1
elif Num_neighbours <5:
newworld[i,j] =0
else:
if Num_neighbours <5 :
newworld[i, j] = 0
elif Num_neighbours >=5:
newworld[i,j] =1
#break from the sytem when status quo hits equibrium
if equal ==N/2:
broken = True
break
if broken == True:
break
So once I hit this break statemnt then I want the system to start running these new rules
for i in range(N):
for j in range(N):
if [i, j] == 1:
if Num_neighbours >= 6:
newworld[i, j] = 1
elif Num_neighbourse <6:
newworld[i,j] =0
else:
if Num_neighbours <4 :
newworld[i, j] = 0
elif Num_neighbours >=4:
newworld[i,j] =1
world[:] = newworld[:]
return world