Exemplo n.º 1
0
def get_sway_res(model):
    # generating the 10k random solutions
    candidates = list()
    for _ in range(10000):
        ran_dec = [random.random() for _ in range(model.decsNum)]
        can = model.Individual(ran_dec)
        candidates.append(can)
    global M
    M = model
    res = sway(candidates, model.eval, where, comparing)

    return res
Exemplo n.º 2
0
def get_sway_res(model):
    # load the  10k sat solutions
    # with open('./tse_rs/' + model.name + '.txt', 'r') as f:
    # candidates = list()
    #     for l in f:
    #         can = model.Individual(l.strip('\n'))
    #         candidates.append(can)

    candidates = sat_gen_valid_pop(model, 10000)
    res = sway(candidates, model.eval,
               partial(split_products, groupC=min(15, model.featureNum // 7)),
               comparing)
    return res
Exemplo n.º 3
0
def get_sway_res(model):
    def _dist(ind1, ind2):
        d = 0
        for i, j in zip(ind1, ind2):
            d += (i - j)**2
        return d

    def _where(pop):
        rand = random.choice(pop)
        ds = [_dist(i, rand) for i in pop]
        east = pop[ds.index(max(ds))]
        ds = [_dist(i, east) for i in pop]
        west = pop[ds.index(max(ds))]

        c = _dist(east, west)
        cc = 2 * c**0.5

        mappings = list()
        for x in pop:
            a = _dist(x, west)
            b = _dist(x, east)
            d = (a + c - b) / cc
            mappings.append((x, d))

        mappings = sorted(mappings, key=lambda i: i[1])
        mappings = [i[0] for i in mappings]

        n = len(mappings)
        eastItems = mappings[:int(n * 0.2)] + mappings[int(n * 0.5):int(n *
                                                                        0.8)]
        westItems = mappings[int(n * 0.2):int(n * 0.5)] + mappings[int(n *
                                                                       0.8):]

        # westItems = mappings[len(mappings)//2:]
        return west, east, eastItems, westItems

    def _comparing(part1, part2):
        return bin_dominate(part1, part2)

    # Starting SWAY execution here
    model.eval_counts = 0
    candidates = list()
    for _ in range(10000):
        ran_dec = [random.random() for _ in range(model.decsNum)]
        can = model.Individual(ran_dec)
        candidates.append(can)
    res = sway(candidates, functools.partial(model.eval, normalized=False),
               _where, _comparing)
    return emo.sortNondominated(res, len(res), first_front_only=True)[0]
Exemplo n.º 4
0
def tcp_sway(dataset, suite, ver, initial, stop, alg=2):
    # num_tests_dict = {'flex':21, 'grep':193, 'gzip':211, 'sed':36}
    num_tests_dict = {
        'flex': {
            1: 21,
            2: 525
        },
        'grep': {
            1: 193,
            2: 152,
            3: 140
        },
        'gzip': {
            1: 211
        },
        'sed': {
            1: 36,
            2: 360
        }
    }

    # Compare function
    def comparing(part1, part2):  ##Need modification
        if ver == '0':
            return get_apsd(dataset, part1) > get_apsd(dataset, part2)
        else:
            return get_apsd_linux(dataset, suite, ver, part1) > get_apsd_linux(
                dataset, suite, ver, part2)

    if ver == '0':
        path = 'Datasets/' + dataset + '/traces'
        file_list = os.listdir(path)
        length = sum(['dump' in name for name in file_list])
    else:  # linux utils
        length = num_tests_dict[dataset][suite]

    ## Build initial population in a parallel manner
    seed = np.array(range(1, length + 1))
    wrapper = partial(permute_row, seed)
    pool = Pool()
    new_rows = pool.map(wrapper, range(1, initial))
    pool.close()
    candidates = np.concatenate((np.reshape(seed,
                                            (1, length)), np.array(new_rows)))
    # candidates = []
    # for _ in range(initial):
    #     x = list(range(1, length + 1))
    #     while x in candidates:  # avoid any repetitions
    #         random.shuffle(x)
    #     candidates.append(x)

    ## Binary embedding
    if alg == 1:
        raise ValueError("Binary embedding not yet implemented!!")
        # emb_cand, emb_dict = embed(candidates)
        # res = sway(emb_cand, partial(split_products, groupC=min(15, dim // 7)), comparing, alg, emb_dict)
        # return res, candidates

    ## Continuous embedding
    elif alg == 2:
        return sway(candidates, where, comparing, stop, alg, None), candidates
Exemplo n.º 5
0
def optimize(init_pop, eval_func):
    import warnings
    warnings.filterwarnings('ignore')
    return sway(init_pop, eval_func, split_products, comparing)
Exemplo n.º 6
0
def optimize(init_pop, eval_func):
    import warnings
    warnings.filterwarnings('ignore')
    res = sway(init_pop, eval_func, where, comparing)
    return res