Exemple #1
0
def solve_row(r):
    if r == n - 1:
        last_d = sum(M[r])
        if (last_d == max_d or last_d == max_d - 1) and is_diameter2(M):
            sp_M = sp.array(M)
            print(sp_M)
            G = nx.from_numpy_matrix(sp_M)
            nx.draw(G)
            plt.show()
            exit()
        clear(r)
    else:
        left_d = max_d
        for i in range(r):
            M[r][i] = M[i][r]
            left_d -= 1
        if left_d >= 0:
            for c in comb(n - r - 1, left_d):
                j = r + 1
                for b in c:
                    M[r][j] = b
                    j += 1
                solve_row(r + 1)
                clear_sub(r)
            for c in comb(n - r - 1, left_d - 1):
                j = r + 1
                for b in c:
                    M[r][j] = b
                    j += 1
                solve_row(r + 1)
                clear_sub(r)
        else:
            clear(r)
Exemple #2
0
 def create_grid(self):
     # Create the first row of hexagons and measure length
     exit_flag = True
     self.combs.append(comb.comb(self.lat_max, self.lon_max, self.comb_diameter, self.lat_delta, self.lon_delta))
     last_comb = self.combs[-1]
     length_of_first_line = 1.0
     while self.lon_min < last_comb.center_lon:
         self.combs.append(comb.comb(self.lat_max, self.lon_max - 1.5 * length_of_first_line * 4.0 * last_comb.lon_delta, self.comb_diameter, self.lat_delta, self.lon_delta))
         length_of_first_line += 1.0
         last_comb = self.combs[-1]
     while exit_flag:
         # Create the shifted row of hexagons
         this_rows_lat = last_comb.center_lat - last_comb.lat_delta
         this_rows_lon = self.combs[0].center_lon - 0.75 * 4.0 * last_comb.lon_delta
         for i6 in range(int(length_of_first_line)-1):
             self.combs.append(comb.comb(this_rows_lat, this_rows_lon, self.comb_diameter, self.lat_delta, self.lon_delta))
             this_rows_lon -= 1.5 * 4.0 * last_comb.lon_delta
             last_comb = self.combs[-1]
         # Create a concluding full row of hexagons
         this_rows_lat = last_comb.center_lat - last_comb.lat_delta
         this_rows_lon = self.combs[0].center_lon
         for i6 in range(int(length_of_first_line)):
             self.combs.append(comb.comb(this_rows_lat, this_rows_lon, self.comb_diameter, self.lat_delta, self.lon_delta))
             this_rows_lon -= 1.5 * 4.0 * last_comb.lon_delta
             last_comb = self.combs[-1]
         if self.combs[-1].center_lat < self.lat_min:
             exit_flag = False
     return 0
Exemple #3
0
def solve(r, M, c_max_d, c_min_d):
    if c_max_d > n_max_d or c_min_d > n_min_d:
        pass
    elif r == n - 1:
        last_d = M[r].sum()
        if last_d == max_d and c_max_d + 1 == n_max_d and is_diameter2(M):
            print(M)
            draw(M)
            exit()
        elif last_d == max_d - 1 and c_max_d == n_max_d and is_diameter2(M):
            print(M)
            draw(M)
            exit()
    else:
        left_d = max_d - M[r].sum()
        if left_d >= 0 and left_d <= n - r - 1:
            if left_d == 0:
                for j in range(1, n - r):
                    M[r][r + j], M[r + j][r] = 0, 0
                solve(r + 1, M.copy(), c_max_d + 1, c_min_d)
            else:
                bits = [0, 1]
                random.shuffle(bits)
                for bit in bits:
                    for c in comb(n - r - 1, left_d - bit):
                        j = 1
                        for b in c:
                            M[r][r + j], M[r + j][r] = b, b
                            j += 1
                        if bit == 0:
                            solve(r + 1, M.copy(), c_max_d + 1, c_min_d)
                        elif bit == 1:
                            solve(r + 1, M.copy(), c_max_d, c_min_d + 1)
Exemple #4
0
	def get_opt_fields(self,prev_opt_flds,prev_opt_c):
		c = comb()
		combinations = c.get_combinations(len(self.fp.fields),2,2)
		model = Regression()
		opt_fields = None
		opt_c = None
		max_score = 0
		for i,c in enumerate(combinations):
			fields = self.fp.get_fields(c) + prev_opt_flds
			score = model.run(self.df,fields)
			if score > max_score:
				max_score = score
				opt_fields = fields
				opt_c = c
		return max_score,opt_c,opt_fields
Exemple #5
0
def sweepCoincidencesInJogosConsecutives(jogos):
  '''
  Run thru jogos, calculating consecutive coincidences
  '''
  acum = 0; coincMin = 25; coincMax = 0
  for i in range(1,len(jogos)):
    jogo = jogos[i]
    jogoPrevious = jogos[i-1]
    nOfCoinc = getNOfCoincidences(jogo, jogoPrevious)
    print pprint(jogo), 'nOfCoinc', nOfCoinc
    acum += nOfCoinc
    if nOfCoinc < coincMin:
      coincMin = nOfCoinc
    if nOfCoinc > coincMax:
      coincMax = nOfCoinc
  average = acum / (0.0 + len(jogos) - 1)
  print 'average', average
  print 'min coinc', coincMin
  print 'max coinc', coincMax
  import comb as co
  averageInt = int(average) + 1
  nOfCombsWithAverage = co.comb(15,averageInt)
  print 'nOfCombsWithAverage', nOfCombsWithAverage
from ema import ema
from box import box
from comb import comb

Nwindow = 1024

# (a) Ema and box
Neff = 32
h_ema = ema(Neff, Nwindow)

Nbox = Neff / (1-np.exp(-1))
h_box = box(Nbox, Nwindow)

# (b) Comb with period = 256
Nperiod = 256
h_comb = comb(Nperiod, Nwindow)

# (c-f) Convolution
candidate1 = np.convolve(h_comb, h_ema)
candidate2 = np.convolve(h_comb, h_box)

h_ema_replicated = candidate1[:Nwindow]
h_box_replicated = candidate2[:Nwindow]

H_ema = np.absolute(np.fft.fft(h_ema))
H_box = np.absolute(np.fft.fft(h_box))
H_ema_replicated = np.absolute(np.fft.fft(h_ema_replicated)) / Nperiod
H_box_replicated = np.absolute(np.fft.fft(h_box_replicated)) / Nperiod

f, axarr = plt.subplots(2, 2)
Exemple #7
0
                solve(r + 1, M.copy(), c_max_d + 1, c_min_d)
            else:
                bits = [0, 1]
                random.shuffle(bits)
                for bit in bits:
                    for c in comb(n - r - 1, left_d - bit):
                        j = 1
                        for b in c:
                            M[r][r + j], M[r + j][r] = b, b
                            j += 1
                        if bit == 0:
                            solve(r + 1, M.copy(), c_max_d + 1, c_min_d)
                        elif bit == 1:
                            solve(r + 1, M.copy(), c_max_d, c_min_d + 1)


if __name__ == '__main__':
    n = int(sys.argv[1])
    max_d = int(sys.argv[2])
    n_max_d = int(sys.argv[3])
    n_min_d = n - n_max_d

    if max_d > n - 1:
        raise Exception

    M = np.zeros((n, n))

    for c in comb(n - 1, max_d):
        for j, b in enumerate(c):
            M[0][j + 1], M[j + 1][0] = b, b
        solve(1, M.copy(), 1, 0)