示例#1
0
 def test_tabu_search(self):
     n = 50
     m = 100
     loops = 30
     gen = Generator(n, m)
     status = Status(gen.p, gen.c, gen.d)
     status.set_cf(gen.cf)
     status.set_emin(gen.emin)
     status.set_emax(gen.emax)
     status.set_age(gen.age)
     status.set_mage(gen.mage)
     status.set_male(gen.male)
     status.set_sratio(gen.sratio)
     status.set_cmin(gen.cmin)
     status.allowed_time = 300
     status.improving = 1
     status.delta = 1
     status.restarts = 20
     status.attempts = 100000
     status.set_status()
     init = initial_solution_bottom_up(gen.p, gen.c, gen.d)
     results = {}
     tenures = [0, 2, 5, 10]
     legal = [is_legal_not_tabu, is_legal_not_tabu_aspiration]
     for i in range(loops):
         for tenure in tenures:
             status.tenure = tenure
             print("Tenure value: {}".format(tenure))
             for aspiration in legal:
                 print("Legal function: {}".format(aspiration))
                 start = time.time()
                 a, s, score = tabu_search(init, objective_compound_incr, neighborhood_all, aspiration, selection_first_improvement)
                 elapsed = time.time() - start
                 print("Solution (first improvement): {} in {} sec ({} attempts)".format(score, elapsed, a))
                 status.k = 3
                 start = time.time()
                 a_, s_, score_ = tabu_search(init, objective_compound_incr, neighborhood_all, aspiration, selection_best_k)
                 elapsed_ = time.time() - start
                 print("Solution (best 3): {} in {} sec ({} attempts)".format(score_, elapsed_, a_))
                 status.k = 5
                 start = time.time()
                 a__, s__, score__ = tabu_search(init, objective_compound_incr, neighborhood_all, aspiration, selection_best_k)
                 elapsed__ = time.time() - start
                 print("Solution (best 5): {} in {} sec ({} attempts)".format(score__, elapsed__, a__))
                 if i == 0:
                     results[(tenure, aspiration.__name__, "first_improvement")] = [(score, elapsed, a)]
                     results[(tenure, aspiration.__name__, "best_3")] = [(score_, elapsed_, a_)]
                     results[(tenure, aspiration.__name__, "best_5")] = [(score__, elapsed__, a__)]
                 else:
                     results[(tenure, aspiration.__name__, "first_improvement")].append((score, elapsed, a))
                     results[(tenure, aspiration.__name__, "best_3")].append((score_, elapsed_, a_))
                     results[(tenure, aspiration.__name__, "best_5")].append((score__, elapsed__, a__))
     for k, v in sorted(results.items()):
         score_all, elapsed_all, a_all = 0, 0, 0
         for score, elapsed, a in v:
             score_all += score
             elapsed_all += elapsed
             a_all += a
         print("{} -> {:.4f} in {:.2f} sec, {} attempts (average on {} iterations)".format(k, score_all / loops, elapsed_all / loops, a_all / loops, loops))
示例#2
0
        status.k = 3
        status.tenure = 2
        status.improving = 5
        status.delta = 1
        status.allowed_time = 3000
        status.set_status()

        '''launching search'''
        if verbose:
            print("Launching {} search... ({} seconds)".format(("light" if len(declines) > 0 else "heavy"), status.allowed_time))
        s, score = None, 0
        if len(declines) > 0:
            status.attempts = len(declines) * len(events)
            status.tenure = 0
            init = initial_solution_confirmed_only()
            s, score = tabu_search(initial_solution_bottom_up(status.p, status.c, status.d), objective_compound_incr, neighborhood_add, is_legal_not_tabu, selection_best_k)
        else:
            s, score = tabu_search_restarts(initial_solution_bottom_up, objective_compound_incr, neighborhood_all, is_legal_not_tabu, selection_best_k)
        print("Solution score: {}".format(score))
        print("Solution: {}".format(s))

        '''connecting'''
        con = connect(params)
        if not con:
            sys.exit(1)
        cur = con.cursor()
    
        '''updating database'''
        if verbose:
            print("--- Updating database...")
        update_data(cur, uids, eids, declines, s)
示例#3
0
 def test_tabu_search(self):
     n = 50
     m = 100
     loops = 30
     gen = Generator(n, m)
     status = Status(gen.p, gen.c, gen.d)
     status.set_cf(gen.cf)
     status.set_emin(gen.emin)
     status.set_emax(gen.emax)
     status.set_age(gen.age)
     status.set_mage(gen.mage)
     status.set_male(gen.male)
     status.set_sratio(gen.sratio)
     status.set_cmin(gen.cmin)
     status.allowed_time = 300
     status.improving = 1
     status.delta = 1
     status.restarts = 20
     status.attempts = 100000
     status.set_status()
     init = initial_solution_bottom_up(gen.p, gen.c, gen.d)
     results = {}
     tenures = [0, 2, 5, 10]
     legal = [is_legal_not_tabu, is_legal_not_tabu_aspiration]
     for i in range(loops):
         for tenure in tenures:
             status.tenure = tenure
             print("Tenure value: {}".format(tenure))
             for aspiration in legal:
                 print("Legal function: {}".format(aspiration))
                 start = time.time()
                 a, s, score = tabu_search(init, objective_compound_incr,
                                           neighborhood_all, aspiration,
                                           selection_first_improvement)
                 elapsed = time.time() - start
                 print(
                     "Solution (first improvement): {} in {} sec ({} attempts)"
                     .format(score, elapsed, a))
                 status.k = 3
                 start = time.time()
                 a_, s_, score_ = tabu_search(init, objective_compound_incr,
                                              neighborhood_all, aspiration,
                                              selection_best_k)
                 elapsed_ = time.time() - start
                 print(
                     "Solution (best 3): {} in {} sec ({} attempts)".format(
                         score_, elapsed_, a_))
                 status.k = 5
                 start = time.time()
                 a__, s__, score__ = tabu_search(init,
                                                 objective_compound_incr,
                                                 neighborhood_all,
                                                 aspiration,
                                                 selection_best_k)
                 elapsed__ = time.time() - start
                 print(
                     "Solution (best 5): {} in {} sec ({} attempts)".format(
                         score__, elapsed__, a__))
                 if i == 0:
                     results[(tenure, aspiration.__name__,
                              "first_improvement")] = [(score, elapsed, a)]
                     results[(tenure, aspiration.__name__,
                              "best_3")] = [(score_, elapsed_, a_)]
                     results[(tenure, aspiration.__name__,
                              "best_5")] = [(score__, elapsed__, a__)]
                 else:
                     results[(tenure, aspiration.__name__,
                              "first_improvement")].append(
                                  (score, elapsed, a))
                     results[(tenure, aspiration.__name__,
                              "best_3")].append((score_, elapsed_, a_))
                     results[(tenure, aspiration.__name__,
                              "best_5")].append((score__, elapsed__, a__))
     for k, v in sorted(results.items()):
         score_all, elapsed_all, a_all = 0, 0, 0
         for score, elapsed, a in v:
             score_all += score
             elapsed_all += elapsed
             a_all += a
         print(
             "{} -> {:.4f} in {:.2f} sec, {} attempts (average on {} iterations)"
             .format(k, score_all / loops, elapsed_all / loops,
                     a_all / loops, loops))
示例#4
0
 def test_tabu_vs_restarts(self):
     n = 50
     m = 100
     loops = 1
     gen = Generator(n, m)
     status = Status(gen.p, gen.c, gen.d)
     status.set_cf(gen.cf)
     status.set_emin(gen.emin)
     status.set_emax(gen.emax)
     status.set_age(gen.age)
     status.set_mage(gen.mage)
     status.set_male(gen.male)
     status.set_sratio(gen.sratio)
     status.set_cmin(gen.cmin)
     #status.chosen_ones = gen.chosen_ones
     status.allowed_time = 3600
     status.restarts = 20
     status.k = 3
     status.attempts = 10000
     status.set_status()
     init = initial_solution_bottom_up(gen.p, gen.c, gen.d)
     results = {}
     for i in range(loops):
         status.improving = 5
         status.delta = 1
         status.tenure = 2
         start = time.time()
         a, s, score = tabu_search_restarts(initial_solution_bottom_up,
                                            objective_compound_incr,
                                            neighborhood_all,
                                            is_legal_not_tabu,
                                            selection_best_k)
         elapsed = time.time() - start
         if i == 0:
             results[("restarts", "stagn=5", "no aspiration")] = [(a, score,
                                                                   elapsed)]
         else:
             results[("restarts", "stagn=5", "no aspiration")].append(
                 (a, score, elapsed))
         print(
             "Score restarts (tenure=5): {:.4f} in {:.2f} sec ({:.2f} iterations)"
             .format(score, elapsed, a))
         status.tenure = 5
         start = time.time()
         a, s, score = tabu_search(init, objective_compound_incr,
                                   neighborhood_all,
                                   is_legal_not_tabu_aspiration,
                                   selection_best_k, 0)
         elapsed = time.time() - start
         if i == 0:
             results[("tabu", 5, "aspiration")] = [(a, score, elapsed)]
         else:
             results[("tabu", 5, "aspiration")].append((a, score, elapsed))
         print(
             "Score tabu (tenure=5, aspiration): {:.4f} in {:.2f} sec ({:.2f} iterations)"
             .format(score, elapsed, a))
         status.tenure = 0
         start = time.time()
         a, s, score = tabu_search(init, objective_compound_incr,
                                   neighborhood_all, is_legal_not_tabu,
                                   selection_best_k, 0)
         elapsed = time.time() - start
         if i == 0:
             results[("tabu", 0, "no aspiration")] = [(a, score, elapsed)]
         else:
             results[("tabu", 0, "no aspiration")].append(
                 (a, score, elapsed))
         print(
             "Score tabu (tenure=0, no aspiration): {:.4f} in {:.2f} sec ({:.2f} iterations)"
             .format(score, elapsed, a))
         status.tenure = 2
         status.improving = 1
         start = time.time()
         a, s, score = tabu_search_restarts(initial_solution_bottom_up,
                                            objective_compound_incr,
                                            neighborhood_all,
                                            is_legal_not_tabu_aspiration,
                                            selection_best_k)
         elapsed = time.time() - start
         if i == 0:
             results[("restarts", "stagn=1", "aspiration")] = [(a, score,
                                                                elapsed)]
         else:
             results[("restarts", "stagn=1", "aspiration")].append(
                 (a, score, elapsed))
         print(
             "Score restarts (tenure=10): {:.4f} in {:.2f} sec ({:.2f} iterations)"
             .format(score, elapsed, a))
     for k, v in sorted(results.items()):
         score_all, elapsed_all, attempts_all = 0, 0, 0
         for a, score, elapsed in v:
             score_all += score
             elapsed_all += elapsed
             attempts_all += a
         print(
             "{} -> {:.4f} in {:.2f} sec, {:.2f} attempts (average on {} iterations)"
             .format(k, score_all / loops, elapsed_all / loops,
                     attempts_all / loops, loops))
示例#5
0

if __name__ == '__main__':
    for tabu_list_size in [5]:
        # initial_state = np.arange(20)
        # BEST STATE: 17, 5, 7, 1, 6, 19, 15, 20, 8, 13, 4, 2, 12, 11, 16, 18, 14, 10, 3, 9

        # TRIAL 1: INPUT: 6 2 3 19 10 11 15 1 12 18 16 17 5 8 7 13 14 4 0 9 => OUTPUT: 1287.0
        initial_state = deserialize(
            '15 6 11 7 10 5 0 13 16 18 9 14 17 2 12 19 3 8 4 1')

        # initial_state = np.array(list(map(
        #     lambda x: x-1, [17, 7, 5, 1, 6, 4, 20, 8, 11, 13, 19, 15, 12, 10, 16, 18, 2, 14, 3, 9])))
        # initial_state = np.array(
        #     list(map(lambda x: x-1, [2, 9, 8, 12, 1, 7, 11, 19, 13, 10, 5, 18, 3, 17, 14, 15, 6, 16, 20, 4])))
        # np.random.shuffle(initial_state)
        initial_state = serialize(initial_state)

        stopping_condition = gen_stopping_condition(500)

        tabu_search = configure_tabu_search(stopping_condition, get_neighbors,
                                            evaluate, tabu_list_size)

        print(f'Tabu List Size: {tabu_list_size}')
        print(f'Initial State: {initial_state}')
        print(f'Initial Cost: {-evaluate(initial_state)}')
        final_state = tabu_search(initial_state)
        print(f'Final State: {final_state}')
        print(f'Final Cost: {-evaluate(final_state)}')
        print()
示例#6
0
 def test_tabu_vs_restarts(self):
     n = 50
     m = 100
     loops = 1
     gen = Generator(n, m)
     status = Status(gen.p, gen.c, gen.d)
     status.set_cf(gen.cf)
     status.set_emin(gen.emin)
     status.set_emax(gen.emax)
     status.set_age(gen.age)
     status.set_mage(gen.mage)
     status.set_male(gen.male)
     status.set_sratio(gen.sratio)
     status.set_cmin(gen.cmin)
     #status.chosen_ones = gen.chosen_ones
     status.allowed_time = 3600
     status.restarts = 20
     status.k = 3
     status.attempts = 10000
     status.set_status()
     init = initial_solution_bottom_up(gen.p, gen.c, gen.d)
     results = {}
     for i in range(loops):
         status.improving = 5
         status.delta = 1
         status.tenure = 2
         start = time.time()
         a, s, score = tabu_search_restarts(initial_solution_bottom_up, objective_compound_incr, neighborhood_all, is_legal_not_tabu, selection_best_k)
         elapsed = time.time() - start
         if i == 0:
             results[("restarts", "stagn=5", "no aspiration")] = [(a, score, elapsed)]
         else:
             results[("restarts", "stagn=5", "no aspiration")].append((a, score, elapsed))
         print("Score restarts (tenure=5): {:.4f} in {:.2f} sec ({:.2f} iterations)".format(score, elapsed, a))
         status.tenure = 5
         start = time.time()
         a, s, score = tabu_search(init, objective_compound_incr, neighborhood_all, is_legal_not_tabu_aspiration, selection_best_k, 0)
         elapsed = time.time() - start
         if i == 0:
             results[("tabu", 5, "aspiration")] = [(a, score, elapsed)]
         else:
             results[("tabu", 5, "aspiration")].append((a, score, elapsed))
         print("Score tabu (tenure=5, aspiration): {:.4f} in {:.2f} sec ({:.2f} iterations)".format(score, elapsed, a))
         status.tenure = 0
         start = time.time()
         a, s, score = tabu_search(init, objective_compound_incr, neighborhood_all, is_legal_not_tabu, selection_best_k, 0)
         elapsed = time.time() - start
         if i == 0:
             results[("tabu", 0, "no aspiration")] = [(a, score, elapsed)]
         else:
             results[("tabu", 0, "no aspiration")].append((a, score, elapsed))
         print("Score tabu (tenure=0, no aspiration): {:.4f} in {:.2f} sec ({:.2f} iterations)".format(score, elapsed, a))
         status.tenure = 2
         status.improving = 1
         start = time.time()
         a, s, score = tabu_search_restarts(initial_solution_bottom_up, objective_compound_incr, neighborhood_all, is_legal_not_tabu_aspiration, selection_best_k)
         elapsed = time.time() - start
         if i == 0:
             results[("restarts", "stagn=1", "aspiration")] = [(a, score, elapsed)]
         else:
             results[("restarts", "stagn=1", "aspiration")].append((a, score, elapsed))
         print("Score restarts (tenure=10): {:.4f} in {:.2f} sec ({:.2f} iterations)".format(score, elapsed, a))
     for k, v in sorted(results.items()):
         score_all, elapsed_all, attempts_all = 0, 0, 0
         for a, score, elapsed in v:
             score_all += score
             elapsed_all += elapsed
             attempts_all += a
         print("{} -> {:.4f} in {:.2f} sec, {:.2f} attempts (average on {} iterations)".format(k, score_all / loops, elapsed_all / loops, attempts_all / loops, loops))