Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
afourmy committed Feb 23, 2018
1 parent a3e2836 commit d34ef99
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 59 deletions.
10 changes: 6 additions & 4 deletions algorithms/tour_construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion flask_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
109 changes: 55 additions & 54 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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));
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}
}
</script>
</body>
</html>

0 comments on commit d34ef99

Please sign in to comment.