-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
92 lines (85 loc) · 3.22 KB
/
run.py
File metadata and controls
92 lines (85 loc) · 3.22 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
92
from flask import Flask, render_template, request
from flask_socketio import SocketIO, emit
import os
import main
import greedy
import ploter as pl
import numpy as np
import aco
import subprocess
import time
def run_aco(filename, ants, steps, alpha, beta, rho):
FNULL = open(os.devnull, 'w')
args = "aco.exe {} {} {} {} {} {}".format(filename, int(ants),
int(steps), float(alpha),
float(beta), float(rho))
subprocess.call(args, shell=False)
file = open("./output.txt", "r")
distance = float(file.readline())
size = int(file.readline())
path = []
coordinates = []
for city in file:
tmp = city.split()
coordinates.append([int(tmp[1]), int(tmp[2])])
path.append(tmp[0])
coordinates = np.array(coordinates)
return distance, path, np.transpose(coordinates)
app = Flask(__name__)
app.config['SECRET_KEY'] = '123'
socketio = SocketIO(app)
socketio.run(app)
@socketio.on('my event')
def handle_connect(json):
print(json['data'])
@socketio.on('calculate')
def calculate(json):
print("Recieved calculate request")
filenames = []
for root, dirs, files in os.walk("./data/"):
for filename in files:
filenames.append("./data/" + filename)
if json['input'] in filenames:
if int(json['alg']) in [0, 1]:
cities = main.Load(filename=json['input'])
if int(json['alg']) == 0:
before = time.time_ns()
distance, path, coordinates = greedy.main(cities)
after = time.time_ns()
else:
before = time.time_ns()
distance, path, coordinates = run_aco(json['input'], json['ants'], json['steps'], json['alpha'], json['beta'], json['rho'])
after = time.time_ns()
main.PathToFile(path)
pl.generateInteractiveGraph(x=coordinates[0], y=coordinates[1], path=path)
data = {
"distance": distance,
"time": after-before
}
emit('calculated', data)
@socketio.on('generate')
def generate(json):
print("Recieved generate request")
if int(json['size']) >= 0 and int(json['size']) < 1000:
if int(json['alg']) in [0, 1]:
cities = main.Generate(size=int(json['size']), filename=json['filename'])
if int(json['alg']) == 0:
before = time.time_ns()
distance, path, coordinates = greedy.main(cities)
after = time.time_ns()
else:
before = time.time_ns()
distance, path, coordinates = run_aco("./data/"+json['filename']+".txt", json['ants'], json['steps'], json['alpha'], json['beta'], json['rho'])
after = time.time_ns()
main.PathToFile(path)
pl.generateInteractiveGraph(x=coordinates[0], y=coordinates[1], path=path)
data = {
"distance": distance,
"time": after-before
}
emit('generated', data)
@app.route('/')
def index():
dataInput = [file for file in os.listdir("./data") if file.endswith(".txt")]
algorithms = [[0, "greedy"], [1, "ant-colony"]]
return render_template('index.html', dataInput=dataInput, algorithms=algorithms)