1

My intention is to pass the initial guess as an argument of a function instead of directly defining it in the body of the code.

1)Is there a way to do this without getting: TypeError: cannot unpack non-iterable int object

Also, my additional goal is to use this function to iterate over different initial guesses which also produces a float working when defining for example: initial_guess = [8, 0.1], [9, 0.1], [10, 0.1], [11, 0.1] and doing:

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)Is there a way to achieve iteration of various floating parameters for initial_guess list values?

Please note that the optimal control function ocp takes initial_guess as a list in the form initial_guess = [f, g], where f, g floats or integers.

# 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)
TomGiavas
  • 31
  • 4

0 Answers0