Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
db-guided-mrmp
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
rmoan2
db-guided-mrmp
Commits
cb934865
Commit
cb934865
authored
5 months ago
by
rachelmoan
Browse files
Options
Downloads
Patches
Plain Diff
Making traj opt resolver more general
parent
764b2559
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
guided_mrmp/conflict_resolvers/traj_opt_resolver.py
+126
-71
126 additions, 71 deletions
guided_mrmp/conflict_resolvers/traj_opt_resolver.py
with
126 additions
and
71 deletions
guided_mrmp/conflict_resolvers/traj_opt_resolver.py
+
126
−
71
View file @
cb934865
...
@@ -3,34 +3,23 @@ import matplotlib.pyplot as plt
...
@@ -3,34 +3,23 @@ import matplotlib.pyplot as plt
from
matplotlib.patches
import
Circle
,
Rectangle
from
matplotlib.patches
import
Circle
,
Rectangle
from
casadi
import
*
from
casadi
import
*
from
guided_mrmp.conflict_resolvers.local_resolver
import
LocalResolver
class
TrajOptResolver
():
class
TrajOptResolver
(
LocalResolver
):
"""
"""
A class that resolves conflicts using trajectoy optimization.
A class that resolves conflicts using trajectoy optimization.
"""
"""
def
__init__
(
self
,
conflicts
,
all_robots
,
dt
,
robot_radius
,
circle_obstacles
,
def
__init__
(
self
,
num_robots
,
robot_radius
,
starts
,
goals
,
circle_obstacles
,
rectangle_obstacles
,
rectangle_obstacles
,
rob_dist_weight
,
obs_dist_weight
,
time_weight
):
rob_dist_weight
,
obs_dist_weight
,
control_weight
,
time_weight
):
"""
self
.
num_robots
=
num_robots
inputs:
self
.
starts
=
starts
- starts (list): starts for all robots in the traj opt problem
self
.
goals
=
goals
- goals (list): goals for all robots in the traj opt problem
"""
super
.
__init__
(
conflicts
,
all_robots
,
dt
)
self
.
num_robots
=
len
(
all_robots
)
self
.
starts
=
None
self
.
goals
=
None
self
.
circle_obs
=
circle_obstacles
self
.
circle_obs
=
circle_obstacles
self
.
rect_obs
=
rectangle_obstacles
self
.
rect_obs
=
rectangle_obstacles
self
.
rob_dist_weight
=
rob_dist_weight
self
.
rob_dist_weight
=
rob_dist_weight
self
.
obs_dist_weight
=
obs_dist_weight
self
.
obs_dist_weight
=
obs_dist_weight
self
.
control_weight
=
control_weight
self
.
time_weight
=
time_weight
self
.
time_weight
=
time_weight
self
.
robot_radius
=
MX
(
robot_radius
)
self
.
robot_radius
=
MX
(
robot_radius
)
# Set the starts and goals for the robots
self
.
starts
=
[
r
.
current_position
for
r
in
all_robots
]
# the goals should be some point in the near future ...
def
dist
(
self
,
robot_position
,
circle
):
def
dist
(
self
,
robot_position
,
circle
):
"""
"""
Returns the distance between a robot and a circle
Returns the distance between a robot and a circle
...
@@ -58,14 +47,19 @@ class TrajOptResolver(LocalResolver):
...
@@ -58,14 +47,19 @@ class TrajOptResolver(LocalResolver):
def
log_normal_barrier
(
self
,
sigma
,
d
,
c
):
def
log_normal_barrier
(
self
,
sigma
,
d
,
c
):
return
c
*
fmax
(
0
,
2
-
(
d
/
sigma
))
**
2.5
return
c
*
fmax
(
0
,
2
-
(
d
/
sigma
))
**
2.5
def
solve
(
self
,
num_control_intervals
,
initial_guess
):
def
problem_setup
(
self
,
N
,
x_range
,
y_range
):
"""
Solves the trajectory optimization problem for the robots.
TODO: This will not work for generic dynamics. It only works for roomba model.
I don
'
t know how to handle generic dynamics with casadi yet.
"""
"""
Problem setup for the multi-robot collision resolution traj opt problem
inputs:
- N (int): number of control intervals
- x_range (tuple): range of x values
- y_range (tuple): range of y values
N
=
num_control_intervals
outputs:
- problem (dict): dictionary containing the optimization problem
and the decision variables
"""
opti
=
Opti
()
# Optimization problem
opti
=
Opti
()
# Optimization problem
# ---- decision variables --------- #
# ---- decision variables --------- #
...
@@ -75,16 +69,15 @@ class TrajOptResolver(LocalResolver):
...
@@ -75,16 +69,15 @@ class TrajOptResolver(LocalResolver):
y
=
pos
[
1
::
2
,:]
y
=
pos
[
1
::
2
,:]
heading
=
X
[
self
.
num_robots
*
2
:,:]
# heading is the last value
heading
=
X
[
self
.
num_robots
*
2
:,:]
# heading is the last value
circle_obs
=
DM
(
self
.
circle_obs
)
# make the obstacles casadi objects
U
=
opti
.
variable
(
self
.
num_robots
*
2
,
N
)
# control trajectory (v, omega)
U
=
opti
.
variable
(
self
.
num_robots
*
2
,
N
)
# control trajectory (v, omega)
vel
=
U
[
0
::
2
,:]
vel
=
U
[
0
::
2
,:]
omega
=
U
[
1
::
2
,:]
omega
=
U
[
1
::
2
,:]
T
=
opti
.
variable
()
# final time
T
=
opti
.
variable
()
# final time
# ---- obstacle setup ------------ #
# sum up the cost of distance to obstacles
circle_obs
=
DM
(
self
.
circle_obs
)
# make the obstacles casadi objects
# ------ Obstacle dist cost ------ #
# TODO:: Include rectangular obstacles
# TODO:: Include rectangular obstacles
dist_to_other_obstacles
=
0
dist_to_other_obstacles
=
0
for
r
in
range
(
self
.
num_robots
):
for
r
in
range
(
self
.
num_robots
):
...
@@ -92,88 +85,150 @@ class TrajOptResolver(LocalResolver):
...
@@ -92,88 +85,150 @@ class TrajOptResolver(LocalResolver):
for
c
in
range
(
circle_obs
.
shape
[
0
]):
for
c
in
range
(
circle_obs
.
shape
[
0
]):
circle
=
circle_obs
[
c
,
:]
circle
=
circle_obs
[
c
,
:]
d
=
self
.
dist
(
pos
[
2
*
r
:
2
*
(
r
+
1
),
k
],
circle
)
d
=
self
.
dist
(
pos
[
2
*
r
:
2
*
(
r
+
1
),
k
],
circle
)
dist_to_other_obstacles
+=
self
.
apply_quadratic_barrier
(
self
.
robot_radius
+
circle
[
2
]
+
0.5
,
d
,
1
)
dist_to_other_obstacles
+=
self
.
apply_quadratic_barrier
(
2
*
(
self
.
robot_radius
+
circle
[
2
]),
d
,
5
)
# dist_to_other_obstacles += self.log_normal_barrier(5, d, 5)
# ------ Robot dist cost ------ #
dist_to_other_robots
=
0
dist_to_other_robots
=
0
for
k
in
range
(
N
):
for
k
in
range
(
N
):
for
r1
in
range
(
self
.
num_robots
):
for
r1
in
range
(
self
.
num_robots
):
for
r2
in
range
(
self
.
num_robots
):
for
r2
in
range
(
self
.
num_robots
):
if
r1
!=
r2
:
if
r1
!=
r2
:
# print(f"\n{r1} position1 = {pos[2*r1 : 2*(r1+1), k]}")
# print(f"{r2} position2 = {pos[2*r2 : 2*(r2+1), k]}")
# note: using norm 2 here gives an invalid num detected error.
# Must be the sqrt causing an issue
# d = norm_2(pos[2*r1 : 2*(r1+1), k] - pos[2*r2 : 2*(r2+1), k]) - 2*self.robot_radius
d
=
sumsqr
(
pos
[
2
*
r1
:
2
*
(
r1
+
1
),
k
]
-
pos
[
2
*
r2
:
2
*
(
r2
+
1
),
k
])
d
=
sumsqr
(
pos
[
2
*
r1
:
2
*
(
r1
+
1
),
k
]
-
pos
[
2
*
r2
:
2
*
(
r2
+
1
),
k
])
dist_to_other_robots
+=
self
.
apply_quadratic_barrier
(
2
*
self
.
robot_radius
+
.
5
,
d
,
1
)
dist_to_other_robots
+=
self
.
apply_quadratic_barrier
(
2
*
self
.
robot_radius
,
d
,
1
)
# ---- dynamics constraints ---- #
dt
=
T
/
N
# length of a control interval
dt
=
T
/
N
# length of a control interval
# Ensure that the robot moves according to the dynamics
pi
=
[
3.14159
]
*
self
.
num_robots
pi
=
np
.
array
(
pi
)
pi
=
DM
(
pi
)
for
k
in
range
(
N
):
# loop over control intervals
for
k
in
range
(
N
):
# loop over control intervals
dxdt
=
vel
[:,
k
]
*
cos
(
heading
[:,
k
])
dxdt
=
vel
[:,
k
]
*
cos
(
heading
[:,
k
])
dydt
=
vel
[:,
k
]
*
sin
(
heading
[:,
k
])
dydt
=
vel
[:,
k
]
*
sin
(
heading
[:,
k
])
dthetadt
=
omega
[:,
k
]
dthetadt
=
omega
[:,
k
]
opti
.
subject_to
(
x
[:,
k
+
1
]
==
x
[:,
k
]
+
dt
*
dxdt
)
opti
.
subject_to
(
x
[:,
k
+
1
]
==
x
[:,
k
]
+
dt
*
dxdt
)
opti
.
subject_to
(
y
[:,
k
+
1
]
==
y
[:,
k
]
+
dt
*
dydt
)
opti
.
subject_to
(
y
[:,
k
+
1
]
==
y
[:,
k
]
+
dt
*
dydt
)
opti
.
subject_to
(
heading
[:,
k
+
1
]
==
heading
[:,
k
]
+
dt
*
dthetadt
)
opti
.
subject_to
(
heading
[:,
k
+
1
]
==
fmod
(
heading
[:,
k
]
+
dt
*
dthetadt
,
2
*
pi
))
# ------ Control panalty ------ #
# Calculate the sum of squared differences between consecutive heading angles
heading_diff_penalty
=
0
for
k
in
range
(
N
-
1
):
heading_diff_penalty
+=
sumsqr
(
fmod
(
heading
[:,
k
+
1
]
-
heading
[:,
k
]
+
pi
,
2
*
pi
)
-
pi
)
# ------ cost function ------ #
opti
.
minimize
(
self
.
rob_dist_weight
*
dist_to_other_robots
opti
.
minimize
(
self
.
rob_dist_weight
*
dist_to_other_robots
+
self
.
obs_dist_weight
*
dist_to_other_obstacles
+
self
.
obs_dist_weight
*
dist_to_other_obstacles
+
self
.
time_weight
*
T
)
+
self
.
time_weight
*
T
+
self
.
control_weight
*
heading_diff_penalty
)
# ---
v and omega
constraints --- #
# ---
--- control
constraints ---
---
#
for
k
in
range
(
N
):
for
k
in
range
(
N
):
for
r
in
range
(
self
.
num_robots
):
for
r
in
range
(
self
.
num_robots
):
opti
.
subject_to
(
sumsqr
(
vel
[
r
,
k
])
<=
0.2
**
2
)
opti
.
subject_to
(
sumsqr
(
vel
[
r
,
k
])
<=
0.2
**
2
)
opti
.
subject_to
(
sumsqr
(
omega
[
r
,
k
])
<=
0.
1
**
2
)
opti
.
subject_to
(
sumsqr
(
omega
[
r
,
k
])
<=
0.
2
**
2
)
# ---
position constraints
--- #
# ---
--- bound x, y, and time ---
--- #
opti
.
subject_to
(
opti
.
bounded
(
0
,
x
,
10
))
opti
.
subject_to
(
opti
.
bounded
(
x_range
[
0
],
x
,
x_range
[
1
]
))
opti
.
subject_to
(
opti
.
bounded
(
0
,
y
,
10
))
opti
.
subject_to
(
opti
.
bounded
(
y_range
[
0
],
y
,
y_range
[
1
]
))
opti
.
subject_to
(
opti
.
bounded
(
0
,
T
,
100
))
# ----
start/go
al conditions ------
--
# ----
-- initi
al conditions ------
#
for
r
in
range
(
self
.
num_robots
):
for
r
in
range
(
self
.
num_robots
):
# opti.subject_to(vel[r, 0]==0)
opti
.
subject_to
(
pos
[
2
*
r
:
2
*
(
r
+
1
),
0
]
==
self
.
starts
[
r
])
opti
.
subject_to
(
heading
[
r
,
0
]
==
self
.
starts
[
r
][
2
])
opti
.
subject_to
(
pos
[
2
*
r
:
2
*
(
r
+
1
),
0
]
==
self
.
starts
[
r
][
0
:
2
])
opti
.
subject_to
(
pos
[
2
*
r
:
2
*
(
r
+
1
),
-
1
]
==
self
.
goals
[
r
])
opti
.
subject_to
(
pos
[
2
*
r
:
2
*
(
r
+
1
),
-
1
]
==
self
.
goals
[
r
])
# ---- misc. constraints ----------
return
{
'
opti
'
:
opti
,
'
X
'
:
X
,
'
T
'
:
T
}
opti
.
subject_to
(
opti
.
bounded
(
0
,
T
,
100
))
# ---- initial values for solver ---
def
solve_optimization_problem
(
self
,
problem
,
initial_guesses
=
None
,
solver_options
=
None
):
opti
.
set_initial
(
T
,
20
)
opti
=
problem
[
'
opti
'
]
if
initial_guess
is
not
None
:
if
initial_guesses
:
opti
.
set_initial
(
pos
,
initial_guess
)
for
param
,
value
in
initial_guesses
.
items
():
print
(
f
"
param =
{
param
}
"
)
# ---- solve NLP ------
print
(
f
"
value =
{
value
}
"
)
opti
.
solver
(
"
ipopt
"
)
# set numerical backend
opti
.
set_initial
(
problem
[
param
],
value
)
sol
=
opti
.
solve
()
# actual solve
# Set numerical backend, with options if provided
if
solver_options
:
opti
.
solver
(
'
ipopt
'
,
solver_options
)
else
:
opti
.
solver
(
'
ipopt
'
)
try
:
sol
=
opti
.
solve
()
# actual solve
status
=
'
succeeded
'
except
:
sol
=
None
status
=
'
failed
'
results
=
{
'
status
'
:
status
,
'
solution
'
:
sol
,
}
if
sol
:
for
var_name
,
var
in
problem
.
items
():
if
var_name
!=
'
opti
'
:
results
[
var_name
]
=
sol
.
value
(
var
)
return
results
def
solve
(
self
,
N
,
x_range
,
y_range
,
initial_guesses
):
"""
Setup and solve a multi-robot traj opt problem
# print(f"pos = {opti.debug.value(pos[2:4,:])}")
input:
- N (int): the number of control intervals
- x_range (tuple):
- y_range (tuple):
"""
problem
=
self
.
problem_setup
(
N
,
x_range
,
y_range
)
results
=
self
.
solve_optimization_problem
(
problem
,
initial_guesses
)
return
sol
,
pos
X
=
results
[
'
X
'
]
sol
=
results
[
'
solution
'
]
def
get_local_controls
(
self
):
# Extract the values that we want from the optimizer's solution
pos
=
X
[:
self
.
num_robots
*
2
,:]
x_vals
=
pos
[
0
::
2
,:]
y_vals
=
pos
[
1
::
2
,:]
theta_vals
=
X
[
self
.
num_robots
*
2
:,:]
return
sol
,
pos
,
x_vals
,
y_vals
,
theta_vals
def
get_local_controls
(
self
,
controls
):
"""
Get the local controls for the robots in the conflict
"""
l
=
self
.
num_robots
final_trajs
=
[
None
]
*
l
for
c
in
self
.
conflicts
:
for
c
in
self
.
conflicts
:
# Get the robots involved in the conflict
# Get the robots involved in the conflict
robots
=
[
self
.
all_robots
[
r
.
label
]
for
r
in
c
]
robots
=
[
self
.
all_robots
[
r
.
label
]
for
r
in
c
]
robot_positions
=
[
r
.
current_position
for
r
in
robots
]
# Solve the trajectory optimization problem
# Solve the trajectory optimization problem
initial_guess
=
None
initial_guess
=
None
sol
,
x_opt
=
self
.
solve
(
10
,
initial_guess
)
sol
,
x_opt
,
vels
,
omegas
,
xs
,
ys
=
self
.
solve
(
20
,
initial_guess
)
pos_vals
=
np
.
array
(
sol
.
value
(
x_opt
))
# Update the controls for the robots
# Update the controls for the robots
for
r
,
pos
in
zip
(
robots
,
x_opt
):
for
r
,
vel
,
omega
,
x
,
y
in
zip
(
robots
,
vels
,
omegas
,
xs
,
ys
):
r
.
next_control
=
r
.
tracker
.
get_next_control
(
pos
)
controls
[
r
.
label
]
=
[
vel
,
omega
]
final_trajs
[
r
.
label
]
=
[
x
,
y
]
return
controls
,
final_trajs
def
plot_paths
(
self
,
x_opt
):
def
plot_paths
(
self
,
x_opt
):
fig
,
ax
=
plt
.
subplots
()
fig
,
ax
=
plt
.
subplots
()
...
@@ -204,8 +259,8 @@ class TrajOptResolver(LocalResolver):
...
@@ -204,8 +259,8 @@ class TrajOptResolver(LocalResolver):
ax
.
legend
()
ax
.
legend
()
ax
.
set_aspect
(
'
equal
'
,
'
box
'
)
ax
.
set_aspect
(
'
equal
'
,
'
box
'
)
plt
.
ylim
(
0
,
1
0
)
plt
.
ylim
(
0
,
64
0
)
plt
.
xlim
(
0
,
1
0
)
plt
.
xlim
(
0
,
48
0
)
plt
.
title
(
'
Robot Paths
'
)
plt
.
title
(
'
Robot Paths
'
)
plt
.
grid
(
False
)
plt
.
grid
(
False
)
plt
.
show
()
plt
.
show
()
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment