Exemplo n.º 1
0
def main(elements, adjust, exp):
    '''
    Principal
    '''
    insertions         = elements
    searches           = int(0.5*elements)
    not_found_searches = int(0.25*searches)
    swaps_amount       = int(0.05*elements) # aumentando la cantidad de swaps de cada iteracion
    init               = False
    analizer           = ExperimentAnalyzer(exp)
    print('n :' + str(elements))
    print('experiments: ' + str(exp))

    for e in range(exp):
        sequence           = RandomSequence(elements, 10**5)

        while (not init) or abb_comps - abbr_comps > abbr_comps*adjust:
            if not init:
                mean_height = 0
                iteration   = 0
                init = True

            # Número de iteracion para acercar comportamiento de árboles
            iteration += 1
            # print('.'),
            sys.stdout.flush()

            # Se definen las estructuras
            abb_tree  = ABB()
            abbr_tree = ABBRandom()
            skip_list = SkipList()

            # Contadores
            abb_comps    = 0
            abbr_comps   = 0
            skip_comps   = 0

            abb_search_comps    = 0
            abbr_search_comps   = 0
            skip_search_comps   = 0

            # Lista para ABB
            abb_elements = sequence.k_swaps(swaps_amount)

            # Inserciones
            for i in range(insertions):
                abb_comps  += abb_tree.insert(abb_elements[i])
                abbr_comps += abbr_tree.insert(sequence.get_element(i))
                skip_comps += skip_list.insert(sequence.get_element(i))

            # Busquedas
            for i in range(searches):
                if i < not_found_searches:
                    elem = sequence.get_random_free_element()
                else:
                    elem = sequence.get_random_element()
                abb_search_comps  += abb_tree.search(elem)[1]
                abbr_search_comps += abbr_tree.search(elem)[1]
                skip_search_comps += skip_list.search(elem)[1]

            # Altura de skip list
            mean_height += 1.0*skip_list.max_height

            # Siguiente iteración
            sequence.elements = abb_elements

            # print(str(1.0*abb_comps/insertions) + ' ' + str(1.0*abbr_comps/insertions) + ' ' + str(1.0*skip_comps/insertions))

        # Suma de resultados promedio (n operaciones)
        # Resultados: Inserciones
        abb_comps  = 1.0*abb_comps/insertions
        abbr_comps = 1.0*abbr_comps/insertions
        skip_comps = 1.0*skip_comps/insertions

        # Resultados: Busquedas
        abb_search_comps  = 1.0*abb_search_comps/searches
        abbr_search_comps = 1.0*abbr_search_comps/searches
        skip_search_comps = 1.0*skip_search_comps/searches

        # Resultados: Altura de Skip List
        mean_height = 1.0*mean_height/iteration

        print('\ninsertions: ' + str(abb_comps) + ',' + str(abbr_comps) + ',' + str(skip_comps))
        print('searches: ' + str(abb_search_comps) + ',' + str(abbr_search_comps) + ',' + str(skip_search_comps))
        print('skiplist height: ' + str(mean_height))
        print('total swaps: ' + str(swaps_amount*iteration))

        analizer.add_abb_result(abb_comps, abb_search_comps, swaps_amount*iteration)
        analizer.add_abbr_result(abbr_comps, abbr_search_comps)
        analizer.add_skip_list_result(skip_comps, skip_search_comps, mean_height)

        init = False

    print('\n')
    analizer.compute_errors()
    analizer.show_results()
    print('\n')
Exemplo n.º 2
0
def main(elements, exp):
    '''
    Principal
    '''
    insertions         = elements
    searches           = int(0.5*elements)
    not_found_searches = int(0.25*searches)
    analizer           = ExperimentAnalyzer(exp)
    print('n :' + str(elements))
    print('experiments: ' + str(exp))

    for e in range(exp):
        sequence = RandomSequence(elements, 10**5)
        swaps_amount = int(0.7*elements) # swaps

        # Contadores
        abb_comps    = 0
        abbr_comps   = 0
        skip_comps   = 0

        abb_search_comps    = 0
        abbr_search_comps   = 0
        skip_search_comps   = 0

        # Se definen las estructuras
        abb_tree  = ABB()
        abbr_tree = ABBRandom()
        skip_list = SkipList()

        # Lista para ABB
        abb_elements = sequence.k_swaps(swaps_amount)

        # Inserciones
        for i in range(insertions):
            abb_comps  += abb_tree.insert(abb_elements[i])
            abbr_comps += abbr_tree.insert(abb_elements[i])
            skip_comps += skip_list.insert(abb_elements[i])

        # Busquedas
        for i in range(searches):
            if i < not_found_searches:
                elem = sequence.get_random_free_element()
            else:
                elem = sequence.get_random_element()
            abb_search_comps  += abb_tree.search(elem)[1]
            abbr_search_comps += abbr_tree.search(elem)[1]
            skip_search_comps += skip_list.search(elem)[1]

        # Altura de skip list
        mean_height = 1.0*skip_list.max_height

        # Suma de resultados promedio (n operaciones)
        # Resultados: Inserciones
        abb_comps  = 1.0*abb_comps/insertions
        abbr_comps = 1.0*abbr_comps/insertions
        skip_comps = 1.0*skip_comps/insertions

        # Resultados: Busquedas
        abb_search_comps  = 1.0*abb_search_comps/searches
        abbr_search_comps = 1.0*abbr_search_comps/searches
        skip_search_comps = 1.0*skip_search_comps/searches

        print('\ninsertions: ' + str(abb_comps) + ',' + str(abbr_comps) + ',' + str(skip_comps))
        print('searches: ' + str(abb_search_comps) + ',' + str(abbr_search_comps) + ',' + str(skip_search_comps))
        print('skiplist height: ' + str(mean_height))
        print('total swaps: ' + str(swaps_amount))

        analizer.add_abb_result(abb_comps, abb_search_comps, swaps_amount)
        analizer.add_abbr_result(abbr_comps, abbr_search_comps)
        analizer.add_skip_list_result(skip_comps, skip_search_comps, mean_height)

    analizer.compute_errors()
    analizer.show_results()
    print('\n')