Sunday, April 10, 2016

Finding Shortest Route

This program calculates the shortest route between multiple points. The user inputs all the points. The first one becomes the starting point. Permutation is used here to explore every possibility.


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.Border;

public class ShortestDistance extends JFrame{
    private JLabel lresult=null;
    private JPanel pmain=null;
    private JButton bcalculate=null;
    private ArrayList<Point> pall=null;
    private ArrayList<Point> pall2=null;
    private ArrayList<Integer> porder=null;
    private ArrayList<String> routes=null;  //all possibilities
    private ArrayList<String> routes2=null; //start point = point 1
    private ArrayList<Integer> routes2dist=null;
    
    public ShortestDistance(){
        super("Shortest Distance Finder");
        setSize(500,450);
        
        lresult=new JLabel(" ");
        lresult.setBackground(Color.WHITE);
        
        pmain=new PanelDraw();
        pmain.setBackground(Color.WHITE);
        pmain.addMouseListener(new MouseHandler());
        pmain.addMouseMotionListener(new MouseHandler());
        
        bcalculate=new JButton("Calculate");
        bcalculate.addActionListener(new ButtonHandler());
        
        setLayout(new BorderLayout());
        add("North",lresult);
        add("Center",pmain);
        add("South",bcalculate);
        
        pall=new ArrayList<Point>();
        pall2=new ArrayList<Point>();
        porder=new ArrayList<Integer>();
        routes=new ArrayList<String>();
        routes2=new ArrayList<String>();
        routes2dist=new ArrayList<Integer>();
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        show();
    }
    
    class PanelDraw extends JPanel{
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            
            if(!pall.isEmpty()){
                for(int i=0;i<pall.size();i++){
                    g.fillOval(pall.get(i).x,pall.get(i).y,5,5);
                    g.drawString(porder.get(i)+"",(pall.get(i).x)+2,(pall.get(i).y)+2);
                    
                }
            }
        }
    }
    
    class MouseHandler implements MouseMotionListener, MouseListener{
        public void mouseMoved(MouseEvent e){
            
        }
        public void mouseDragged(MouseEvent e){
            
        }
        public void mouseEntered(MouseEvent e){
        
        }
        public void mouseExited(MouseEvent e){
            
        }
        public void mouseReleased(MouseEvent e){           
            
        }
        public void mousePressed(MouseEvent e){
            
        }
        public void mouseClicked(MouseEvent e){
            pall.add(e.getPoint());
            pall2.add(e.getPoint());
            if(porder.isEmpty()){
               porder.add(0,1);
            }
            if(!porder.isEmpty()){
                //System.out.println(porder.get(porder.size()-1));
                porder.add(porder.get(porder.size()-1)+1);
            }
            repaint();
        }
    }
    
    class ButtonHandler implements ActionListener{
        public void actionPerformed(ActionEvent e){
            int counter=pall.size()*pall.size();
            
            String[] pstart=new String[counter];
            String[] pend=new String[counter];
            Integer[] distance=new Integer[counter];
            
            counter=0;
            for(int i=0;i<pall.size();i++){
                for(int j=0;j<pall2.size();j++){
                    int x1=pall.get(i).x;
                    int y1=pall.get(i).y;
                    int x2=pall2.get(j).x;
                    int y2=pall2.get(j).y;
                    
                    int x=Math.abs(x2-x1);
                    int y=Math.abs(y2-y1);
                    
                    pstart[counter]=""+(i+1);
                    pend[counter]=""+(j+1);
                    distance[counter]=((int)Math.sqrt((x*x)+(y*y)));
                    
                    counter++;
                }
            }
            
            ArrayList<String> pname=new ArrayList<String>();
            
            counter=0;
            for(int i=0;i<pstart.length;i++){
                if(pname.contains(pstart[i])){
                    continue;
                }
                else{
                    pname.add(counter,pstart[i]);
                    counter++;
                }
            }
            
            String allpoints="";
            for(int i=0;i<pname.size();i++){
                allpoints=allpoints+pname.get(i);
            }
            
            funcPermutation("",allpoints);
            
            for(int i=0;i<routes.size();i++){
                char c=routes.get(i).charAt(0);
                if(c=='1'){
                    routes2.add(routes.get(i));
                }
            }
            
            int totaldistance=0;
            for(int i=0;i<routes2.size();i++){
                String temproute=routes2.get(i);

                for(int j=0;j<temproute.length()-1;j++){
                    String routeleg=temproute.substring(j,j+2);
                    //System.out.println(routeleg);
                    for(int k=0;k<pstart.length;k++){
                        if(routeleg.equals(pstart[k]+pend[k])){
                            totaldistance=totaldistance+distance[k];
                        }
                    }
                }
                //System.out.println(temproute+":"+totaldistance);
                routes2dist.add(totaldistance);
                totaldistance=0;
            }
            
            int indexshortest=0;
            int shortest=routes2dist.get(0);
            for(int i=1;i<routes2.size();i++){
                //System.out.println(routes2.get(i)+":"+routes2dist.get(i));
                if(routes2dist.get(i)<shortest){
                    shortest=routes2dist.get(i);
                    indexshortest=i;
                }
            }
            
            String stresult="";
            for(int i=0;i<routes2.get(indexshortest).length();i++){
                stresult=stresult+routes2.get(indexshortest).charAt(i);
                if(i<routes2.get(indexshortest).length()-1){
                    stresult=stresult+" - ";
                }
            }
            lresult.setText(stresult);
        }
        
        public void funcPermutation(String st1,String st2){
            if(st2.length()<=1){
                routes.add(st1+st2);
            }
            else
                for (int i= 0;i<st2.length();i++) {
                try {
                    String stresult=st2.substring(0,i)+st2.substring(i+1);
                    funcPermutation(st1+st2.charAt(i),stresult);
                }catch(Exception ex) {
                    
                }
            }
        }
    }
    
    public static void main(String[] args){
        new ShortestDistance();
    }
}

No comments:

Post a Comment