Skip to content

Commit e84cf6b

Browse files
committed
Change verbose output
1 parent 2a35497 commit e84cf6b

4 files changed

Lines changed: 54 additions & 36 deletions

File tree

bindings/python/examples/floating_frame/pendulum_traj_opt.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107

108108

109109
minu = min_var.create(f"minu{i}", ocp.stage(i).u, ocp.stage(i).du)
110-
minu.setWeight(1e-6 * np.eye(model.nv))
110+
minu.setWeight(1e-9 * np.eye(model.nv))
111111
minus.append(minu)
112112

113113

@@ -118,7 +118,7 @@
118118

119119
# tau_min
120120
tau_lim = DynamicsConstraint(ocp.stage(i).model, ocp.stage(i).dx, ocp.stage(i).du)
121-
tau_lim.setTorqueLimit([10., 10.])
121+
tau_lim.setTorqueLimit([10., 1e-6])
122122
const.append(tau_lim)
123123
ocp.stage(i).stack << tau_lim
124124

@@ -136,10 +136,11 @@
136136
solver = swSQP(ocp)
137137
solver.getOptions().max_iters = 1000
138138
solver.getOptions().verbose = True
139-
solver.getOptions().use_line_search = True
140-
solver.getOptions().beta = 1e-2
139+
solver.getOptions().line_search_strategy = 1
140+
solver.getOptions().beta = 1e-4
141+
solver.getOptions().min_abs_delta_solution = 1e-2
142+
solver.init()
141143
print(f"{solver.getOptions().print()}")
142-
solver.getOptions().min_abs_delta_solution = 1e-3
143144
print("...solver inited!")
144145

145146

include/OpenSoT/solvers/swSQP.h

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class swSQP{
1616
struct stage_statistics
1717
{
1818
double cost;
19+
double constraint_violation;
1920
};
2021

2122
struct statistics
@@ -28,6 +29,7 @@ class swSQP{
2829
std::vector<stage_statistics> stages_statistics;
2930
int iters;
3031
double cost;
32+
double constraint_violation;
3133
double alpha;
3234
int line_search_iters;
3335
bool line_search_accepted;
@@ -43,28 +45,34 @@ class swSQP{
4345
_oss<<"=== swSQP Statistics ==="<<std::endl;
4446
_oss << " iter : " << iters << std::endl;
4547
_oss << " cost : " << cost << std::endl;
48+
_oss << " sum-constr-viol : " << constraint_violation << std::endl;
4649
_oss << " iter time : " << iter_time << std::endl;
4750
_oss << " total time : " << total_time << std::endl;
4851
_oss << " === LineSearch Statistics === " << std::endl;
4952
_oss << " accepted : " << line_search_accepted << std::endl;
5053
_oss << " alpha : " << alpha << std::endl;
5154
_oss << " ls iters : " << line_search_iters << std::endl;
52-
// TODO constraint violation
5355

5456

5557
_oss << "=== swSQP Stage Statistics ===" << std::endl;
56-
// Header row
57-
_oss << std::setw(15) << "Statistic";
58-
for (size_t i = 0; i < stages_statistics.size(); ++i) {
59-
_oss << std::setw(12) << ("Stage " + std::to_string(i));
60-
}
61-
_oss << std::endl;
6258

63-
_oss << std::setw(15) << "cost";
64-
for (const auto& s : stages_statistics) {
65-
_oss << std::setw(12) << s.cost;
59+
// Column headers
60+
_oss << std::setw(10) << "Stage"
61+
<< std::setw(15) << "Cost"
62+
<< std::setw(25) << "Constr-Viol"
63+
<< std::endl;
64+
65+
// Print a separator line (optional)
66+
_oss << std::string(50, '-') << std::endl;
67+
68+
// Print one row per stage
69+
for (size_t i = 0; i < stages_statistics.size(); ++i) {
70+
const auto& s = stages_statistics[i];
71+
_oss << std::setw(10) << i
72+
<< std::setw(15) << s.cost
73+
<< std::setw(25) << s.constraint_violation
74+
<< std::endl;
6675
}
67-
_oss << std::endl;
6876

6977
return _oss;
7078
}
@@ -135,7 +143,6 @@ class swSQP{
135143

136144

137145
void computeDynamics(const unsigned int i, Eigen::MatrixXd& A, Eigen::MatrixXd& B, Eigen::VectorXd& b);
138-
// void computeQuadraticApproximation(const unsigned int i, Eigen::MatrixXd& H, Eigen::VectorXd& g);
139146
void computeCost(const unsigned int i,
140147
Eigen::MatrixXd& Q, Eigen::VectorXd& q,
141148
Eigen::MatrixXd& R, Eigen::VectorXd& r,
@@ -145,6 +152,7 @@ class swSQP{
145152

146153

147154
void linearize(); // update linearization/quadritization matrices
155+
void update_statistics();
148156
void step(double alpha); //step of the solver
149157
bool break_criteria(); // sqp solvers breaking criteria
150158

src/solvers/hpipmOC.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ hpipmOC::hpipmOC(const unsigned int Ns):
66
_Ns(Ns)
77
{
88
//These are taken from examples in hpipm-cpp
9-
_solver_settings.mode = hpipm::HpipmMode::Balance;
10-
_solver_settings.iter_max = 30;
11-
_solver_settings.alpha_min = 1e-8;
12-
_solver_settings.mu0 = 1e2;
13-
_solver_settings.tol_stat = 1e-04;
14-
_solver_settings.tol_eq = 1e-04;
15-
_solver_settings.tol_ineq = 1e-04;
16-
_solver_settings.tol_comp = 1e-04;
17-
_solver_settings.reg_prim = 1e-12;
18-
_solver_settings.warm_start = 0;
19-
_solver_settings.pred_corr = 1;
20-
_solver_settings.ric_alg = 0;
21-
_solver_settings.split_step = 1;
9+
_solver_settings.mode = hpipm::HpipmMode::Speed;
10+
// _solver_settings.iter_max = 30;
11+
// _solver_settings.alpha_min = 1e-8;
12+
// _solver_settings.mu0 = 1e2;
13+
// _solver_settings.tol_stat = 1e-04;
14+
// _solver_settings.tol_eq = 1e-04;
15+
// _solver_settings.tol_ineq = 1e-04;
16+
// _solver_settings.tol_comp = 1e-04;
17+
// _solver_settings.reg_prim = 1e-12;
18+
// _solver_settings.warm_start = 0;
19+
// _solver_settings.pred_corr = 1;
20+
// _solver_settings.ric_alg = 0;
21+
// _solver_settings.split_step = 1;
2222

2323
_qp.resize(Ns);
2424

src/solvers/swSQP.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ void swSQP::linearize()
6464
{
6565
for(unsigned int k = 0; k < _ocp->getNumberOfNodes(); ++k)
6666
{
67-
_stats.stages_statistics[k].cost = _ocp->stage(k)->stage_cost();
68-
6967
// --- Dynamics (only for k < N) ---
7068
if(k < _ocp->getNumberOfNodes()-1)
7169
{
@@ -172,7 +170,7 @@ bool swSQP::solve(const std::vector<Eigen::VectorXd>& x0, const std::vector<Eige
172170

173171
if(_opt.verbose)
174172
{
175-
_stats.cost = _ocp->cost();
173+
update_statistics();
176174
std::chrono::duration<double> iter_elapsed = std::chrono::high_resolution_clock::now() - iter_start;
177175
_stats.iter_time = iter_elapsed.count();
178176
std::cout<<_stats.toOSS().str()<<"\n"<<std::endl;
@@ -182,6 +180,7 @@ bool swSQP::solve(const std::vector<Eigen::VectorXd>& x0, const std::vector<Eige
182180

183181
_ocp->update(_x0, _u0);
184182
_stats.cost = _ocp->cost();
183+
_stats.constraint_violation = _ocp->constraint_violation();
185184

186185
std::chrono::duration<double> elapsed = std::chrono::high_resolution_clock::now() - start;
187186
_stats.total_time = elapsed.count();
@@ -196,10 +195,9 @@ bool swSQP::break_criteria()
196195
double max_dw = -INFINITY;
197196
for(unsigned int i = 0; i < _ocp->getNumberOfNodes() ; ++i)
198197
max_dw = std::max(max_dw, (_x0[i] - _x0_candidate[i]).cwiseAbs().maxCoeff());
199-
200-
// std::cout<< max_dw << ","<< _ocp->constraint_violation()<< std::endl;
198+
// #TODO: USE THE MANIFOLD OMINUS
201199

202-
return max_dw <= _opt.min_abs_delta_solution && _ocp->constraint_violation() <= _opt.min_abs_delta_solution;
200+
return max_dw <= _opt.min_abs_delta_solution; //&& _ocp->constraint_violation() <= _opt.min_abs_delta_solution;
203201

204202
}
205203

@@ -252,6 +250,17 @@ bool swSQP::ls_merit()
252250
return false;
253251
}
254252

253+
void swSQP::update_statistics()
254+
{
255+
_stats.cost = _ocp->cost();
256+
for (uint i = 0; i < _ocp->getNumberOfNodes(); i++)
257+
{
258+
_stats.stages_statistics[i].cost = _ocp->stage(i)->stage_cost();
259+
_stats.stages_statistics[i].constraint_violation = _ocp->stage(i)->stage_constraint_violation();
260+
}
261+
262+
}
263+
255264

256265
bool swSQP::ls_filter()
257266
{

0 commit comments

Comments
 (0)