def test_beta():
    N = 30
    dv = 3
    dc = 6
    num_codewords = 10
    H = get_H(N, dv, dc)
    codewords = get_codewords(H, num_codewords)
    p = 0.25
    max_iter = 10
    trials = 100
    print 'GOT ALL CODEWORDS'
    step = 0.1
    b = np.arange(0, 0.5, step)
    results = set()
    for beta in b:
        success_rate = [0] * num_codewords
        print beta,
        for i, word in enumerate(codewords):
            for _ in xrange(trials):
                codeword = word
                codeword = convert_to_posneg(codeword)
                codeword = simulateBEC(p, codeword)
                f = FactorGraph(H)
                codeword = f.decode_BEC_memoryless(codeword, beta, max_iter)
                codeword = convert_to_zeroone(codeword)
                if tuple(codeword) == word:
                    success_rate[i] += 1 / float(trials)
        stat = np.average(success_rate)
        print stat
        results.add(tuple([beta, stat]))
    print results
    for point in results:
        plt.plot(point[0], point[1], 'ro')
    plt.show()
示例#2
0
def test_beta():
    N = 30
    dv = 3
    dc = 6
    num_codewords = 10
    H = get_H(N,dv,dc)
    codewords = get_codewords(H, num_codewords)
    p = 0.25
    max_iter = 10
    trials = 100
    print 'GOT ALL CODEWORDS'
    step = 0.1
    b = np.arange(0, 0.5, step)
    results = set()
    for beta in b:
        success_rate = [0] * num_codewords
        print beta,
        for i, word in enumerate(codewords):
            for _ in xrange(trials):
                codeword = word
                codeword = convert_to_posneg(codeword)
                codeword = simulateBEC(p, codeword)
                f = FactorGraph(H)
                codeword = f.decode_BEC_memoryless(codeword, beta, max_iter)
                codeword = convert_to_zeroone(codeword)
                if tuple(codeword) == word:
                    success_rate[i] += 1 / float(trials)
        stat = np.average(success_rate)
        print stat
        results.add(tuple([beta, stat]))
    print results
    for point in results:
        plt.plot(point[0], point[1], 'ro')
    plt.show()
示例#3
0
 def __init__(self, verbose=False, progress=False, color=False, setformula=True, stats=False, scheduler=4, skip=True, size=3, stdout=sys.stdout, utf8=True):
     self.verbose = verbose
     self.progress = progress
     self.color = color
     self.setformula = setformula
     self.stats = stats
     self.scheduler = scheduler
     self.skip = skip
     self.stdout = stdout
     self.utf8 = utf8
     self.p = size
     self.q = self.p*self.p
     if not self.setformula and self.scheduler == 0:
         if self.stats:
             self.op = NumpyOperationsStats(self.q)
         else:
             self.op = NumpyOperations(self.q)
     else:
         if self.stats:
             self.op = SudokuOperationsStats(self.q)
         else:
             self.op = SudokuOperations(self.q)
     self.counter = 0
     self.graph = FactorGraph(self.q*self.q, 3*self.q, self.edge, self.op, self.print_sudoku if self.verbose and self.progress else lambda *args: None)
def test_N():
    use_sql = False
    if (use_sql):
        conn = sqlite3.connect('blocklength.db')
        c = conn.cursor()
        c.execute('''CREATE TABLE points
                 (N integer, alpha real, beta real, perror real, sd real)''')
    dv = 3
    dc = 6
    alpha = 0.25
    beta = 0.1
    START = 450
    END = 450
    STEP = START
    REPEAT = 10
    TRIALS = 15
    for N in xrange(START, END + STEP, STEP):
        if (use_sql):
            conn.commit()
        print '***', N, '***'
        H = get_H(N, dv, dc)
        # print 'H DONE'
        word = [0] * N
        max_iter = N / 4
        failure_rate = [0] * TRIALS
        noiseless_failure_rate = [0] * TRIALS
        for i in xrange(TRIALS):
            for _ in xrange(REPEAT):
                # print i
                codeword = word
                codeword = convert_to_posneg(codeword)
                codeword = simulateBEC(alpha, codeword)
                f = FactorGraph(H)
                codeword = f.decode_BEC_memory(codeword, beta, max_iter)
                codeword = convert_to_zeroone(codeword)
                if codeword != word:
                    failure_rate[i] += 1.0 / REPEAT
        # print 'SD', np.std(failure_rate)
        if N == START:
            plt.errorbar(N,
                         np.average(failure_rate),
                         np.std(failure_rate),
                         linestyle='None',
                         marker='o',
                         color='red',
                         label='beta = 0.1')
        else:
            plt.errorbar(N,
                         np.average(failure_rate),
                         np.std(failure_rate),
                         linestyle='None',
                         marker='o',
                         color='red')
        if (use_sql):
            c.execute('INSERT INTO points VALUES (?,?,?,?,?)',
                      (N, alpha, beta, np.average(failure_rate),
                       np.std(failure_rate)))
        '''
        for i in xrange(TRIALS):
            for _ in xrange(REPEAT):
                # print i
                codeword = word
                codeword = convert_to_posneg(codeword)
                codeword = simulateBEC(alpha, codeword)
                f = FactorGraph(H)
                codeword = f.decode_BEC_memoryless(codeword, 0, max_iter)
                codeword = convert_to_zeroone(codeword)
                if codeword != word:
                    noiseless_failure_rate[i] += 1.0 / REPEAT
        if N == START:
            plt.errorbar(N, np.average(noiseless_failure_rate), np.std(noiseless_failure_rate), linestyle='None', marker='o', color='blue', label='beta = 0')
        else:
            plt.errorbar(N, np.average(noiseless_failure_rate), np.std(noiseless_failure_rate), linestyle='None', marker='o', color='blue')
        if(use_sql):
            c.execute('INSERT INTO points VALUES (?,?,?,?,?)', (N, alpha, 0, np.average(noiseless_failure_rate), np.std(noiseless_failure_rate)))
        #plt.show()
        '''
    #for point in noisy_results:
    #    plt.plot(point[0], point[1], 'ro')
    #for point in noiseless_results:
    #    plt.plot(point[0], point[1], 'bo')
    plt.axis((0, N + STEP, -0.05, 0.5))
    plt.xlabel('Block Length')
    plt.ylabel('Probability of Error')
    plt.legend(loc='upper right')
    plt.suptitle('Block Length vs. Probability of Error for alpha = 0.25')
    plt.xticks(np.arange(0, END + STEP, STEP))
    plt.yticks(np.arange(0, 0.5 + 0.05, 0.05))
    if (use_sql):
        conn.commit()
    print 'DONE'
    plt.show()
示例#5
0
def test_N():
    use_sql = False
    if(use_sql):
        conn = sqlite3.connect('blocklength.db')
        c = conn.cursor()
        c.execute('''CREATE TABLE points
                 (N integer, alpha real, beta real, perror real, sd real)''')
    dv = 3
    dc = 6
    alpha = 0.25
    beta = 0.1
    START = 450
    END = 450
    STEP = START
    REPEAT = 10
    TRIALS = 15
    for N in xrange(START, END + STEP, STEP):
        if(use_sql):
            conn.commit()
        print '***', N, '***'
        H = get_H(N, dv, dc)
        # print 'H DONE'
        word = [0] * N
        max_iter = N / 4
        failure_rate = [0] * TRIALS
        noiseless_failure_rate = [0] * TRIALS
        for i in xrange(TRIALS):
            for _ in xrange(REPEAT):
                # print i
                codeword = word
                codeword = convert_to_posneg(codeword)
                codeword = simulateBEC(alpha, codeword)
                f = FactorGraph(H)
                codeword = f.decode_BEC_memory(codeword, beta, max_iter)
                codeword = convert_to_zeroone(codeword)
                if codeword != word:
                    failure_rate[i] += 1.0 / REPEAT
        # print 'SD', np.std(failure_rate)
        if N == START:
            plt.errorbar(N, np.average(failure_rate), np.std(failure_rate), linestyle='None', marker='o', color='red', label='beta = 0.1')
        else:
            plt.errorbar(N, np.average(failure_rate), np.std(failure_rate), linestyle='None', marker='o', color='red')
        if(use_sql):
            c.execute('INSERT INTO points VALUES (?,?,?,?,?)', (N, alpha, beta, np.average(failure_rate), np.std(failure_rate)))
        '''
        for i in xrange(TRIALS):
            for _ in xrange(REPEAT):
                # print i
                codeword = word
                codeword = convert_to_posneg(codeword)
                codeword = simulateBEC(alpha, codeword)
                f = FactorGraph(H)
                codeword = f.decode_BEC_memoryless(codeword, 0, max_iter)
                codeword = convert_to_zeroone(codeword)
                if codeword != word:
                    noiseless_failure_rate[i] += 1.0 / REPEAT
        if N == START:
            plt.errorbar(N, np.average(noiseless_failure_rate), np.std(noiseless_failure_rate), linestyle='None', marker='o', color='blue', label='beta = 0')
        else:
            plt.errorbar(N, np.average(noiseless_failure_rate), np.std(noiseless_failure_rate), linestyle='None', marker='o', color='blue')
        if(use_sql):
            c.execute('INSERT INTO points VALUES (?,?,?,?,?)', (N, alpha, 0, np.average(noiseless_failure_rate), np.std(noiseless_failure_rate)))
        #plt.show()
        '''
    #for point in noisy_results:
    #    plt.plot(point[0], point[1], 'ro')
    #for point in noiseless_results:
    #    plt.plot(point[0], point[1], 'bo')
    plt.axis((0, N + STEP, -0.05, 0.5))
    plt.xlabel('Block Length')
    plt.ylabel('Probability of Error')
    plt.legend(loc='upper right')
    plt.suptitle('Block Length vs. Probability of Error for alpha = 0.25')
    plt.xticks(np.arange(0, END + STEP, STEP))
    plt.yticks(np.arange(0, 0.5 + 0.05, 0.05))
    if(use_sql):
        conn.commit()
    print 'DONE'
    plt.show()
示例#6
0
def file_reader(file_name, exact_method=True):
    """Parse the graph structure and the look up table from the uai and evid file"""
    dir = os.path.dirname(os.path.realpath(__file__))
    ### read the uai file
    with open(dir + '/uai/' + file_name) as f:
        line = f.readline()
        graph_type = line.strip('\n')
        #print (graph_type)
        node_number = int(f.readline())
        #print (node_number)
        line = f.readline().strip(' \n').strip(' ')
        try:
            line2 = line.split(' ')
            variable_cardinality = list(map(int, line2))
        except Exception as e:
            line2 = line.split("  ")
            variable_cardinality = list(map(int, line2))
        #print (variable_cardinality)
        clique_number = int(f.readline())
        #print (clique_number)
        graph = Graph(graph_type, node_number, variable_cardinality,
                      clique_number)

        for i in range(clique_number):  # start from 0
            line = f.readline().strip('\n').strip(' ')
            #print(line)
            try:
                line = np.array(list(map(int, line.split('\t'))))
            except Exception as e:
                line = np.array(list(map(int, line.split(' '))))
            # print(line)
            size = line[0]
            # print(size)
            nodes = list(line[1:])
            # print(nodes)
            clique = Clique(size, nodes)
            graph.add_clique(i, clique)

        for i in range(clique_number):
            line = f.readline().strip('\n')
            if line == '':
                line = f.readline().strip('\n')
            table_size = int(line)
            # print(table_size)
            read = 0
            psi = []
            while read < table_size:
                line = f.readline().strip('\n').strip(' ').split(' ')
                read = read + len(line)
                psi.append(list(map(float, line)))
            graph.add_clique_table(i, psi)

    ### read the evidence file
    """
    with open(dir + '/uai/' + file_name +'.evid') as evidence:
        line = evidence.readline().strip('\n').strip(' ')
        try:
            line = np.array(list(map(int,line.split('\t'))))
        except Exception as e:
            line = np.array(list(map(int,line.split(' '))))    
        evid_size = line[0]
        if evid_size != 0:
            evidence = {} 
            for i in range(0, evid_size):
                evidence[line[2*i+1]] = line[2*i+2]
            graph.set_evidence(evidence)
    """

    # test for the parse
    #print(graph.adjacent[1])
    #for v in graph.node_sharing_cliques[1]:
    #print(v.table)
    #print(graph.evidence)

    # graph.triangulation()
    # graph.maxcliques()
    #
    #
    #

    if exact_method:
        start = time.clock()
        #print('test result')
        #graph.test()
        JT = graph.generate_JT()
        JT.traverse()

        elapsed = (time.clock() - start)
        print("Time used:", elapsed, 's')

    else:
        #######################
        #Here to run the LBP
        ######################
        start = time.clock()
        graph.pairwise()
        factorgraph = FactorGraph(graph)
        factorgraph.LBP(normalize=True)

        factorgraph.calculate_z()
        print("Time used", time.clock() - start, 's')
示例#7
0
class Sudoku:
    def __init__(self, verbose=False, progress=False, color=False, setformula=True, stats=False, scheduler=4, skip=True, size=3, stdout=sys.stdout, utf8=True):
        self.verbose = verbose
        self.progress = progress
        self.color = color
        self.setformula = setformula
        self.stats = stats
        self.scheduler = scheduler
        self.skip = skip
        self.stdout = stdout
        self.utf8 = utf8
        self.p = size
        self.q = self.p*self.p
        if not self.setformula and self.scheduler == 0:
            if self.stats:
                self.op = NumpyOperationsStats(self.q)
            else:
                self.op = NumpyOperations(self.q)
        else:
            if self.stats:
                self.op = SudokuOperationsStats(self.q)
            else:
                self.op = SudokuOperations(self.q)
        self.counter = 0
        self.graph = FactorGraph(self.q*self.q, 3*self.q, self.edge, self.op, self.print_sudoku if self.verbose and self.progress else lambda *args: None)
    
    def solve(self, s):
        self.graph.initial_values(np.array([self.op.one(int(c,base=36)-1) for c in s], dtype=bool))
        self.firstprint = True
        if self.verbose:
            print("Initial values:", file=self.stdout)
            self.print_sudoku(self.graph, False)
            print("Solving:", file=self.stdout)
        result = self.graph.solve(method=self.scheduler, doskip=self.skip, doprogress=self.progress, dostats=self.stats)
        solvable = True
        if any([np.count_nonzero(r) == 0 for r in result]):
            solvable = False
        elif any([np.count_nonzero(r) > 1 for r in result]):
            solvable = None
        if self.verbose:
            if not self.firstprint:
                print('\033['+str((self.p+1)*self.q+1)+'A',end='', file=self.stdout)
            if solvable == False:
                print("There's no solution:", file=self.stdout)
            elif solvable == None:
                print("We can't find a unique solution:", file=self.stdout)
            else:
                print("We found a solution:", file=self.stdout)
            self.print_sudoku(self.graph, False)
        stats = self.graph.get_stats()
        return (stats, solvable, result)
    
    def edge(self,i,j):
        if j < self.q:      # Row constraints
            return (i//self.q == j)
        elif j < 2*self.q:  # Column constraints
            return (i%self.q == j%self.q)
        else:               # Sub-grid constraints
            return (((i//self.q)//self.p)*self.p + ((i%self.q)//self.p) == j%self.q)
    
    def print_sudoku(self, graph, erase=True):
        digits = "123456789abcdefghijklmnopqrstuvwxyz"
        out = []
        for i1 in range(self.p):
            for i2 in range(self.p):
                for ii in range(self.p):
                    out.append(list(
                        '┃'.join([
                            '│'.join(' '*self.p for col in range(self.p))
                            for col in range(self.p)
                        ])
                    ))
                out.append(list(
                    '╂'.join([
                        '┼'.join('─'*self.p for col in range(self.p))
                        for col in range(self.p)
                    ])
                ))
            out.pop()
            out.append(list(
                '╋'.join([
                    '┿'.join('━'*self.p for col in range(self.p))
                    for col in range(self.p)
                ])
            ))
        out.pop()
        for i in range(self.q):
            for j in range(self.q):
                m = self.graph.PseudoPosterior[i*self.q+j]
                if np.count_nonzero(m) == 1:
                    a = i*(self.p+1)+(self.p//2)
                    b = j*(self.p+1)+(self.p//2)
                    for n in range(self.q):
                        if m[n]:
                            if self.color:
                                out[a][b] = '\033[32m'+digits[n]+'\033[0m'
                            else:
                                out[a][b] = digits[n]
                elif np.count_nonzero(m) > 0:
                    for n in range(self.q):
                        if m[n]:
                            a = i*(self.p+1)+(n//self.p)
                            b = j*(self.p+1)+(n%self.p)
                            out[a][b] = digits[n]
        
        if erase:
            if self.firstprint:
                self.firstprint = False
            else:
                print('\033['+str((self.p+1)*self.q)+'A',end='', file=self.stdout)
        out = '\n'.join([''.join(o) for o in out])
        if not self.utf8:
            out = out.replace('╂','+').replace('┼','+').replace('╋','+').replace('┿','+').replace('┃','|').replace('│','|').replace('─','-').replace('━','-')
        print(out, file=self.stdout)
        print(file=self.stdout)