prefer first result

This commit is contained in:
Moritz Kobitzsch 2016-09-22 12:27:55 +02:00
parent d1f555dcef
commit 0abd32fca3
4 changed files with 29 additions and 13 deletions

View File

@ -1,6 +1,10 @@
var util = require('util'); var util = require('util');
module.exports = function () { module.exports = function () {
function add(a, b) {
return a + b;
}
this.When(/^I plan a trip I should get$/, (table, callback) => { this.When(/^I plan a trip I should get$/, (table, callback) => {
var got; var got;
@ -43,6 +47,7 @@ module.exports = function () {
} }
var subTrips; var subTrips;
var trip_durations;
if (res.statusCode === 200) { if (res.statusCode === 200) {
if (headers.has('trips')) { if (headers.has('trips')) {
subTrips = json.trips.filter(t => !!t).map(t => t.legs).map(tl => Array.prototype.concat.apply([], tl.map((sl, i) => { subTrips = json.trips.filter(t => !!t).map(t => t.legs).map(tl => Array.prototype.concat.apply([], tl.map((sl, i) => {
@ -52,6 +57,12 @@ module.exports = function () {
return toAdd; return toAdd;
}))); })));
} }
if(headers.has('durations')) {
var all_durations = json.trips.filter(t => !!t).map(t => t.legs).map(tl => Array.prototype.concat.apply([], tl.map(sl => {
return sl.duration;
})));
trip_durations = all_durations.map( a => a.reduce(add, 0));
}
} }
var ok = true, var ok = true,
@ -62,7 +73,6 @@ module.exports = function () {
if (si >= subTrips.length) { if (si >= subTrips.length) {
ok = false; ok = false;
} else { } else {
ok = false;
// TODO: Check all rotations of the round trip // TODO: Check all rotations of the round trip
for (var ni=0; ni<sub.length; ni++) { for (var ni=0; ni<sub.length; ni++) {
var node = this.findNodeByName(sub[ni]), var node = this.findNodeByName(sub[ni]),
@ -70,8 +80,8 @@ module.exports = function () {
if (this.FuzzyMatch.matchLocation(outNode, node)) { if (this.FuzzyMatch.matchLocation(outNode, node)) {
encodedResult += sub[ni]; encodedResult += sub[ni];
extendedTarget += sub[ni]; extendedTarget += sub[ni];
ok = true;
} else { } else {
ok = false;
encodedResult += util.format('? [%s,%s]', outNode[0], outNode[1]); encodedResult += util.format('? [%s,%s]', outNode[0], outNode[1]);
extendedTarget += util.format('%s [%d,%d]', sub[ni], node.lat, node.lon); extendedTarget += util.format('%s [%d,%d]', sub[ni], node.lat, node.lon);
} }
@ -87,6 +97,8 @@ module.exports = function () {
got.trips = extendedTarget; got.trips = extendedTarget;
} }
got.durations = trip_durations;
for (var key in row) { for (var key in row) {
if (this.FuzzyMatch.match(got[key], row[key])) { if (this.FuzzyMatch.match(got[key], row[key])) {
got[key] = row[key]; got[key] = row[key];

View File

@ -8,7 +8,7 @@ Feature: Basic trip planning
Scenario: Testbot - Trip planning with less than 10 nodes Scenario: Testbot - Trip planning with less than 10 nodes
Given the node map Given the node map
| a | b | | a | b |
| d | c | | c | d |
And the ways And the ways
| nodes | | nodes |
@ -18,8 +18,9 @@ Feature: Basic trip planning
| da | | da |
When I plan a trip I should get When I plan a trip I should get
| waypoints | trips | | waypoints | trips | durations |
| a,b,c,d | dcba | | a,b,c,d | abcd | 7.6 |
| d,b,c,a | dbca | 7.6 |
Scenario: Testbot - Trip planning with more than 10 nodes Scenario: Testbot - Trip planning with more than 10 nodes
Given the node map Given the node map
@ -55,8 +56,8 @@ Feature: Basic trip planning
| k | | | f | | k | | | f |
| j | i | h | g | | j | i | h | g |
| | | | | | | | | |
| m | n | | | | q | m | n | |
| p | o | | | | | p | o | |
And the ways And the ways
| nodes | | nodes |
@ -75,12 +76,13 @@ Feature: Basic trip planning
| mn | | mn |
| no | | no |
| op | | op |
| pm | | pq |
| qm |
When I plan a trip I should get When I plan a trip I should get
| waypoints | trips | | waypoints | trips |
| a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p | cbalkjihgfedc,ponm | | a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p | cbalkjihgfedc,mnop |
# Test single node in each component #1850 # Test single node in each component #1850
Scenario: Testbot - Trip planning with less than 10 nodes Scenario: Testbot - Trip planning with less than 10 nodes

View File

@ -53,8 +53,7 @@ std::vector<NodeID> BruteForceTrip(const NodeIDIterator start,
const auto component_size = std::distance(start, end); const auto component_size = std::distance(start, end);
std::vector<NodeID> perm(start, end); std::vector<NodeID> perm(start, end);
std::vector<NodeID> route; std::vector<NodeID> route = perm;
route.reserve(component_size);
EdgeWeight min_route_dist = INVALID_EDGE_WEIGHT; EdgeWeight min_route_dist = INVALID_EDGE_WEIGHT;
@ -68,7 +67,10 @@ std::vector<NodeID> BruteForceTrip(const NodeIDIterator start,
do do
{ {
const auto new_distance = ReturnDistance(dist_table, perm, min_route_dist, component_size); const auto new_distance = ReturnDistance(dist_table, perm, min_route_dist, component_size);
if (new_distance <= min_route_dist) // we can use `<` instead of `<=` here, since all distances are `!=` INVALID_EDGE_WEIGHT
// In case we really sum up to invalid edge weight for all permutations, keeping the very
// first one is fine too.
if (new_distance < min_route_dist)
{ {
min_route_dist = new_distance; min_route_dist = new_distance;
route = perm; route = perm;

View File

@ -119,7 +119,7 @@ std::vector<NodeID> FindRoute(const std::size_t &number_of_locations,
// add the location to the current trip such that it results in the shortest total // add the location to the current trip such that it results in the shortest total
// tour // tour
if (insert_candidate.first >= farthest_distance) if (insert_candidate.first > farthest_distance)
{ {
farthest_distance = insert_candidate.first; farthest_distance = insert_candidate.first;
next_node = *i; next_node = *i;