def find_automatic_alignment_stats(ave_stats_writer):
    # open input files
    hits_input = csv.reader(open(sys.argv[5], 'rU'), delimiter = ",")
    
    hits_num = 0
    ave_stats = []
    for row in hits_input:
        try:
            # skip first line
            if row[0] == "source":
                continue
            
            print(row[0])
            print(hits_num + 1)
            
            # convert all alignment results to sets
            sure_sub = set(row[4].split())
            sure_ans = set(row[8].split())
            
            # calculate stats of alignment results
            stats_list = [precision(sure_sub, sure_ans), recall(sure_sub, sure_ans)]
            stats_list.append(f1(stats_list[0], stats_list[1]))
            
            # if answer key has two possible alignments, take one with the higher f1
            if row[12]:
                print("two answers exists")
                sure_ans_2 = set(row[12].split())
                stats_list_2 = [precision(sure_sub, sure_ans_2), recall(sure_sub, sure_ans_2)]
                stats_list_2.append(f1(stats_list_2[0], stats_list_2[1]))
                print("1st F1 value: " + str(stats_list[2]))
                print("2nd F1 value: " + str(stats_list_2[2]))
            
                if stats_list_2[2] > stats_list[2]:
                    print("selected answer B")
                    stats_list = stats_list_2
                else:
                    print("selected answer A")
                
            # add values to the average calculation
            # the case where this is the first HIT in the list
            if hits_num == 0:
                hits_num += 1
                ave_stats = stats_list
            
            # the case where this is not the first HIT in the list
            else:
                hits_num += 1
                for i in range(0, 3):
                    ave_stats[i] = float(ave_stats[i]) + ((stats_list[i] - ave_stats[i]) / float(hits_num))
            
            print(ave_stats)
            print("")
            
        except:
            pass
    
    # print averages
    ave_stats_writer.writerow(["automatic_alignments", hits_num] + ave_stats)
    
    return
def scan_results(qual_type, results, worker_dict):
    for row in results:
        try:
            workerId = row[15]
            
            # skip first line
            if row[0] == "HITId":
                continue
            print(row[0])
            print("WORKERID: " + workerId)
            
            # if an "unchanged" value is encountered, replace with the proper value
            if row[50] == "unchanged": #sureAlignments
                row[50] = row[31]
            
            # convert all alignment results to sets
            sure_sub = set(row[50].split())
            sure_ans = set(row[35].split())
             
            # calculate stats of worker and store in stats_list
            # stats_list[0] = precision, stats_list[1] = recall, stats_list[2] = f1
            stats_list = [precision(sure_sub, sure_ans), recall(sure_sub, sure_ans)]
            stats_list.append(f1(stats_list[0], stats_list[1]))

            # if answer key has two possible alignments, take one with the higher f1
            if row[39]:
                sure_ans_2 = set(row[39].split())
                stats_list_2 = [precision(sure_sub, sure_ans_2), recall(sure_sub, sure_ans_2)]
                stats_list_2.append(f1(stats_list_2[0], stats_list_2[1]))
                print("1st F1 value: " + str(stats_list[2]))
                print("2nd F1 value: " + str(stats_list_2[2]))
            
                if stats_list_2[2] > stats_list[2]:
                    print("selected answer 2")
                    stats_list = stats_list_2
                else:
                    print("selected answer 1")
                
            # add worker to worker_dict
            # the case where worker does not exist in worker_dict
            if workerId not in worker_dict:
                worker_list = [1, qual_type]
                worker_list = worker_list + stats_list
                worker_dict[workerId] = worker_list
            
            # the case where worker already exists in worker_dict
            else:
                worker_list = worker_dict[workerId]
                worker_list[0] = worker_list[0] + 1
                worker_list[2] = float(worker_list[2]) + ((stats_list[0] - worker_list[2]) / float(worker_list[0]))
                worker_list[3] = float(worker_list[3]) + ((stats_list[1] - worker_list[3]) / float(worker_list[0]))
                worker_list[4] = float(worker_list[4]) + ((stats_list[2] - worker_list[4]) / float(worker_list[0]))
            
        except:
            pass
        
        print("")

    return
Exemple #3
0
 def evaluate(self, numFuncion):
     if numFuncion == 1:
         for agent in self.agents:
             f1(agent)
     elif numFuncion == 2:
         for agent in self.agents:
             f2(agent)
     else:
         for agent in self.agents:
             f3(agent)
def find_automatic_alignment_stats(ave_stats_writer):
    # open input files
    hits_input = csv.reader(open(sys.argv[5], 'rU'), delimiter = ",")
    
    hits_num = 0
    ave_stats = []
    for row in hits_input:
        try:
            # skip first line
            if row[0] == "source":
                continue
            
            print(row[0])
            
            # if answer key has two possible alignments, choose one at random
            if row[8] and row[12]:
                print("selecting random answer")
                ans_string = random.choice([row[8], row[12]])
            else:
                ans_string = row[8]
                
            if ans_string == row[8]:
                print("selected answer 1")
            else:
                print("selected answer 2")
            
            # convert all alignment results to sets
            sure_sub = set(row[4].split())
            sure_ans = set(ans_string.split())
            
            print(sure_sub)
            print(sure_ans)
            
            # calculate stats of alignment results
            stats_list = [precision(sure_sub, sure_ans), recall(sure_sub, sure_ans)]
            stats_list.append(f1(stats_list[0], stats_list[1]))
            print("selected F1 value: " + str(stats_list[2]))
                
            # add values to the average calculation
            # the case where this is the first HIT in the list
            if hits_num == 0:
                hits_num += 1
                ave_stats = stats_list
            
            # the case where this is not the first HIT in the list
            else:
                hits_num += 1
                for i in range(0, 3):
                    ave_stats[i] = float(ave_stats[i]) + ((stats_list[i] - ave_stats[i]) / float(hits_num))
            
            print("")
            
        except:
            pass
    
    # print averages
    ave_stats_writer.writerow(["automatic_alignments", hits_num] + ave_stats)
    
    return
Exemple #5
0
def showSine():
    print "Adding Sine Curve"
    var = Tk.BooleanVar()
    x = func.T1()
    y = func.f1(func.T1())
    sine = Tk.Checkbutton(root,
                          text="Sine Curve",
                          variable=var,
                          command=lambda: plot(var, x, y, "Sine"))
    sine.pack(side=Tk.TOP, anchor=Tk.W)
Exemple #6
0
def main():
    x = [[None], [None]]
    print("Решение системы при аналитическом методе: ")
    x = newton1(1, 1, 20, 1e-9, 1e-9)
    print("\tx0 =", x)
    print("Значение 1-ой функции в данной точке: ",
          format(f1(x[0], x[1]), '.9f'))
    print("Значение 2-ой функции в данной точке: ",
          format(f2(x[0], x[1]), '.9f'))
    print("\nРешение системы при численном методе: ")
    xM1 = newton2(1, 1, 20, 1e-9, 1e-9, 0.01)
    xM2 = newton2(1, 1, 20, 1e-9, 1e-9, 0.05)
    xM3 = newton2(1, 1, 20, 1e-9, 1e-9, 0.1)
    print("M = 0.01\tx0 =", xM1)
    print("M = 0.05\tx0 =", xM2)
    print("M = 0.10\tx0 =", xM3)
    print("Значение 1-ой функции в данной точке: ",
          format(f1(xM1[0], xM1[1]), '.9f'))
    print("Значение 2-ой функции в данной точке: ",
          format(f2(xM1[0], xM1[1]), '.9f'))
Exemple #7
0
 def print_matrix(matrix, classes):
     p = precision(matrix)
     r = recall(matrix)
     f = f1(p, r)
     table = BeautifulTable()
     table.column_headers = [
         "Class Name", "Precision", "Recall", "F1 Score"
     ]
     for i in range(len(classes)):
         table.append_row([classes[i][0], p[i], r[i], f[i]])
     print(table)
     total_f1 = np.sum(f) / len(f)
     print("Total F1 score: " + str(total_f1))
     return total_f1
Exemple #8
0
    def print_score(matrix, classes, points=8):
        def to_str(num):
            return ("%." + str(points) + "f") % round(float(num), points)

        p = precision(matrix)
        r = recall(matrix)
        f = f1(p, r)
        table = BeautifulTable()
        table.column_headers = [
            "Class Name", "Precision", "Recall", "F1 Score"
        ]
        for i in range(len(classes)):
            table.append_row(
                [classes[i][0],
                 to_str(p[i]),
                 to_str(r[i]),
                 to_str(f[i])])
        print(table)
        total_f1 = np.sum(f) / len(f)
        print("Total F1 score: " + to_str(total_f1))
        return total_f1
Exemple #9
0
def getNewY(x, y):
    return y - (f.f2(x, y) * f.f1_dx(x, y) - f.f1(x, y) * f.f2_dx(x, y)) / (
        f.f1_dx(x, y) * f.f2_dy(x, y) - f.f2_dx(x, y) * f.f1_dy(x, y))
Exemple #10
0
def getNewX(x, y):
    return x - (f.f1(x, y) * f.f2_dy(x, y) - f.f2(x, y) * f.f1_dy(x, y)) / (
        f.f1_dx(x, y) * f.f2_dy(x, y) - f.f2_dx(x, y) * f.f1_dy(x, y))
def scan_results(qual_type, filename, worker_dict):
    print("****************************************************")
    print(qual_type)
    print(filename)
    print("****************************************************")
    results = csv.reader(open(filename, 'rU'), delimiter = ",")
    
    for row in results:
        try:
            print("")
            hit_id = row[0]
            worker_id = row[15]
            
            # skip first line and a particular user who was completed both "all" and "participated" HITs
            if hit_id == "HITId" or (worker_id == "AURYD2FH3FUOQ" and qual_type == "all"):
                continue
            
            print("")
            print(row)
            print("HITID: " + str(hit_id))
            print("WORKERID: " + str(worker_id))
            print("QUAL TYPE: " + qual_type)
            
            # make corrections to fields labeled "unchanged" or "{}"
            if row[46] == "unchanged": 
                row[46] = row[31]
            if row[43] == "unchanged":
                row[43] = row[32]
            for i in [35, 36, 43, 46]:
                if row[i] == "{}":
                    row[i] = ""
            
            sure_submission = row[46]
            poss_submission = row[43]
            sure_control = row[35]
            poss_control = row[36]
            print("defined rows")
                
            # convert all alignments to sets
            sure_submission_set = set(sure_submission.split())
            print("SURE_SUBMISSION_STRING: " + str(sure_submission))
            print("SURE_SUBMISSION_SET: " + str(sure_submission_set))
            all_submission_set = set(poss_submission.split()) | sure_submission_set
            print("ALL_SUBMISSION_SET: " + str(all_submission_set))
            sure_control_set = set(sure_control.split())
            print("SURE_CONTROL_SET: " + str(sure_control_set))
            all_control_set = set(poss_control.split()) | sure_control_set
            print("ALL_CONTROL_SET: " + str(all_control_set))
            
            # create a list of accuracy stats for the current HIT
            # stats_list[0] = precision, stats_list[1] = recall, stats_list[2] = f1
            stats_list = [precision(sure_submission_set, all_control_set), recall(all_submission_set, sure_control_set)]
            print("STATS_LIST_INITIAL: " + str(stats_list))
            stats_list.append(f1(stats_list[0], stats_list[1]))
            print("STATS_LIST_FINAL: " + str(stats_list))
            
            # add worker to worker_dict
            # the case where worker does not exist in worker_dict
            if worker_id not in worker_dict:
                worker_list = [1, qual_type]
                worker_list = worker_list + stats_list
                worker_dict[worker_id] = worker_list
            
            # the case where worker already exists in worker_dict
            else: 
                worker_list = worker_dict[worker_id]
                worker_list[0] = worker_list[0] + 1
                for i in range(0, 3):
                    worker_list[i + 2] = float(worker_list[i + 2]) + ((stats_list[i] - worker_list[i + 2]) / float(worker_list[0]))
            
        except:
            pass
Exemple #12
0
import functions as f

while (1):
    print(
        "Press i to access function number i(eg:-1 for fun1)\nPress 6 to exit")
    c = int(input())
    if c == 1:
        print("Enter x and y:")
        x, y = input().split()
        x = int(x)
        y = int(y)
        print("Result=", f.f1(x, y))
    elif c == 2:
        print("Enter n and r:")
        n, r = input().split()
        n = int(n)
        r = int(r)
        print("Result=", f.f2(n, r))
    elif c == 3:
        n = int(input("Enter n:"))
        print("Result=", f.f3(n))
    elif c == 4:
        print("Enter m and n")
        m, n = input().split()
        m = int(m)
        n = int(n)
        print("Result=", f.f4(m, n))
    elif c == 5:
        print("Enter m and x")
        m, x = input().split()
        m = int(m)
def scan_csv(results, worker_dict, hits_result_writer):
    # Dictionary indexes hits by hitId, each hitId maps to a list 
    # where l[0] = # hits completed and l[1] = # hits correct
    hits_dict = {}
    
    num_rows = 0
    for row in results:
        print(num_rows)
        num_rows += 1
        try:
            hitId = row[0]
            workerId = row[15]
            
            print("hitId: "  + hitId)
            print("workerId: "  + workerId)
            
            # skip first line
            if hitId == "HITId":
                continue    
            
            ### skip HITs 311HQEI8RS1Q91M7H2OGRN5V4US7ZI and 37Y5RYYI0PQNN46K4NY6PTLGJI8SXE
            ### This is because those HITs have strange answer values
            if hitId == "311HQEI8RS1Q91M7H2OGRN5V4US7ZI" or hitId == "37Y5RYYI0PQNN46K4NY6PTLGJI8SXE":
                continue
            
            # if an "unchanged" value is encountered, replace with the proper value
            if row[48] == "unchanged": #sureAlignments
                row[48] = row[31]
            if row[42] == "unchanged": #possAlignments
                row[42] = "{}"
            if row[44] == "unchanged": #sourceHighlights
                row[44] = "{}"
            if row[50] == "unchanged": #targetHighlights
                row[50] = "{}"
            
            # convert all alignment results to sets
            sure_sub_f = set(row[48].split())
            sure_sub_i = set(row[49].split())
            sure_ans = set(row[35].split())
            pos_sub_f = set(row[42].split())
            pos_sub_i = set(row[43].split())
            pos_ans = set(row[36].split())
            src_sub_f = set(row[44].split())
            src_sub_i = set(row[45].split())
            src_ans = set(row[37].split())
            tgt_sub_f = set(row[50].split())
            tgt_sub_i = set(row[51].split())
            tgt_ans = set(row[38].split())
            
            prec_i = precision(sure_sub_i, sure_ans)
            print("prec_i" + str(prec_i))
            rec_i = recall(sure_sub_i, sure_ans)
            print("rec_i" + str(rec_i))
            f1_i = f1(prec_i, rec_i)
            print("f1_i" + str(f1_i))
            prec_f = precision(sure_sub_f, sure_ans)
            print("prec_f" + str(prec_f))
            rec_f = recall(sure_sub_f, sure_ans)
            print("rec_f" + str(rec_f))
            f1_f = f1(prec_f, rec_f)
            print("f1_f" + str(f1_f))
                        
            # create dictionary of HITs data
            if hitId not in hits_dict:
                hits_list = [1, 0]
                hits_dict[hitId] = hits_list
            else:
                hits_list = hits_dict[hitId]
                hits_list[0] = hits_list[0] + 1
            
            # the case where the worker does not exist in worker_dict
            if workerId not in worker_dict:
                # initialize initial worker_list
                completed_HITs = [hitId]
                worker_list = [1, 0, 0, prec_i, rec_i, f1_i, prec_f, rec_f, f1_f, completed_HITs]
                worker_dict[workerId] = worker_list
                
                # check if user's final submission is correct
                if sure_sub_f == sure_ans and pos_sub_f == pos_ans and src_sub_f == src_ans and tgt_sub_f == tgt_ans:
                    print("Correct submission")
                    worker_list[1] = worker_list[1] + 1
                    hits_list[1] = hits_list[1] + 1 ### DELETE THIS LATER, DEBUGGING ONLY
                    
                # print out errors if encountered
                if sure_sub_f != sure_ans:
                    print("Sure alignments incorrect, expected " + str(sure_ans) + ", got " + str(sure_sub_f))
                
                if pos_sub_f != pos_ans:
                    print("Possible alignments incorrect, expected " + str(pos_ans) + ", got " + str(pos_sub_f))
                    
                if src_sub_f != src_ans:
                    print("Source highlights incorrect, expected " + str(src_ans) + ", got " + str(src_sub_f))
                    
                if tgt_sub_f != tgt_ans:
                    print("Target highlights incorrect, expected " + str(tgt_ans) + ", got " + str(tgt_sub_f))
                
                #change correctness rate
                worker_list[2] = worker_list[1]
                    
            # the case where the worker already exists in work_dict
            else:
                
                worker_list = worker_dict[workerId]
                
                # if user has already completed this HIT, skip
                if hitId in worker_list[9]:
                    continue
            
                # mark the worker as having completed this HIT
                worker_list[9].append(hitId)
                
                
                # increase worker's completed HIT count   
                worker_list[0] = worker_list[0] + 1
                
                # check if user's final submission is correct
                if sure_sub_f == sure_ans and pos_sub_f == pos_ans and src_sub_f == src_ans and tgt_sub_f == tgt_ans:
                    print("Correct submission")
                    worker_list[1] = worker_list[1] + 1
                    hits_list[1] = hits_list[1] + 1 
                    
                ### print out errors if encountered
                if sure_sub_f != sure_ans:
                    print("Sure alignments incorrect, expected " + str(sure_ans) + ", got " + str(sure_sub_f))
                
                if pos_sub_f != pos_ans:
                    print("Possible alignments incorrect, expected " + str(pos_ans) + ", got " + str(pos_sub_f))
                    
                if src_sub_f != src_ans:
                    print("Source highlights incorrect, expected " + str(src_ans) + ", got " + str(src_sub_f))
                    
                if tgt_sub_f != tgt_ans:
                    print("Target highlights incorrect, expected " + str(tgt_ans) + ", got " + str(tgt_sub_f))

                    
                # update correctness rate
                worker_list[2] = float(worker_list[1]) / float(worker_list[0])
                
                # update precision, recall and f1 for initial guesses
                worker_list[3] = float(worker_list[3]) + ((prec_i - worker_list[3]) / float(worker_list[0]))
                
                worker_list[4] = float(worker_list[4]) + ((rec_i - worker_list[4]) / float(worker_list[0]))
                
                worker_list[5] = float(worker_list[5]) + ((f1_i - worker_list[5]) / float(worker_list[0]))
                
                # update precision, recall and f1 after seeing answer key
                worker_list[6] = float(worker_list[6]) + ((prec_f - worker_list[6]) / float(worker_list[0]))
                
                worker_list[7] = float(worker_list[7]) + ((rec_f - worker_list[7]) / float(worker_list[0]))
                
                worker_list[8] = float(worker_list[8]) + ((f1_f - worker_list[8]) / float(worker_list[0]))
            
            print("")
            
        except:
            pass
        
    # write HITs statistics 
    for hitId in hits_dict:
        hits_list = hits_dict[hitId]
        hits_result_writer.writerow([hitId, hits_list[0], hits_list[1], float(hits_list[1]) / float(hits_list[0])])
        print (str(hitId) + ":  [" + str(hits_list[0]) + ", " + str(hits_list[1]) + ", " + str((float(hits_list[1]) / float(hits_list[0]))) + "]")
    print("")
        
    return
def aggsub(x, prob, eps, mit, tau, sig1, delta, sig2, c1, c2, tlimit):
    """ Aggregate subgradient method for nonsmooth DC optimization.
    
    Input:
        x       - a starting point;
        prob    - selection of problem:
                0 - PWLRL1 problem
                1 - auxiliary min-problem
        eps     - optimality tolerance;
        mit     - maximum number of inner iterations;   
        tau     - proximity parameter, tau > 0;
        sig1    - decrease parameter for tau, sig1 in (0,1);
        delta   - inner iteration tolerance, delta > 0; 
        sig2    - decrease parameter for delta, sig2 in (0,1];
        c1,c2   - line search parameters, 0 < c2 <= c1 < 1; 
        tlimit  - time limit for aggsub.
        
    Global parameters:    
        a       - an input matrix;
        
    Output:
        x       - a solution obtained;
        f       - the objective value at x;   
        nit     - number of iterations;
        nf      - number of function evaluations;        
        ng      - number of subgradient evaluations;
    """

    # Import DC functions and their subgradients
    import functions
    #print(config.a)   # just testing
    # import time
    import time

    # Starting time for aggsub
    usedtime0 = time.clock()

    fdiff = -99.0
    # Initialization
    if prob == 0:
        f1 = functions.f1(x)
        f2 = functions.f2(x)
    else:
        f1 = functions.auxminf1(x)
        f2 = functions.auxminf2(x)

    fold = f1 - f2  # Original function value
    print "f original is", fold
    print "Computing..."
    nf = 1  # Number of function evaluations.
    ng = 0  # Number of subgradient evaluations.
    nit = 0  # Number of iterations.
    maxii = max(min(x.size, 500), 50)  # Maximum number of inner iterations.
    stopii = -1  # reason to stop the inner iteration:
    # stopii =-1 - not stopped yet;
    # stopii = 0 - small gradient: canditate solution;
    # stopii = 1 - decent direction found;
    # stopii = 2 - too many inner iterations without progress.
    small = 1e-5
    nrmnew = 1e+10
    ii = 0

    # Outer iteration
    while True:
        # Step 1
        nii = 0  # Number of inner iterations.
        dd = np.ones_like(x) / np.sqrt(
            x.size)  # Initialization of the direction dd.
        if prob == 0:
            f1 = functions.f1(x)  # Needed for correct confic tables.
            g2 = functions.df2(x)  # Subgradient of the DC component f2.
            f1 = functions.f1(x + tau * dd)  # test for bug fix
            g1 = functions.df1(x +
                               tau * dd)  # Subgradient of the DC component f1.
        else:
            f1 = functions.auxminf1(x)  # Needed for correct confic tables.
            g2 = functions.dauxminf2(x)  # Subgradient of the DC component f2.
            f1 = functions.auxminf1(x + tau * dd)  # test for bug fix
            g1 = functions.dauxminf1(
                x + tau * dd)  # Subgradient of the DC component f1.

        # No need to recompute g2 if returning from Step 6.
        ng += 1
        sg = g1 - g2  # Approx. subgradient of the function.
        asg = sg  # Aggregate subgradient.
        nrmasg = 0.0  # Norm of the aggregate subgradient.

        # Inner iteration
        while True:
            nii += 1

            # Step 2
            sgdiff = np.dot(sg - asg, sg - asg)
            if sgdiff > small:
                #lam = (nrmasg*nrmasg - np.dot(sg,asg))/sgdiff
                lam = -np.dot(
                    asg, sg - asg) / sgdiff  # this should be the same as above
            else:
                lam = 0.50

            if lam > 1.0 or lam < 0.0:
                print "Projecting lambda = ", lam, "to [0,1]."
                if lam < 0:
                    lam = 0.0
                else:
                    lam = 1.0

            # If lam=0 nothing changes and we end up to inner iteration termination 2.

            # Step 3
            asg = lam * sg + (1.0 -
                              lam) * asg  # The new aggregate subgradient.
            nrmasg = np.linalg.norm(asg)  # Norm of the aggregate subgradient.

            if nrmasg < delta:  # Inner iteration termination
                stopii = 0
                break  # With this we should go to Step 6.

            if nii % 5 == 0:  # Inner iteration termination 2
                nrmold = nrmnew
                nrmnew = nrmasg
                if np.abs(nrmold - nrmnew) < 0.0001:
                    #print "Norm is not changing."
                    stopii = 0
                    break  # With this we should go to Step 6.

            # Step 4: Search direction
            dd = -asg / nrmasg

            # Step 5
            xtau = x + tau * dd
            if prob == 0:
                f1 = functions.f1(xtau)
                f2 = functions.f2(xtau)
            else:
                f1 = functions.auxminf1(xtau)
                f2 = functions.auxminf2(xtau)

            fnew = f1 - f2
            nf += 1
            dt = fnew - fold

            #if (dt > -c1*tau*nrmasg and -dt/fold > 0.1): # makes results worse (limited testing)
            if (dt > -c1 * tau * nrmasg):  # Not a descent direction
                #if (dt < 0 and -dt/fold > 0.05): # just for testing purposes
                #    print "No descent but should it be?",-dt/fold,dt,-c1*tau*nrmasg,fold,fnew
                if prob == 0:
                    g1 = functions.df1(xtau)
                else:
                    g1 = functions.dauxminf1(xtau)
                sg = g1 - g2
                ng += 1
            else:
                stopii = 1
                break  # with this we should go to Step 7.

            # Additional termination from inner iteration
            if nii > maxii:
                if tau > eps:
                    #print "Too many inner iterations. Adjusting tau."
                    stopii = 2
                else:
                    print "Too many inner iterations with no descent direction found."  #,nit,fnew,fold
                    if ii == 0:
                        ii = 1
                        nii = 0  # Number of inner iterations starts again from zero.
                        #dd = -np.ones_like(x)/np.sqrt(x.size)  # Initialization of the direction dd.
                        dd = -dd  # Opposite of the direction dd.
                        if config.nfea < 200:  # tau > 0, default = 10, if n<200,
                            tau = 10.0  #                    50, otherwise.
                        else:
                            tau = 50.0
                        if prob == 0:
                            f1 = functions.f1(x + tau * dd)  # test for bug fix
                            g1 = functions.df1(
                                x + tau *
                                dd)  # Subgradient of the DC component f1.
                        else:
                            f1 = functions.auxminf1(
                                x + tau * dd)  # test for bug fix
                            g1 = functions.dauxminf1(
                                x + tau *
                                dd)  # Subgradient of the DC component f1.

                        ng += 1
                        sg = g1 - g2  # Approx. subgradient of the function.
                        asg = sg  # Aggregate subgradient.
                        nrmasg = 0.0  # Norm of the aggregate subgradient.
                        print "Trying opposite direction."
                        print "Computing..."
                        continue

                    else:
                        print "Exit."
                        stopii = 3
                break

        # Step 6: Stopping criterion

        if stopii == 0:  # Small norm
            if tau <= eps:
                print "Critical point found."
                break  # Critical point found
            else:
                tau *= sig1
                delta *= sig2
                stopii = -1
                nit += 1
                continue

        # Step 7: Line search
        elif stopii == 1:  # Descent direction
            if ii == 1:
                print "Descent direction found."
                print "Computing..."
                ii = 0

            step = tau  # Here fnew = f(xtau), fold = f(x)
            count = 0
            while count < 10:  # Maximum of 10 search are made
                count += 1
                step *= 2.0
                xnew = x + step * dd
                if prob == 0:
                    f1 = functions.f1(xnew)
                    f2 = functions.f2(xnew)
                else:
                    f1 = functions.auxminf1(xnew)
                    f2 = functions.auxminf2(xnew)

                nf += 1
                fdiff = f1 - f2 - fold

                if fdiff <= -c2 * step * nrmasg:
                    xtau = xnew
                    fnew = f1 - f2
                else:
                    break

            # Step 8: Update step
            # The last f1 and f2 are computed at xnew =/ xtau, need to compute f1(xtau) to get max_line correctly

            x = xtau
            fold = fnew

        if tau > eps:
            tau *= sig1  # tau too small is not good, needs safeguard
        elif np.abs(fdiff) < 1e-7:
            print "Termination with small change in function values."  #,fdiff
            break

        delta *= sig2

        nit += 1

        # Termination with maximum number of iterations.
        if nit >= mit:  # Number of iterations > mit
            print "Termination with maximum number of iterations."
            break

        # Termination with the time limit for AggSub.
        usedtime = time.clock() - usedtime0
        if usedtime > tlimit:
            print "Termination with the time limit for AggSub."
            break  # with this we should go to Step 7.

        if stopii == 3:

            #print "No decent direction found in inner iteration."
            break

        stopii = -1  # To next inner iteration.

    # Outer iteration ends

    f = fold

    return x, f, nit, nf, ng
Exemple #15
0
import functions
from State import State
import numpy as np
from Timer import Timer
import mcmc

s1 = State(np.random.randn(3), 1.0)
with Timer(digits=6):
    functions.f1(s1, 1000)

with Timer(digits=6):
    np.random.randn(100 * 100).sum()

with Timer('test_metropolis (cython)', digits=6):
    mcmc.test_metropolis(1000 * 1000)