Advertisement
Guest User

Untitled

a guest
Sep 10th, 2021
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.44 KB | None | 0 0
  1. import random, math, time
  2.  
  3.  
  4. class Grid(object):
  5.     def __init__(self, n):
  6.         self.n = n
  7.         self.a = [[0]*n for _ in range(n)]
  8.         self.filled = 0
  9.  
  10.     def __str__(self):
  11.         return "\n".join("".join(str(c) for c in r) for r in self.a)
  12.  
  13.     def on(self, i, j):
  14.         if not self.a[i][j]: self.filled += 1
  15.         self.a[i][j] = 1
  16.  
  17.     def off(self, i, j):
  18.         if self.a[i][j]: self.filled -= 1
  19.         self.a[i][j] = 0
  20.  
  21.     def get_rand_ind(self):
  22.         return (random.randint(0, self.n-1),random.randint(0, self.n-1))
  23.    
  24.  
  25.     def check_squares(self, i, j):
  26.         for d in range(-i, self.n-i):
  27.             if d==0: continue
  28.             for sgn in [-1, 1]:
  29.                 #if any(t<0 or t>=self.n for t in (i+d, j+sgn*d)): continue
  30.                 y1 = j+sgn*d
  31.                 if y1<0 or y1>=self.n: continue
  32.                 crns = [(i+d, j), (i+d, y1), (i, y1)]
  33.                 if all(self.a[x][y] for x,y in crns):
  34.                     #print ("corners ", crns, " form square")
  35.                     return True
  36.         return False
  37.  
  38.  
  39.  
  40.     def sim_an(self, kMax=10**6):
  41.         T = 1.0
  42.         k = 0
  43.        
  44.         bestFilled = self.filled
  45.         bestGrid = str(self)
  46.         startT = time.time()
  47.         while k<kMax:
  48.             T = 1.0 - float(k)/kMax
  49.             i,j = self.get_rand_ind()
  50.             if self.a[i][j]:
  51.                 if random.random()<math.exp(-1.0/T):
  52.                     self.off(i,j)
  53.             else:
  54.                 if not self.check_squares(i,j):
  55.                     self.on(i,j)
  56.             if self.filled>bestFilled:
  57.                 bestFilled = self.filled
  58.                 bestGrid = str(self)
  59.                 print ("New Best: %d" %bestFilled)
  60.                 print ("T = %f" %T, "k = %d" %k)
  61.                 #print (bestGrid)
  62.             k += 1
  63.        
  64.         print ("Simulated annealing over.")
  65.         print ("Time: %f" %(time.time()-startT))
  66.         print ("Best found: %d" %bestFilled)
  67.         print (bestGrid)
  68.  
  69.     #for testing
  70.     def print_possibs(self):
  71.         ret = ""
  72.         for i in range(self.n):
  73.             for j in range(self.n):
  74.                 ret += "x" if self.check_squares(i,j) else "*"
  75.             if i==self.n-1: break
  76.             ret += "\n"
  77.         print (ret)
  78.  
  79.  
  80.  
  81. testN = 100
  82. g = Grid(testN)
  83.  
  84. g.sim_an(kMax = 10**9)
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92. """
  93. T = 0.105702 k = 894298386
  94. Simulated annealing over.
  95. Time: 24389.976206
  96. Best found: 3334
  97.  
  98. 0110100011001011000000011000110100110000011101011010000100100010010011010000101110100000011110000000
  99. 1000100101001010100110100100100000010110010101001010000010100011101101100110101001001011110101000110
  100. 1100100000100111010101100101100001110000000000001100110111100101000110010011110011000001001001100000
  101. 0011101100100010000111001001000101001101100010111001000000110110001010000001001000110110010000100000
  102. 0101011010010011000010101011000010000110100010001011000110011100000111011000110101010011100000000001
  103. 0100010101101001000000001101001000110010110000110100000010010011100010001101011001000010000100011011
  104. 1001000011001000100101100001101010011110001011000001100101010001001010010000101100000010001001001101
  105. 0101010001011000011111000000101000100011000101110000001111100000001111100010010000001010100011001000
  106. 1011100011000010010010110001010000100101100101011101001000111101010001000100011110100011110100010001
  107. 1010000001100100100011011000001001001010110011000101001001101011010000001100100000100000101000110010
  108. 0111010111000000010001000010110001001011010001001001100000001110110001110011100010010000000000100001
  109. 0001011100001001001111000010011011000010010111100000001111010000010001000110001100000001000010110010
  110. 0010001001000001100100001000101000010111101001000000110100111000111000111000111000011000000110000000
  111. 1110101010000100000110110000000100011000000110010100010110000100001001101111010010001001010011001001
  112. 1001100000111100100010011011100010100010000010100011010011100000001000100101100001000100100110001001
  113. 1100110100000000100010010100010010100001100111000100110010000011010000010000010001001011000001000100
  114. 0001101010100100110000000110011000110000100000010001001101001110010000100000000011011110000100000100
  115. 0001000110111000000000110000001011001001101000000011101011110000000000100001010100010001000001110111
  116. 1011011000000101111000100100000000111101011110001010100100000100110000001100000100001000001100101001
  117. 0100101110001000000100000010000001101000000010011111000000000110100100000000000110100010011010111010
  118. 1001100001000000001000010111000101000110001000010000010101100001000001000110010100110101000010100010
  119. 1010100100101110001001100010100110000010101101010011010000010000010000001000010100101100111100000001
  120. 0110010011111001000000000100010000001010101001000010010000000011010001101100001010110000000001001101
  121. 0010011100000001000010010000010100111110000110110010000001000110000110010000001010010100001001000110
  122. 1101010001000011000100000110000100010010000000010101011000011000000100000000100001010010011011000011
  123. 0110001011000000000001010100001111000100000001010000000101000101011000000000010001000110100101010010
  124. 1000000100100000011100100001110010000000010000101100011001010100010100011001100000000001010000100100
  125. 1100110110010011010100000000000010001000000100000101000000100000011110001100000001010100010000101110
  126. 0100000000100110111010100000000110000000000000000011010000101111110010000001000101001000100011011000
  127. 1100001100000101100100011000000010100000000000000001010001101010010000010010000001001001110001110000
  128. 0010001010000100100011000010000000011011001010000000001110000000000001000001100010111111000000010000
  129. 1101001001001001001001001100011001100000000010010010000001011000001001101010000000000000111000011100
  130. 0000100011001100001011001010100100000001000000010111010100001000010001000001011000000100000110100101
  131. 1110111000100100000101000100000100000000000010100010001101001000000011000110010001000010100100100011
  132. 0011000110101100010010100101100000000000100000000000001000011100001010010001101100101100100110010110
  133. 0010010101000000111010001010000100001100001000001100100000110000000001001010000000010101011100000000
  134. 0000111100100001100100001001000000000000001101010010001100001010000000000001110001010100010000011001
  135. 1001010000111000100101000001000000001000101000110001010000000001001110100100000001101011101000100000
  136. 0101010001010010100000110000100000000000000110000000111001001100000010110000010100101010011010110000
  137. 1100110010010010100001000010000000001000000000100000100001000001000011000000100001001000100101000111
  138. 0010000000111111011000000101000000001000100000100000000000101000000000000100000110000011110001111010
  139. 1111010100010000110000001010001100000000000000100100100000000001100011000010100100001100011000001110
  140. 0001010000001000000100110010000101010001111001100000100010000000101010000000000010101000101000011001
  141. 0000010000001010100111100110010010100000101000010000000000000000001010000000000010001111000111110001
  142. 0100111110000110000000011000010100001000001100000000000000000010100101011001001011001000000100101101
  143. 1001010010001101000000000001101110000000010000001000100100001100100000001100011000100100100000110100
  144. 0101100000000101100011111001010001010000001000000000000000010001101000100000000000100001110110001000
  145. 0100000001101011000000000001100001101000010001000001100001010001001100011000100011000000000000101010
  146. 0001111001011100000000100000000000000000000000010000000001100111001000101100111110110000000001000110
  147. 1111010101100100000000011000001010000000000000000000001100010000111100010111101001010011101000000001
  148. 0000000000000001001111000000111110010110111011101001000000101000000000000000000010000000001001111011
  149. 1000010010011101100000011001101001111011000000100000001000001000000100000000000000011111011000001100
  150. 0111101110000100011100110000000000000001010000000000001000001010110001010001010111100010010111000000
  151. 0000100010001001110001001000010100001000110000100000000000000000001001110110110100011000001100000101
  152. 1100000011101100001100000010010011110000000000001000000000001001100010000010001100100110000110001001
  153. 0100001000101010011001101000010000000000011000000100001001011000100000100000001011101010000001101000
  154. 0100001111000011010110101110111000000000000000101000101000000000000000000100000100010101000110001111
  155. 1101100100010000001100000010101100000000000000010010101000000000000000001000111001010001100011011010
  156. 0000100011000000011000100110000111110001000100001001100001000000100100011001000000011001000100001100
  157. 0011111110011001001110001100000000000000010000000000000000000001110101001100010011000001011101100010
  158. 0101000011010000111010000111000100000001100010101000000000000010000000011010010010100110100000100010
  159. 1111000000110000010100010100000000110000010000000000010010000001000100000101011100110000010000010111
  160. 0010000110100000100001001110010000001001000000011000000010010000000100001101001000101101010001100101
  161. 1100010000010110111001000001000010000110010010100000100110100000111000001000000110011001101010000100
  162. 0000100001001100101000000111010001110010000001001000101010100000000000101110001001000100011110010000
  163. 1111011001100100110000000100000000000011000000001000001000000000001001110000011101001000100001010000
  164. 0010101101001011100011010000000000000001010000100000100001000111001100001000000000000110100111011001
  165. 0000011010011100000001100000000011000000010110101100001010000000100100100011110011000010000000110110
  166. 0000001000010000010001001011001001100100100000010111011111000010010011000000000100010000001000100000
  167. 1001111000000000000010000010001011011101111000010100100000000000000001101110000110000101010000011000
  168. 1100100111000010010000000000000100100111010001001110000100010001001000100011000011101010001000100010
  169. 0001000010010100000100001110010100101000011000000000000110100110000000010101000000000001001011011101
  170. 0100100100110011100000110100100100010010110010000010000010010011100000101000000000001100101010000011
  171. 1111010100000001001001000100000010010100000101010010100010000010000010000010011101110011100010000010
  172. 0010000101111010001100001010000101000111000110000000101011000100000010001111010000000000010001110100
  173. 0010000101010101000100010000000010001100010100001011000000100101001010110100100010111001100100011010
  174. 1010101100000000001000000000110000000110111101100010100100101001000000010000100100000011010101000100
  175. 1011010001100100100001011000000100010100001001000111000110011001000110000010000000001000011011010010
  176. 0000010001010100101110010100010000100000001110000101010000001100001101100000110010000000101010011000
  177. 0101101010100110101000100000100000000000010001101100111010110000000110000000000000000100010101001111
  178. 0000110010111001100100100000111000011101100010001010000110000100010100010000010100100000100000110100
  179. 1010100000101101010000010101000000001011011011010001000001000110101100000001101100010000101010001000
  180. 0000010100011000110000101010011010010010001010010000010100100100100000110011000101001100000000101010
  181. 0001000100010010010011011001100100010011000010101001010111010100010001000010000000100111100100000100
  182. 1000111000100001001000001001011000111000010001000001000000100111001010000110000000010000100111110001
  183. 1011001000011110000100101011001010001010100111000110010010000000100001110001000001110010010001011100
  184. 0001011001000001110001010110100000011101101101000000001000011110110000001000110111000000001001100101
  185. 0010010011110010000101000001110011000101000100111011010000010010001000000011101010110100000000001111
  186. 0100011101000110010011000101010100101000100001000001010101010010000100101010101001101000001110101000
  187. 1011000010001010100100001001001011100100010111011000010010000101000100011010001100010000001010000110
  188. 0000000011001000101101000000000110100100011100000000010001001010010101110000001001011101010000100101
  189. 0010111010000110000000101100100001111010000001001011011000000011010000100101111000000010010000001111
  190. 0010001100010001100000111000010001000111100000000110110100100101110010000100101001000100110110110000
  191. 1110000000100111011110010010010010001001001000010100000111010000000000010010010000001010100000011100
  192. 1010000010000000000010011000101011000010010101010100100101111110100001010100001001100001001100110111
  193. 1101011110011000111000000000100100001010101110011000001000010010110001011010010110010000111000001010
  194. 0100100010101001010001111000001101010001000100110000110001100000010110001000111011010010100101001011
  195. 0100000001000011010010001001001000110001000111010110000001000001110001101101010010011010010111000110
  196. 1100001000000000001010100011011001101100000001110011011000101001011011010001100101000111001000000010
  197. 0010100110100000011111100101001010110010100000100110110010111000001100010010100010001001001000101110
  198.  
  199. """
  200.  
  201.  
  202.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement