Пример #1
0
def exploration_voisinage(solution, n=1, max_depth=6, crit_stagnation=50):
    """
    Explore le voisinage d'une solution
    :param solution: Solution
    :param n: int
    :param max_depth: int
    :param crit_stagnation: double
    :return: DiGraph
    """
    graph = nx.MultiDiGraph()
    temps1 = time.time()
    ecart = 10
    Fichier = Logger(
        solution.instance, "exploration_voisinage", **{
            "n": n,
            "max_depth": max_depth,
            "crit_stagnation": crit_stagnation,
            "Séquence de départ": solution.sequence
        })
    to_be_explored = []
    solution.voisinage()
    opti = solution.makeSpan

    to_be_explored.append(solution)
    graph.add_node(solution.nom, makespan=solution.makeSpan)
    depth, alert, compt = 0, 0, 0
    explored = []
    best = solution.sequence
    while to_be_explored:
        if depth < max_depth:
            if alert == crit_stagnation:
                Fichier.addLine("Stagnation du makespan")
                break
            else:
                depth = to_be_explored[0].depth
                to_be_explored, explored, make_min, seq = explore_deeper(
                    depth, to_be_explored, explored, graph, n, Fichier)
            if make_min < opti:
                alert = 0
                opti = make_min
                best = seq
            else:
                alert += 1
            compt += 1
        else:
            Fichier.addLine("Profondeur atteinte")
            break

    temps = time.time() - temps1
    Fichier.makespanFile(opti, best)
    Fichier.itFile(compt)
    Fichier.tpsFile(temps)

    return graph
def main_genetique(nom, ratiovoisin=0.5, selec=100, random=False, crois=100,
                   alpha=0.85, duel=True, gene=100, beta=0.15, withplot=False):
    pop = Population(nom)
    temps1 = time.time()
    temps2, temps_opti = 0, 0
    alert = 0
    meanfit = []
    maxfit = []

    genese(crois, pop, ratio_voisin=ratiovoisin)
    fichier = Logger(pop.instance, "algo_génétique",
                     **{"% de voisins de SPT et LPT": ratiovoisin, "Nombre selection": selec,
                        "Type de selection (random)": random, "Nombre croisement": crois,
                        "% de croisement": alpha, "Type de croisement (duel)": duel,
                        "Nombre génération": gene, "Facteur de mutation": beta, "Seed": pop.hash})

    while pop.generation <= gene and temps2 < 900 and alert < int(gene * 0.3):
        print("Géneration: ", pop.generation, file=open(fichier.location, 'a'))

        evaluation(pop)
        selection(pop, selec, random=random)

        meanfit.append(pop.MeanFit())
        maxfit.append(pop.MaxFit())
        croisement(pop, alpha=alpha, beta=beta, duel=duel, n=crois)
        evaluation(pop)

        if pop.change:
            alert = 0
            print("==========================Nouvel elite !==========================",
                  file=open(fichier.location, 'a'))
            print(pop.elite, file=open(fichier.location, 'a'))
            temps_opti = time.time() - temps1
            pop.change = False
        else:
            alert += 1
    temps2 = time.time() - temps1

    fichier.makespanFile(pop.elite.cout, pop.elite.sequence)
    fichier.fitOverTIme(meanfit, maxfit)

    fichier.tpsFile(temps_opti, "Optimum trouvé au bout de: ")
    fichier.tpsFile(temps2, "Temps total: ")

    if withplot:
        plot_genetique_fitness(meanfit, maxfit, fichier)

    return meanfit, maxfit, temps_opti