Beispiel #1
0
def dist_exp(f_path, mutation_rate, crossover_rate, w_social):
    runs = 5
    data = {}

    pop_size = 500
    num_gens = 2000

    for gc in GAMES_CODES:
        print(f'\n\n Game code = {gc}')
        allrows = []
        for pth, best in TEST_SET_1[:2]:
            row = []
            print('>> Instance', instance_name(pth))
            for run in range(runs):
                print(f'>> Run{run+1}/{runs}')
                vrp = VRP(*read_file(os.path.abspath(pth)))
                game = gameClassFactory(gc)
                result, routes, strategy_dist = ga_social_interaction_vrp(
                    vrp,
                    game,
                    pop_size,
                    num_gens,
                    mutation_rate=mutation_rate,
                    crossover_rate=crossover_rate,
                    wgt_solution=1 - w_social,
                    wgt_social=w_social)
                row = row + strategy_dist
            allrows.append(row)
        with open(f'results/dists/{gc}_dist_exp.csv', 'w') as file:
            writer = csv.writer(file)
            writer.writerows(allrows)
Beispiel #2
0
def weights_exp(f_path, mutation_rate, crossover_rate, game_code):
    runs = 10
    data = {}

    pop_size = 500
    num_gens = 2000

    all_data = []
    header = ['solution fitness weight', 'social fitness weight'] + list(
        map(lambda x: "% Gap in " + instance_name(x[0]),
            TEST_SET_1)) + ['Avg % Gap']
    all_data.append(header)
    print(header)
    for n in range(1, 12):
        w_social = n * 0.05
        w_solution = 1 - w_social
        print(f'\n\n solution wgt = {w_solution}; social wgt = {w_social}')
        row = [w_solution, w_social]
        for pth, best in TEST_SET_1:
            print('>> Instance', instance_name(pth))
            instance_results = []
            for run in range(runs):
                print(f'>> Run{run+1}/{runs}')
                vrp = VRP(*read_file(os.path.abspath(pth)))
                game = gameClassFactory(game_code)
                result, routes, strategy_dist = ga_social_interaction_vrp(
                    vrp,
                    game,
                    pop_size,
                    num_gens,
                    mutation_rate=mutation_rate,
                    crossover_rate=crossover_rate,
                    wgt_solution=w_solution,
                    wgt_social=w_social)
                instance_results.append(result)
            row.append(gap_percentage(mean(instance_results), best))
        row.append(mean(row[2:]))
        print(row)
        all_data.append(row)
    with open('results/weights_exp.csv', 'w') as file:
        writer = csv.writer(file)
        writer.writerows(all_data)
Beispiel #3
0
def games_exp(f_path, mutation_rate, crossover_rate):
    runs = 10
    data = {}

    pop_size = 500
    num_gens = 2000

    all_data = []
    header = ['game'] + list(
        map(lambda x: "% Gap in " + instance_name(x[0]),
            TEST_SET_1)) + ['Avg % Gap']
    all_data.append(header)
    print(header)
    for gc in GAMES_CODES:
        print(f'\n\n Game code = {gc}')
        row = [gc]
        for pth, best in TEST_SET_1:
            print('>> Instance', instance_name(pth))
            instance_results = []
            for run in range(runs):
                print(f'>> Run{run+1}/{runs}')
                vrp = VRP(*read_file(os.path.abspath(pth)))
                game = gameClassFactory(gc)
                result, routes, strategy_dist = ga_social_interaction_vrp(
                    vrp,
                    game,
                    pop_size,
                    num_gens,
                    mutation_rate=mutation_rate,
                    crossover_rate=crossover_rate,
                    wgt_solution=0.9,
                    wgt_social=0.1)
                instance_results.append(result)
            row.append(gap_percentage(mean(instance_results), best))
        row.append(mean(row[2:]))
        print(row)
        all_data.append(row)
    with open('results/games_exp.csv', 'w') as file:
        writer = csv.writer(file)
        writer.writerows(all_data)
Beispiel #4
0
def basic_params_exp(f_path):
    runs = 10
    data = {}

    pop_size = 500
    num_gens = 2000

    all_data = []
    header = ['mutation rate', 'crossover rate'] + list(
        map(lambda x: "% Gap in " + instance_name(x[0]),
            TEST_SET_1)) + ['Avg % Gap']
    all_data.append(header)
    print(header)
    for mr, cr in product([0.2, 0.4, 0.6, 0.8], [0.2, 0.4, 0.6, 0.8]):
        print(f'\n\n mutation rate = {mr}; crossover rate = {cr}')
        row = [mr, cr]
        for pth, best in TEST_SET_1:
            print('>> Instance', instance_name(pth))
            instance_results = []
            for run in range(runs):
                print(f'>> Run{run+1}/{runs}')
                vrp = VRP(*read_file(os.path.abspath(pth)))
                game = Game()
                result, routes, strategy_dist = ga_social_interaction_vrp(
                    vrp,
                    game,
                    pop_size,
                    num_gens,
                    mutation_rate=mr,
                    crossover_rate=cr,
                    wgt_solution=1,
                    wgt_social=0)
                instance_results.append(result)
            row.append(gap_percentage(mean(instance_results), best))
        row.append(mean(row[2:]))
        print(row)
        all_data.append(row)
    with open('results/basic_params_exp.csv', 'w') as file:
        writer = csv.writer(file)
        writer.writerows(all_data)
Beispiel #5
0
def comp_exp(f_path, mutation_rate, crossover_rate, game_code, w_social):
    runs = 10
    data = {}

    pop_size = 500
    num_gens = 2000

    all_data = []
    header = ['Algorithm'] + list(
        map(lambda x: "% Gap in " + instance_name(x[0]),
            TEST_SET_1)) + ['Avg % Gap']
    all_data.append(header)
    print(header)

    row = ['GASI']
    for pth, best in TEST_SET_1:
        print('>> Instance', instance_name(pth))
        instance_results = []
        for run in range(runs):
            print(f'>> Run{run+1}/{runs}')
            vrp = VRP(*read_file(os.path.abspath(pth)))
            game = gameClassFactory(game_code)
            result, routes, strategy_dist = ga_social_interaction_vrp(
                vrp,
                game,
                pop_size,
                num_gens,
                mutation_rate=mutation_rate,
                crossover_rate=crossover_rate,
                wgt_solution=1 - w_social,
                wgt_social=w_social)
            instance_results.append(result)
        row.append(gap_percentage(mean(instance_results), best))
    row.append(mean(row[2:]))
    all_data.append(row)
    print(row)

    row = ['GA']
    for pth, best in TEST_SET_1:
        print('>> Instance', instance_name(pth))
        instance_results = []
        for run in range(runs):
            print(f'>> Run{run+1}/{runs}')
            vrp = VRP(*read_file(os.path.abspath(pth)))
            game = gameClassFactory(game_code)
            result, routes, strategy_dist = ga_social_interaction_vrp(
                vrp,
                Game(),
                pop_size,
                num_gens,
                mutation_rate=mutation_rate,
                crossover_rate=crossover_rate,
                wgt_solution=1,
                wgt_social=0)
            instance_results.append(result)
        row.append(gap_percentage(mean(instance_results), best))
    row.append(mean(row[2:]))
    all_data.append(row)
    print(row)

    with open('results/comp_exp.csv', 'w') as file:
        writer = csv.writer(file)
        writer.writerows(all_data)