Exemplo n.º 1
0
def Trap(a,b,n,h):
	integral = (f(a)+f(b))/2
	
	x = a
	for i in range(1, int(n)):
		x = x + h
		integral = integral + f(x)

	return integral * h
Exemplo n.º 2
0
def do(c, r):
    n_file = open('res.txt', 'a')
    n_file.write("dotuch\n")
    i = 0
    buf = 2.
    while abs(func.f(c)) > func.e or abs(buf - c) > func.e:
        buf = c
        if func.df(c) == 0.:
            break
        c = c - func.f(c) / func.df(c)
        i += 1
        n_file.write('nabluzh=' + str(c) + ' f(x)=' + str(func.f(c)) + "\n")
    n_file.write(str(r) + " answer: " + str(c) + " " + str(i) + "iterations\n")
    n_file.close()
    return c
Exemplo n.º 3
0
def newtonup():
    print 'newton up\n'
    t = a
    i = 0
    while abs(t - b) > func.e:
        l = newu(t, x_zn, y_zn, n)
        eps = abs(func.f(t) - l)
        graph[2, i] = eps
        i += 1
        print 'iteration: ' + str(i) + ' x= ' + str(t) + ' f(x)= ' + str(
            func.f(t)) + ' newup= ' + str(l) + ' e= ' + str(eps) + '\n'
        t += h / 5
    i += 1
    l = newu(t, x_zn, y_zn, n)
    eps = abs(func.f(t) - l)
    print 'iteration: ' + str(i) + ' x= ' + str(t) + ' f(x)= ' + str(
        func.f(t)) + ' newup= ' + str(l) + ' e= ' + str(eps) + '\n'
Exemplo n.º 4
0
def bi(a, b, r):
    n_file = open('res.txt', 'a')
    n_file.write("biec\n")
    i = 0
    c = 1
    while abs(b - a) > func.e or abs(func.f(c)) > func.e:
        c = (a+b)/2.
        i += 1
        if func.f(c) == 0.:
            break
        if func.f(a)*func.f(c) < 0.:
            b = c
        else:
            a = c
        n_file.write(" a=" + str(a) + ' f(a)=' + str(func.f(a)) + ' b=' + str(b) + ' f(b)=' + str(func.f(b)) + "\n")
    n_file.write(str(r) + " answer: " + str(c) + " " + str(i) + "iterations\n")
    n_file.close()
    return c
Exemplo n.º 5
0
def splain():
    print 'splain\n'
    t = a
    i = 0
    while abs(t - b) > func.e:
        l = spl(t, x_zn, y_zn, n)
        eps = abs(func.f(t) - l)
        graph[1, i] = eps
        i += 1
        print 'iteration: ' + str(i) + ' x= ' + str(t) + ' f(x)= ' + str(
            func.f(t)) + ' splain= ' + str(l) + ' e= ' + str(eps) + '\n'
        t += h / 5
    i += 1
    l = spl(t, x_zn, y_zn, n)
    eps = abs(func.f(t) - l)

    print 'iteration: ' + str(i) + ' x= ' + str(t) + ' f(x)= ' + str(
        func.f(t)) + ' splain= ' + str(l) + ' e= ' + str(eps) + '\n'
Exemplo n.º 6
0
def newtondown():
    print 'newton down\n'
    t = b
    x_d = np.zeros(n)
    y_d = np.zeros(n)
    for i in range(0, n, 1):
        x_d[i] = x_zn[n - i - 1]
        y_d[i] = y_zn[n - i - 1]
    i = 0
    while abs(a - t) > func.e:
        l = newu(t, x_d, y_d, n)
        eps = abs(func.f(t) - l)
        graph[3, (n - 1) * 5 - i] = eps
        i += 1
        print 'iteration: ' + str(i) + ' x= ' + str(t) + ' f(x)= ' + str(
            func.f(t)) + ' newdown= ' + str(l) + ' e= ' + str(eps) + '\n'
        t -= h / 5
    i += 1
    l = newu(t, x_zn, y_zn, n)
    eps = abs(func.f(t) - l)

    print 'iteration: ' + str(i) + ' x= ' + str(t) + ' f(x)= ' + str(
        func.f(t)) + ' newdown= ' + str(l) + ' e= ' + str(eps) + '\n'
Exemplo n.º 7
0
def lagr():
    print 'lagrang\n'
    # print x_zn
    # print y_zn
    t = a
    i = 0
    while abs(t - b) > func.e:
        l = pollag(t, x_zn, y_zn, n)
        eps = abs(func.f(t) - l)
        graph[0, i] = eps
        gr_x[i] = t
        i += 1
        print 'iteration: ' + str(i) + ' x= ' + str(t) + ' f(x)= ' + str(
            func.f(t)) + ' Lag= ' + str(l) + ' e= ' + str(eps) + '\n'
        t += h / 5

    gr_x[i] = t
    i += 1
    l = pollag(t, x_zn, y_zn, n)
    eps = abs(func.f(t) - l)

    print 'iteration: ' + str(i) + ' x= ' + str(t) + ' f(x)= ' + str(
        func.f(t)) + ' Lag= ' + str(l) + ' e= ' + str(eps) + '\n'
Exemplo n.º 8
0
def xo(a, b, r):
    n_file = open('res.txt', 'a')
    n_file.write("xord\n")
    i = 0
    c = 1
    buf = 2.
    while abs(buf - c) > func.e or abs(func.f(c)) > func.e:
        buf = c
        c = (a * func.f(b) - b * func.f(a)) / (func.f(b) - func.f(a))
        i += 1
        if func.f(c) == 0.:
            break
        if func.f(a) * func.f(c) < 0.:
            b = c
        else:
            a = c
        n_file.write(" a=" + str(a) + ' f(a)=' + str(func.f(a)) + ' b=' + str(b) + ' f(b)=' + str(func.f(b)) + "\n")
    n_file.write(str(r) + " answer: " + str(c) + " " + str(i) + "iterations\n")
    n_file.close()
    return c
Exemplo n.º 9
0
def location1():
    print("here")
    if request.method == 'POST':
        u = session['username']
        print(u)
        location = request.form.get('s', False)
        m = request.form.get('m', False)
        print(location)
        print(m)
        month = calendar.month_name[int(m)]
        print(month)
        a = location
        crops = func.f(a, m)
        c = crops.split(',')
        print(c)
        return render_template("result.html",
                               lst=c,
                               user=u,
                               loc=location,
                               month=month)
        return render_template("location1.html")
Exemplo n.º 10
0
def work():
    while 1:
        try:
            file = open('text.txt', 'r')
        except Exception:
            print("Невозможно найти путь к файлу")
            continue

        m = int(file.readline())
        print(m)
        a = int(file.readline())
        print(a)
        c = int(file.readline())
        print(c)
        file.close()

        res = 0
        res = func.f(m, a, c)
        print("Остаточный результат:  " + str(res))
        ans = open('ans.txt', 'w')
        ans.write("Остаточный ответ:  " + str(res))
        ans.close()
        break
Exemplo n.º 11
0
def coordinator(clf,e,n_minibatch,total_workers):
    pub_results = Pub('results')
    pub_init = Pub('Initialize')
    pub_th = Pub('Theta')
    pub_endr = Pub('EndRound')
    pub_endsub = Pub('EndSubRound')
    #pub_ask_state = Pub('AskState')
    sub_incr = Sub('Increment')
    sub_f = Sub('Fs')
    sub_x = Sub('Xs')
    # sub_try=Sub('lets')
    # pub_try=Pub('receive')
    
    # get increments from workers
    def get_incr():    
            try:
                incr=sub_incr.get(timeout=5)
                print("Coo Received increments...",incr)
                if incr<0: # works as a flag to let coordinator know that chunks are out
                    print("Coo received notice of chunks ended...")
                return incr
            except TimeoutError:
                return 0
    # get fi's from all workers
    def get_fi(n_workers):  
        fis=[]
        print("try to get fis workers:",n_workers)
        for i in range(n_workers):
            try:
                fi=sub_f.get(timeout=5) 
                print("Coo received",i+1,"fi") 
                fis.append(fi)
            except TimeoutError:
                print('Fis Lost worker/workers num=',len(fis))
                break
        return fis
        

    # get xi's from all workers
    def get_xi(n_workers):  
        drifts=[]
        print("try to get xi workers:",n_workers)
        for i in range(n_workers):
            try:
                xi=sub_x.get(timeout=6)
                print("Coo received",i+1,"xi") 
                drifts.append(xi)
            except TimeoutError:
                print('Lost worker/workers')
                break
        print("Num of workers",len(drifts))
        return drifts

    def check_subcribers(pub,n_workers):
        print("Check...")
        if n_workers==0:
            print("No workers left")
            return "end"
        
        while len(pub.subscribers)<n_workers: #if not all workers subscribe sleep
                time.sleep(0.01)
        
        print("OK Check")
        return "ok"

    #____________________________Start coordinator_________________________________
    E=None
    
    th=0
    fis=0
    drifts=0
    sum_xi=0
    incr=0
    e_y=0.01
    workers=[]
    time_stamb=0
    n_rounds=0
    
    print("Coo started ...")
    client= get_client()
    for i in range(len(total_workers)-1):
        workers.append(client.submit(worker_f,i,clf,n_minibatch,e,workers=total_workers[i+1]))
    
    time.sleep(1)
    flag=True #use this flag to finish future if chunks are out
    start_time=time.time()
    while flag==True:
        n_subs=0
        workers_status=[w.status for w in workers]
        k=workers_status.count('pending')
        print("NUMBER OF WORKERS...",k)
        if E is None: #if E=0 we need to update E
            pub_init.put(None)
            print("Warmup...Coo Sended E=0...") 
            drifts=get_xi(k) #get local drifts (Xi's)
            print("Coo received xi's...workers=",k)
            
            sum_xi=add_x(drifts)
            e1=sum_xi[0]/len(drifts)
            e2=sum_xi[1]/len(drifts)
            E=[e1,e2]
            pub_init.put(E)
            print("Coo Sended E")
        else:
            pub_init.put(E)
            print("Coo Sended E")
        n_rounds+=1

        y=k*f([[0],0],E,e)
        barrier=e_y*k*f([[0],0],E,e)
        

        #start of the round...
        print("START ROUND:",n_rounds," workers ",k)
        while y<=barrier: 
            th=-y/(2*k)

            pub_th.put(th) #send theta
            print("Coo Sended theta")
            n_subs+=1
            print("START SUBROUND:",n_subs," workers ",k)
            c=0
            fis=[]
            
            #start of the subround...
            while c<k: 
                
                incr=get_incr() #Get increments
                if incr<0: # works as a flag to let coordinator know that chunks are out
                    incr=0
                workers_status=[w.status for w in workers]
                k=workers_status.count('pending')
                if k==0:
                    flag=False
                c=c+incr
                #subrounds ended...
            
            pub_endsub.put(0) #let workers know that subrounds ended
            print("Coo Sended endofSub... num_workers",k)
            workers_status=[w.status for w in workers]
            k=workers_status.count('pending') 
            fis=get_fi(k) #get F(Xi)'s from workers
            
            if len(fis)==0:
                pub_endr.put(0)
                break
            print("Coo Received fi's workers=",k)
            y=add_f(fis)
            print("y",y)
            if flag==False: #if false chunks are out end future
                print("Coo Sended endofSub..")
                break
            
        #rounds ended...
        
        pub_endr.put(0) #let workers know that rounds ended 
        
        print("Coo Sended endofround... num_workers",k)
        drifts=get_xi(len(fis)) #get local drifts (Xi's)
        print("len of drifts",len(drifts))
        print("Coo Received xi's workers=",k)
        if len(drifts)==0: break

        sum_xi=add_x(drifts)
        e1=E[0]+(sum_xi[0]/len(drifts)) #len(drifts)
        e2=E[1]+(sum_xi[1]/len(drifts)) #len(drifts)
        E=[e1,e2]
        time_stamb=time.time()-start_time
        pub_results.put([E,n_subs,k,time_stamb])
        if flag==False:
            break
    print("Coo ended...")
    return E,n_rounds,n_subs,k,time_stamb
Exemplo n.º 12
0
def test_f():
    assert f() == 'hello world'
Exemplo n.º 13
0
def test_f():
    assert f() == 4
Exemplo n.º 14
0
	def test(self):
		self.assertEqual(func.f(3),9)
Exemplo n.º 15
0
    print(board)

    board2 = []
    for b in board:
        print(b)
        tmp = []
        for c in b:
            num = format("{:02d}".format(ord(c) - ord('A')))
            tmp.append(num)
        board2.append(tmp)

    pb(board)
    pb(board2)
    board = board2
    orig = deepcopy(board)
    moves = f(orig)

    solve = []
    for faen, move in enumerate(moves):
        i = int(move[1:])
        d = move[0]
        cmd = ""
        if d in "UD":  # column
            cmd = "c{}{}".format(i, d.lower())
        else:  # row
            cmd = "r{}{}".format(i, d.lower())

        print(cmd)
        solve.append(cmd)

    print(solve)
Exemplo n.º 16
0
from func import f
from expressions.AssignmentStatement import var

f2 = f()


class FunctionDefineStatement:
    def __init__(self, functype, name, args, body):
        self.functype = functype
        self.name = name
        self.args = args
        self.body = body
        var.set(self.name, "function")

    def execute(self):
        # TODO разобраться в этом, подкоректировать не нужное
        f2.f = True
        args = ""
        i = 0
        l = len(self.args)
        while i <= l - 1:
            args += f"{self.args[i]} { self.args[i+1]}, "
            i += 2
        open("func.txt",
             "a").write(f"{self.functype} {self.name} ({args[:-2]})" + "{\n")
        self.body.execute()
        f2.f = False
        open("func.txt", "a").write("}")

    def __str__(self):
        return "def (" + self.args.__str__ + ") " + self.body.__str__
Exemplo n.º 17
0
def before_request():
	incval = f()
	c.inc(incval)
Exemplo n.º 18
0
trainFlag = True

fig = plt.figure()
subplot = fig.add_subplot(111)

#writing data to files
for pointi in range(0, pointsNo):
    if trainFlag:
        if (pointi / float(pointsNo)) >= dividingRatio:
            trainFlag = False

    temp = random.randrange(2)
    curX = random.uniform(MIN_X, MAX_X)

    if temp == 0:
        curY = func.f(curX) + intrinsicNoise
    else:
        curY = func.f(curX) - intrinsicNoise

    if trainFlag:
        subplot.plot(curX,
                     curY,
                     color=POINT_COLOR,
                     marker="o",
                     markeredgecolor=POINT_EDGE_COLOR)
        trainFile.write(str(curX) + " " + str(curY) + "\n")
    else:
        testFile.write(str(curX) + " " + str(curY) + "\n")

#plotting graph
sampleX = numpy.arange(MIN_X, MAX_X, GRAPH_LINE_LEAST_COUNT)
Exemplo n.º 19
0
        coeff[j][i] = tempCoeff

    coeff[j][j] = coeff[j][j] - lambhda

#solving equations to get W vector
W = numpy.linalg.solve(coeff, cons)

#plotting graphs
sampleX = numpy.arange(MIN_X, MAX_X, GRAPH_LINE_LEAST_COUNT)
estimatedFxLine, = subplot.plot(sampleX,
                                getY(W, sampleX),
                                'k',
                                color=ESTIMATED_GRAPH_LINE_COLOR,
                                linewidth=2)
actualFxLine, = subplot.plot(sampleX,
                             func.f(sampleX),
                             'k',
                             color=ORIGINAL_GRAPH_LINE_COLOR,
                             linewidth=2)

plt.legend([actualFxLine, estimatedFxLine],
           ["Original f(x)", "Estimated f(x)"])

subplot.set_xlabel("x")
subplot.set_ylabel("y")

plt.axis([
    MIN_X, MAX_X, func.MIN_Y - intrinsicNoise - plotVerticalPadding,
    func.MAX_Y + intrinsicNoise + plotVerticalPadding
])
Exemplo n.º 20
0
import numpy as np
import matplotlib.pyplot as plt
import func
import mag
n = 8
x_zn = np.zeros(n)
y_zn = np.zeros(n)
#interval a - b
a = 0.1
b = 0.15
h = (b - a) / (n - 1)
t = a
for i in range(0, n, 1):
    x_zn[i] = t
    y_zn[i] = func.f(x_zn[i])
    t += h
gr_x = np.zeros((n - 1) * 5 + 1)
graph = np.zeros((4, (n - 1) * 5 + 1))


def pollag(x, xv, yv, n):
    lag_pol = 0.
    bas_pol = 1.
    for i in range(0, n, 1):
        for j in range(0, n, 1):
            if i != j:
                bas_pol *= (x - xv[j]) / (xv[i] - xv[j])
        lag_pol += bas_pol * yv[i]
        bas_pol = 1.
    return lag_pol
Exemplo n.º 21
0
def worker_f(name, clf, parts, e):
    sub_init = Sub('Initialize')
    sub_th = Sub('Theta')
    sub_endr = Sub('EndRound')
    sub_endsub = Sub('EndSubRound')
    pub_incr = Pub('Increment')
    pub_f = Pub('Fs')
    pub_x = Pub('Xs')

    # get initial E value from coordinator
    def get_init():
        w_id = get_worker().name
        try:
            print(w_id, "waits to receive E...")
            init = sub_init.get(timeout=20)
            print(w_id, "Received E")
            return init
        except TimeoutError:
            print(w_id, 'Error E not received')
            return False

    #get theta from cordinator
    def get_th():
        w_id = get_worker().name
        try:
            print(w_id, "waits to receive th...")
            th = sub_th.get(timeout=1)
            print(w_id, "Received theta")
            return th
        except TimeoutError:
            print(w_id, 'Theta aknowlegment not received')
            return None

    #get aknowlegment for continue or stop the rounds
    def get_endr():
        try:
            endr = sub_endr.get(timeout=1)
            print(w_id, 'End of round received')
            return endr
        except TimeoutError:
            return None

    #get aknowlegment for continue or stop the subrounds
    def get_endsub():
        try:
            endsub = sub_endsub.get(timeout=1)
            print(w_id, 'End of subround received')
            return endsub
        except TimeoutError:
            return None

    #                       ____Start of worker____

    th = 0
    w_id = get_worker().name  #get worker id
    print("worker", w_id, "started...")
    flag = True
    E = [[0], 0]
    Si = [0, 0]
    S_prev = [0, 0]
    Xi = [[0], 0]

    count_chunks = 0
    minibatches = 0

    #TAG chunks assigned and load first one
    X_chunk_array, y_chunk_array = load_chunks(
        name)  #get the array with the chunk names assigned to this worker

    X_chunk, y_chunk = load_np(X_chunk_array, y_chunk_array, count_chunks)
    count_chunks += 1

    while flag == True:  #while this flag stays true there are chunks
        E = get_init()  # get E from coordinator
        if E is False:
            pub_incr.put(-1)
            return clf

        if E is None:  #if E=0 compute Xi and return Xi to update E
            #TODO make it prettier
            print(w_id, "Warmup....")
            temp = get_minibatch(X_chunk, y_chunk, minibatches,
                                 parts)  #get_newSi(count_chunks,f_name)

            if temp is None:
                minibatches = 0
                load = load_np(X_chunk_array, y_chunk_array, count_chunks)
                if load is None:
                    print(w_id, "End of chunks")
                    flag = False
                    pub_incr.put(-1)
                    break
                X_chunk, y_chunk = load
                count_chunks += 1
                temp = get_minibatch(X_chunk, y_chunk, minibatches, parts)

            minibatches += 1
            X, y = temp

            clf.partial_fit(X, y, np.unique(([0, 1])))
            Si = [clf.coef_[0], clf.intercept_[0]]
            Xi = [clf.coef_[0], clf.intercept_[0]]
            while len(pub_x.subscribers) != 1:
                time.sleep(0.01)
            pub_x.put(Xi)
            print(w_id, "Sended Xi")
            E = get_init()  # get E from coordinator
            if E is False:
                pub_incr.put(-1)
                break
        print(w_id, "Start of round")
        clf.coef_[0] = E[0]
        clf.intercept_[0] = E[1]
        S_prev[0] = np.array(list(E[0]))
        S_prev[1] = E[1]
        Xi = [[0], 0]
        #begin of round...
        #FIXME do not send message every time & check rounds and subrounds
        while get_endr() == None:

            ci = 0
            # Xi=[[0],0]
            th = get_th()  #get theta
            if th == None:
                print("nonreceive")
                continue
            print(w_id, "Received start of subround")
            #begin of subround...
            while get_endsub() == None:
                zi = f(Xi, E, e)
                temp = get_minibatch(X_chunk, y_chunk, minibatches, parts)

                while temp is None:
                    load = load_np(X_chunk_array, y_chunk_array, count_chunks)
                    if load is None:
                        print(w_id, "End of chunks")
                        flag = False
                        break
                    X_chunk, y_chunk = load
                    count_chunks += 1
                    minibatches = 0
                    temp = get_minibatch(X_chunk, y_chunk, minibatches, parts)
                if flag == False:

                    break
                else:
                    minibatches += 1
                    X, y = temp
                    clf.partial_fit(X, y, np.unique([0, 1]))
                    Si[0] = clf.coef_[0]
                    Si[1] = clf.intercept_[0]
                    Xi = [Si[0] - S_prev[0], Si[1] - S_prev[1]]
                    c_th = 0
                    if th != 0:  #avoid division with 0 if th=0 c_th=0
                        c_th = (f(Xi, E, e) - zi) / th
                    ci_new = max(ci, math.floor(c_th))
                    if ci != ci_new:  #if we detect a difference send it to the coordinator
                        incr = ci_new - ci
                        pub_incr.put(incr)
                        ci = ci_new
                        print(w_id, "Sended...", incr)
            while len(pub_f.subscribers) != 1:
                time.sleep(0.01)
            pub_f.put(f(Xi, E, e))
            print(w_id, "Sended Fi")
            print(w_id, "End of subround")
            if flag == False:
                break

            #end of subround...

        if all([v == 0 for v in Xi[0]]):
            print(w_id, "ZERO XI")
        else:

            pub_x.put(Xi)  # send Xi
            print(w_id, "Sended Xi")
            Xi = [[0], 0]
        if flag == False:
            break
    # pub_incr.put(-1)
    print(w_id, "Ended...")
    return clf