Logistics Optimization System Based on Dijkstra algorithm (Java language) Guo shilong Algorithm Introduction
The Dijkstra algorithm was discovered by Dutch computer scientist azger dixtra. The algorithm solves the shortest path problem in a directed graph. For example, if the vertex in the graph represents the delivery point, and the weight on the edge represents the distance between the delivery points. Dijkstra algorithm can be used to find the shortest path between two delivery points. The input of the Dijkstra algorithm contains a weighted directed graph G and A Source Vertex s in G. We use V to represent the set of all vertices in G. The edge in each graph is an ordered element pair formed by two vertices. (U, v) indicates that a path is connected from vertex u to v. It is a set of all edges of E, and the edge weight is defined by the weight function W: e → [0, ∞. Therefore, w (u, v) is the non-negative cost value (cost) from vertex u to vertex v ). The edge cost can be imagined as the distance between two vertices. The cost value of the path between any two points is the total cost value of all edges in the path. It is known that vertices S and T exist in V. Dijkstra algorithm can find the minimum cost path (I. e. Shortest Path) from S to T ). This algorithm can also find the shortest path from vertex s to any other vertex in a graph.
The basic idea is: Set the set S of a vertex and constantly expand the set. A vertex belongs to the set S. if and only when the path from the source point to the point has been obtained. At the beginning, only the Source Vertex exists in S, and the shortest path length of the non-s Midpoint is adjusted. Find the current Shortest Path and add it to the set s until the end vertex is in S. For more information about the algorithm process, see graph theory.
SYSTEM INTRODUCTIONThe system uses Wuhan as an example to customize some configuration points as fixed points, extract feasible roads as edges, and customize the weights between edges. The weights represent the driving time. 1, Figure 2. The system implements the Shortest Path 3 between any two points. As an additional function, the screen capture optimization graph and the print optimization graph function are implemented. Figure 1 Wuhan map 2 abstract vertices and edges
Figure 3 Optimal Path
Program ParsingIn the program implemented by the system, six classes are declared: the main framework class logistics, which inherits the jframe class and serves as the container and Event Response of the drawing panel, text box, and menu; the drawing panel class computermap, this class inherits the jpanel class and serves as the coordinate system and graph drawing. The print class printer implements the printable interface for Map Printing; dijkstra class and minicompath class are used to implement the dixtra algorithm; side class is used to implement edge operations. The plotting function is implemented through the computermap class. This class inherits from the jpanel class. jpanel is used as a drawing panel. The paintcompenent () function in the jpanel class is used to draw components. This function is automatically called when necessary, for example, the program is started for the first time, the Panel size or position is adjusted, and new components are added to the Panel. The program cannot directly call the paintcompenent () function, otherwise it will cause confusion in the inkjet system. However, you can call the repaint () function to re-paint the component, repaint () the function will call the paintcompenet () function when appropriate. The most likely method to be overwritten by a program is paintcompenent (), which is one of the three methods of component re-painting itself. The three methods take place in the following order: paintcomponent-the main method of painting. By default, it first draws the background, and then draws a customized image. Paintborder -- draw the border of a widget. You cannot directly call or override this method. Paintchildren -- the notification component redraws itself to repaint the child component. You cannot directly call or override this method. The program implements the plotting function by overwriting paintcomponent. First, call the superclass paintcomponent () function in paintcomponent () to implement the superclass super class function, and then customize the image to be drawn. Code: Public void paintcomponent (Graphics Ge)
{
Graphics2d G = (graphics2d) Ge;
Super. paintcomponent (g); // white, gray if no super
Panelsize = getsize ();
Rectpoint = This. getlocation (); // getlocation () obtains the screen coordinates of the current component.
// G. setcolor (color. Gray );
G. setcolor (color. Green );
// Draw the X and Y axes
G. drawline (10, panelsize. Height-10, 10, 0 );
G. drawline (10, panelsize. Height-10, panelsize. Width, panelsize. Height-10 );
G. drawstring ("0", 3, panelsize. Height-2 );
G. drawstring (New INTEGER (INT) (panelsize. Height/10). tostring (), 20, 20 );
G. drawstring (New INTEGER (INT) (panelsize. width/10). tostring (), panelsize. Width-30, panelsize. Height-20 );
// Draw the y-axis arrow
G. drawline (panelsize. Width, panelsize. Height-10, panelsize. Width-10, panelsize. Height-5 );
G. drawline (panelsize. Width, panelsize. Height-10, panelsize. Width-10, panelsize. Height-15 );
// Draw the x-axis arrow
G. drawline (10, 0, 5 );
G. drawline (10, 0, 15, 5 );
// Draw the scale
// Draw the Y axis Scale
Int isomerous1 = panelsize. width/10;
For (Int J = 0; j <isomerous1; j ++)
{
G. drawline (J * 10, panelsize. Height-10, J * 10, panelsize. Height-15 );
}
// Draw the X axis Scale
Int isomerous2 = panelsize. Height/10;
For (Int J = 0; j <isomerous2; j ++)
{
G. drawline (10, panelsize. Height-J * 10, 15, panelsize. Height-J * 10 );
}
// To draw logistics map sides
For (side iterator: mapside)
{
Float mappointx1 = 30 + (arrylistline. Get (iterator. getnode ()-1). X) * panelsize. width/12;
Float mappointy1 = 30 + (arrylistline. Get (iterator. getnode ()-1). Y) * panelsize. Height/9;
Float mappointx2 = 30 + (arrylistline. Get (iterator. getprenode ()-1). X) * panelsize. width/12;
Float mappointy2 = 30 + (arrylistline. Get (iterator. getprenode ()-1). Y) * panelsize. Height/9;
Int X1 = (INT) mappointx1; int Y1 = (INT) mappointy1;
Int X2 = (INT) mappointx2; int y2 = (INT) mappointy2;
Color colorcurrent = G. getcolor ();
G. setcolor (color. Orange );
G. setstroke (New basicstroke (2.0f ));
G. drawoval (X1-2, Y1-2, 6, 6 );
G. drawstring (New INTEGER (mapside. get (0 ). getprenode ()). tostring (), (INT) (0.4 * panelsize. width/12 + 30-5), (INT) (1.0 * panelsize. height/9 + 30-5 ));
G. drawstring (New INTEGER (iterator. getnode (). tostring (), X1-5, Y1-5 );
G. drawoval (X2-2, Y2-2, 6, 6 );
G. setstroke (New basicstroke ());
G. setcolor (color. Gray );
G. drawline (x1, Y1, X2, Y2 );
G. setcolor (colorcurrent );
// To draw the minishort path, the elements of arrylistoptline are added into arrylistoptline in dijkatra class.
For (INT I = 0; I <arrylistoptline. Size ()-1; I ++)
{
G. setcolor (color. Blue );
G. setstroke (New basicstroke (0.5f ));
Float pointx1 = 0 + (arrylistline. Get (arrylistoptline. Get (0)-1). X) * panelsize. width/12;
Float pointy1 = 0 + (arrylistline. Get (arrylistoptline. Get (0)-1). Y) * panelsize. Height/9;
Float pointx2 = 60 + (arrylistline. Get (arrylistoptline. Get (arrylistoptline. Size ()-1)-1). X) * panelsize. width/12;
Float pointy2 = 150 + (arrylistline. Get (arrylistoptline. Get (arrylistoptline. Size ()-1)-1). Y) * panelsize. Height/9;
G. drawrect (INT) pointx1, (INT) pointy1, (INT) pointx2-(INT) pointx1), (INT) pointy2-(INT) pointy1 ));
Float optmappointx1 = 30 + (arrylistline. Get (arrylistoptline. Get (I)-1). X) * panelsize. width/12;
Float optmappointy1 = 30 + (arrylistline. Get (arrylistoptline. Get (I)-1). Y) * panelsize. Height/9;
Float optmappointx2 = 30 + (arrylistline. Get (arrylistoptline. Get (I + 1)-1). X) * panelsize. width/12;
Float optmappointy2 = 30 + (arrylistline. Get (arrylistoptline. Get (I + 1)-1). Y) * panelsize. Height/9;
Color colcurrent = G. getcolor ();
G. setcolor (color. Green );
G. setstroke (New basicstroke (3.0f ));
G. drawline (INT) optmappointx1, (INT) optmappointy1, (INT) optmappointx2, (INT) optmappointy2 );
G. setcolor (colcurrent );
G. setstroke (New basicstroke ());
}
}
}
WeaknessesThe program uses the directed graph method. The starting point number is on the right and the ending point number is on the left and cannot be optimized. If you need source code, leave a message.
Hard work and happy life