Skip to content

Commit

Permalink
TSP: right click to append location to tail
Browse files Browse the repository at this point in the history
  • Loading branch information
ge0ffrey committed Apr 21, 2015
1 parent a29657c commit 9cce447
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
Expand Up @@ -66,7 +66,7 @@ public void writeSolution() throws IOException {
bufferedWriter.write("EOF\n");
}

private Standstill findNextVisit(Standstill standstill) {
private Visit findNextVisit(Standstill standstill) {
for (Visit visit : tour.getVisitList()) {
if (visit.getPreviousStandstill() == standstill) {
return visit;
Expand Down
Expand Up @@ -133,6 +133,36 @@ public void doChange(ScoreDirector scoreDirector) {
});
}

public void moveVisitToTail(AirLocation clickLocation) {
TravelingSalesmanTour tour = getTravelingSalesmanTour();
Visit visit = null;
double minimumAirDistance = Double.MAX_VALUE;
for (Visit selectedVisit : tour.getVisitList()) {
double airDistance = selectedVisit.getLocation().getAirDistanceDoubleTo(clickLocation);
if (airDistance < minimumAirDistance) {
visit = selectedVisit;
minimumAirDistance = airDistance;
}
}
logger.info("Moving visit ({}) to tail.", visit);
Standstill standstill = tour.getDomicile();
for (Visit nextVisit = findNextVisit(tour, standstill); nextVisit != null; nextVisit = findNextVisit(tour, standstill)) {
standstill = nextVisit;
}
doMove(visit, standstill);
getWorkflowFrame().resetScreen();
}

private Visit findNextVisit(TravelingSalesmanTour tour, Standstill standstill) {
// Using an @InverseRelationShadowVariable on the model like in vehicle routing is far more efficient
for (Visit visit : tour.getVisitList()) {
if (visit.getPreviousStandstill() == standstill) {
return visit;
}
}
return null;
}

public void doMove(Visit visit, Standstill toStandstill) {
solutionBusiness.doChangeMove(visit, "previousStandstill", toStandstill);
}
Expand Down
Expand Up @@ -76,7 +76,11 @@ public void mousePressed(MouseEvent e) {
if (translator != null) {
double longitude = translator.translateXToLongitude(e.getX());
double latitude = translator.translateYToLatitude(e.getY());
TspWorldPanel.this.tspPanel.insertLocationAndVisit(longitude, latitude);
if (e.getButton() == MouseEvent.BUTTON1) {
TspWorldPanel.this.tspPanel.insertLocationAndVisit(longitude, latitude);
} else if (e.getButton() == MouseEvent.BUTTON3) {
TspWorldPanel.this.tspPanel.moveVisitToTail(new AirLocation(-1L, latitude, longitude));
}
}
}
});
Expand Down Expand Up @@ -154,7 +158,11 @@ public void resetPanel(TravelingSalesmanTour travelingSalesmanTour) {
g.drawString(locationsSizeString,
((int) width - g.getFontMetrics().stringWidth(locationsSizeString)) / 2, (int) height - 5);
if (travelingSalesmanTour.getDistanceType() == DistanceType.AIR_DISTANCE) {
String clickString = "Click anywhere in the map to add a visit.";
String clickString = "Left click anywhere in the map to add a visit.";
g.drawString(clickString, (int) width - 5 - g.getFontMetrics().stringWidth(clickString), (int) height - 10 - TEXT_SIZE);
}
if (travelingSalesmanTour.getDistanceType() == DistanceType.AIR_DISTANCE) {
String clickString = "Right click near a visit to append it to the end.";
g.drawString(clickString, (int) width - 5 - g.getFontMetrics().stringWidth(clickString), (int) height - 5);
}
// Show soft score
Expand All @@ -164,7 +172,7 @@ public void resetPanel(TravelingSalesmanTour travelingSalesmanTour) {
String distanceString = travelingSalesmanTour.getDistanceString(NUMBER_FORMAT);
g.setFont(g.getFont().deriveFont(Font.BOLD, (float) TEXT_SIZE * 2));
g.drawString(distanceString,
(int) width - g.getFontMetrics().stringWidth(distanceString) - 10, (int) height - 10 - TEXT_SIZE);
(int) width - g.getFontMetrics().stringWidth(distanceString) - 10, (int) height - 15 - 2 * TEXT_SIZE);
}
repaint();
}
Expand Down

0 comments on commit 9cce447

Please sign in to comment.