Skip to content

Commit 5f3b635

Browse files
committed
simulator修正、次pedstate
1 parent 7c2e99b commit 5f3b635

File tree

9 files changed

+80
-40
lines changed

9 files changed

+80
-40
lines changed

.vscode/launch.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
// IntelliSense を使用して利用可能な属性を学べます。
3+
// 既存の属性の説明をホバーして表示します。
4+
// 詳細情報は次を確認してください: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
8+
{
9+
"name": "Python デバッガー: 現在のファイル",
10+
"type": "debugpy",
11+
"request": "launch",
12+
"program": "${file}",
13+
"console": "integratedTerminal"
14+
},
15+
{
16+
"name": "Python: main",
17+
"type": "debugpy",
18+
"request": "launch",
19+
"program": "pysocialforce-master/mains/main.py",
20+
"console": "integratedTerminal"
21+
}
22+
]
23+
}

PySocialForce-master/mains/main.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,26 @@
44

55

66
if __name__ == "__main__":
7-
# initial states, each entry is the position, velocity and goal of a pedestrian in the form of (px, py, vx, vy, gx, gy)
7+
# 歩行者の位置、速度、目標を次の形式で表します (px, py, vx, vy, gx, gy)
88
initial_state = np.array(
99
[
10-
[0.0, 10, -0.5, -0.5, 0.0, 0.0],
11-
[0.5, 10, -0.5, -0.5, 0.5, 0.0],
12-
[0.0, 0.0, 0.0, 0.5, 1.0, 10.0],
10+
[0.0, 10, 0.0, -0.5, 0.0, 0.0],
11+
[0.5, 10, 0.0, -0.5, 0.5, 0.0],
12+
#[0.0, 0.0, 0.0, 0.5, 0.0, 10.0],
1313
# [1.0, 0.0, 0.0, 0.5, 2.0, 10.0],
1414
# [2.0, 0.0, 0.0, 0.5, 3.0, 10.0],
1515
# [3.0, 0.0, 0.0, 0.5, 4.0, 10.0],
1616
]
1717
)
1818
# age type
19-
types = [0,1,0,] # 0: adult, 1: elderly, 2: child(未実装)
19+
#types = [0,1,0,] # 0: adult, 1: elderly, 2: child(未実装)
20+
types = [0,1]
2021
# social groups informoation is represented as lists of indices of the state array
21-
groups = [[0, 1], [2]] # 括弧内がグループ
22+
#groups = [[0, 1], [2]] # 括弧内がグループ
23+
groups = [[0],[1]]
2224
# list of linear obstacles given in the form of (x_min, x_max, y_min, y_max)
2325
# obs = [[-1, -1, -1, 11], [3, 3, -1, 11]]
24-
obs = [[1, 2, 7, 8]]
26+
obs = [[0, 1, 5, 5]]
2527
# obs = None
2628
# initiate the simulator,
2729
s = psf.Simulator(

PySocialForce-master/mains/main.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ n = 2
7878
n_prime = 3
7979

8080
[types.0.forces.obstacle_force]
81-
factor = 2.0
81+
factor = 10.0
8282
sigma = 0.2
8383
threshold = 3.0
8484

-119 KB
Loading

PySocialForce-master/pysocialforce/forces.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def _get_force(self) -> np.ndarray:
4343
raise NotImplementedError
4444

4545
def get_force(self, debug=False):
46-
force = self._get_force()
46+
force = self._get_force() # 現在のインスタンスの _get_force を呼び出す
4747
if debug:
4848
logger.debug(f"{camel_to_snake(type(self).__name__)}:\n {repr(force)}")
4949
return force
@@ -280,24 +280,30 @@ def __init__(self, config):
280280
self.n_prime = config("n_prime", 3)
281281

282282
def _get_force(self):
283-
pos_diff = stateutils.each_diff(self.peds.pos())
284-
diff_direction, diff_length = stateutils.normalize(pos_diff)
285-
vel_diff = -1.0 * stateutils.each_diff(self.peds.vel())
283+
pos_diff = stateutils.each_diff(self.peds.pos()) # 歩行者間の位置差ベクトル
284+
diff_direction, diff_length = stateutils.normalize(pos_diff) # 正規化された方向ベクトル、歩行者間の距離
285+
vel_diff = -1.0 * stateutils.each_diff(self.peds.vel()) # 歩行者間の速度差ベクトル
286286

287287
# 相互作用方向
288-
interaction_vec = self.lambda_importance * vel_diff + diff_direction
289-
interaction_direction, interaction_length = stateutils.normalize(interaction_vec)
288+
interaction_vec = self.lambda_importance * vel_diff + diff_direction # 速度差と位置さを組み合わせた相互作用方向ベクトル
289+
interaction_direction, interaction_length = stateutils.normalize(interaction_vec) # 相互作用ベクトルの正規化方向、相互作用ベクトルの大きさ(スカラー値)
290290

291-
theta = stateutils.vector_angles(interaction_direction) - stateutils.vector_angles(diff_direction)
292-
B = self.gamma * interaction_length
291+
theta = stateutils.vector_angles(interaction_direction) - stateutils.vector_angles(diff_direction) # 相互作用方向と位置差の方向の角度差(歩行者がどの程度別の方向を向いているか)
292+
B = self.gamma * interaction_length # 相互作用の減衰係数(距離を基づいて力を調整)
293293

294294
# 力の計算
295-
force_velocity = np.exp(-diff_length / B - (self.n_prime * B * theta) ** 2).reshape(-1, 1) * interaction_direction
296-
force_angle = -np.sign(theta).reshape(-1, 1) * np.exp(-diff_length / B - (self.n * B * theta) ** 2) * stateutils.left_normal(interaction_direction)
297-
force = force_velocity + force_angle
298-
299-
return np.sum(force.reshape((self.peds.size(), -1, 2)), axis=1) * self.factor
295+
force_velocity_amount = np.exp(-1.0 * diff_length / B - np.square(self.n_prime * B * theta))
296+
force_angle_amount = -np.sign(theta) * np.exp(
297+
-1.0 * diff_length / B - np.square(self.n * B * theta)
298+
)
299+
force_velocity = force_velocity_amount.reshape(-1, 1) * interaction_direction
300+
force_angle = force_angle_amount.reshape(-1, 1) * stateutils.left_normal(
301+
interaction_direction
302+
)
300303

304+
force = force_velocity + force_angle # n*(n-1) x 2
305+
force = np.sum(force.reshape((self.peds.size(), -1, 2)), axis=1)
306+
return force * self.factor
301307

302308
class ObstacleForce(Force):
303309
"""Calculates the force between this agent and the nearest obstacle in this

PySocialForce-master/pysocialforce/scene.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,12 @@ def __init__(self, state, types, groups, scene_configs):
1919
self.types = types
2020
self.scene_configs = scene_configs
2121

22-
agent_settings = self.initialize_agent_settings() # 各歩行者の設定を適応
23-
2422
self.max_speeds = None
2523
self.initial_speeds = None
2624

2725
self.ped_states = []
2826
self.group_states = []
29-
self.update(state, groups, agent_settings)
27+
self.update(state, groups)
3028

3129
def initialize_agent_settings(self):
3230
"""タイプごとにシーン設定を適用"""
@@ -46,12 +44,12 @@ def initialize_agent_settings(self):
4644
}) # 辞書形式でリストに保存
4745
return settings
4846

49-
def update(self, state, groups, agent_setting):
47+
def update(self, state, groups):
5048
# タイプごとの初期化処理
49+
self.agent_settings = self.initialize_agent_settings() # 各歩行者の設定を適応
5150
self.state = state
5251
self.groups = groups
53-
self.agent_settings = agent_setting # 各歩行者の設定を適応
54-
52+
5553
@property
5654
def state(self):
5755
"""歩行者の状態を取得・設定"""
@@ -115,8 +113,8 @@ def step(self, force, groups=None):
115113
next_groups = groups
116114
self.update(next_state, next_groups)
117115

118-
# def initial_speeds(self):
119-
# return stateutils.speeds(self.ped_states[0])
116+
def initial_speeds(self):
117+
return stateutils.speeds(self.ped_states[0])
120118

121119
def desired_directions(self):
122120
"""各歩行者が目標地点に向かう方向ベクトルを返す"""

PySocialForce-master/pysocialforce/simulator.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from pysocialforce.scene import PedState, EnvState
99
from pysocialforce import forces
1010

11+
import json
1112

1213
class Simulator:
1314
"""Simulate social force model.
@@ -42,18 +43,17 @@ def __init__(self, state, types=None, groups=None, obstacles=None, config_file=N
4243
self.config = DefaultConfig()
4344
if config_file:
4445
self.config.load_config(config_file)
45-
self.group_config = self.config.sub_config("scene")
4646

47+
print("Config内容:", vars(self.config)) # オブジェクトの属性を表示
48+
4749
# typeごとの設定を保持
4850
self.types = types # 0: adult, 1: elderly, 2: child
4951

50-
# シーン設定の読み込み
51-
# setで重複を削除しユニークなタイプの集合を作成([0,1,0,1]→[0,1])
52-
self.scene_configs = {t: self.config.sub_config(f"types.{t}.scene") for t in set(self.types)}
53-
self.force_configs = {t: self.config.sub_config(f"types.{t}.forces") for t in set(self.types)}
52+
self.scene_configs = self.get_scene_configs(types, self.config)
53+
print("\nscene_configs: ", json.dumps(self.scene_configs, indent=4))
5454

5555
# initiate obstacles
56-
self.env = EnvState(obstacles, self.config("resolution", 10.0))
56+
self.env = EnvState(obstacles, self.config("resolution", 50.0))
5757

5858
# initiate agents
5959
self.peds = PedState(state, types, groups, self.scene_configs)
@@ -69,6 +69,15 @@ def __init__(self, state, types=None, groups=None, obstacles=None, config_file=N
6969
# construct forces
7070
self.forces = self.make_forces()
7171

72+
# configからtypeとsceneを抽出し、辞書を構築
73+
def get_scene_configs(self, types, config):
74+
scene_configs = {}
75+
# setで種類の数だけ実行([0,1,0,2] → [0,1,2])
76+
for i in set(types):
77+
type_config = config.sub_config("types").sub_config(f"{i}").sub_config("scene").config # typeに対応するsceneを抽出
78+
scene_configs[str(i)] = {"scene": type_config} # typeを追加し結果に追加
79+
return scene_configs
80+
7281
def make_forces(self):
7382
"""Construct forces for each pedestrian type and include group forces if enabled."""
7483
force_list = []
@@ -82,10 +91,10 @@ def make_forces(self):
8291
forces.DesiredForce(type_force_config),
8392
forces.SocialForce(type_force_config),
8493
forces.ObstacleForce(type_force_config),
85-
forces.PedRepulsiveForce(type_force_config),
86-
forces.SpaceRepulsiveForce(type_force_config),
94+
#forces.PedRepulsiveForce(type_force_config),
95+
#forces.SpaceRepulsiveForce(type_force_config),
8796
]
88-
print("Force list after adding individual forces:", force_list)
97+
print("Force list:", force_list)
8998

9099
# グループ関連の力を有効化する場合
91100
if self.config("scene", {}).get("enable_group", False):

PySocialForce-master/pysocialforce/utils/logging.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55

66
# Create a custom logger
77
logger = logging.getLogger("root")
8-
logger.setLevel(logging.DEBUG)
8+
#logger.setLevel(logging.DEBUG)
9+
logger.setLevel(logging.WARNING)
910
FORMAT = "%(levelname)s:[%(filename)s:%(lineno)s %(funcName)20s() ] %(message)s"
1011

1112
# Create handlers
1213
c_handler = logging.StreamHandler()
1314
f_handler = logging.FileHandler("file.log")
14-
c_handler.setLevel(logging.DEBUG)
15+
#c_handler.setLevel(logging.DEBUG)
16+
c_handler.setLevel(logging.WARNING)
1517
f_handler.setLevel(logging.WARNING)
1618

1719
# Create formatters and add it to handlers

file.log

Whitespace-only changes.

0 commit comments

Comments
 (0)