(self, position, velocity, H, epsilon, damping, dt)
| 48 | return position |
| 49 | |
| 50 | def _step(self, position, velocity, H, epsilon, damping, dt): |
| 51 | import numpy as np |
| 52 | |
| 53 | from sklearn.metrics import euclidean_distances |
| 54 | |
| 55 | """ |
| 56 | One step of the simulation. |
| 57 | """ |
| 58 | v2v_dist = euclidean_distances(position) |
| 59 | e_center = np.matmul(H.T, position) / H.sum(axis=0).reshape(-1, 1) |
| 60 | v2e_dist = euclidean_distances(position, e_center) * H |
| 61 | e2e_dist = euclidean_distances(e_center) |
| 62 | |
| 63 | centers = self.centers |
| 64 | |
| 65 | force = np.zeros_like(position) |
| 66 | if self.node_attraction is not None: |
| 67 | f = ( |
| 68 | self._node_attraction(position, e_center, v2e_dist) |
| 69 | * self.node_attraction |
| 70 | ) |
| 71 | assert np.isnan(f).sum() == 0 |
| 72 | force += f |
| 73 | if self.node_repulsion is not None: |
| 74 | f = self._node_repulsion(position, v2v_dist) |
| 75 | if self.n_centers == 1: |
| 76 | f *= self.node_repulsion[0] |
| 77 | else: |
| 78 | masks = np.zeros((position.shape[0], 1)) |
| 79 | masks[: self.nums[0]] = self.node_repulsion[0] |
| 80 | masks[self.nums[0] :] = self.node_repulsion[1] |
| 81 | f *= masks |
| 82 | assert np.isnan(f).sum() == 0 |
| 83 | force += f |
| 84 | if self.edge_repulsion is not None: |
| 85 | f = self._edge_repulsion(e_center, H, e2e_dist) * self.edge_repulsion |
| 86 | assert np.isnan(f).sum() == 0 |
| 87 | force += f |
| 88 | if self.center_gravity is not None: |
| 89 | masks = [np.zeros((position.shape[0], 1)), np.zeros((position.shape[0], 1))] |
| 90 | masks[0][: self.nums[0]] = 1 |
| 91 | masks[1][self.nums[0] :] = 1 |
| 92 | for center, gravity, mask in zip(centers, self.center_gravity, masks): |
| 93 | v2c_dist = euclidean_distances(position, center.reshape(1, -1)).reshape( |
| 94 | -1, 1 |
| 95 | ) |
| 96 | f = self._center_gravity(position, center, v2c_dist) * gravity * mask |
| 97 | assert np.isnan(f).sum() == 0 |
| 98 | force += f |
| 99 | |
| 100 | force *= damping |
| 101 | |
| 102 | force = np.clip(force, -0.1, 0.1) |
| 103 | position += force * dt |
| 104 | velocity = force |
| 105 | |
| 106 | return position, velocity, self._stop_condition(velocity, epsilon) |
| 107 |
no test coverage detected