def testAllPairs(self):
     for i in range(50):
         adjList = self.randG.randomGnmGraph(n=50, M=1000, isWeighted=True)
         nodes, edges = self.randG.toNodeEdges(adjList)
         src = random.choice(list(adjList.keys()))
         dist1, prev1 = floyd_warshall(nodes, edges)
         dist2, prev2 = johnson(nodes, edges)
         self.assertEqual(dist1, dist2)
Exemple #2
0
def generating(iter):
    number_of_machines = int(input("Ile maszyn?: "))
    number_of_tasks = int(input("Ile zadan?: "))
    times = []
    durationBruteforce = []
    durationJohnson = []
    i = 0
    while i < iter:
        generatedTasks = []
        cnt_tasks = 0
        cnt_machines = 0
        for cnt_tasks in range(0, number_of_tasks):
            rows = []
            for cnt_machines in range(0, number_of_machines):
                rows.append(int(random.uniform(1, 10)))
            print("{}".format(rows))
            generatedTasks.append(Task(cnt_tasks, rows))
        bruteforceOrder, timeBruteforce = bruteforce(
            copy.deepcopy(generatedTasks), number_of_machines)
        johnsonOrder, timeJohnson = johnson(copy.deepcopy(generatedTasks),
                                            number_of_machines)
        durationBruteforce.append(timeBruteforce)
        durationJohnson.append(timeJohnson)
        bruteforceMakespan = makespan(bruteforceOrder, generatedTasks,
                                      number_of_machines)
        johnsonMakespan = makespan(johnsonOrder, generatedTasks,
                                   number_of_machines)
        i += 1
        if johnsonMakespan == bruteforceMakespan:
            times.append(johnsonMakespan)
        else:
            times.append(-1)
    x = PrettyTable()
    print("")
    print("----------------------------------------------------------")
    x.field_names = [
        "l.p.", "Johnson Makespan", "Poprawnosc", "Czas Bruteforce [ms]",
        "Czas Johnson [ms]"
    ]

    k = 0
    for k in range(0, iter):
        if times[k] == -1:
            x.add_row([
                k + 1, "{}".format(times[k]), "Nie",
                "{}".format(durationBruteforce[k]),
                "{}".format(durationJohnson[k])
            ])
        else:
            x.add_row([
                k + 1, "{}".format(times[k]), "Tak",
                "{}".format(durationBruteforce[k]),
                "{}".format(durationJohnson[k])
            ])
    print(x)
def john_(k, n, m=3, debug=False, d=None, **kwargs):
    """
    Fonction interface avec la méthode approchée de Johnson.
    Récupère un type d'instance et effectue l'ordonnancement
    sur une instance de ce type (n=#taches, m=#machines).
    Il est aussi possible d'envoyer directement un jeu de donnée.
    """

    if d == None: d = instances_dict[k][0](n, m)
    if debug: print(d)
    sol, time = johnson(d)
    return sol, time
Exemple #4
0
def cds(times):
    jobs_count = len(times)
    machine_count = len(times[0])

    perms = []
    times_merged = [[0, sum(job_times)] for job_times in times]
    for i in range(0, machine_count - 1):
        for k in range(0, jobs_count):
            times_merged[k][0] += times[k][i]
            times_merged[k][1] -= times[k][i]
        perms.append(johnson(times_merged))

    return min(perms, key=lambda p: makespan(p, times))
Exemple #5
0
def cds(times):
    jobs_count = len(times)
    machine_count = len(times[0])

    perms = []
    times_merged = [[0, sum(job_times)] for job_times in times]
    for i in range(0, machine_count - 1):
        for k in range(0, jobs_count):
            times_merged[k][0] += times[k][i]
            times_merged[k][1] -= times[k][i]
        perms.append(johnson(times_merged))

    return min(perms, key=lambda p: makespan(p, times))
Exemple #6
0
def main():
    import sys
    filename = sys.argv[1]
    method = sys.argv[2]

    edges = read_input(filename)
    graph = build_graph(edges)
    print 'a graph with %s nodes, %s edges' % (len(graph), len(edges))

    if method in ('johnson', 'both'):
        paths1 = johnson(graph)
        paths = paths1
    if method in ('floyd_warshall', 'both'):
        paths2 = floyd_warshall(graph)
        paths = paths2

    if method == 'both':
        assert paths1 == paths2

    if paths == 'negative cycle':
        print paths
    else:
        print min(paths, key=lambda x: x[2])
Exemple #7
0
def main():
    import sys
    filename = sys.argv[1]
    method = sys.argv[2]

    edges = read_input(filename)
    graph = build_graph(edges)
    print 'a graph with %s nodes, %s edges' % (len(graph), len(edges))

    if method in ('johnson', 'both'):
        paths1 = johnson(graph)
        paths = paths1
    if method in ('floyd_warshall', 'both'):
        paths2 = floyd_warshall(graph)
        paths = paths2

    if method == 'both':
        assert paths1 == paths2
    
    if paths == 'negative cycle':
        print paths
    else:
        print min(paths, key=lambda x: x[2])
    # TEST DE LA METHODE ARBORESCENTE (INDEPENDANT)#
    from instances import *
    import time
    import johnson

    DEBUG = True

    # Verification sur un jeu de donnée connu
    depth, sol = 0, None
    d = instance5()
    d = d.T
    if DEBUG: print(d)
    pi, piprime = [], np.array(range(np.size(d, 0)))
    sol, duration = arbre(d, pi, piprime, depth, sol, True, 0)
    print("Arbre - Solution: ", sol, "Durée:", duration)
    sol1, time1 = johnson.johnson(d)
    print("Johnson - Solution: ", sol1, "Durée:", time1)

    # # Test de la durée d'exection avec et sans élagage
    # earned = []
    # durations = []
    # n_iteration = 1
    # alpha = 0.1
    # n, m = 3, 3
    # for k in range(n_iteration):
    #     sol, pi, piprime = None, [], np.array(range(np.size(d, 0)))
    #     k = np.random.randint(len(instances_dict))
    #     d = instances_dict[k][0](n, m)
    #     if DEBUG: print(d)
    #     sol1, time1 = johnson.johnson(d)
    #     #print(sol1,time1, "Johnson")