impelementation of force directed layout with python (numpy and pytorch)
The main purpose of this experiment is to simulate the force-directed layout algorithm. Therefore, only the following simple mechanical simulations will be carried out:
- Repulsive forces between particles
- Treating edges as springs, calculating the spring force based on distance and the ideal length of the spring.
- Import edge information from a file.
- Generate position information for 499 nodes.
- Calculate the repulsive forces acting on each point.
- Calculate the spring forces acting on each point.
- Calculate the resultant force on each point based on (3) and (4), and update its coordinates. 6, Repeat steps (3) to (5) until convergence or the specified target is reached.
Two points
- For nodes without mass
$$F_{repulsion} = \frac{k_r}{distance^2}$$ - For nodes with mass
$$F_{repulsion} = \frac{k_r * m_A * m_B * y}{distance^2}$$
Then update the spring forces experienced by A and B respectively.
For each point
- File structure
| n100-withmass_fps_120.mp4 #demo video
| n499-nomass_fps_120.mp4 #demo video
| ForceDirected.py #basic implementation
| NumpyForceDirected.py #implementation with numpy
| PytorchNumpyForceDirected.py #implementation with pytorch and numpy
| writeVideo.py #transfer imgs to video
+---imgs saving results
\---saved_data #initial files
edges_epoch_latest.txt
network.txt
nodes_epoch_latest.txt
- For help
python [xxx.py] -h
- Quick startup
Run and make dir imgs/test
, img of each epoch is saved in imgs/test/imgs/
python ForceDirected.py --load_edge --node_num 499 --dir test
Get demo video
python writeVideo.py --fps 200 --dir test --name test_video
- recommendation
enable--notsave --notdraw
to save running time