0

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
bhapi
  • 97
  • 3
  • I'm going to assume your 2D array is a numpy array; if not, please edit the question to be more specific. Anyway, your problem is underspecified, but once you decide on the math you need, implementing it will be easy. If "the rate of change of A" Is it just the number of cells that are A minus the number of cells that were A in the previous step, then delta A will always be -delta B, so your rule just tests for them both being 0, which is very easy—just subtract the last step's array from the current one, sum the result, and see if it's 0. – abarnert May 05 '18 at 19:06
  • More generally: "I seem to be implementing this incorrectly" is a very hard problem to solve without seeing a [mcve]. If you show us code that doesn't work, with input and desired vs. actual output, it should be easy to show how to fix it. But just giving us a vague problem specification and then telling us that some code we can't see seems to be wrong in some unspecified way, how can we help? – abarnert May 05 '18 at 19:07
  • @abarnert I added my code if you wanna take a look at it ? – bhapi May 05 '18 at 19:23
  • From a quick skim: `if [i, j] == 1` is never going to be true. You're asking if a list of two integers equals an integer, which is impossible. You've also got at least one `IndentationError`. – abarnert May 05 '18 at 19:44
  • @abarnert The first part of the code does work , I'm not actually asking if a list of two integers is one integer I'm asking if the value of the grid at position i,j is equal to 1, which it will be if that cell is voting for A – bhapi May 05 '18 at 19:47
  • No you aren't. `grid[i, j]` may be the value of some grid at position `i, j`, but just `[i, j]` is a list of the two values `i` and `j`. – abarnert May 05 '18 at 19:48
  • @abarnert Sorry , I misunderstood what you meant , that was a typo it was supposed to be world[i,j] , i thought you were referring to earlier in the code. The problem is though that I can't get to that part of my code because the function is never hitting the break , because I'm defining when equilbrium is reached incorrectly, here equal ==N/2 is how i was trying to implement it – bhapi May 05 '18 at 19:52
  • I _was_ referring to earlier in the code—although now I see that you made the same mistake _again_ in the second block of code. And meanwhile, the code still won't run because of `IndentationError`s and possibly other problems, which makes it very hard to debug. – abarnert May 05 '18 at 19:56

0 Answers0