def gridSearch(X,
               instance,
               numAnts,
               max_evals,
               max_gens,
               rho_list,
               alpha_list,
               beta_list,
               ph_min_list,
               ph_max_list,
               BBO=True):
    plt.ion()
    fig, ax = plt.subplots()
    ax.set_xlabel("# evaluations")
    ax.set_ylabel("fitness")
    lab = ""

    # cm = plt.get_cmap('gist_rainbow')
    cm = plt.get_cmap('tab20')
    markers = ["x", "^"]
    markerind = 0
    NUM_COLORS = len(rho_list) * len(alpha_list) * len(beta_list)
    ax.set_prop_cycle(
        color=[cm(1. * i / NUM_COLORS) for i in range(NUM_COLORS)])

    for rho in rho_list:
        for alpha in alpha_list:
            for beta in beta_list:
                for ph_min in ph_min_list:
                    for ph_max in ph_max_list:
                        if BBO:
                            ACO_instance = ACO.ACO_BBO2(
                                instance, numAnts, max_evals, max_gens, rho,
                                ph_max, ph_min, alpha)
                            lab = r"$\rho = %.2f, \alpha = %.2f $" % (rho,
                                                                      alpha)
                        else:
                            ACO_instance = ACO.ACO_GBO(instance, numAnts,
                                                       max_evals, max_gens,
                                                       rho, ph_max, ph_min,
                                                       alpha, beta)
                            lab = r"$\rho = %.2f, \alpha = %.2f, \beta = %.2f $" % (
                                rho, alpha, beta)

                        x, y, e, _ = rn.executeXtimes(X, ACO_instance)
                        ax.errorbar(x,
                                    y,
                                    e,
                                    linestyle='None',
                                    marker=markers[markerind],
                                    label=lab)
                        ax.legend(loc="lower right")
                        plt.draw(), plt.pause(1e-4)
                        markerind = 1 - markerind

    plt.ioff()
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()
    max_gens = 100
    # rho = 1 - 0.01**(1/(max_evals/4))

    # 
    rho = 0.21
    ph_max=10
    ph_min=0.1
    alpha = 0.7

    fig, ax = plt.subplots()
    fig2, ax2 = plt.subplots()

    ACO_BBO = ACO.ACO_BBO(instance,numAnts,max_evals,max_gens,rho,ph_max,ph_min,alpha,ax)
    ACO_BBO.run()

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

    # GBO
    rho = 0.41
    alpha = 1.5
    beta = 1

    # ACO_GBO = ACO.ACO_GBO(instance,numAnts,max_evals,max_gens,rho,ph_max,ph_min,alpha,beta,ax)
    # ACO_GBO.run()

    # ACO_GBO2 = ACO.ACO_GBO2(instance,numAnts,max_evals,max_gens,rho,ph_max,ph_min,alpha,beta,ax)
    # ACO_GBO2.run()

    ax2.plot(ACO_BBO.numEvalsList,ACO_BBO.archiveElitistList,color="black",label="Black Box archive elitist fitness")
    ax2.plot(ACO_BBO.numEvalsList,ACO_BBO.averageFitnessList,'-*',color="black",label="Black Box average population fitness")