python 路径优化(1)多项式曲线优化

python 路径优化(1)多项式曲线优化

python 路径优化(1)多项式曲线优化
python 路径平滑(2)指定函数曲线优化
python 路径平滑(3)贝塞尔曲线优化

python 路径多项式曲线优化

通过多项式曲线优化路线,让原本曲率不连续的路径变得曲率连续
参考大佬

优点
曲率连续
(代码简单)

缺点
无法固定起点终点的曲率
无法控制最大曲率
在路径规划中无法控制优化出来的路径是否与障碍物碰撞
需要控制多项式次数来调整曲线

代码

import math

import matplotlib.pyplot as plt
import numpy as np

class PolynomialSmooth():
    def fit(self,route_x,route_y,polynomial_times=3):
        z1 = np.polyfit(route_x,route_y,polynomial_times) # 用polynomial_times次多项式拟合
        p1 = np.poly1d(z1)
        return p1

    def smooth(self,route_x,route_y,route_theta=None,polynomial_times=3):
        p1=self.fit(route_x, route_y,polynomial_times=polynomial_times)
        new_route_y=p1(route_x)
        theta_p=p1.deriv(1)
        dy=theta_p(route_x)
        new_route_theta=[]
        for i in dy:
            new_route_theta.append(math.atan2(i,1))
        for i in range(len(route_x)):
            print(route_x[i],new_route_y[i],new_route_theta[i]*180/math.pi)
        return route_x,new_route_y,new_route_theta

测试效果

from polynomial_smooth import PolynomialSmooth
import matplotlib.pyplot as plt
import numpy as np

def new_test():

    x = np.arange(1, 17, 1)
    y = np.array([4.00, 6.40, 8.00, 8.80, 9.22, 9.50, 9.70, 9.86, 10.00, 10.20, 10.32, 10.42, 10.50, 10.55, 10.58, 10.60])
    poly=PolynomialSmooth()

    x,yvals,theta=poly.smooth(x,y,theta,4)


    plot1=plt.plot(x, y, '*',label='original values')
    plot2=plt.plot(x, yvals, 'r',label='polyfit values')
    plt.xlabel('x axis')
    plt.ylabel('y axis')
    plt.legend(loc=4) # 指定legend的位置,读者可以自己help它的用法
    plt.title('polyfitting')
    plt.show()



if __name__ == '__main__':
    new_test()

要使用Python进行RANSAC多项式曲线拟合,可以按照以下步骤进行操作: 1. 导入所需的库: ```python import numpy as np from sklearn.linear_model import RANSACRegressor from sklearn.preprocessing import PolynomialFeatures ``` 2. 准备数据: 准备输入的自变量和因变量数据,例如: ```python x = np.array([1, 2, 3, 4, 5, 6, 7]) y = np.array([3, 5, 7, 9, 11, 13, 15]) ``` 3. 创建多项式特征对象: 使用`PolynomialFeatures`创建多项式特征对象,指定要拟合的多项式的次数。例如,如果要拟合二次曲线,则设置`degree=2`。 ```python poly_features = PolynomialFeatures(degree=2) ``` 4. 生成多项式特征矩阵: 使用多项式特征对象对输入的自变量数据进行转换,生成多项式特征矩阵。 ```python X = poly_features.fit_transform(x.reshape(-1, 1)) ``` 5. 创建RANSACRegressor对象: 使用`RANSACRegressor`创建一个RANSAC回归器对象,并指定线性回归模型作为基础模型。 ```python ransac = RANSACRegressor(base_estimator=LinearRegression()) ``` 6. 拟合数据: 使用RANSAC回归器对多项式特征矩阵和因变量数据进行拟合。 ```python ransac.fit(X, y) ``` 7. 获取拟合结果: 获取RANSAC回归器的拟合结果,包括拟合的多项式系数和内点索引。 ```python inlier_mask = ransac.inlier_mask_ outlier_mask = np.logical_not(inlier_mask) inlier_coefficients = ransac.estimator_.coef_ intercept = ransac.estimator_.intercept_ ``` 8. 预测: 使用拟合得到的多项式模型对新的自变量数据进行预测。 ```python new_x = np.array([8, 9, 10]) new_X = poly_features.transform(new_x.reshape(-1, 1)) predictions = ransac.predict(new_X) ``` 这样就完成了使用Python进行RANSAC多项式曲线拟合的过程。请注意,你可以根据需要调整多项式的次数和其他参数来获得更好的拟合效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值