Beispiel #1
0
def mutation(prob, sol, local_search, ls_args):
    logging.info("Mutation")
    diam = prob.get_diam()
    bounds = np.zeros(prob.v_size, dtype=int)
    for i in range(sol.v_size):
        i_col = min(sol[i], diam - 1) + 1
        bound = np.sum(prob.dist_matrix[i] == i_col)
        bounds[i] = bound

    v = np.argmax(bounds)
    v_col = min(sol[v], diam - 1) + 1
    adj_mat = (prob.dist_matrix == 1)
    # changes = (prob.dist_matrix[v] == v_col)
    changes = np.logical_or((prob.dist_matrix[v] == v_col), adj_mat[v])
    adj_mat[v] = changes
    adj_mat[..., v] = np.transpose(changes)
    new_prob = GraphProblem(adj_mat)
    new_sol = PackColSolution(new_prob)
    new_sol = rlf_algorithm(new_prob)
    new_sol = local_search(new_prob, sol=new_sol, **ls_args)

    # mutated = PackColSolution(prob)
    # mutated[v] = new_sol[v]
    # mutated = rlf_algorithm(prob, sol=mutated)

    ordering = new_sol.get_greedy_order()
    mutated = greedy_algorithm(prob, ordering)
    logging.info("mutation diff: " +
                 str(np.sum(np.equal(mutated[:], sol[:])) / prob.v_size))
    return mutated
def aics_algorithm(prob, iter_count=200, random_init=True):
    if random_init:
        best_sol = greedy_algorithm(prob, random_order(prob))
    else:
        best_sol = rlf_algorithm(prob)

    best_score = best_sol.get_max_col()
    mean_score = best_score
    # print(best_sol, "->", best_score)
    blame = best_sol[:]
    # print(blame, "\n")

    permut = np.arange(1, best_sol.get_max_col() + 1, dtype=int)
    permut = rd.permutation(permut)
    priority_seq = best_sol.get_by_permut(permut)
    for i in np.arange(1, iter_count + 1):
        # The Constructor
        cur_sol = greedy_algorithm(prob, priority_seq)
        cur_score = cur_sol.get_max_col()
        # print(np.round(mean_score, decimals=2), cur_score)

        # The Analyzer
        if cur_score >= np.floor(mean_score):
            w = [mean_score, cur_score]
            blame = np.average([cur_sol[:], blame], axis=0, weights=w)
            permut = np.arange(1, cur_sol.get_max_col() + 1, dtype=int)
            permut = rd.permutation(permut)
            priority_seq = cur_sol.get_by_permut(permut)
        else:
            if cur_score < best_score:
                best_sol = cur_sol
                best_score = best_sol.get_max_col()
            blame = np.average([cur_sol[:], blame], axis=0)
            priority_seq = np.arange(prob.v_size)[np.argsort(blame)]

        mean_score = (mean_score * (3. / 4)) + (cur_score * (1. / 4))

        # print(np.round(blame, decimals=2), "\n")

        # The Prioritizer

    return best_sol
Beispiel #3
0
def crossover_permut(prob, sols):
    logging.info("Crossover stupid and easy")
    p1 = sols[0].get_greedy_order()
    p2 = sols[1].get_greedy_order()
    child_permut = p1
    chrom_size = np.floor(len(child_permut) / 2)
    to_change = rd.choice(np.arange(prob.v_size), chrom_size, replace=False)
    deleted = child_permut[to_change]
    replacing = np.intersect1d(p2, deleted)
    child_permut[to_change] = replacing
    return greedy_algorithm(prob, child_permut)
def generate_population(prob, size, heuristic, init_args):
    logging.info("Init population by permut")
    pop = []
    indiv = heuristic(prob, **init_args)
    permut = np.arange(1, indiv.get_max_col()+1, dtype=int)
    pop.append(indiv)
    logging.info("init candidate 0: " + str(indiv.get_max_col()))
    for i in range(1, size):
        new_permut = rd.permutation(permut)
        priority = indiv.get_by_permut(new_permut)
        pop.append(greedy_algorithm(prob, priority))
        logging.info("init candidate " + str(i) + ": " + str(pop[i].get_max_col()))
    return pop
Beispiel #5
0
def crossover_cx(prob, sols):
    logging.info("Crossover cycle")
    p1 = sols[0].get_greedy_order()
    p2 = sols[1].get_greedy_order()
    child1 = np.zeros(prob.v_size, dtype=int)
    child2 = np.zeros(prob.v_size, dtype=int)

    positions = np.arange(prob.v_size, dtype=int)
    order1 = np.argsort(p1)
    inplace = p1 == p2
    child1[inplace] = p1[inplace]
    child2[inplace] = p2[inplace]
    positions[inplace] = -1

    cycle_nbr = 0
    while np.any(positions != -1):
        cycle = [np.argmax(positions > -1)]

        while p1[cycle[0]] != p2[cycle[-1]]:
            step = p2[cycle[-1]]
            pos = order1[step]
            cycle.append(pos)
        positions[cycle] = -1

        if cycle_nbr % 2 == 0:
            child1[cycle] = p1[cycle]
            child2[cycle] = p2[cycle]
        else:
            child1[cycle] = p2[cycle]
            child2[cycle] = p1[cycle]
        cycle_nbr += 1

    sol1 = greedy_algorithm(prob, child1)
    sol2 = greedy_algorithm(prob, child2)
    if sol1.get_max_col() <= sol2.get_max_col():
        return sol1
    else:
        return sol2
Beispiel #6
0
def pertub(prob, sol, heuristic, local_search):
    bounds = np.zeros(prob.v_size, dtype=int)
    for i in range(sol.v_size):
        i_col = sol[i]
        bound = np.sum(prob.dist_matrix[i] == i_col)
        bounds[i] = bound

    v = np.argmax(bounds)
    adj_mat = (prob.dist_matrix == 1)
    changes = (prob.dist_matrix[v] == sol[v])
    adj_mat[v] = changes
    adj_mat[..., v] = np.transpose(changes)
    new_prob = GraphProblem(adj_mat)
    new_sol = heuristic(new_prob)
    new_sol = local_search(new_prob, sol=new_sol, duration=5)
    return greedy_algorithm(prob, new_sol.get_greedy_order())
def swo_algorithm(prob,
                  iter_count=500,
                  blame_value=25,
                  blame_rate=0.85,
                  random_init=True):
    if random_init:
        priority_seq = random_order(prob)
    else:
        priority_seq = (rlf_algorithm(prob)).get_greedy_order()

    best_score = float("inf")
    best_sol = None
    for i in np.arange(iter_count):
        cur_sol = greedy_algorithm(prob, priority_seq)
        cur_score = cur_sol.get_max_col()
        # print(cur_score)

        if cur_score < best_score:
            best_sol = cur_sol.copy()
            best_score = best_sol.get_max_col()
            priority_seq = best_sol.get_greedy_order()
            blame_treshold = np.ceil(blame_rate *
                                     min(best_score, prob.get_diam()))

        b_v = blame_value + abs(cur_score - blame_treshold)
        blame = np.zeros(prob.v_size, dtype=int)
        for v in np.arange(prob.v_size):
            if cur_sol[v] > blame_treshold:
                # blame[v] += max(b_v, cur_sol.pack_size(cur_sol[v]-1))
                # blame[v] += np.ceil(blame_value * (1 + (cur_sol[v] / cur_score)))
                blame[v] += b_v

        prior_blame = blame[priority_seq]
        order = (-prior_blame, np.arange(prob.v_size) - prior_blame)
        priority_seq = priority_seq[np.lexsort(order)]

    return best_sol