Exemple #1
0
        sol,rmn,card = iterated_expansion(nodes, adj, expand_dyn_deg, max_iterations, report_sol)
    elif method == "plateau_rand":
        sol,rmn,card = multistart_local_search(nodes, adj, expand_rand, max_iterations, length, report_sol)
    elif method == "plateau_stat_deg":
        sol,rmn,card = multistart_local_search(nodes, adj, expand_stat_deg, max_iterations, length, report_sol)
    elif method == "plateau_dyn_deg":
        sol,rmn,card = multistart_local_search(nodes, adj, expand_dyn_deg, max_iterations, length, report_sol)
    elif method == "ltm_rand":
        sol,rmn,card = ltm_search(nodes, adj, expand_rand, max_iterations, length, report_sol)
    elif method == "ltm_stat_deg":
        sol,rmn,card = ltm_search(nodes, adj, expand_stat_deg, max_iterations, length, report_sol)
    elif method == "ltm_dyn_deg":
        sol,rmn,card = ltm_search(nodes, adj, expand_dyn_deg, max_iterations, length, report_sol)
    else:
        if method != "hybrid":
            print("unknown method; using hybrid version")
        degree = [len(adj[i]) for i in nodes]   # used on 'expand_sta_deg'
        sol,rmn,card = hybrid(nodes, adj, max_iterations, length, report_sol)

    # import profile
    # profile.run('sol,rmn,card = exp_rand(nodes, adj, max_iterations)')
    # profile.run('sol,rmn,card = exp_plateau(nodes, adj, max_iterations)')

    xcard, xinfeas, xb = evaluate(nodes, adj, sol)
    assert card == xcard and xinfeas == 0

    print()
    print("final solution: z =", len(sol))
    print(sorted(sol))
    print()
def ts_intens_divers(nodes, adj, sol, max_iter, tabulen, report):
    """Execute a tabu search run using intensification/diversification."""
    n = len(nodes)
    tabu = [0 for i in nodes]

    card, infeas, b = evaluate(nodes, adj, sol)
    assert infeas == 0
    bestsol, bestcard, bestb = set(sol), card, list(b)

    D = 1  # self-tuning diversification parameter
    count = 0  # counter for consecutive non-improving iterations
    lastcard = card
    if LOG:
        print("iter:", 0, "non-impr: %d/%d" % (count,D), \
              "\tcard: %d (%d conflicts)" % (card,infeas), "/ best:", bestcard) # , "\t", sol
    for it in range(max_iter):
        tabuIN = 1 + int(tabulen / 100. *
                         card)  # update tabu parameter for inserting vertices
        tabuOUT = 1 + int(
            tabulen / 100. *
            (n - card))  # update tabu parameter for removing vertices
        if infeas == 0:  # solution is feasible, add a new vertex
            infeas += move_in(nodes, adj, sol, b, tabu, tabuIN, tabuOUT, it)
            card += 1
        else:  # solution is infeasible, remove a vertex
            infeas += move_out(nodes, adj, sol, b, tabu, tabuIN, tabuOUT, it)
            card -= 1

        if LOG:
            print("iter:", it+1, "non-impr: %d/%d" % (count,D), \
                  "\tcard: %d (%d conflicts)" % (card,infeas), "/ best:", bestcard) # , "\t", sol

        if infeas == 0 and card > bestcard:
            # improved best found solution, intensify search
            bestsol, bestcard, bestb = set(sol), card, list(b)
            if report:
                report(card, "iter:%d" % it)
            if LOG:
                print("*** intensifying: clearing tabu list***")
            tabu = [min(tabu[i], it) for i in nodes]  # clear tabu list
            count = 0
        elif infeas == 0 and card > lastcard:
            count = 0  # reset non-improving iterations counter
        else:
            count += 1

        if count > D:  # exceeded allowed non-improving iterations, restart int/div cycle
            if D % 2 == 0:  # intensification: switch to best found solution
                if LOG:
                    print(
                        "*** intensifying: switching to best found solution ***"
                    )
                sol, card, b = set(bestsol), bestcard, list(bestb)
                infeas = 0
                # keep tabu list unchanged, for ensuring a different path
            else:  # diversification: construct maximal stable set from less used vertex
                if LOG:
                    print(
                        "*** diversifying: constructing maximal set from less used vertex ***"
                    )
                # use the tabu history as long-term memory:
                cand = []
                mintabu = Infinity
                for j in set(
                        nodes) - sol:  # find less used vertex (smallest tabu)
                    if tabu[i] < mintabu:
                        cand = [i]
                    if tabu[i] == mintabu:
                        cand.append(i)
                v = random.choice(cand)

                sol = diversify(nodes, adj, v)
                card, infeas, b = evaluate(nodes, adj, sol)
                if infeas == 0 and card > bestcard:
                    bestsol, bestcard, bestb = set(sol), card, list(b)
                    if report:
                        report(card, "iter:%d" % it)
                tabu = [min(tabu[i], it) for i in nodes]  # clear tabu list

            count = 0  # reset counter
            D += 1  # increase self-tuning parameter

        if infeas == 0:
            lastcard = card

    # check solution correctness:
    # xcard, xinfeas, xb = evaluate(nodes, adj, bestsol)
    # assert bestcard == xcard and xinfeas == 0
    return bestsol, bestcard