[英]Travelling Salesman visualisation in Java (Swing)
出於練習目的,我挑戰自己編寫一個解決TSP並逐步可視化結果的程序。
到目前為止,我的程序使用了一個簡單的最近鄰居算法。 我希望我的程序具有靈活性,因此當我添加新算法時,它也將能夠可視化結果,而不會弄亂顯示邏輯。
我遇到的問題之一是-如何逐步顯示解決方案? 我通過創建多個局部解決方案,存儲它們並一個接一個地顯示來解決了該問題。 我覺得可以做得更好,但是我在圖形方面並不是很好,我希望能從中獲得一些線索。
這是一些代碼: Point
類-代表城市。
class Point {
private double x;
private double y;
public double getX() {
return x;
}
public double getY() {
return y;
}
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public Point(){
Random r = new Random();
x=r.nextInt(1000);
y=r.nextInt(650);
}
public double calculateDistanceToPoint(Point p) {
double dist = Math.sqrt(Math.pow(this.x-p.x, 2) + Math.pow(this.y-p.y, 2));
return round(dist,2);
}
private static double round(double value, int places) {
if (places < 0) throw new IllegalArgumentException();
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(places, RoundingMode.HALF_UP);
return bd.doubleValue();
}
}
然后,進行計算的Solver
類:
class Solver {
//list of all points to visit
private static ArrayList<Point> points = new ArrayList<>();
//adjacency matrix
private ArrayList<ArrayList<Double>> adjMatrix = new ArrayList<>();
//found solution
private static ArrayList<Point> solution = new ArrayList<>();
//visited points
private ArrayList<Integer> visitedPoints = new ArrayList<>();
//used for visualisation
private static Solution finalSolution = new Solution();
public void clear() {
points.clear();
solution.clear();
visitedPoints.clear();
adjMatrix.clear();
finalSolution.clear();
}
public void addPoint(Point p) {
points.add(p);
}
public static ArrayList<Point> getPoints() {
return Solver.points;
}
public void fillAdjacencyMatrix() {
int iter_x;
int iter_y;
for (iter_x = 0; iter_x < points.size(); iter_x++) {
ArrayList<Double> temp = new ArrayList<>();
for (iter_y = 0; iter_y < points.size(); iter_y++) {
if (iter_x == iter_y) {
temp.add(-1.0);
} else {
temp.add(points.get(iter_x).calculateDistanceToPoint(points.get(iter_y)));
}
}
adjMatrix.add(temp);
}
}
private int getIndexOfMin(ArrayList<Double> arr) {
Double min = Double.MAX_VALUE;
int index = -2;
for (int i = 0; i < arr.size(); i++) {
Double val = arr.get(i);
if (!(val == -1.0) && !visitedPoints.contains(i) && val < min) {
min = val;
index = i;
}
}
return index;
}
public void solveUsingNN(int startingPoint) {
int noOfVisited = 0;
//find nearest point from the starting one
int nearest = getIndexOfMin(adjMatrix.get(startingPoint));
Solution sol = new Solution();
//until we've visited all points
while (noOfVisited!=points.size()) {
//get next nearest point and add it to visited
nearest = getIndexOfMin(adjMatrix.get(nearest));
visitedPoints.add(nearest);
//add this point to solution
Point newPoint = points.get(nearest);
solution.add(newPoint);
//create a new frame for animation, containing all previous steps and recently added one
SolutionStep ss = new SolutionStep();
Point p;
for (Point newPoint : solution) {
p = new Point(newPoint.getX(), newPoint.getY());
ss.addPoint(p);
}
sol.addStep(ss);
noOfVisited++;
}
finalSolution=sol;
}
}
然后, SolutionStep
類:
class SolutionStep{
public final ArrayList<Point> step = new ArrayList<>();
public SolutionStep(){}
public void addPoint(Point p){
step.add(p);
}
public void draw(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
for (int i = 0; i < step.size()-1; i++) {
g2.draw(new Line2D.Double(step.get(i).getX(), step.get(i).getY(), step.get(i + 1).getX(), step.get(i + 1).getY()));
}
}
}
和Solution
,其中包含許多步驟。
public class Solution {
private ArrayList<Point> points = new ArrayList<>();
private static ArrayList<SolutionStep> playbackSolution = new ArrayList<>();
private int noOfFrames;
public Solution(ArrayList<SolutionStep> listOfSteps, int noOfFrames){
this.noOfFrames=noOfFrames;
playbackSolution=listOfSteps;
}
public Solution(){}
public static ArrayList<SolutionStep> getPlayback(){
return playbackSolution;
}
public void clear(){
playbackSolution.clear();
}
public void addStep(SolutionStep solutionStep){
playbackSolution.add(solutionStep);
}
public void draw(Graphics g) {
int numberOfPoints;
points = Solver.getPoints();
Graphics2D g2 = (Graphics2D) g;
//draw all points
for (Point point : points) {
g2.fill(new Rectangle2D.Double(point.getX(), point.getY(), 6, 6));
}
//draw next line
for(int i = 0;i<noOfFrames;i++) {
playbackSolution.get(i).draw(g);
}
//if we are at the final solution, draw a line from last point to the first
if (noOfFrames == points.size()){
numberOfPoints = points.size();
Point first = playbackSolution.get(0).step.get(0);
Point last = playbackSolution.get(numberOfPoints-1).step.get(numberOfPoints-1);
g2.draw(new Line2D.Double(first.getX(), first.getY(), last.getX(), last.getY()));
}
}
}
最后, Visualisation
class Visualisation extends JFrame {
private DrawingPanel contentPane;
private int noOfPoints = 10;
private int delay_time = 300;
public Visualisation() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 1100, 700);
contentPane = new DrawingPanel();
setContentPane(contentPane);
JButton start = new JButton("Start");
start.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
Solver s = new Solver();
s.clear();
contentPane.displayNoOfSteps = 0;
for (int i=0;i<noOfPoints;i++) {
s.addPoint(new Point());
}
s.fillAdjacencyMatrix();
s.solveUsingNN(0);
new javax.swing.Timer(delay_time, new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
contentPane.repaint();
}
}).start();
contentPane.repaint();
}
});
contentPane.add(start);
}
}
和DrawingPanel
:
class DrawingPanel extends JPanel{
public int displayNoOfSteps = 1;
DrawingPanel(){}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Solution sol = new Solution(Solution.getPlayback(),displayNoOfSteps);
sol.draw(g);
if (displayNoOfSteps< Solution.getPlayback().size())
displayNoOfSteps++;
}
}
主班:
class Main {
public static void main(String[] args){
Visualisation frame = new Visualisation();
frame.setVisible(true);
}
}
現在,在Visualisation
類中,我有一個Timer
。 該計時器每隔delay_time
ms調用DrawingPanel
上的repaint()
,並在每次迭代中增加要顯示的步驟數。 問題是,如果我運行一個仿真,然后再次Start
,則仿真運行得更快,並且在運行幾次之后,它幾乎立即顯示了最后一步。 我不知道怎么了 我該如何處理?
我在程序啟動時遇到錯誤-
at Solution.draw(Solution.java:57)
at DrawingPanel.paintComponent(Visualisation.java:53)
指的是行:
playbackSolution.get(i).draw(g);
和
sol.draw(g);
但是我還沒畫任何東西! repaint()
在JButton
的ActionListener
中。 或者,也許繪制JButton
調用draw()
嗎? 我如何擺脫這個問題?
另外,我覺得我使用了太多的靜態字段和方法,但是,另一方面,最好創建例如Solver
實例,然后使用非靜態方法來獲取解決方案? 還是讓Solver
成為單身人士? 無論如何,只有一個實例。
如前所述,在編寫更復雜的算法(例如模擬退火)之前,我希望獲得有關此代碼的一些反饋,因此很容易保持良好的代碼質量。 我可以在此代碼中進行哪些更改,以使其更易於添加新功能?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.