-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprubik4x2.py
118 lines (96 loc) · 3.46 KB
/
prubik4x2.py
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
from typing import List
import numpy as np
import matplotlib.pyplot as plt
import networkx as nx
import problem_generator as pg
import json
import yaml
import argparse
import time
from common import *
from parallelSwap4x2 import *
from prubik import RTM
class RTM4x2(RTM):
def __init__(self, dim, agents):
super().__init__(dim, agents)
def y_shuffle(self):
agents_dict=dict()
for agent in self.agents:
vid= agent.current[0]//2
if vid not in agents_dict:
agents_dict[vid]=[]
agents_dict[vid].append(agent)
for i in range(0,self.dim[0],2):
vid=i//2
swapper=ParallelSwap4x2(i,i+1,0,self.dim[1]-1,agents_dict[vid],orientation='y')
swapper.swap()
maxspan=max([len(agent.path) for agent in self.agents])
for agent in self.agents:
while len(agent.path)<maxspan:
agent.path.append(agent.path[-1])
def x_shuffle(self):
print("x shuffle called")
agents_dict=dict()
for agent in self.agents:
vid= agent.current[1]//2
if vid not in agents_dict:
agents_dict[vid]=[]
agents_dict[vid].append(agent)
for i in range(0,self.dim[1],2):
vid=i//2
swapper=ParallelSwap4x2(0,self.dim[0]-1,i,i+1,agents_dict[vid],orientation='x')
swapper.swap()
maxspan=max([len(agent.path) for agent in self.agents])
for agent in self.agents:
while len(agent.path)<maxspan:
agent.path.append(agent.path[-1])
def debug():
robots=[]
xmax=30
starts1=[(x,0) for x in range(xmax)]
starts2=[(x,1) for x in range(xmax)]
goals1=[(x,0) for x in range(xmax)]
goals2=[(x,1) for x in range(xmax)]
np.random.shuffle(starts1)
np.random.shuffle(starts2)
starts=starts1+starts2
goals=goals1+goals2
for i in range(len(starts)):
r=Agent(i,starts[i],goals[i])
r.current=starts[i]
r.intermediate=goals[i]
robots.append(r)
swapper=ParallelSwap4x2(0,xmax-1,0,1,robots,orientation="x")
swapper.swap()
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--f", help="filename")
parser.add_argument("--o",help='output')
args = parser.parse_args()
if args.f:
graph,starts,goals=pg.read_from_yaml(args.f)
ymax = max([x for x, y in graph.nodes()]) + 1
xmax = max([y for x, y in graph.nodes()]) + 1
print(xmax,ymax)
agents=agents_from_starts_and_goals(starts,goals)
# pg.save_instance_txt(graph,starts,goals,"./48x72.map","full_demo.scen")
p=RTM4x2([xmax,ymax],agents)
t1=time.clock()
p.three_n_shuffle_sim()
t2=time.clock()
save_output(p.agents,computation_time=t2-t1,filename=args.o,save_path=False)
#save_as_txt(agents,computation_time=t2-t1,filename=args.o,save_path=True)
else:
graph,starts,goals=pg.read_from_yaml('./90.yaml')
xmax = max([x for x, y in graph.nodes()]) + 1
ymax = max([y for x, y in graph.nodes()]) + 1
agents=agents_from_starts_and_goals(starts,goals)
p=RTM4x2([xmax,ymax],agents)
#t1=time.clock()
p.three_n_shuffle()
#t2=time.clock()
#graph=pg.generate_full_graph(12,2)
#starts,goals=pg.generate_instance(graph,24)
if __name__=="__main__":
main()
# debug()