Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An improvement about Obstacle calculation #2

Open
0oshowero0 opened this issue Nov 8, 2020 · 0 comments
Open

An improvement about Obstacle calculation #2

0oshowero0 opened this issue Nov 8, 2020 · 0 comments

Comments

@0oshowero0
Copy link
Contributor

Hi, thanks for your great work about this social force simulator. I've tried this simulator and find it very useful.
I have a suggestion to improve the computation efficiency about the force calculation, that is the ObstacleForce class.
the _get_force() func will calculate the distance first for all the points in obstacle lines, then it add masks. When there are lots of obstacles this function will greatly slow-down the process.

To speed up this process, now I add a simple judgement to pre-select nearby obstacles. This improvement will speed up the process from 14sec/step to 0.05sec/step in our case. Note that the solution here is not very elegant. You can try to improve by this thought.

class ObstacleForce(Force):
    def _get_force(self):
        sigma = self.config("sigma", 0.2)
        threshold = self.config("threshold", 0.2) + self.peds.agent_radius
        force = np.zeros((self.peds.size(), 2))
        if len(self.scene.get_obstacles()) == 0:
            return force
        obstacles = np.vstack(self.scene.get_obstacles())
        pos = self.peds.pos()

        for i, p in enumerate(pos):
            diff = p - obstacles
            diff_select = diff[np.logical_and(np.logical_and(diff[:,0]<10,diff[:,0]>-10),np.logical_and(diff[:,1]<10,diff[:,1]>-10))]
            if diff_select.shape[0] == 0:
                continue
            else:
                directions, dist = stateutils.normalize(diff_select)
                dist = dist - self.peds.agent_radius
                if np.all(dist >= threshold):
                    continue
                dist_mask = dist < threshold
                directions[dist_mask] *= np.exp(-dist[dist_mask].reshape(-1, 1) / sigma)
                force[i] = np.sum(directions[dist_mask], axis=0)

        return force * self.factor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant