Skip to content

Commit

Permalink
TSP: add support to render hoursminsec and kmmeter string
Browse files Browse the repository at this point in the history
  • Loading branch information
ge0ffrey committed Mar 20, 2015
1 parent 92fe448 commit dffefb7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
Expand Up @@ -4,7 +4,7 @@ TYPE : TSP
DIMENSION : 50
EDGE_WEIGHT_TYPE: EXPLICIT
EDGE_WEIGHT_FORMAT: FULL_MATRIX
EDGE_WEIGHT_UNIT_OF_MEASUREMENT: m
EDGE_WEIGHT_UNIT_OF_MEASUREMENT: meter
NODE_COORD_SECTION
0 30.681803 -88.014426 "USS Alabama, Battleship Parkway, Mobile, AL"
1 36.106965 -112.112997 "Grand Canyon National Park, Arizona"
Expand Down
Expand Up @@ -16,6 +16,7 @@

package org.optaplanner.examples.tsp.domain;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -124,4 +125,31 @@ public Collection<? extends Object> getProblemFacts() {
return facts;
}

public String getDistanceString(NumberFormat numberFormat) {
if (score == null) {
return null;
}
long distance = - score.getScore();
if (distanceUnitOfMeasurement == null) {
return numberFormat.format(((double) distance) / 1000.0);
}
if (distanceUnitOfMeasurement.equals("sec")) { // TODO why are the values 1000 larger?
long hours = distance / 3600000;
long minutes = distance % 3600000 / 60000;
long seconds = distance % 60000 / 1000;
long milliseconds = distance % 1000;
return hours + "h " + minutes + "m " + seconds + "s " + milliseconds + "ms";
} else if (distanceUnitOfMeasurement.equals("km")) { // TODO why are the values 1000 larger?
long km = distance / 1000;
long meter = distance % 1000;
return km + "km " + meter + "m";
} else if (distanceUnitOfMeasurement.equals("meter")) {
long km = distance / 1000;
long meter = distance % 1000;
return km + "km " + meter + "m";
} else {
return numberFormat.format(((double) distance) / 1000.0) + " " + distanceUnitOfMeasurement;
}
}

}
Expand Up @@ -161,11 +161,10 @@ public void resetPanel(TravelingSalesmanTour travelingSalesmanTour) {
g.setColor(TangoColorFactory.ORANGE_3);
SimpleLongScore score = travelingSalesmanTour.getScore();
if (score != null) {
double fuel = ((double) - score.getScore()) / 1000.0;
String fuelString = NUMBER_FORMAT.format(fuel) + " fuel";
String distanceString = travelingSalesmanTour.getDistanceString(NUMBER_FORMAT);
g.setFont(g.getFont().deriveFont(Font.BOLD, (float) TEXT_SIZE * 2));
g.drawString(fuelString,
(int) width - g.getFontMetrics().stringWidth(fuelString) - 10, (int) height - 10 - TEXT_SIZE);
g.drawString(distanceString,
(int) width - g.getFontMetrics().stringWidth(distanceString) - 10, (int) height - 10 - TEXT_SIZE);
}
repaint();
}
Expand Down
Expand Up @@ -143,13 +143,17 @@ public String getDistanceString(NumberFormat numberFormat) {
if (distanceUnitOfMeasurement == null) {
return numberFormat.format(((double) distance) / 1000.0);
}
if (distanceUnitOfMeasurement.equals("sec")) {
if (distanceUnitOfMeasurement.equals("sec")) { // TODO why are the values 1000 larger?
int hours = distance / 3600000;
int minutes = distance % 3600000 / 60000;
int seconds = distance % 60000 / 1000;
int milliseconds = distance % 1000;
return hours + "h " + minutes + "m " + seconds + "s " + milliseconds + "ms";
} else if (distanceUnitOfMeasurement.equals("km")) {
} else if (distanceUnitOfMeasurement.equals("km")) { // TODO why are the values 1000 larger?
int km = distance / 1000;
int meter = distance % 1000;
return km + "km " + meter + "m";
} else if (distanceUnitOfMeasurement.equals("meter")) {
int km = distance / 1000;
int meter = distance % 1000;
return km + "km " + meter + "m";
Expand Down

0 comments on commit dffefb7

Please sign in to comment.