示例#1
0
 def __init__(self, shape, T):
     self.shape = shape
     self.T = T
     n, m = shape
     tests = []
     for i in range(T):
         perc = Percolation((n, m))
         while not perc.percolates():
             lin = random.randint(0, n - 1)
             col = random.randint(0, m - 1)
             if not perc.is_open(lin, col):
                 perc.open(lin, col)
         tests.append(perc.no_open())
     testes = np.array(tests)
     self.testes = testes / (n * m)
示例#2
0
    def __init__(self, n, t):
        if n <= 0 or t <= 0:
            raise ValueError("N and T should be greater than 0")

        self._vals = []
        self._t = t

        for _ in range(t):
            p = Percolation(n)
            count = 0.0
            while not p.percolates():
                x = np.random.random_integers(1, n)
                y = np.random.random_integers(1, n)
                if p.is_open(x, y):
                    continue
                else:
                    p.open(x, y)
                    count += 1
            ans = count / (n * n)
            self._vals.append(ans)
示例#3
0
 def __init__(self, shape, T):
     grade = Percolation(shape)
     self.grade = Percolation(shape)
     self.T = T
     self.x = []  #inicia a lista de armazena os limiares de percolação
     self.numsitios = grade.op  #guarda o numero de sitios da grade
     for i in range(T):
         contagem = 0  #reinicia a contagem de sitios abertos
         while grade.percolates() == False:
             valorlinha = random.randint(
                 0, grade.lin - 1
             )  #produz um valor inteiro aleatorio dentro do shape da grade
             valorcoluna = random.randint(0, grade.col - 1)
             if grade.is_open(
                     valorlinha, valorcoluna
             ) == False:  #caso não seja BLOCKED abre o ponto da grade
                 grade.open(valorlinha,
                            valorcoluna)  #e adiciona +1 na contagem
                 contagem += 1
         self.x.append(
             contagem /
             self.numsitios)  #adiciona um valor de limiar na lista
         grade = Percolation(shape)  #refaz a grade
import random

from percolation import Percolation

parser = argparse.ArgumentParser(description='Calculate the percolation ratio')
parser.add_argument('-n', action='store', dest='n', type=int,
                    help='The grid size n * n')
parser.add_argument('-t', action='store', dest='t', type=int,
                    help='How often should the experiment be executed.')


results = parser.parse_args()
n = results.n
t = results.t

percolation_results = []
for i in range(0, results.t):
    perc = Percolation(n)
    open_sites = 0
    while not perc.percolates():
        row = random.randint(1, n)
        column = random.randint(1, n)
        if not perc.is_open(row, column):
            perc.open(row, column)
            open_sites += 1
    percolation = (open_sites * 1.0) / (n * n)
    print percolation
    percolation_results.append(percolation)

mean = sum(percolation_results) / len(percolation_results)
print "Mean: ", mean