2. Python Code to Randomize color of alternate cell of HeatMap and fixing the adjacent and vertex cells when same color match found

/*
Task: To create 100 x 100 Heat Map, with random colors. Leaving edge cells, keep changing the vertex 4 cells or adjacent 4 cells until it matches with center cell color.

Additionally to check how long the code will take to solve the above puzzle, time difference between code start and end is been captured for tracking purpose.
*/

# 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

/* We will first define number of squares required for Heat Map, as edge cells will not be touched for color for fixing the color, we need to select "Odd" number of matrix, so either 101 or 99 will be close for 100 X 100 Matrix\Heat Map
Below m is chosen as 101 to for deriving number of squares.
c is number of colors, lower number can be chosen to reduce the run-time of the code
Klimit is been added to control\limit the endless loop or iterations of the code.
*/

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

m=101
Klimit = 10000
c=5

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


/*Below Compare Matrix of alternate target cells is created by assigning 1 for
Center cells and zero for changing cells
 */

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


/*Below xinitial Matrix of m X m random matrix with integer varying from 0 to c

*/


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

print(compare)
print(xinitial)

/*

subplot 121 means in 1 row with 2 pictures are created and last 1 indicate this plot will belong to 1st picture

*/

plt.subplot(121)
plt.pcolor(xinitial)
plt.title('Before')
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):
            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))
                                   
                                   
                                    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.subplot(122)
plt.pcolor(x)
plt.title('After')
plt.grid(True)
plt.show()
print("done")

/**********************Thank You********************************/





No comments:

Post a Comment