Skip to content

Commit 0c04162

Browse files
committed
Starting traveling ants problem
1 parent e09a925 commit 0c04162

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

CS612/a2/ants/dj38.tsp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
NAME: dj38
2+
COMMENT : 38 locations in Djibouti
3+
COMMENT : Derived from National Imagery and Mapping Agency data
4+
COMMENT : This file is a corrected version of dj89, where duplications
5+
COMMENT: have been removed. Thanks to Jay Muthuswamy and others for
6+
COMMENT: requesting data sets without duplications.
7+
TYPE: TSP
8+
DIMENSION: 38
9+
EDGE_WEIGHT_TYPE: EUC_2D
10+
NODE_COORD_SECTION
11+
1 11003.611100 42102.500000
12+
2 11108.611100 42373.888900
13+
3 11133.333300 42885.833300
14+
4 11155.833300 42712.500000
15+
5 11183.333300 42933.333300
16+
6 11297.500000 42853.333300
17+
7 11310.277800 42929.444400
18+
8 11416.666700 42983.333300
19+
9 11423.888900 43000.277800
20+
10 11438.333300 42057.222200
21+
11 11461.111100 43252.777800
22+
12 11485.555600 43187.222200
23+
13 11503.055600 42855.277800
24+
14 11511.388900 42106.388900
25+
15 11522.222200 42841.944400
26+
16 11569.444400 43136.666700
27+
17 11583.333300 43150.000000
28+
18 11595.000000 43148.055600
29+
19 11600.000000 43150.000000
30+
20 11690.555600 42686.666700
31+
21 11715.833300 41836.111100
32+
22 11751.111100 42814.444400
33+
23 11770.277800 42651.944400
34+
24 11785.277800 42884.444400
35+
25 11822.777800 42673.611100
36+
26 11846.944400 42660.555600
37+
27 11963.055600 43290.555600
38+
28 11973.055600 43026.111100
39+
29 12058.333300 42195.555600
40+
30 12149.444400 42477.500000
41+
31 12286.944400 43355.555600
42+
32 12300.000000 42433.333300
43+
33 12355.833300 43156.388900
44+
34 12363.333300 43189.166700
45+
35 12372.777800 42711.388900
46+
36 12386.666700 43334.722200
47+
37 12421.666700 42895.555600
48+
38 12645.000000 42973.333300

CS612/a2/ants/travellingAnts.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/python
2+
import sys
3+
import string
4+
import math
5+
6+
class ant:
7+
8+
def __init__(self, _id):
9+
self._id = _id
10+
self.i = 0
11+
self.tour = []
12+
13+
def tick(self, world):
14+
print("ant-%d at %d" % (self._id, self.i))
15+
16+
17+
class world:
18+
19+
def __init__(self, filename, cpu):
20+
lines = open(filename, 'r').readlines()
21+
while lines[0][0] not in string.digits:
22+
lines.pop(0)
23+
self.size = len(lines)
24+
25+
self.g = [[None for _ in range(self.size)] for _ in range(self.size)]
26+
x_coords = []
27+
y_coords = []
28+
for l in lines:
29+
i, x, y = l.strip().split()
30+
x_coords.append(float(x))
31+
y_coords.append(float(y))
32+
for i in range(self.size):
33+
for j in range(self.size):
34+
x_delta = abs(x_coords[i] - x_coords[j])
35+
y_delta = abs(y_coords[i] - y_coords[j])
36+
self.g[i][j] = math.sqrt(x_delta**2 + y_delta**2)
37+
38+
self.ants = [ant(i) for i,_ in enumerate(range(5))]
39+
self.pher = [[0.0 for _ in range(self.size)] for _ in range(self.size)]
40+
41+
def distance(self, i, j):
42+
return self.g[i][j]
43+
44+
def tick(self):
45+
for ant in self.ants:
46+
ant.tick(self)
47+
48+
49+
if __name__ == "__main__":
50+
if len(sys.argv) < 3:
51+
print("usage: python travellingAnts.py input.tsp maxseconds")
52+
sys.exit(0)
53+
54+
w = world(sys.argv[1], sys.argv[2])
55+
56+
while True:
57+
w.tick()
58+
break

0 commit comments

Comments
 (0)