3. Python code

# Below import lines loads the necessary libraries required for running the code

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.pyplot import cm
import time
import pylab


# m number of squares
# c number of colors
# Klimit numbre of iterations limit

m=21
Klimit = 10000
c=5

 #Below code assign the current system time to start_time
start_time = time.time()


compare = np.zeros((m,m))
compare[1::2,1::2]=1



xinitial = np.random.randint(c, size=(m, m))

print(compare)
print(xinitial)


plt.subplot(121)
plt.pcolor(xinitial)
plt.title('Initial Random Plot')
plt.grid(True)

# Below K is used for incremental purpose for comparing Klimit with it

K=0


x = xinitial
for i in range(0,len(x),1):
            plt.subplot(122)
            plt.pcolor(x)
            plt.show(block=False)
            plt.title('After')
            plt.pause(0.1)
            if (i >= 1) and (i <(len(x))-1):
                for j in range(0,len(x[i]),1):
                    if (j >= 1) and (j <(len(x))-1):        

                        if compare[i][j]==1:
                            K=0
                            while (K<Klimit) or not((x[i-1][j-1]==x[i+1][j+1]==x[i-1][j+1]==x[i+1][j-1])) or not(x[i-1][j]==x[i-1][j]==x[i][j-1]==x[i][j+1]):
                                    K=K+1
                                    #print(K)
                                    if compare[i-1][j]!=2:
                                        x[i-1][j]=np.random.randint(c, size=(1, 1))
                                    if  compare[i-1][j]!=2:
                                        x[i-1][j]=np.random.randint(c, size=(1, 1))
                                    if compare[i][j-1]!=2:
                                        x[i][j-1]=np.random.randint(c, size=(1, 1))
                                    if compare[i][j+1]!=2:
                                        x[i][j+1]=np.random.randint(c, size=(1, 1))
                                   
                                    if compare[i-1][j-1]!=2:                                  
                                        x[i-1][j-1]=np.random.randint(c, size=(1, 1))
                                      
                                    if compare[i+1][j+1]!=2:
                                        x[i+1][j+1]=np.random.randint(c, size=(1, 1))
                                      
                                    if compare[i-1][j+1]!=2:  
                                        x[i-1][j+1]=np.random.randint(c, size=(1, 1))
                                      
                                    if compare[i+1][j-1]!=2:  
                                        x[i+1][j-1]=np.random.randint(c, size=(1, 1))
                                   
                                    x[i,j]=np.random.randint(c,size=(1,1))
                                  
                                    if K==Klimit-1:
                                        print(str(i)+" rownotmapped"+str(j)+" colnotmapped")
                                      
                                    if (x[i-1][j]==x[i-1][j]==x[i][j-1]==x[i][j+1]):
                                      
                                        compare[i-1][j]=2
                                        compare[i-1][j]=2
                                        compare[i][j-1]=2
                                        compare[i][j+1]=2
                                     
                                    if (x[i-1][j-1]==x[i+1][j+1]==x[i-1][j+1]==x[i+1][j-1]):
                                      
                                        compare[i-1][j-1]=2
                                        compare[i+1][j+1]=2
                                        compare[i-1][j+1]=2
                                        compare[i+1][j-1]=2
                         
                         
print(x)
print(compare)
print("--- %s seconds ---" % (time.time() - start_time))
plt.show(block=True)

print("done")

No comments:

Post a Comment