Exemplo n.º 1
0
def fast_time(input_file, r, b, path, bbox=False, k=5, memory=False):
    """INPUT
    (str)input_file: path of input file
    (int)r: number of rows
    (int)b: number of bands
    (str)path: path to times
    (bool)bbox: True if BB prioritization
    (int)k: k-shingle size (for BB prioritization)
    (bool)memory: if True keep signature in memory and do not store them to file

    OUTPUT
    (list)P: prioritized test suite
    """

    times = priorTime.getTimesMap(path)

    # ▼ DEFINE SELECTION FUNCTION ▼
    def time_fn(candidates, tcs_minhashes, selected_tcs_minhash,
                prioritized_tcs, tcs, n, times):
        selected_tc = random.choice(tuple(candidates))
        for candidate in candidates:
            if times[candidate] < times[selected_tc]:
                selected_tc = candidate

        for i in xrange(n):
            if tcs_minhashes[selected_tc][i] < selected_tcs_minhash[i]:
                selected_tcs_minhash[i] = tcs_minhashes[selected_tc][i]

        prioritized_tcs.append(selected_tc)
        tcs -= set([selected_tc])
        del tcs_minhashes[selected_tc]

    # ▲ DEFINE SELECTION FUNCTION ▲

    mh_time, ptime, prioritized_tcs, dissimilarity_value = fast(input_file,
                                                                r,
                                                                b,
                                                                time_fn,
                                                                times=times,
                                                                bbox=False,
                                                                k=5,
                                                                memory=False)

    time_value = time_obj_fun(prioritized_tcs, times)

    # print("  Dissimilarity: #%f#"%dissimilarity_value)
    # print("  Time         : #%f#"%time_value)

    return mh_time, ptime, prioritized_tcs, dissimilarity_value, time_value
Exemplo n.º 2
0
def getUsedTime(prioritization, fault_matrix, times_path):
    faults_dict = getFaultDetected(fault_matrix)
    usedTimes = []
    timesMap = priorTime.getTimesMap(times_path)
    for v in xrange(1, len(faults_dict) + 1):
        faulty_tcs = set(faults_dict[v])
        position = 1
        usedTime = 0
        acum = 0
        for tc_ID in prioritization:
            acum += timesMap[tc_ID]
            if tc_ID in faulty_tcs:
                usedTime = acum
                break
            position += 1

        usedTimes.append(usedTime)

    return usedTimes
Exemplo n.º 3
0
def getUsedTimeParallel(batches, fault_matrix, times_path):
    faults_dict = getFaultDetected(fault_matrix)
    usedTimes = []
    timesMap = priorTime.getTimesMap(times_path)
    for v in faults_dict.keys():
        faulty_tcs = set(faults_dict[v])
        acum = 0

        for batch in batches:
            min_time = None
            max_batch = 0
            for tc_ID in batch:
                if timesMap[tc_ID] > max_batch:
                    max_batch = timesMap[tc_ID]
                if tc_ID in faulty_tcs:
                    if min_time is None or timesMap[tc_ID] < min_time:
                        min_time = timesMap[tc_ID]
            if min_time is not None:
                usedTimes.append(acum + min_time)
                break
            else:
                acum += max_batch

    return usedTimes
Exemplo n.º 4
0
def bboxPrioritization(name, prog, v, ctype, k, n, r, b, repeats, selsize):
    javaFlag = True if v == "v0" else False

    fin = "input/{}_{}/{}-{}.txt".format(prog, v, prog, ctype)
    if javaFlag:
        fault_matrix = "input/{}_{}/fault_matrix.pickle".format(prog, v)
    else:
        fault_matrix = "input/{}_{}/fault_matrix_key_tc.pickle".format(prog, v)
    outpath = OUTPUT_FOLDER.format(prog, v, name, repeats)
    ppath = outpath + "prioritized/"

    timesMap = priorTime.getTimesMap("input/{}_{}/".format(prog, v))

    objective_function_values = []

    if ("{}-{}.tsv".format(name, ctype)) not in set(os.listdir(outpath)):
        ptimes, stimes, apfds, realDetectionTime, apfds_c = [], [], [], [], []
        for run in xrange(repeats):
            print " Run", run
            # SELECT APROACH
            memory = not javaFlag
            if name == "FAST-" + selsize.__name__[:-1]:
                stime, ptime, prioritization = fast.fast_(
                        fin, selsize, r, b, bbox=True, k=k, memory=memory)
            elif name == "FAST-pw":
                stime, ptime, prioritization = fast.fast_pw(
                        fin, r, b, bbox=True, k=k, memory=memory)
            elif name == "FAST-time":
                stime, ptime, prioritization, dissimilarity_value, time_value = fast.fast_time(
                        fin, r, b, "input/{}_{}/".format(prog, v), bbox=True, k=k, memory=memory)
            elif name == "TIME-FAST":
                stime, ptime, prioritization = fast.time_fast(
                        fin, r, b, "input/{}_{}/".format(prog, v), bbox=True, k=k, memory=memory)
            elif name == "I-TSD":
                stime, ptime, prioritization = competitors.i_tsd(fin)
            elif name == "STR":
                stime, ptime, prioritization = competitors.str_(fin)
            else:
                print("Wrong input.")
                print(usage)
                exit()

            writePrioritization(ppath, name, ctype, run, prioritization)
            apfd = metric.apfd(prioritization, fault_matrix, javaFlag)
            apfd_c = metric.apfd_c(prioritization, fault_matrix, timesMap)
            realDetectionTime = metric.getUsedTime(prioritization, fault_matrix, "input/{}_{}/".format(prog, v))
            apfds.append(apfd)
            apfds_c.append(apfd_c)
            stimes.append(stime)
            ptimes.append(ptime)
            print("  Progress: 100%  ")
            print "  Running time:", stime + ptime
            if javaFlag:
                print "  APFD:", sum(apfds[run]) / len(apfds[run])
            else:
                print "  APFD:", apfd

            # Only for multi-objetive
            if name == "FAST-time":
                objective_function_values.append((dissimilarity_value, time_value, apfd_c))

        rep = (name, stimes, ptimes, apfds, apfds_c, realDetectionTime)
        #writeOutput(outpath, ctype, rep, javaFlag)
        writeOutputWithTestTime(outpath, ctype, rep, javaFlag)
        print("")

        # Save all solutions
        outpath = OUTPUT_FOLDER.format(prog, v, name, repeats)
        fileout = "{}/{}-{}.tsv".format(outpath, name, 'results')
        with open(fileout, "w") as fout:

            # Only for multi-objetive
            if name == "FAST-time":
                solutions = []
                fout.write("Index\tDissimilarity\tTime\tAPFD_c\n")
                for idx, (dissimilarity_value, time_value, apfd_c) in enumerate(objective_function_values):
                    fout.write("{}\t{}\t{}\t{}\n".format(
                        idx, dissimilarity_value, time_value,  sum(apfd_c)/len(apfd_c)))
                    solutions.append(
                        [idx, dissimilarity_value, time_value,  sum(apfd_c)/len(apfd_c)])
                # Generate and save pareto frontier and graphic
                get_pareto_frontier_and_plot(solutions, outpath)
            
            # Others algorithms
            else: 
                fout.write("Index\tAPFD_c\n")
                for idx, apfd_c in enumerate(apfds_c):
                    fout.write("{}\t{}\n".format( idx, sum(apfd_c)/len(apfd_c)))
    else:
        print name, "already run."
Exemplo n.º 5
0
def time_fast(input_file,
              r,
              b,
              path,
              bbox=False,
              k=5,
              memory=False,
              num_clusters=72,
              fault_matrix=[]):
    """INPUT
    (str)input_file: path of input file
    (int)r: number of rows
    (int)b: number of bands
    (bool)bbox: True if BB prioritization
    (int)k: k-shingle size (for BB prioritization)
    (bool)memory: if True keep signature in memory and do not store them to file

    OUTPUT
    (list)P: prioritized test suite
    """
    times = priorTime.getTimesMap(path)

    # ▼ DEFINE SELECTION FUNCTION ▼
    def pw_fn(candidates, tcs_minhashes, selected_tcs_minhash, prioritized_tcs,
              tcs, n, times):
        selected_tc, max_dist = random.choice(tuple(candidates)), -1
        for candidate in tcs_minhashes:
            if candidate in candidates:
                dist = lsh.jDistanceEstimate(selected_tcs_minhash,
                                             tcs_minhashes[candidate])
                if dist > max_dist:
                    selected_tc, max_dist = candidate, dist

        for i in xrange(n):
            if tcs_minhashes[selected_tc][i] < selected_tcs_minhash[i]:
                selected_tcs_minhash[i] = tcs_minhashes[selected_tc][i]

        prioritized_tcs.append(selected_tc)
        tcs -= set([selected_tc])
        del tcs_minhashes[selected_tc]

    # ▲ DEFINE SELECTION FUNCTION ▲

    groups = cluster2(times.items(), num_clusters)
    prioritization = []
    mh_times = []
    ptimes = []
    for g in groups:
        g_subset = [x[0] for x in g]
        mh_time, ptime, sub_prioritization = fast(input_file,
                                                  r,
                                                  b,
                                                  pw_fn,
                                                  times=times,
                                                  bbox=False,
                                                  k=5,
                                                  memory=False,
                                                  sub_set=g_subset)
        mh_times.append(mh_time)
        ptimes.append(ptime)
        prioritization = prioritization + sub_prioritization

    return sum(mh_times), sum(ptimes), prioritization