-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteractions.py
111 lines (67 loc) · 3.16 KB
/
interactions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import numpy as np
from dynamics_utils import Phase
from math_utils import math_utils
'''
集群规则定义,尽量可扩展
'''
M = math_utils()
class interactions:
def RepulsionLin(self, Phase, V_Rep_l, p_l, R_0_l, WhichAgent, Dim_l, normlize):
'''
斥力作用函数
:Phase: 状态空间
:
'''
n = 0
OutputVelocity = np.zeros(3)
AgentsCoordinates = Phase.Coordinates[WhichAgent]
# print(AgentsCoordinates)
for i in range(Phase.NumberOfAgents):
if i == WhichAgent:
continue
NeighbourCoordinates = Phase.Coordinates[i]
# print(NeighbourCoordinates)
DifferenceVector = M.VectDifference(AgentsCoordinates, NeighbourCoordinates)
if Dim_l == 2:
DifferenceVector[2] = 0
DistanceFromNeighbour = M.VectAbs(DifferenceVector)
# print("DistancFromNeighbour=", DistanceFromNeighbour)
if DistanceFromNeighbour >= R_0_l:
continue
n += 1
DifferenceVector = M.UnitVect(DifferenceVector)
# print(DifferenceVector)
DifferenceVector = M.MultiplicateWithScalar(DifferenceVector, M.SigmoidLin(DistanceFromNeighbour, p_l, V_Rep_l, R_0_l), Dim_l)
# print(DifferenceVector)
OutputVelocity = M.VectSum(OutputVelocity, DifferenceVector)
# print(OutputVelocity)
if (normlize and n) > 1:
length = M.VectAbs(OutputVelocity) / n
OutputVelocity = M.UnitVect(OutputVelocity)
OutputVelocity = M.MultiplicateWithScalar(OutputVelocity, length, Dim_l)
return OutputVelocity
def FrictionLinSqrt(self, Phase, C_Frict_l, V_Frict_l, Acc_l, p_l, R_0_l, WhichAgent, Dim_l): # 吸引力
OutputVelocity = np.zeros(3)
AgentsCoordinates = Phase.Coordinates[WhichAgent]
AgentVelocity = Phase.Velocities[WhichAgent]
for i in range(Phase.NumberOfAgents):
if i == WhichAgent:
continue
# 计算邻居距离
NeighbourCoordinates = Phase.Coordinates[i]
DifferenceVector = M.VectDifference(NeighbourCoordinates, AgentsCoordinates)
DistanceFromNeighbour = M.VectAbs(DifferenceVector)
# 计算和邻居的速度差
NeiboursVelocity = Phase.Velocities[i]
DifferenceVector = M.VectDifference(NeiboursVelocity, AgentVelocity)
if 2 == Dim_l:
DifferenceVector[2] = 0
VelDiff = M.VectAbs(DifferenceVector)
DifferenceVector = M.UnitVect(DifferenceVector)
MaxVelDiff = max(V_Frict_l, M.VelDecayLinSqrt(DistanceFromNeighbour, p_l, Acc_l, VelDiff, R_0_l))
if VelDiff > MaxVelDiff:
DifferenceVector = M.MultiplicateWithScalar(DifferenceVector, C_Frict_l * (VelDiff-MaxVelDiff), Dim_l)
# print(OutputVelocity)
# print(DifferenceVector)
OutputVelocity = M.VectSum(OutputVelocity, DifferenceVector)
return OutputVelocity