Skip to content

Commit 167f78f

Browse files
committed
small algorithm optimizations
1 parent d5109dd commit 167f78f

5 files changed

Lines changed: 29 additions & 19 deletions

File tree

bindings/python/examples/floating_frame/franka_mpc.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from scipy.spatial.transform import Rotation as R
1919
import unittest
2020
import os
21+
from ttictoc import tic, toc
2122

2223

2324
np.set_printoptions(linewidth=np.inf)
@@ -140,7 +141,7 @@ def publish_horizon(self, joint_state_msg):
140141

141142
time.sleep(2)
142143

143-
Ns = 20 # number of nodes
144+
Ns = 10 # number of nodes
144145
tf = 0.2 # final time
145146
dt = tf/Ns
146147

@@ -399,8 +400,10 @@ def euler(x, xdot, dt):
399400

400401
cartesian_task.setReference(pose_ref)
401402

402-
ocp.update(x0, u0)
403+
tic()
403404
success = solver.solve(x0, u0)
405+
b = toc()
406+
print(b)
404407

405408
x0 = solver.getStateSolution()
406409
u0 = solver.getControlSolution()

bindings/python/examples/floating_frame/franka_mpc_real.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import unittest
1616
import os
1717
from utils import *
18+
from ttictoc import tic, toc
1819

1920

2021
np.set_printoptions(linewidth=np.inf)
@@ -258,10 +259,10 @@ def publish(self, joint_state_msg:JointState):
258259

259260

260261
print("Initing solver...")
261-
solver = swSQP(ocp)
262+
solver = pysot.swSQP(ocp)
262263
solver.getOptions().max_iters = 10
263264
solver.getOptions().verbose = False
264-
solver.getOptions().line_search_strategy = 2
265+
solver.getOptions().line_search_strategy = 0
265266
solver.getOptions().beta = 1e-2
266267
solver.getOptions().min_abs_delta_solution = 1e-3
267268
solver.init()
@@ -287,7 +288,7 @@ def publish(self, joint_state_msg:JointState):
287288

288289

289290
print("-"*100)
290-
291+
# b = toc()
291292
msg = JointState()
292293
msg.name = model.getJointNames()
293294
# print(msg.name)
@@ -296,8 +297,10 @@ def publish(self, joint_state_msg:JointState):
296297
cartesian_task.setReference(node.pose_ref.copy())
297298

298299
# x0[0] = node.state
299-
ocp.update(x0, u0)
300+
# tic()
300301
success = solver.solve(x0, u0)
302+
# b = toc()
303+
# print(b)
301304

302305
x0 = solver.getStateSolution()
303306
u0 = solver.getControlSolution()

include/OpenSoT/solvers/swSQP.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ class swSQP{
192192
std::vector<Eigen::VectorXd> _x0, _u0;
193193
std::vector<Eigen::VectorXd> _x0_candidate, _u0_candidate;
194194

195+
Eigen::VectorXd _dx0; //initial delta state constraint (_dx0 = 0)
196+
197+
195198
};
196199

197200
}

src/oc/EulerVector.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,16 @@ EulerVector::EulerVector(const XBot::ModelInterface& robot,
1919
_dt(dt)
2020
{
2121

22+
_Fx = Eigen::MatrixXd::Identity(_dX.getOutputSize(), _dX.getOutputSize());
23+
_Fu = Eigen::MatrixXd::Identity(_dU.getOutputSize(), _dU.getOutputSize()) * _dt;
24+
2225
_W.setIdentity(dX.getOutputSize(), dX.getOutputSize());
2326

2427
update();
2528
}
2629

2730
void EulerVector::_update()
2831
{
29-
_Fx = Eigen::MatrixXd::Identity(_dX.getOutputSize(), _dX.getOutputSize());
30-
31-
_Fu = Eigen::MatrixXd::Identity(_dU.getOutputSize(), _dU.getOutputSize()) * _dt;
32-
3332
_dXnext = _Fx * _dX + _Fu * _dU + (_Xk.getValue() + _Uk.getValue()*_dt - _Xk_1.getValue());
3433

3534
_A = _dXnext.getM();

src/solvers/swSQP.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,7 @@ bool swSQP::solve(const std::vector<Eigen::VectorXd>& x0, const std::vector<Eige
109109
ddefect_dw[i] = _ocp->stage(i)->stage_ddefect_dw();
110110
}
111111

112-
Eigen::VectorXd dx0(_A[0].cols()); //initial delta state constraint (dx0 = 0)
113-
dx0.setZero();
112+
_dx0.setZero();
114113

115114
for(unsigned int iter = 1; iter <= _opt.max_iters; ++iter)
116115
{
@@ -122,22 +121,22 @@ bool swSQP::solve(const std::vector<Eigen::VectorXd>& x0, const std::vector<Eige
122121
auto iter_start = std::chrono::high_resolution_clock::now();
123122

124123
// relinarize and update qp
125-
_ocp->update(_x0_candidate, _u0_candidate);
126124
linearize();
127125

128126
// solve
129-
if (!_qp_solver->solve(dx0))
127+
if (!_qp_solver->solve(_dx0))
130128
{
131129
std::cout<< "nosolve"<< std::endl;
132130
return false;
133131
}
134132

135133
// first update
136134
step(_stats.alpha);
137-
_ocp->update(_x0_candidate, _u0_candidate);
135+
138136

139137
while(_opt.line_search_strategy!=0 && _stats.alpha >= _opt.alpha_min)
140138
{
139+
_ocp->update(_x0_candidate, _u0_candidate);
141140
if((this->*ls_function)())
142141
{
143142
_stats.line_search_accepted=true;
@@ -146,10 +145,15 @@ bool swSQP::solve(const std::vector<Eigen::VectorXd>& x0, const std::vector<Eige
146145
_stats.alpha /= 2.;
147146
_stats.line_search_iters++;
148147
step(_stats.alpha);
149-
_ocp->update(_x0_candidate, _u0_candidate);
150148

151149

152150
}
151+
if (_opt.line_search_strategy==0)
152+
_ocp->update(_x0_candidate, _u0_candidate);
153+
154+
_x0 = _x0_candidate;
155+
_u0 = _u0_candidate;
156+
153157

154158
// check break criteria on QP solution
155159
if (break_criteria())
@@ -160,10 +164,7 @@ bool swSQP::solve(const std::vector<Eigen::VectorXd>& x0, const std::vector<Eige
160164
break;
161165
}
162166

163-
_x0 = _x0_candidate;
164-
_u0 = _u0_candidate;
165167

166-
_ocp->update(_x0, _u0); //TODO I am not sure about this update, maybe its not needed
167168
_prev_cost = _ocp->cost();
168169
_prev_defect = _ocp->dynamics_defect();
169170
_prev_viol = _ocp->constraint_violation();
@@ -307,6 +308,7 @@ void swSQP::init()
307308

308309
_qp_solver->setStageDynamics(k, A, B, b);
309310
}
311+
_dx0.setZero(_A[0].cols()); //initial delta state constraint (_dx0 = 0)
310312

311313
// --- Cost (always) ---
312314
_H.push_back(Eigen::MatrixXd(_ocp->stage(k)->stack->getStack()[0]->getA().cols(), _ocp->stage(k)->stack->getStack()[0]->getA().cols()));

0 commit comments

Comments
 (0)