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
1f2530ec
Commit
1f2530ec
authored
2 months ago
by
rachelmoan
Browse files
Options
Downloads
Patches
Plain Diff
Updated print intermdiate callback function in optimizer
parent
540bd5a6
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
guided_mrmp/optimizer.py
+45
-26
45 additions, 26 deletions
guided_mrmp/optimizer.py
with
45 additions
and
26 deletions
guided_mrmp/optimizer.py
+
45
−
26
View file @
1f2530ec
import
casadi
as
ca
class
Optimizer
:
def
__init__
(
self
,
problem
):
self
.
problem
=
problem
...
...
@@ -7,7 +9,6 @@ class Optimizer:
X
=
self
.
problem
[
'
X
'
]
U
=
self
.
problem
[
'
U
'
]
if
initial_guesses
:
for
param
,
value
in
initial_guesses
.
items
():
...
...
@@ -23,44 +24,59 @@ class Optimizer:
opti
.
solver
(
'
ipopt
'
)
def
print_intermediates_callback
(
i
):
# print the current value of the objective function
print
(
"
Iteration:
"
,
i
,
"
Current cost cost:
"
,
opti
.
debug
.
value
(
self
.
problem
[
'
cost
'
]))
print
(
"
Iteration:
"
,
i
,
"
Current robot cost:
"
,
opti
.
debug
.
value
(
self
.
problem
[
'
dist_to_other_robots
'
]))
# print("Iteration:", i, "Current obstacle cost:", opti.debug.value(self.problem['obs_cost']))
# print("Iteration:", i, "Current control cost:", opti.debug.value(self.problem['control_cost']))
# print("Iteration:", i, "Current time cost:", opti.debug.value(self.problem['time_cost']))
# print("Iteration:", i, "Current goal cost:", opti.debug.value(self.problem['goal_cost']))
# print("Iteration:", i, "Current solution:", opti.debug.value(X), opti.debug.value(U))
X_debug
=
opti
.
debug
.
value
(
X
)
U_debug
=
opti
.
debug
.
value
(
U
)
#
X_debug = opti.debug.value(X)
#
U_debug = opti.debug.value(U)
# plot the state and the control
# split a figure in half. The left side will show the positions, the right side will plot the controls
# X[i*3, :] is the ith robot's x position, X[i*3+1, :] is the y position, X[i*3+2, :] is the heading
# U[i*2, :] is the ith robot's linear velocity, U[i*2+1, :] is the ith robot's angular velocity
import
matplotlib.pyplot
as
plt
fig
,
axs
=
plt
.
subplots
(
1
,
2
,
figsize
=
(
12
,
6
))
for
j
in
range
(
X_debug
.
shape
[
0
]
//
3
):
axs
[
0
].
plot
(
X_debug
[
j
*
3
,
:],
X_debug
[
j
*
3
+
1
,
:],
label
=
f
"
Robot
{
j
}
"
)
axs
[
0
].
scatter
(
X_debug
[
j
*
3
,
0
],
X_debug
[
j
*
3
+
1
,
0
],
color
=
'
green
'
)
axs
[
0
].
scatter
(
X_debug
[
j
*
3
,
-
1
],
X_debug
[
j
*
3
+
1
,
-
1
],
color
=
'
red
'
)
axs
[
0
].
set_title
(
"
Robot Positions
"
)
axs
[
0
].
set_xlabel
(
"
X
"
)
axs
[
0
].
set_ylabel
(
"
Y
"
)
axs
[
0
].
legend
()
axs
[
1
].
plot
(
U_debug
[
j
*
2
,
:],
label
=
f
"
Robot
{
j
}
"
)
axs
[
1
].
plot
(
U_debug
[
j
*
2
+
1
,
:],
label
=
f
"
Robot
{
j
}
"
)
axs
[
1
].
set_title
(
"
Robot Controls
"
)
axs
[
1
].
set_xlabel
(
"
Time
"
)
axs
[
1
].
set_ylabel
(
"
Control
"
)
axs
[
1
].
legend
()
plt
.
show
()
#
import matplotlib.pyplot as plt
#
fig, axs = plt.subplots(1, 2, figsize=(12, 6))
#
for j in range(X_debug.shape[0]//3):
#
axs[0].plot(X_debug[j*3, :], X_debug[j*3+1, :], label=f"Robot {j}")
#
axs[0].scatter(X_debug[j*3, 0], X_debug[j*3+1, 0], color='green')
#
axs[0].scatter(X_debug[j*3, -1], X_debug[j*3+1, -1], color='red')
#
axs[0].set_title("Robot Positions")
#
axs[0].set_xlabel("X")
#
axs[0].set_ylabel("Y")
#
axs[0].legend()
#
axs[1].plot(U_debug[j*2, :], label=f"Robot {j}
velocity
")
#
axs[1].plot(U_debug[j*2+1, :], label=f"Robot {j}
omega
")
#
axs[1].set_title("Robot Controls")
#
axs[1].set_xlabel("Time")
#
axs[1].set_ylabel("Control")
#
axs[1].legend()
#
plt.show()
# opti.callback(print_intermediates_callback)
# sol = opti.solve()
# print("/solving optimization problem")
# import time
# start = time.time()
try
:
sol
=
opti
.
solve
()
# actual solve
status
=
'
succeeded
'
except
:
sol
=
None
status
=
'
failed
'
# end = time.time()
# print(f"Time taken to solve optimization problem = {end - start}")
results
=
{
'
status
'
:
status
,
...
...
@@ -78,10 +94,13 @@ class Optimizer:
if
sol
:
for
var_name
,
var
in
self
.
problem
.
items
():
if
var_name
!=
'
opti
'
:
results
[
var_name
]
=
sol
.
value
(
var
)
try
:
results
[
var_name
]
=
sol
.
value
(
var
)
except
:
results
[
var_name
]
=
var
opti
=
self
.
problem
[
'
opti
'
]
lam_g
=
sol
.
value
(
opti
.
lam_g
)
results
[
'
lam_g
'
]
=
lam_g
return
results
,
sol
return
results
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