1

我的意图是将初始猜测作为函数的参数传递,而不是直接在代码主体中定义它。

1)有没有办法做到这一点而不得到:TypeError:无法解压不可迭代的int对象

另外,我的另一个目标是使用这个函数来迭代不同的初始猜测,这也会在定义例如时产生一个浮点工作: initial_guess = [8, 0.1], [9, 0.1], [10, 0.1], [11, 0.1]和做:

for i in initial_guess:
    ...
    ...
    result1 = opt.solve_ocp(
        vehicle, horizon, x0, quad_cost, initial_guess[i], log=True,
        minimize_method='trust-constr',
        minimize_options={'finite_diff_rel_step': 0.01},
    )
    ...
    ...
    return(t1, y1, u1)

2)有没有办法实现initial_guess列表值的各种浮动参数的迭代?

请注意,最优控制函数 ocp 将 initial_guess 作为一个列表,形式为 initial_guess = [f, g],其中 f, g 是浮点数或整数。

# Set up the cost functions
Q = np.diag([20, 20, 0.01])       # keep lateral error low
R = np.diag([10, 10])            # minimize applied inputs
quad_cost = opt.quadratic_cost(vehicle, Q, R, x0=xf, u0=uf)

# Define the time horizon (and spacing) for the optimization
horizon = np.linspace(0, Tf, Tf, endpoint=True)

# Provide an intial guess (will be extended to entire horizon)
#bend_left = [8, 0.01] # slight left veer


########################################################################################################################
def Approach1(Velocity_guess, Steer_guess):
    # Turn on debug level logging so that we can see what the optimizer is doing
    logging.basicConfig(
        level=logging.DEBUG, filename="steering-integral_cost.log",
        filemode='w', force=True)

    #constraints = [ opt.input_range_constraint(vehicle, [8, -0.1], [12, 0.1]) ]
    
    initial_guess = [Velocity_guess, Steer_guess]

    # Compute the optimal control, setting step size for gradient calculation (eps)
    start_time = time.process_time()
    result1 = opt.solve_ocp(
        vehicle, horizon, x0, quad_cost, initial_guess, log=True,
        minimize_method='trust-constr',
        minimize_options={'finite_diff_rel_step': 0.01},
    )
    print("* Total time = %5g seconds\n" % (time.process_time() - start_time))

    # If we are running CI tests, make sure we succeeded
    if 'PYCONTROL_TEST_EXAMPLES' in os.environ:
        assert result1.success

    # Extract and plot the results (+ state trajectory)
    t1, u1 = result1.time, result1.inputs
    t1, y1 = ct.input_output_response(vehicle, horizon, u1, x0)

    Final_x_deviation = xf[0] - y1[0][len(y1[0])-1]
    Final_y_deviation = xf[1] - y1[1][len(y1[1])-1]
    V_variation = uf[0] - u1[0][len(u1[0])-1]
    Angle_Variation = uf[1] - u1[1][len(u1[1])-1]

    plot_results(t1, y1, u1, xf, uf, Tf, yf=xf[0:2])
    return(t1, u1, y1)
4

0 回答 0