コード例 #1
0
def run_tabu_search(seconds, initial_cadence, critical_event):
    print('Running Tabu Search for ' + str(seconds) + ' seconds...')
    print()

    ts = TabuSearch(states, seconds, initial_cadence, critical_event,
                    inc_support, dec_support)
    ts.run()
    print('Found optimal route with value of ' + str(ts.best_solution.value) +
          '.')
    print(
        str(ts.best_solution.calculate_real_value()) +
        ' electoral votes were collected.')
    ts.best_solution.print()
    print()
コード例 #2
0
        with open('datasets/{}'.format(dataset["name"]), 'r') as file:
            data = file.read().splitlines()
            num_items, capacity, items = int(data[0]), int(data[1]), data[2:]
            log("\n\nDATASET {}: num_items {} capacity {} items_read {}".
                format(dataset["name"], num_items, capacity, len(items)))
        items = [Item(size=int(i)) for i in items]
        log("  Iteration", end=" ")
        # Perform 30 independent iterations.
        for iteration in range(30):
            log(iteration + 1, end=" ")
            # Randomize the order of the items in the item list.
            shuffle(items)
            thing = TabuSearch(capacity, items)

            start_time = datetime.now()
            total_iterations, stagnation, combination = thing.run()
            execution_time = datetime.now() - start_time

            # Record the relevant data for analysis
            summary = {
                "execution_time": str(execution_time),
                "num_bins": len(thing.bins),
                "fitness":
                sum(b.fitness() for b in thing.bins) / len(thing.bins),
                "iterations": total_iterations,
                "stagnation": stagnation,
                "combination": combination,
                "tabu_list": list(thing.tabu_list)
            }
            dataset["results"].setdefault("TabuSearch", []).append(summary)
    # Write the captured data to disk.
コード例 #3
0
def execute_algo(name, capacity, items):
    if (name == 'SA'):
        sa = SA(0.7, capacity, items, 500, 10, 8)
        start_time = datetime.now()
        sa.run()
        execution_time = datetime.now() - start_time
        bins_algo = len(sa.bins)
        time_algo = execution_time.total_seconds()
    elif (name == 'RT'):
        thing = TabuSearch(capacity, items)
        start_time = datetime.now()
        total_iterations, stagnation, combination = thing.run()
        execution_time = datetime.now() - start_time
        bins_algo = len(thing.bins)
        time_algo = execution_time.total_seconds()
    elif (name == 'GA'):
        thing = GeneticAlgorithm(capacity, items)
        start_time = datetime.now()
        total_iterations, stagnation = thing.run()
        execution_time = datetime.now() - start_time
        time_algo = execution_time.total_seconds()
        bins_algo = thing.best_solution.num_bins
    elif (name == 'HRH'):
        thing = HRH(capacity, items)
        start_time = datetime.now()
        result1, result2, total_iterationsAG, stagnationAG, total_iterationsTB, stagnationTB, combinationTB = thing.run(
        )
        execution_time = datetime.now() - start_time
        time_algo = execution_time.total_seconds()
        bins_algo = len(result2.bins)
    elif (name == 'HRH_RS'):
        thing = HRH_RS(capacity, items)
        start_time = datetime.now()
        sa = thing.run()
        execution_time = datetime.now() - start_time
        time_algo = execution_time.total_seconds()
        bins_algo = len(sa.bins)
    elif (name == 'LTH'):
        thing = LTH(capacity, items)
        start_time = datetime.now()
        total_iterations, stagnation = thing.run()
        execution_time = datetime.now() - start_time
        time_algo = execution_time.total_seconds()
        bins_algo = thing.best_solution.num_bins
    elif (name == 'LTH2'):
        thing = LTH2(capacity, items)
        start_time = datetime.now()
        total_iterations, stagnation = thing.run()
        execution_time = datetime.now() - start_time
        time_algo = execution_time.total_seconds()
        bins_algo = thing.best_solution.num_bins
    # elif(name == 'HTH'):
    #     thing = HTH(capacity, items)
    #     start_time = datetime.now()
    #     sa = thing.run()
    #     execution_time = datetime.now() - start_time
    #     time_algo = execution_time.total_seconds()
    #     bins_algo = [len(a) for a in sa]

    elif (name == 'FirstFit'):
        start_time = datetime.now()
        bins = [Bin(capacity=capacity)]
        for item in items:
            bins = FirstFit.apply(item, bins)
        execution_time = datetime.now() - start_time
        time_algo = execution_time.total_seconds()
        bins_algo = len(bins)

    elif (name == 'NextFit'):
        start_time = datetime.now()
        bins = [Bin(capacity=capacity)]
        for item in items:
            bins = NextFit.apply(item, bins)
        execution_time = datetime.now() - start_time
        time_algo = execution_time.total_seconds()
        bins_algo = len(bins)

    elif (name == 'BestFit'):
        start_time = datetime.now()
        bins = [Bin(capacity=capacity)]
        for item in items:
            bins = BestFit.apply(item, bins)
        execution_time = datetime.now() - start_time
        time_algo = execution_time.total_seconds()
        bins_algo = len(bins)

    elif (name == 'WorstFit'):
        start_time = datetime.now()
        bins = [Bin(capacity=capacity)]
        for item in items:
            bins = WorstFit.apply(item, bins)
        execution_time = datetime.now() - start_time
        time_algo = execution_time.total_seconds()
        bins_algo = len(bins)

    elif (name == 'FirstFitDec'):
        start_time = datetime.now()
        bins = [Bin(capacity=capacity)]
        item_sort = sorted(items, key=lambda x: x.size, reverse=True)
        for item in item_sort:
            bins = FirstFitDec.apply(item, bins)
        execution_time = datetime.now() - start_time
        time_algo = execution_time.total_seconds()
        bins_algo = len(bins)

    return bins_algo, time_algo
コード例 #4
0
def benchmark():
    REPEATS = 10
    SECONDS = [5, 10, 30, 60, 300, 1200]

    for seconds in SECONDS:
        v = 0
        time_s = datetime.now()
        for k in range(REPEATS):
            rs = RandomSearch(states, seconds, inc_support, dec_support)
            rs.run()
            v += rs.best_solution.value
        time_e = datetime.now()
        tt = (time_e - time_s).total_seconds()
        print_csv('Random Search', str(seconds), str(v / REPEATS),
                  str(tt / REPEATS))

    for seconds in SECONDS:
        v = 0
        time_s = datetime.now()
        for k in range(REPEATS):
            ls = LocalSearch(states, seconds, inc_support, dec_support)
            ls.run()
            v += ls.best_solution.value
        time_e = datetime.now()
        tt = (time_e - time_s).total_seconds()
        print_csv('Local Search', str(seconds), str(v / REPEATS),
                  str(tt / REPEATS))

    for seconds in SECONDS:
        for initial_cadence in [10, 25, 50]:
            for critical_event in [10, 25, 50]:
                v = 0
                time_s = datetime.now()
                for k in range(REPEATS):
                    ts = TabuSearch(states, seconds, initial_cadence,
                                    critical_event, inc_support, dec_support)
                    ts.run()
                    v += ts.best_solution.value
                time_e = datetime.now()
                tt = (time_e - time_s).total_seconds()
                print_csv('Tabu Search', str(seconds), str(initial_cadence),
                          str(critical_event), str(v / REPEATS),
                          str(tt / REPEATS))

    for crossover in ['pmx', 'ox']:
        for mutate in ['transposition', 'insertion', 'inversion']:
            for seconds in SECONDS:
                for population_size in [10, 25, 50]:
                    v = 0
                    time_s = datetime.now()
                    for k in range(REPEATS):
                        ga = GeneticAlgorithm(states, seconds, population_size,
                                              crossover, mutate, inc_support,
                                              dec_support)
                        ga.run()
                        v += ga.best_solution.value
                    time_e = datetime.now()
                    tt = (time_e - time_s).total_seconds()
                    print_csv('Genetic Algorithm ' + crossover + ' ' + mutate,
                              str(seconds), str(population_size),
                              str(v / REPEATS), str(tt / REPEATS))

    for initial_temperature in [100, 500, 1000]:
        for cooling_coefficient in [0.9, 0.99, 0.999, 0.9999]:
            for minimal_temperature in [
                    initial_temperature * 0.25, initial_temperature * 0.5,
                    initial_temperature * 0.75
            ]:
                v = 0
                time_s = datetime.now()
                for k in range(REPEATS):
                    sa = SimulatedAnnealing(states, initial_temperature,
                                            cooling_coefficient,
                                            minimal_temperature, inc_support,
                                            dec_support)
                    sa.run()
                    v += sa.best_solution.value
                time_e = datetime.now()
                tt = (time_e - time_s).total_seconds()
                print_csv('Simulated Annealing', str(initial_temperature),
                          str(cooling_coefficient), str(minimal_temperature),
                          str(v / REPEATS), str(tt / REPEATS))