我正在将手推车和杆子仿真与python 3.7和Julia 1.2进行比较。在python中,仿真被编写为类对象,如下所示,而在Julia中,它只是一个函数。我得到一致的0.2秒时间来解决使用Julia的问题,这比python慢得多。我对朱莉娅的理解不够深刻,无法理解为什么。我的猜测是它与编译或建立循环的方式有关。
import math
import random
from collections import namedtuple
RAD_PER_DEG = 0.0174533
DEG_PER_RAD = 57.2958
State = namedtuple('State', 'x x_dot theta theta_dot')
class CartPole:
""" Model for the dynamics of an inverted pendulum
"""
def __init__(self):
self.gravity = 9.8
self.masscart = 1.0
self.masspole = 0.1
self.length = 0.5 # actually half the pole's length
self.force_mag = 10.0
self.tau = 0.02 # seconds between state updates
self.x = 0
self.x_dot = 0
self.theta = 0
self.theta_dot = 0
@property
def state(self):
return State(self.x, self.x_dot, self.theta, self.theta_dot)
def reset(self, x=0, x_dot=0, theta=0, theta_dot=0):
""" Reset the model of a cartpole system to it's initial conditions
" theta is in radians
"""
self.x = x
self.x_dot = x_dot
self.theta = theta
self.theta_dot = theta_dot
def step(self, action):
""" Move the state of the cartpole simulation forward one time unit
"""
total_mass = self.masspole + self.masscart
pole_masslength = self.masspole * self.length
force = self.force_mag if action else -self.force_mag
costheta = math.cos(self.theta)
sintheta = math.sin(self.theta)
temp = (force + pole_masslength * self.theta_dot ** 2 * sintheta) / total_mass
# theta acceleration
theta_dotdot = (
(self.gravity * sintheta - costheta * temp)
/ (self.length *
(4.0/3.0 - self.masspole * costheta * costheta /
total_mass)))
# x acceleration
x_dotdot = temp - pole_masslength * theta_dotdot * costheta / total_mass
self.x += self.tau * self.x_dot
self.x_dot += self.tau * x_dotdot
self.theta += self.tau * self.theta_dot
self.theta_dot += self.tau * theta_dotdot
return self.state
Run Code Online (Sandbox Code Playgroud)
为了运行模拟,使用了以下代码
from cartpole import CartPole
import time
cp = CartPole()
start = time.time()
for i in range(100000):
cp.step(True)
end = time.time()
print(end-start)
Run Code Online (Sandbox Code Playgroud)
朱莉娅代码是
function cartpole(state, action)
"""Cart and Pole simulation in discrete time
Inputs: cartpole( state, action )
state: 1X4 array [cart_position, cart_velocity, pole_angle, pole_velocity]
action: Boolean True or False where true is a positive force and False is a negative force
"""
gravity = 9.8
masscart = 1.0
masspole = 0.1
l = 0.5 # actually half the pole's length
force_mag = 10.0
tau = 0.02 # seconds between state updates
# x = 0
# x_dot = 0
# theta = 0
# theta_dot = 0
x = state[1]
x_dot = state[2]
theta = state[3]
theta_dot = state[4]
total_mass = masspole + masscart
pole_massl = masspole * l
if action == 0
force = force_mag
else
force = -force_mag
end
costheta = cos(theta)
sintheta = sin(theta)
temp = (force + pole_massl * theta_dot^2 * sintheta) / total_mass
# theta acceleration
theta_dotdot = (gravity * sintheta - costheta * temp)/ (l *(4.0/3.0 - masspole * costheta * costheta / total_mass))
# x acceleration
x_dotdot = temp - pole_massl * theta_dotdot * costheta / total_mass
x += tau * x_dot
x_dot += tau * x_dotdot
theta += tau * theta_dot
theta_dot += tau * theta_dotdot
new_state = [x x_dot theta theta_dot]
return new_state
end
Run Code Online (Sandbox Code Playgroud)
运行代码是
@time include("cartpole.jl")
function run_sim()
"""Runs the cartpole simulation
No inputs or ouputs
Use with @time run_sim() for timing puposes.
"""
state = [0 0 0 0]
for i = 1:100000
state = cartpole( state, 0)
#print(state)
#print("\n")
end
end
@time run_sim()
Run Code Online (Sandbox Code Playgroud)
您的Python版本在我的笔记本电脑上耗时0.21秒。以下是同一系统上原始Julia版本的计时结果:
julia> @time run_sim()
0.222335 seconds (654.98 k allocations: 38.342 MiB)
julia> @time run_sim()
0.019425 seconds (100.00 k allocations: 10.681 MiB, 37.52% gc time)
julia> @time run_sim()
0.010103 seconds (100.00 k allocations: 10.681 MiB)
julia> @time run_sim()
0.012553 seconds (100.00 k allocations: 10.681 MiB)
julia> @time run_sim()
0.011470 seconds (100.00 k allocations: 10.681 MiB)
julia> @time run_sim()
0.025003 seconds (100.00 k allocations: 10.681 MiB, 52.82% gc time)
Run Code Online (Sandbox Code Playgroud)
第一次运行包括JIT编译,大约需要0.2s,之后每次运行需要10-20ms。这分解为约10ms的实际计算时间和约10s的每四个调用触发一次的垃圾收集时间。这意味着Julia的速度比Python快10到20倍,不包括JIT编译时间,这对于直接移植来说还不错。
为什么在基准测试时不计算JIT时间?因为您实际上并不关心运行诸如基准之类的快速程序所花费的时间。您正在安排一些小的基准测试问题,以推断在速度确实很重要的情况下运行较大问题所需的时间。JIT编译时间与您要编译的代码量成正比,而不与问题的大小成正比。因此,当解决您实际上关心的较大问题时,JIT编译仍将仅花费0.2s,这对于大型问题而言执行时间可以忽略不计。
现在,让我们看看如何使Julia代码更快。这实际上非常简单:在状态中使用元组而不是行向量。因此,将状态初始化为state = (0, 0, 0, 0),然后类似地更新状态:
new_state = (x, x_dot, theta, theta_dot)
Run Code Online (Sandbox Code Playgroud)
就是这样,否则代码是相同的。对于此版本,计时为:
julia> @time run_sim()
0.132459 seconds (479.53 k allocations: 24.020 MiB)
julia> @time run_sim()
0.008218 seconds (4 allocations: 160 bytes)
julia> @time run_sim()
0.007230 seconds (4 allocations: 160 bytes)
julia> @time run_sim()
0.005379 seconds (4 allocations: 160 bytes)
julia> @time run_sim()
0.008773 seconds (4 allocations: 160 bytes)
Run Code Online (Sandbox Code Playgroud)
第一次运行仍包含JIT时间。现在,后续运行时间为5-10毫秒,比Python版本快25-40倍。请注意,几乎没有分配-固定数量的较小分配仅用于返回值,如果从循环中的其他代码中调用了GC,则不会触发GC。
小智 2
标准性能提示适用:https://docs.julialang.org/en/v1/manual/performance-tips/index.html 特别是,使用点来避免分配和熔断循环。另外,对于这种小数组计算,请考虑使用https://github.com/JuliaArrays/StaticArrays.jl,它更快
| 归档时间: |
|
| 查看次数: |
96 次 |
| 最近记录: |