コード例 #1
0
def doGridsearchExperiment():
    np.set_printoptions(formatter={'float': lambda x: "{0:0.2f}".format(x)})
    instances_directory = 'instances/'
    instances_directory = 'testinstances/'
    # instances_directory = 'from_assignment_3/'
    opt_directory = 'opts/'
    # instancename = "maxcut_4x4_1_1_donut"
    # instancename = "maxcut_2x2_1_1_donut"
    instancename = 'newL25_2'  #opt = 530
    # instancename = 'newL12_1' # opt = 127
    # instancename = 'newL12_2' # opt = 124
    instancename = "newL50_1_opt2078"
    # instancename = "newL50_2_opt2056"
    instancename = "maxcut_random_128_0.4_100_instance_3"

    instance = maxcut.MaxCut(instancename + ".txt", instances_directory,
                             opt_directory)
    numAnts = 20
    max_evals = 1000000
    max_gens = 200

    X = 10

    # For GBO:
    # rho_list = np.arange(0.01,0.51,0.1)
    # alpha_list = np.arange(0.75,2,0.25)
    # alpha_list = np.arange(0.5,1.5,0.1)

    # For BBO:
    # rho_list = np.arange(0.01,0.51,0.1)
    # alpha_list = np.arange(0.6,1.1,0.1)

    # For BBO2, in neighbourhood of:
    # rho = 0.2
    # ph_max=100
    # ph_min=0.1
    # alpha = 0.7

    rho_list = np.arange(0.15, 0.4, 0.05)
    alpha_list = np.arange(0.6, 1, 0.1)

    beta_list = [1]
    ph_min_list = [0.1]
    ph_max_list = [100]
    rn.gridSearch(X,
                  instance,
                  numAnts,
                  max_evals,
                  max_gens,
                  rho_list,
                  alpha_list,
                  beta_list,
                  ph_min_list,
                  ph_max_list,
                  BBO=True)

    plt.show()
コード例 #2
0
def doGlobalTest():
    np.set_printoptions(formatter={'float': lambda x: "{0:0.2f}".format(x)})
    instances_directory = 'testinstances/'
    opt_directory = 'opts/'
    instancenamelist = [
        "maxcut_random_16_0.4_100_instance_0",
        "maxcut_random_32_0.4_100_instance_1",
        "maxcut_random_64_0.4_100_instance_2",
        "maxcut_random_128_0.4_100_instance_3",
        "maxcut_random_256_0.4_100_instance_4",
        "maxcut_random_512_0.4_100_instance_5"
    ]
    # instancenamelist = ["maxcut_random_16_0.4_100_instance_0","maxcut_random_32_0.4_100_instance_1"]
    plt.ion()

    f = open("ACOend_results.txt", "a")
    f.write(
        "filename \t\t\t\t\t\t\t evaluations \t mean \t std \t maxfitness \n")
    # f2 = open("ACOrun_results.txt","w")
    for instancename in instancenamelist:
        fig, ax = plt.subplots()

        instance = maxcut.MaxCut(instancename + ".txt", instances_directory,
                                 opt_directory)
        numAnts = 20
        max_evals = 10000000000
        max_gens = 200

        # best parameters for instance newL50_1_opt2078 without local search, rho = 0.2
        rho = 0.3  # result for with local search on N=128, 0.3 or 0.35
        ph_max = 100
        ph_min = 0.1
        alpha = 0.7

        ACO_instance = ACO.ACO_BBO2(instance, numAnts, max_evals, max_gens,
                                    rho, ph_max, ph_min, alpha)

        X = 10
        x, y, e, maxfit = rn.executeXtimes(X, ACO_instance)
        ax.errorbar(x, y, e, linestyle='None', marker="x", label=instancename)
        ax.legend(loc="lower right")
        plt.draw(), plt.pause(1e-4)

        f.write("%s \t %.0f \t %.2f \t %.5f \t %.0f \n" %
                (instancename, x[-1], y[-1], e[-1], maxfit))
        f.flush()
        # f.write("end mean: %.2f \n" % y[-1])
        # f.write("end std: %.5f \n" % e[-1])
        # f.write("best fitness found: %.0f" % maxfit)

    plt.ioff()
    f.close()
    plt.show()
コード例 #3
0
import numpy as np
from random import randint
import maxcut_acoBBO
import math
from operator import attrgetter

if __name__ == "__main__":
    np.set_printoptions(formatter={'float': lambda x: "{0:0.2f}".format(x)})
    instances_directory = 'instances/'
    # instances_directory = 'from_assignment_3/'
    opt_directory = 'opts/'
    instancename = "maxcut_4x4_1_1_donut"
    # instancename = "maxcut_2x2_1_1_donut"
    # instancename = 'newL25_2' #opt = 530

    instance = maxcut.MaxCut(instancename + ".txt", instances_directory,
                             opt_directory)
    numAnts = 4
    max_its = 50
    rho = 1 - 0.01**(1 / (max_its / 4))
    ph_max = 2
    ph_min = 1
    alpha = 1
    ACO = maxcut_acoBBO.ACO_BBO(instance, numAnts, max_its, rho, ph_max,
                                ph_min, alpha)
    ACO.run()


class ACO_BBO:
    def __init__(self,
                 instance,
                 numAnts,