import numpy as np
import random
import sys
import matplotlib.pyplot as plt

def mysign(x):
    return np.where(x<0,-1,1)

data1 = [[-1,-1,1,-1,-1,
        -1,1,-1,1,-1,
        -1,1,-1,1,-1,
        1,1,1,1,1,
        1,-1,-1,-1,1],
        [1,1,1,-1,-1,
        1,-1,1,-1,-1,
        1,1,1,1,-1,
        1,-1,-1,1,-1,
        1,1,1,1,-1,],
        [1,1,1,-1,-1,
        1,-1,-1,-1,-1,
        1,-1,-1,-1,-1,
        1,-1,-1,-1,-1,
        1,1,1,-1,-1],
        [1,1,1,-1,-1,
        1,-1,-1,1,-1,
        1,-1,-1,1,-1,
        1,-1,-1,1,-1,
        1,1,1,-1,-1],
        [1,1,1,1,-1,
        1,-1,-1,-1,-1,
        1,1,1,1,-1,
        1,-1,-1,-1,-1,
        1,1,1,1,-1],
        [1,1,1,1,-1,
        1,-1,-1,-1,-1,
        1,1,1,-1,-1,
        1,-1,-1,-1,-1,
        1,-1,-1,-1,-1]
        ]


class hpf:
    def __init__(self,data,error,n):
        self.data=np.array(data)
        self.data=self.data[:n,:]
        self.n=self.data.shape[0]
        self.l=self.data.shape[1]
        self.c=np.zeros((self.l,self.l))
        for i in range(self.n):
            self.c+=(np.matrix(self.data[i,:]).T*np.matrix(self.data[i,:]))

        self.w=(self.c-np.diag(np.diag(self.c)))/float(self.n)

        self.error=error

    def update(self):
        self.th=0.0

        rate=0.0
        for b in range(1000):
            test = np.copy(self.data)
            a=random.randint(0,self.n-1)
            if (self.error>0):
                for i in range(self.l-1):
                    s=random.randint(0,99)
                    if(s<self.error):
                            test[a,i]=-test[a,i]
            # print(test.shape)
            test=test[a,:]
            for c in range(100):
                f=mysign(np.dot(np.matrix(test),self.w)-self.th)
                sys.stdout.write("\r update b={0:2d} a={0:2d}".format(b, a))
                sys.stdout.flush()
                if np.all(f==test):
                    if np.all(f==self.data[a]):
                        # print("correct")
                        rate+= 1.0/10
                        break
                    else:
                        for j in range(self.l-1):
                            if(f[0,j]==self.data[a,j]):
                                rate+=1.0/(self.l*10)
                        break
                else:
                    test=f
        return rate

    def energy(self,test,w):
        return -np.sum(w*np.dot(self.data.T,self.data))/2+np.sum(test*self.th)

if __name__ =="__main__":
#    hf=hpf(data1,0,2)
#    hf.update()
    res=np.zeros((21,6))
    res2=np.zeros((21,6))
    for k in range (6):
        for i in range(21):
            hf=hpf(data1,i,k+1)
            # print(i,k)
            res[i,k]+=hf.update()



    trange=np.arange(0,21,1)
    plt.plot(trange,res)
    plt.show()

419991de78211c0685b753e3fc79de24a2e713b4
<mask token>


def mysign(x):
    return np.where(x < 0, -1, 1)


<mask token>


class hpf:

    def __init__(self, data, error, n):
        self.data = np.array(data)
        self.data = self.data[:n, :]
        self.n = self.data.shape[0]
        self.l = self.data.shape[1]
        self.c = np.zeros((self.l, self.l))
        for i in range(self.n):
            self.c += np.matrix(self.data[i, :]).T * np.matrix(self.data[i, :])
        self.w = (self.c - np.diag(np.diag(self.c))) / float(self.n)
        self.error = error

    def update(self):
        self.th = 0.0
        rate = 0.0
        for b in range(1000):
            test = np.copy(self.data)
            a = random.randint(0, self.n - 1)
            if self.error > 0:
                for i in range(self.l - 1):
                    s = random.randint(0, 99)
                    if s < self.error:
                        test[a, i] = -test[a, i]
            test = test[a, :]
            for c in range(100):
                f = mysign(np.dot(np.matrix(test), self.w) - self.th)
                sys.stdout.write('\r update b={0:2d} a={0:2d}'.format(b, a))
                sys.stdout.flush()
                if np.all(f == test):
                    if np.all(f == self.data[a]):
                        rate += 1.0 / 10
                        break
                    else:
                        for j in range(self.l - 1):
                            if f[0, j] == self.data[a, j]:
                                rate += 1.0 / (self.l * 10)
                        break
                else:
                    test = f
        return rate

    def energy(self, test, w):
        return -np.sum(w * np.dot(self.data.T, self.data)) / 2 + np.sum(
            test * self.th)


<mask token>

<mask token>


def mysign(x):
    return np.where(x < 0, -1, 1)


<mask token>


class hpf:

    def __init__(self, data, error, n):
        self.data = np.array(data)
        self.data = self.data[:n, :]
        self.n = self.data.shape[0]
        self.l = self.data.shape[1]
        self.c = np.zeros((self.l, self.l))
        for i in range(self.n):
            self.c += np.matrix(self.data[i, :]).T * np.matrix(self.data[i, :])
        self.w = (self.c - np.diag(np.diag(self.c))) / float(self.n)
        self.error = error

    def update(self):
        self.th = 0.0
        rate = 0.0
        for b in range(1000):
            test = np.copy(self.data)
            a = random.randint(0, self.n - 1)
            if self.error > 0:
                for i in range(self.l - 1):
                    s = random.randint(0, 99)
                    if s < self.error:
                        test[a, i] = -test[a, i]
            test = test[a, :]
            for c in range(100):
                f = mysign(np.dot(np.matrix(test), self.w) - self.th)
                sys.stdout.write('\r update b={0:2d} a={0:2d}'.format(b, a))
                sys.stdout.flush()
                if np.all(f == test):
                    if np.all(f == self.data[a]):
                        rate += 1.0 / 10
                        break
                    else:
                        for j in range(self.l - 1):
                            if f[0, j] == self.data[a, j]:
                                rate += 1.0 / (self.l * 10)
                        break
                else:
                    test = f
        return rate

    def energy(self, test, w):
        return -np.sum(w * np.dot(self.data.T, self.data)) / 2 + np.sum(
            test * self.th)


if __name__ == '__main__':
    res = np.zeros((21, 6))
    res2 = np.zeros((21, 6))
    for k in range(6):
        for i in range(21):
            hf = hpf(data1, i, k + 1)
            res[i, k] += hf.update()
    trange = np.arange(0, 21, 1)
    plt.plot(trange, res)
    plt.show()

<mask token>


def mysign(x):
    return np.where(x < 0, -1, 1)


data1 = [[-1, -1, 1, -1, -1, -1, 1, -1, 1, -1, -1, 1, -1, 1, -1, 1, 1, 1, 1,
    1, 1, -1, -1, -1, 1], [1, 1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, 1, -
    1, 1, -1, -1, 1, -1, 1, 1, 1, 1, -1], [1, 1, 1, -1, -1, 1, -1, -1, -1, 
    -1, 1, -1, -1, -1, -1, 1, -1, -1, -1, -1, 1, 1, 1, -1, -1], [1, 1, 1, -
    1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, 1, 1, 1, -
    1, -1], [1, 1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, 1, 1, -1, 1, -1, -1, 
    -1, -1, 1, 1, 1, 1, -1], [1, 1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, 1, -
    1, -1, 1, -1, -1, -1, -1, 1, -1, -1, -1, -1]]


class hpf:

    def __init__(self, data, error, n):
        self.data = np.array(data)
        self.data = self.data[:n, :]
        self.n = self.data.shape[0]
        self.l = self.data.shape[1]
        self.c = np.zeros((self.l, self.l))
        for i in range(self.n):
            self.c += np.matrix(self.data[i, :]).T * np.matrix(self.data[i, :])
        self.w = (self.c - np.diag(np.diag(self.c))) / float(self.n)
        self.error = error

    def update(self):
        self.th = 0.0
        rate = 0.0
        for b in range(1000):
            test = np.copy(self.data)
            a = random.randint(0, self.n - 1)
            if self.error > 0:
                for i in range(self.l - 1):
                    s = random.randint(0, 99)
                    if s < self.error:
                        test[a, i] = -test[a, i]
            test = test[a, :]
            for c in range(100):
                f = mysign(np.dot(np.matrix(test), self.w) - self.th)
                sys.stdout.write('\r update b={0:2d} a={0:2d}'.format(b, a))
                sys.stdout.flush()
                if np.all(f == test):
                    if np.all(f == self.data[a]):
                        rate += 1.0 / 10
                        break
                    else:
                        for j in range(self.l - 1):
                            if f[0, j] == self.data[a, j]:
                                rate += 1.0 / (self.l * 10)
                        break
                else:
                    test = f
        return rate

    def energy(self, test, w):
        return -np.sum(w * np.dot(self.data.T, self.data)) / 2 + np.sum(
            test * self.th)


if __name__ == '__main__':
    res = np.zeros((21, 6))
    res2 = np.zeros((21, 6))
    for k in range(6):
        for i in range(21):
            hf = hpf(data1, i, k + 1)
            res[i, k] += hf.update()
    trange = np.arange(0, 21, 1)
    plt.plot(trange, res)
    plt.show()

import numpy as np
import random
import sys
import matplotlib.pyplot as plt


def mysign(x):
    return np.where(x < 0, -1, 1)


data1 = [[-1, -1, 1, -1, -1, -1, 1, -1, 1, -1, -1, 1, -1, 1, -1, 1, 1, 1, 1,
    1, 1, -1, -1, -1, 1], [1, 1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, 1, -
    1, 1, -1, -1, 1, -1, 1, 1, 1, 1, -1], [1, 1, 1, -1, -1, 1, -1, -1, -1, 
    -1, 1, -1, -1, -1, -1, 1, -1, -1, -1, -1, 1, 1, 1, -1, -1], [1, 1, 1, -
    1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, 1, 1, 1, -
    1, -1], [1, 1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, 1, 1, -1, 1, -1, -1, 
    -1, -1, 1, 1, 1, 1, -1], [1, 1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, 1, -
    1, -1, 1, -1, -1, -1, -1, 1, -1, -1, -1, -1]]


class hpf:

    def __init__(self, data, error, n):
        self.data = np.array(data)
        self.data = self.data[:n, :]
        self.n = self.data.shape[0]
        self.l = self.data.shape[1]
        self.c = np.zeros((self.l, self.l))
        for i in range(self.n):
            self.c += np.matrix(self.data[i, :]).T * np.matrix(self.data[i, :])
        self.w = (self.c - np.diag(np.diag(self.c))) / float(self.n)
        self.error = error

    def update(self):
        self.th = 0.0
        rate = 0.0
        for b in range(1000):
            test = np.copy(self.data)
            a = random.randint(0, self.n - 1)
            if self.error > 0:
                for i in range(self.l - 1):
                    s = random.randint(0, 99)
                    if s < self.error:
                        test[a, i] = -test[a, i]
            test = test[a, :]
            for c in range(100):
                f = mysign(np.dot(np.matrix(test), self.w) - self.th)
                sys.stdout.write('\r update b={0:2d} a={0:2d}'.format(b, a))
                sys.stdout.flush()
                if np.all(f == test):
                    if np.all(f == self.data[a]):
                        rate += 1.0 / 10
                        break
                    else:
                        for j in range(self.l - 1):
                            if f[0, j] == self.data[a, j]:
                                rate += 1.0 / (self.l * 10)
                        break
                else:
                    test = f
        return rate

    def energy(self, test, w):
        return -np.sum(w * np.dot(self.data.T, self.data)) / 2 + np.sum(
            test * self.th)


if __name__ == '__main__':
    res = np.zeros((21, 6))
    res2 = np.zeros((21, 6))
    for k in range(6):
        for i in range(21):
            hf = hpf(data1, i, k + 1)
            res[i, k] += hf.update()
    trange = np.arange(0, 21, 1)
    plt.plot(trange, res)
    plt.show()

import numpy as np
import random
import sys
import matplotlib.pyplot as plt

def mysign(x):
    return np.where(x<0,-1,1)

data1 = [[-1,-1,1,-1,-1,
        -1,1,-1,1,-1,
        -1,1,-1,1,-1,
        1,1,1,1,1,
        1,-1,-1,-1,1],
        [1,1,1,-1,-1,
        1,-1,1,-1,-1,
        1,1,1,1,-1,
        1,-1,-1,1,-1,
        1,1,1,1,-1,],
        [1,1,1,-1,-1,
        1,-1,-1,-1,-1,
        1,-1,-1,-1,-1,
        1,-1,-1,-1,-1,
        1,1,1,-1,-1],
        [1,1,1,-1,-1,
        1,-1,-1,1,-1,
        1,-1,-1,1,-1,
        1,-1,-1,1,-1,
        1,1,1,-1,-1],
        [1,1,1,1,-1,
        1,-1,-1,-1,-1,
        1,1,1,1,-1,
        1,-1,-1,-1,-1,
        1,1,1,1,-1],
        [1,1,1,1,-1,
        1,-1,-1,-1,-1,
        1,1,1,-1,-1,
        1,-1,-1,-1,-1,
        1,-1,-1,-1,-1]
        ]


class hpf:
    def __init__(self,data,error,n):
        self.data=np.array(data)
        self.data=self.data[:n,:]
        self.n=self.data.shape[0]
        self.l=self.data.shape[1]
        self.c=np.zeros((self.l,self.l))
        for i in range(self.n):
            self.c+=(np.matrix(self.data[i,:]).T*np.matrix(self.data[i,:]))

        self.w=(self.c-np.diag(np.diag(self.c)))/float(self.n)

        self.error=error

    def update(self):
        self.th=0.0

        rate=0.0
        for b in range(1000):
            test = np.copy(self.data)
            a=random.randint(0,self.n-1)
            if (self.error>0):
                for i in range(self.l-1):
                    s=random.randint(0,99)
                    if(s<self.error):
                            test[a,i]=-test[a,i]
            # print(test.shape)
            test=test[a,:]
            for c in range(100):
                f=mysign(np.dot(np.matrix(test),self.w)-self.th)
                sys.stdout.write("\r update b={0:2d} a={0:2d}".format(b, a))
                sys.stdout.flush()
                if np.all(f==test):
                    if np.all(f==self.data[a]):
                        # print("correct")
                        rate+= 1.0/10
                        break
                    else:
                        for j in range(self.l-1):
                            if(f[0,j]==self.data[a,j]):
                                rate+=1.0/(self.l*10)
                        break
                else:
                    test=f
        return rate

    def energy(self,test,w):
        return -np.sum(w*np.dot(self.data.T,self.data))/2+np.sum(test*self.th)

if __name__ =="__main__":
#    hf=hpf(data1,0,2)
#    hf.update()
    res=np.zeros((21,6))
    res2=np.zeros((21,6))
    for k in range (6):
        for i in range(21):
            hf=hpf(data1,i,k+1)
            # print(i,k)
            res[i,k]+=hf.update()



    trange=np.arange(0,21,1)
    plt.plot(trange,res)
    plt.show()
