Pipal  1.2.0
Penalty Interior-Point ALgorithm
Loading...
Searching...
No Matches
Output.hxx
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
2 * Copyright (c) 2025, Davide Stocco and Enrico Bertolazzi. *
3 * *
4 * The Pipal project is distributed under the MIT License. *
5 * *
6 * Davide Stocco Enrico Bertolazzi *
7 * University of Trento University of Trento *
8 * e-mail: davide.stocco@unitn.it e-mail: enrico.bertolazzi@unitn.it *
9\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
10
11#pragma once
12
13#ifndef INCLUDE_PIPAL_OUTPUT_HXX
14#define INCLUDE_PIPAL_OUTPUT_HXX
15
16namespace Pipal
17{
18
26 template <typename Real>
27 class Output
28 {
29 static_assert(std::is_floating_point_v<Real>,
30 "Pipal::Output<Real>: Real must be a floating-point type.");
31
32 using MicroSeconds = std::chrono::microseconds;
33 using SteadyClock = std::chrono::steady_clock;
34 using TimePoint = SteadyClock::time_point;
35
36 std::ostream & s{std::cout};
37 std::string l;
38 std::string q;
39 std::string n;
41
42 public:
47 this->t = SteadyClock::now();
48 this->l = "======+=========================+====================================+=========================+===========================================================================+=======================";
49 this->q = "Iter. | Objective Infeas. | Pen. Par. I.P. Par. Opt. Error | Merit P.I.P. Err.| Shift ||P.Step|| ||D.Step|| Lin. Red. Quad. Red. Quality | Pri. Step. Dual Step.";
50 this->n = "----------- ---------- | ---------- ---------- ---------- ----------- ----------- ----------- | ---------- ----------";
51 }
52
56 ~Output() = default;
57
63 void
64 printHeader(Input<Real> const & i, Iterate<Real> const & z) const {
65 this->s
66 << "Problem name\n"
67 << "============\n"
68 << " " << i.name << '\n'
69 << '\n';
70
71 this->s
72 << "Problem size\n"
73 << "============\n"
74 << " Number of variables....................... : " << i.nV << '\n'
75 << " Number of equality constraints............ : " << i.nE << '\n'
76 << " Number of inequality constraints.......... : " << i.nI << '\n'
77 << '\n';
78
79 this->s
80 << "Problem sparsity\n"
81 << "================\n"
82 << " Nonzeros in Hessian of Lagrangian......... : " << z.Hnnz << '\n'
83 << " Nonzeros in equality constraint Jacobian.. : " << z.JEnnz << '\n'
84 << " Nonzeros in inequality constraint Jacobian : " << z.JInnz << '\n'
85 << '\n';
86 }
87
92 void
93 printBreak(Counter const & c) const {
94 if (c.k % 20 == 0) {
95 this->s
96 << this->l << '\n'
97 << this->q << '\n'
98 << this->l << '\n';
99 }
100 }
101
107 void
109 Iterate<Real> const & z,
110 Direction<Real> const & d
111 ) const {
112 this->s
113 << std::scientific << std::setprecision(4) << std::showpos << z.phi << " " << std::noshowpos
114 << z.kkt[2] << " | " << z.shift << " " << d.x_norm << " " << d.l_norm << " " << std::showpos
115 << d.ltred << " " << d.qtred << " " << d.m << std::noshowpos << " | ";
116 }
117
123 void
125 Counter const & c,
126 Iterate<Real> const & z
127 ) const {
128 this->s
129 << std::setw(5) << c.k << " | " << std::scientific << std::setprecision(4) << std::showpos << z.f
130 << std::noshowpos << " " << z.v << " | " << z.rho << " " << z.mu << " " << z.kkt[1] << " | ";
131 }
132
137 void
139 this->s << std::scientific << std::setprecision(4) << a.p << " " << a.d;
140 if (a.s == 1) { this->s << " SOC"; }
141 this->s << '\n';
142 }
143
150 void
152 Counter const & c,
153 Iterate<Real> const & z,
154 Integer const b
155 ) const {
156 this->printIterate(c, z);
157 this->s
158 << this->n << '\n' << this->l << '\n'
159 << '\n';
160
161 this->s
162 << "Final result\n"
163 << "============\n";
164 switch (b) {
165 case 0: this->s << " EXIT: No termination message set\n"; break;
166 case 1: this->s << " EXIT: Optimal solution found\n"; break;
167 case 2: this->s << " EXIT: Infeasible stationary point found\n"; break;
168 case 3: this->s << " EXIT: Iteration limit reached\n"; break;
169 case 4: this->s << " EXIT: Invalid bounds\n"; break;
170 case 5: this->s << " EXIT: Function evaluation error\n"; break;
171 default: this->s << " EXIT: Unknown termination\n"; break;
172 }
173
174 this->s
175 << '\n'
176 << "Final values\n"
177 << "============\n" << std::showpos
178 << " Objective function........................ : " << z.fu << '\n'
179 << " Feasibility violation..................... : " << z.vu << '\n'
180 << " Optimality error (feasibility)............ : " << z.kkt(0) << '\n'
181 << " Optimality error (penalty)................ : " << z.kkt(1) << '\n'
182 << " Optimality error (penalty-interior-point). : " << z.kkt(2) << '\n'
183 << " Penalty parameter......................... : " << z.rho << '\n'
184 << " Interior-point parameter.................. : " << z.mu << '\n'
185 << std::noshowpos
186 << '\n';
187
188 this->s
189 << "Final counters" << '\n'
190 << "==============" << '\n'
191 << " Iterations................................ : " << c.k << '\n'
192 << " Function evaluations...................... : " << c.f << '\n'
193 << " Gradient evaluations...................... : " << c.g << '\n'
194 << " Hessian evaluations....................... : " << c.H << '\n'
195 << " Matrix factorizations..................... : " << c.M << '\n'
196 << " CPU millseconds........................... : "
197 << std::scientific << std::setprecision(4)
198 << std::chrono::duration_cast<MicroSeconds>(SteadyClock::now() - this->t).count()/1.0e3
199 << '\n';
200 }
201 }; // class Output
202
203} // namespace Pipal
204
205#endif // INCLUDE_PIPAL_OUTPUT_HXX
std::string q
Definition Output.hxx:38
void printBreak(Counter const &c) const
Print a formatted break line and column headers periodically.
Definition Output.hxx:93
void printFooter(Counter const &c, Iterate< Real > const &z, Integer const b) const
Print final summary footer and termination message.
Definition Output.hxx:151
Output()
Default constructor.
Definition Output.hxx:46
TimePoint t
Definition Output.hxx:40
void printAcceptance(Acceptance< Real > const &a) const
Print acceptance information for the chosen trial step.
Definition Output.hxx:138
void printIterate(Counter const &c, Iterate< Real > const &z) const
Print a single iterate row to the console table.
Definition Output.hxx:124
void printHeader(Input< Real > const &i, Iterate< Real > const &z) const
Print problem header information.
Definition Output.hxx:64
std::string l
Definition Output.hxx:37
std::ostream & s
Definition Output.hxx:36
~Output()=default
Default destructor.
std::chrono::steady_clock SteadyClock
Definition Output.hxx:33
std::string n
Definition Output.hxx:39
std::chrono::microseconds MicroSeconds
Definition Output.hxx:32
SteadyClock::time_point TimePoint
Definition Output.hxx:34
void printDirection(Iterate< Real > const &z, Direction< Real > const &d) const
Print a summary of the current search direction.
Definition Output.hxx:108
Namespace for the Pipal library.
Definition Acceptance.hxx:16
PIPAL_DEFAULT_INTEGER_TYPE Integer
The Integer type as used for the API.
Definition Types.hxx:110
struct Counter { Integer f{0}; Integer g{0}; Integer H{0}; Integer k{0}; Integer M{0}; Counter()=default; Counter(Counter const &)=delete; Counter &operator=(Counter const &)=delete; } Counter
Internal counters for solver statistics.
Definition Types.hxx:285
Class for managing the acceptance criteria of the solver.
Definition Types.hxx:486
bool s
Definition Types.hxx:490
Real d
Definition Types.hxx:489
Real p
Definition Types.hxx:488
Class for managing the current search direction of the solver.
Definition Types.hxx:446
Real l_norm
Definition Types.hxx:456
Real qtred
Definition Types.hxx:460
Real x_norm
Definition Types.hxx:448
Real m
Definition Types.hxx:461
Real ltred
Definition Types.hxx:459
Input structure holding all the data defining the optimization problem.
Definition Types.hxx:316
Integer nV
Definition Types.hxx:348
std::string name
Definition Types.hxx:317
Integer nI
Definition Types.hxx:349
Integer nE
Definition Types.hxx:350
Class for managing the current iterate of the solver.
Definition Types.hxx:377
Real rho
Definition Types.hxx:381
Real v
Definition Types.hxx:401
Integer Hnnz
Definition Types.hxx:400
Real shift
Definition Types.hxx:407
Real mu
Definition Types.hxx:383
Real fu
Definition Types.hxx:385
Integer JEnnz
Definition Types.hxx:391
Real vu
Definition Types.hxx:402
Integer JInnz
Definition Types.hxx:397
Real f
Definition Types.hxx:384
Vector< Real > kkt
Definition Types.hxx:409
Real phi
Definition Types.hxx:404