diff --git a/algorithms/tour_construction.py b/algorithms/tour_construction.py index 6efde4a..e6c11a8 100644 --- a/algorithms/tour_construction.py +++ b/algorithms/tour_construction.py @@ -10,6 +10,7 @@ class TourConstructionHeuristics(BaseAlgorithm): # returns the neighbor as well as the distance between the two def closest_neighbor(self, tour, node, in_tour=False, farthest=False): neighbors = self.distances[node] + print(tour, node, neighbors) current_dist = [(c, d) for c, d in neighbors.items() if (c in tour if in_tour else c not in tour)] return sorted(current_dist, key=itemgetter(1))[-farthest] @@ -31,11 +32,12 @@ def add_closest_to_tour(self, tour): return best_dist, new_tour def nearest_neighbor(self): - best_tour, best_length, best_lengths = None, float('inf'), [] - city = randrange(self.size) + city = randrange(1, self.size) current, tour, tour_length, tour_lengths = city, [city], 0, [] while len(tour) != len(self.cities): + print(tour) arg_min, edge_length = self.closest_neighbor(tour, current) + print(arg_min, edge_length) tour_length += edge_length tour_lengths.append(tour_length) tour.append(arg_min) @@ -49,7 +51,7 @@ def nearest_neighbor(self): return intermediate_steps[2:], tour_lengths def nearest_insertion(self, farthest=False): - city = randrange(self.size) + city = randrange(1, self.size) tour, tours, tour_lengths = [city], [], [] # we find the closest node R to the first node neighbor, length = self.closest_neighbor(tour, city, False, farthest) @@ -93,7 +95,7 @@ def cheapest_insertion(self): best_tour, best_length = None, float('inf') # store intermediate tours for visualization purposes best_tours, best_lengths = [], [] - city = randrange(self.size) + city = randrange(1, self.size) # we start the tour with one node I tour, tours, tour_lengths = [city], [], [] # we find the closest node R to the first node diff --git a/flask_app.py b/flask_app.py index 911f6fe..533003d 100644 --- a/flask_app.py +++ b/flask_app.py @@ -39,7 +39,7 @@ def configure_socket(app): def import_cities(): with open(join(path_app, 'data', 'cities.json')) as data: for city_dict in load(data): - if int(city_dict['population']) < 1400000: + if int(city_dict['population']) < 1300000: continue city = City(**city_dict) db.session.add(city) diff --git a/templates/index.html b/templates/index.html index ffbdddf..e05b18d 100644 --- a/templates/index.html +++ b/templates/index.html @@ -151,10 +151,8 @@ document.getElementById("file").onchange = function() { $("#fileform").submit(); }; - namespace = '/'; var socket = io.connect(location.protocol + '//' + document.domain + ':' + location.port + namespace); - var currentAlgorithm = undefined; function tourConstruction(algorithm){ clearLines(); @@ -199,10 +197,7 @@ 'gm': 'http://mt0.google.com/vt/lyrs=y&hl=en&x={x}&y={y}&z={z}&s=Ga', 'nasa': 'http://tileserver.maptiler.com/nasa/{z}/{x}/{y}.jpg' }; - - // lines of the path displayed - var polylines = []; - + function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } @@ -222,16 +217,20 @@ }); } - var rates = ['Crossover', 'Mutation']; - for (h = 0; h < rates.length; h++) { - $('#' + rates[h] + '_rate').slider({ - tooltip_position:'left', - formatter: function(value) { - return rates[h] + ' rate: ' + value; - } - }); - }; - + $('#Crossover_rate').slider({ + tooltip_position: 'left', + formatter: function(value) { + return 'Crossover rate: ' + value; + } + }); + + $('#Mutation_rate').slider({ + tooltip_position: 'left', + formatter: function(value) { + return 'Mutation rate: ' + value; + } + }); + // creation of the socket listener socket.on('draw', function(paths, lengths, best) { @@ -332,49 +331,51 @@ - lengths for updating the length in real-time, if createPath is not called from another function like createIntermediatePath */ - async function draw(paths, lengths){ - for (j = 0; j < paths.length; j++) { - // update the score - if (lengths) { - $("#score").text(Math.round(Number(lengths[j]))); - } - var color = '#0000ff'; - clearLines(); - for (i = 0; i < paths[j].length - 1; i++) { + // lines of the path displayed + var polylines = []; + async function draw(paths, lengths){ + for (j = 0; j < paths.length; j++) { + // update the score + if (lengths) { + $("#score").text(Math.round(Number(lengths[j]))); + } + var color = '#0000ff'; + clearLines(); + for (i = 0; i < paths[j].length - 1; i++) { - var source_lat = paths[j][i][0]; - source_lon = paths[j][i][1]; - destination_lat = paths[j][i+1][0]; - destination_lon = paths[j][i+1][1]; + var source_lat = paths[j][i][0]; + source_lon = paths[j][i][1]; + destination_lat = paths[j][i+1][0]; + destination_lon = paths[j][i+1][1]; - {% if view == '2D' %} - var pointA = new L.LatLng(source_lat, source_lon); - var pointB = new L.LatLng(destination_lat, destination_lon); - var pointList = [pointA, pointB]; + {% if view == '2D' %} + var pointA = new L.LatLng(source_lat, source_lon); + var pointB = new L.LatLng(destination_lat, destination_lon); + var pointList = [pointA, pointB]; + + var polyline = new L.Polyline(pointList, { + color: color, + weight: 3, + opacity: 1, + smoothFactor: 1 + }); - var polyline = new L.Polyline(pointList, { - color: color, - weight: 3, - opacity: 1, - smoothFactor: 1 - }); - - polyline.addTo(map); - polylines.push(polyline); - {% else %} - var polygonSD = WE.polygon( - [[source_lat, source_lon], [destination_lat, destination_lon], - [source_lat, source_lon]], {color: color,opacity: 20}).addTo(earth); - var polygonDS = WE.polygon( - [[destination_lat, destination_lon], [source_lat, source_lon], - [destination_lat, destination_lon]], {color: color,opacity: 20}).addTo(earth); - polylines.push(polygonSD, polygonDS) - {% endif %} - } - var speed = $('#base_slider').val(); - await sleep(50000/speed); + polyline.addTo(map); + polylines.push(polyline); + {% else %} + var polygonSD = WE.polygon( + [[source_lat, source_lon], [destination_lat, destination_lon], + [source_lat, source_lon]], {color: color,opacity: 20}).addTo(earth); + var polygonDS = WE.polygon( + [[destination_lat, destination_lon], [source_lat, source_lon], + [destination_lat, destination_lon]], {color: color,opacity: 20}).addTo(earth); + polylines.push(polygonSD, polygonDS) + {% endif %} } + var speed = $('#base_slider').val(); + await sleep(50000/speed); } + }