Repository navigation
Expand file tree
/
Copy pathtesting_multi.py
More file actions
91 lines (89 loc) · 3.31 KB
/
Copy pathtesting_multi.py
File metadata and controls
91 lines (89 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import time
import pandas as pd
import numpy as np
from Generator import generate_instance_nd
from Transport_Problem import (
genetic_algorithm_islands,
northwest_seed, least_cost_seed, HiGHS_seed, random_seed
)
import sys
sys.stdout.reconfigure(encoding="utf-8")
# Параметры экспериментов
EXPERIMENTS = [
{'dims': [100, 100], 'num_tasks': 10, 'runs': 10},
{'dims': [3, 3, 3, 3, 3, 3, 3], 'num_tasks': 10, 'runs': 10},
{'dims': [10, 10, 10, 10], 'num_tasks': 10, 'runs': 10},
{'dims': [3, 3], 'num_tasks': 10, 'runs': 10},
{'dims': [5, 5], 'num_tasks': 10, 'runs': 10},
{'dims': [7, 7], 'num_tasks': 10, 'runs': 10},
{'dims': [3, 3, 3], 'num_tasks': 10, 'runs': 10},
{'dims': [5, 5, 5], 'num_tasks': 10, 'runs': 10},
{'dims': [7, 7, 7], 'num_tasks': 10, 'runs': 10},
{'dims': [3, 3, 3, 3], 'num_tasks': 10, 'runs': 10},
{'dims': [5, 5, 5, 5], 'num_tasks': 10, 'runs': 10},
]
POP_SIZE = 100
GENERATIONS = 100
ISLANDS = 4
SEED_RATIO = 0.25
MUTATION_RATE = 0.02
SEED_METHODS = [
('Northwest', northwest_seed),
('LeastCost', least_cost_seed),
('LP', HiGHS_seed),
('Random', random_seed)
]
def run_experiment(exp, balance_margins=False):
dims = exp['dims']
num_tasks = exp['num_tasks']
runs = exp['runs']
records = []
for task_id in range(1, num_tasks + 1):
instance = generate_instance_nd(dims, balance_margins=False, normalize_costs_flag=False)
constraints = [np.array(m) for m in instance['margins']]
costs = np.array(instance['costs'])
for name, method in SEED_METHODS:
start = time.time()
plan = method(constraints, costs)
t = time.time() - start
cost_val = float(np.sum(plan * costs))
records.append({
'dims': tuple(dims),
'task_id': task_id,
'method': name,
'run_id': 0,
'best_cost': cost_val,
'time_sec': t
})
for run_id in range(1, runs + 1):
start = time.time()
_, best_cost, _ = genetic_algorithm_islands(
constraints, costs,
pop_size=POP_SIZE,
generations=GENERATIONS,
islands=ISLANDS,
seed_ratio=SEED_RATIO,
mutation_rate=MUTATION_RATE
)
t = time.time() - start
records.append({
'dims': tuple(dims),
'task_id': task_id,
'method': 'GA',
'run_id': run_id,
'best_cost': float(best_cost),
'time_sec': t
})
print(f"Completed dims={dims}, task {task_id}/{num_tasks}")
return pd.DataFrame(records)
def main(balance_margins=False):
all_dfs = []
for exp in EXPERIMENTS:
print(f"Running experiment for dims={exp['dims']}")
df = run_experiment(exp, balance_margins)
all_dfs.append(df)
full_df = pd.concat(all_dfs, ignore_index=True)
full_df.to_excel('multi_test_results.xlsx', index=False)
print("All experiments done. Results saved to multi_test_results.xlsx")
if __name__ == '__main__':
main(balance_margins=False)