如何优化REST API的SpeedFcCat参数,使API_speed与Real_Speed匹配
需求说明
我使用HERE提供的REST API,请求时传入起点、终点坐标以及包含8个数值的SpeedFcCat参数。API返回JSON数据后,我从中提取算法生成的车辆速度曲线(API_speed),并与实际记录的速度曲线(Real_Speed)对比,得到如下数据:
| Real_Speed | API_Speed |
|---|---|
| 16 | 22 |
| 40 | 31 |
| 66 | 58 |
| 64 | 69 |
| 75 | 70 |
| 81 | 76 |
| 76 | 84 |
| 55 | 63 |
SpeedFcCat的8个数值决定API返回的速度曲线,我需要编写程序自动校准这些数值,让Real_Speed和API_speed达到最佳匹配。
现有实现及问题
目前我尝试通过校准SpeedFcCat,让Real_Speed和API_speed的均值差绝对值不超过阈值(比如5),代码如下:
initial_SpeedFcCat = "80,77,70,63,55,49,45,30" params = { 'waypoint0': "48.22696,6.431135", 'waypoint1': "46.19643,-0.07133", "SpeedFcCat": initial_SpeedFcCat, "apiKey": some_key, } response = requests.post(url=url, params=params) data = resp.json()
处理JSON得到API_speed并生成数据框后,执行以下逻辑:
avg_real_speed = df["Real_Speed"].mean() avg_API_speed = df["API_Speed"].mean() step = 0.025 while abs(avg_real_speed - avg_API_speed) > treshold: sign = 1 if avg_API_speed < avg_real_speed else -1 speedFcCat_new *= step params = { 'waypoint0': "48.22696,6.431135", 'waypoint1': "46.19643,-0.07133", "SpeedFcCat": speedFcCat_new, "apiKey": some_key, } response = requests.post(url=url, params=params) data = resp.json()
但这个方法只匹配了均值,无法保证速度曲线的相似性——比如均值相同的两条曲线,波动情况可能完全不同。因此需要优化方案,让SpeedFcCat的校准同时匹配均值和方差,并且要对8个数值进行加权调整,而非用统一步长更新。
内容的提问来源于stack exchange,提问作者Fotis
相关产品推荐
相关产品推荐

