• Community Moderator

Hi Mustafa,

could you please specify what is the difficulty you are dealing with? What is the model you are trying to implement? Which two outputs are you trying to extract from your objective function?

Best regards
Jonasz

The main difficulty, I can set a boolean variable for acceleration

acc = cvx.Variable(N-1, boolean=True)

which true accelerates false don't.

But for steering angle how I can model turn left, turn right or do nothing. How should I define it?

The main model works like the below:

    if acc: #output from Gurobi( True or False)        moved = True        player_car.move_forward()    # if angle_left:    #     player_car.rotate(left=True)     # if angle_right:    #     player_car.rotate(right=True)

So when writing vehicle equations should I consider both options in the equation?

    for t in range(N-1): # look ahead        dt = t//15 + 1 # let time get coarser further in the look ahead        playerVel+=playerAcc*acc[t]*dt        x += playerVel*math.cos(angle)*angle_left[t]*dt + playerVel*math.cos(angle)*angle_right[t]*dt # update x position        y += playerVel*math.sin(angle)*angle_left[t]*dt + playerVel*math.sin(angle)*angle_right[t]*dt # update x position
• Community Moderator

Hi Mustafa,

Could you please share a formulation of the mathematical model (LP, MIP, ...) you are trying to solve? Rather than code, a mathematical formulation would be useful.

An angle, in which a vehicle should move, could be modeled, for example, by a continuous or integer variable in the range $$[0,180]$$. But without understanding the full model I can't give you any further guidance.

Best regards
Jonasz

Hello Jonasz,

Thanks for the help.

• Community Moderator

If my understanding of your problem is correct, these equations are used to update the position of a car. I also infer that you want Gurobi to tell you the acceleration and vehicle's angle. What we need to know is

1. What is the overall aim of this code, i.e. what is the objective? What should be optimized in each step?
2. Which constraints should be taken into account when deciding about the acceleration and angle in each step?

Best regards
Jonasz

I have a red car below and would like to reach the finish line.

Objective function= Minimize(x_car-X_finish)

For constraints:

• green vehicles.

def solve(playerx,playery,playervel_hiz,angle,computercar_x,computercar_y):    playerAcc    = 0.1#  0.1   # players  accleration      # unpack path variables    y = path[:,0]    vy = path[:,1]    x=path[:,2]    playerVel=path[:,3]    c = [] # init constraint list    #print('ilk cccc',c)    #c += [y <= RightRoadBorder, y >= LeftRoadBorder] # constraints for highway boundaries    #c += [y <= secondLaneBorder] # constraints for highway boundaries       #c += [y[0] == playery,  vy[0] == playerVel] # initial conditions vx[0]==playerVel,x[0]==playerx,    c += [x[0] == playerx,  playerVel[0] == playervel_hiz] # initial conditions vx[0]==playerVel,x[0]==playerx,      obj = 0    #y = playery    #ys = [y] # init x list    for t in range(N-1): # look ahead        dt = t//15 + 1 # let time get coarser further in the look ahead        c += [playerVel[t+1] == playerVel[t] + playerAcc*acc[t]*dt]        c += [y[t + 1] ==  y[t] + playerVel[t]*math.sin(angle)*dt ] # add y constraint, dy/dt = a        c += [x[t + 1] ==  x[t] + playerVel[t]*math.cos(angle)*dt ] # add y constraint, dy/dt = a        #vehicle_c, dist = getVehicleConstraintsDistance(x, y[t+1], computercar_x,computercar_y) # add pipe constraints        #c += vehicle_c        #c += [playerVel<=100]        #obj += dist    objective = cvx.Maximize( cvx.sum(acc))#cvx.Maximize(playerVel)#+cvx.Minimize(cvx.sum(angle_list))    prob = cvx.Problem(objective, c)

Now the model is like this, how I should add angle to the objective or how I should get output from it?

• Community Moderator

Hi Mustafa,

I am afraid I still have too little information to help you. You could try writing down the exact mathematical model to be solved with each iteration, before updating the values for v, x, y. We might be then able to help you.

You could also look for inspiration e.g. here. Maybe their approaches could serve as inspiration.

Also, to query the solution via cvxpy you could first optimize and - assuming a solution is found - iterate over your variables and query their value parameter. You can find more information here.

Best regards
Jonasz

Hello Jonasz,

I have shared mathematical equations in the previous post.

I think you are asking  x_dot=Ax +bu form of equations but do I need this?

Because currently, my model works for acceleration with the current setup. I am not sure how to get output for the steering angle!

Thanks

• Community Moderator

Hi Mustafa,

Some more questions and thoughts:

1) Why do you model angles as binary variables?

angle_list=cvx.Variable(N-1, boolean=True)

Don't they have to take some other value, for example between 0 and 180 degrees or 0 and 2*pi radians?

2) Gurobi allows for the inclusion of trigonometric functions (for example, sine). To be exact, a piecewise linearization of the function is added, but you can control its granularity. I am no expert in cvxpy, and hence can't tell you how to access this Gurobi-specific constructor from cvxpy.

3) Once you include variables for angle and the appropriate constraints (as discussed in points 1) and 2)) you should be able to optimize and then query each variable and its value (assuming the optimization is successful). You can find an example in one of my posts above.

Hope this helps.

Best regards
Jonasz

1) Why do you model angles as binary variables?

left_angle=cvx.Variable(N-1, boolean=True)right_angle=cvx.Variable(N-1, boolean=True)

My main vehicle model changes angle 1 by one so I was thinking to get True or False for angle and if the left angle is true rotate one degree left or if the right angle is true, rotate one degree right.

    if Acc:        moved = True        player_car.move_forward()    if left_angle:        player_car.rotate(left=True)    if right_angle:        player_car.rotate(right=True)
        c += [playerVel[t+1] == playerVel[t] + playerAcc*acc[t]*dt]        c += [angle == angle + 1*left_angle[t] - 1*right_angle[t] ]        #angle=math.radians(angle)        c += [y[t + 1] ==  y[t] + playerVel[t]*math.sin(angle)*dt ] # add y constraint, dy/dt = a        c += [x[t + 1] ==  x[t] + playerVel[t]]*math.cos(angle)*dt ] # add y constraint, dy/dt = a