Pipal  1.2.0
Penalty Interior-Point ALgorithm
Loading...
Searching...
No Matches
Types.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_DEFINES_HXX
14#define INCLUDE_PIPAL_DEFINES_HXX
15
16// Standard libraries
17#include <string>
18#include <iostream>
19#include <iomanip>
20#include <ostream>
21
22// STL
23#include <cmath>
24#include <limits>
25#include <algorithm>
26#include <functional>
27#include <type_traits>
28#include <numeric>
29#include <memory>
30
31// Eigen library
32#ifndef PIPAL_EIGEN_EXTERNAL
33#include <Eigen/Dense>
34#include <Eigen/Sparse>
35#include <Eigen/SparseCholesky>
36#endif
37
38// Print Pipal errors
39#ifndef PIPAL_ERROR
40#define PIPAL_ERROR(MSG) \
41 { \
42 std::ostringstream os; \
43 os << MSG; \
44 throw std::runtime_error(os.str()); \
45 }
46#endif
47
48// Assert for Pipal
49#ifndef PIPAL_ASSERT
50#define PIPAL_ASSERT(COND, MSG) \
51 if (!(COND)) \
52 { \
53 PIPAL_ERROR(MSG); \
54 }
55#endif
56
57// Warning for Pipal
58#ifndef PIPAL_WARNING
59#define PIPAL_WARNING(MSG) \
60 { \
61 std::cout << MSG << std::endl; \
62 }
63#endif
64
65// Warning assert for Pipal
66#ifndef PIPAL_ASSERT_WARNING
67#define PIPAL_ASSERT_WARNING(COND, MSG) \
68 if (!(COND)) \
69 { \
70 PIPAL_WARNING(MSG); \
71 }
72#endif
73
74#ifndef PIPAL_DEFAULT_INTEGER_TYPE
75#define PIPAL_DEFAULT_INTEGER_TYPE int
76#endif
77
102namespace Pipal
103{
111
112 template<typename Real> using Vector = Eigen::Vector<Real, Eigen::Dynamic>;
113 template<typename Real> using Matrix = Eigen::Matrix<Real, Eigen::Dynamic, Eigen::Dynamic>;
114 template<typename Real> using SparseMatrix = Eigen::SparseMatrix<Real>;
115 template<typename Real> using Array = Eigen::Array<Real, Eigen::Dynamic, 1>;
116
117 using Indices = Eigen::Array<Integer, Eigen::Dynamic, 1>;
118 using Mask = Eigen::Array<bool, Eigen::Dynamic, 1>;
119
126 using Algorithm = enum class Algorithm : Integer {CONSERVATIVE = 0, ADAPTIVE = 1};
127
133 static Indices find(Mask const & mask)
134 {
135 Indices out(mask.count());
136 for (Integer i{0}, j{0}; i < mask.size(); ++i) {if (mask[i]) {out[j++] = i;}}
137 return out;
138 }
139
149 template <typename Real>
150 static void insert_block(SparseMatrix<Real> & mat, SparseMatrix<Real> const & blk,
151 Integer const row_offset, Integer const col_offset)
152 {
153 #define CMD "Pipal::Solver::insert_block(...): "
154
155 // Get sizes
156 const Integer mat_cols{static_cast<Integer>(mat.cols())}, mat_rows{static_cast<Integer>(mat.rows())};
157 const Integer blk_cols{static_cast<Integer>(blk.cols())}, blk_rows{static_cast<Integer>(blk.rows())};
158
159 // Check dimensions
160 PIPAL_ASSERT(row_offset >= 0,
161 CMD "row offset must be non-negative.");
162 PIPAL_ASSERT(col_offset >= 0,
163 CMD "column offset must be non-negative.");
164 PIPAL_ASSERT(row_offset + blk_rows <= mat_rows,
165 CMD "inserting block exceeds matrix row dimensions.");
166 PIPAL_ASSERT(col_offset + blk_cols <= mat_cols,
167 CMD "inserting block exceeds matrix column dimensions.");
168
169 // Insert block
170 for (Integer r{0}; r < blk_rows; ++r) {
171 for (Integer c{0}; c < blk_cols; ++c) {
172 if (blk.coeff(r, c) != 0) {mat.coeffRef(r + row_offset, c + col_offset) = blk.coeff(r, c);}
173 }
174 }
175
176 #undef CMD
177 }
178
190 template <typename Real>
191 static void insert_block(SparseMatrix<Real> & mat, SparseMatrix<Real> const & blk,
192 Indices const row_index, Indices const col_index, Integer const row_offset, Integer const col_offset)
193 {
194 #define CMD "Pipal::Solver::insert_block(...): "
195
196 // Get sizes
197 const Integer mat_cols{static_cast<Integer>(mat.cols())}, mat_rows{static_cast<Integer>(mat.rows())};
198 const Integer idx_rows{static_cast<Integer>(row_index.size())};
199 const Integer idx_cols{static_cast<Integer>(col_index.size())};
200
201 // Check dimensions
202 PIPAL_ASSERT(row_offset >= 0,
203 CMD "row offset must be non-negative.");
204 PIPAL_ASSERT(col_offset >= 0,
205 CMD "column offset must be non-negative.");
206 PIPAL_ASSERT(row_offset + idx_rows <= mat_rows,
207 CMD "inserting block exceeds matrix row dimensions.");
208 PIPAL_ASSERT(col_offset + idx_cols <= mat_cols,
209 CMD "inserting block exceeds matrix column dimensions.");
210
211 // Insert block
212 for (Integer r{0}; r < idx_rows; ++r) {
213 for (Integer c{0}; c < idx_cols; ++c) {
214 if (blk.coeff(row_index(r), col_index(c)) != 0) {
215 mat.coeffRef(r + row_offset, c + col_offset) = blk.coeff(row_index(r), col_index(c));
216 }
217 }
218 }
219
220 #undef CMD
221 }
222
227 template<typename Real>
229 {
230 static constexpr Real rhs_bnd{1.0e+18};
231 static constexpr Real grad_max{1.0e+02};
232 static constexpr Real infeas_max{1.0e+02};
233 static constexpr Real nnz_max{2.0e+04};
234 static constexpr Integer opt_err_mem{6};
235 static constexpr Real ls_factor{5.0e-01};
236 static constexpr Real ls_thresh{1.0e-08};
237 static constexpr Real ls_frac{1.0e-02};
238 static constexpr Real slack_min{1.0e-20};
239 static constexpr Real shift_min{1.0e-12};
240 static constexpr Real shift_factor1{5.0e-01};
241 static constexpr Real shift_factor2{6.0e-01};
242 static constexpr Real shift_max{1.0e+08};
243 static constexpr Real rho_init{1.0e-01};
244 static constexpr Real rho_min{1.0e-12};
245 static constexpr Real rho_factor{5.0e-01};
246 static constexpr Integer rho_trials{8};
247 static constexpr Real mu_init{1.0e-01};
248 static constexpr Real mu_min{1.0e-12};
249 static constexpr Real mu_factor{1.0e-01};
250 static constexpr Real mu_factor_exp{1.5};
251 static constexpr Integer mu_trials{4};
252 static constexpr Real mu_max{1.0e-01};
253 static constexpr Real mu_max_exp0{0.0};
254 static constexpr Real update_con_1{1.0e-02};
255 static constexpr Real update_con_2{1.0e-02};
256 static constexpr Real update_con_3{1.01};
257
258 Real opt_err_tol{1.0e-6};
260 Algorithm algorithm{Algorithm::ADAPTIVE};
261 Real mu_max_exp{0.0};
262
264
268 Parameter() = default;
269
273 Parameter(Parameter const &) = delete;
274
278 Parameter & operator=(Parameter const &) = delete;
279
280 }; // struct Parameter
281
285 using Counter = struct Counter
286 {
287 Integer f{0};
288 Integer g{0};
289 Integer H{0};
290 Integer k{0};
291 Integer M{0};
292
296 Counter() = default;
297
301 Counter(Counter const &) = delete;
302
306 Counter & operator=(Counter const &) = delete;
307
308 }; // struct Counter
309
314 template<typename Real>
370
375 template<typename Real>
439
444 template<typename Real>
446 {
448 Real x_norm;
449 Real x_norm_;
456 Real l_norm;
457 Real lred0;
458 Real ltred0;
459 Real ltred;
460 Real qtred;
461 Real m;
462
466 Direction() = default;
467
471 Direction(Direction const &) = delete;
472
476 Direction & operator=(Direction const &) = delete;
477
478 }; // struct Direction
479
484 template<typename Real>
486 {
487 Real p0{0.0};
488 Real p{0.0};
489 Real d{0.0};
490 bool s{false};
491
495 Acceptance() = default;
496
500 Acceptance(Acceptance const &) = delete;
501
505 Acceptance & operator=(Acceptance const &) = delete;
506
507 }; // struct Acceptance
508
509} // namespace Pipal
510
511#endif // INCLUDE_PIPAL_DEFINES_HXX
#define CMD
#define PIPAL_DEFAULT_INTEGER_TYPE
Definition Types.hxx:75
#define PIPAL_ASSERT(COND, MSG)
Definition Types.hxx:50
Namespace for the Pipal library.
Definition Acceptance.hxx:16
Eigen::Array< Integer, Eigen::Dynamic, 1 > Indices
Definition Types.hxx:117
Eigen::SparseMatrix< Real > SparseMatrix
Definition Types.hxx:114
static Indices find(Mask const &mask)
Select elements from a vector based on a boolean mask.
Definition Types.hxx:133
enum class Algorithm :Integer {CONSERVATIVE=0, ADAPTIVE=1} Algorithm
Enumeration for the algorithm choice.
Definition Types.hxx:126
Eigen::Matrix< Real, Eigen::Dynamic, Eigen::Dynamic > Matrix
Definition Types.hxx:113
PIPAL_DEFAULT_INTEGER_TYPE Integer
The Integer type as used for the API.
Definition Types.hxx:110
Eigen::Array< Real, Eigen::Dynamic, 1 > Array
Definition Types.hxx:115
static void insert_block(SparseMatrix< Real > &mat, SparseMatrix< Real > const &blk, Integer const row_offset, Integer const col_offset)
Insert a sparse block into a sparse matrix at the specified offsets.
Definition Types.hxx:150
Eigen::Array< bool, Eigen::Dynamic, 1 > Mask
Definition Types.hxx:118
Eigen::Vector< Real, Eigen::Dynamic > Vector
Definition Types.hxx:112
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
Acceptance(Acceptance const &)=delete
Delete copy constructor and assignment operator.
bool s
Definition Types.hxx:490
Real d
Definition Types.hxx:489
Acceptance & operator=(Acceptance const &)=delete
Delete copy constructor and assignment operator.
Real p
Definition Types.hxx:488
Real p0
Definition Types.hxx:487
Acceptance()=default
Default constructor.
Real l_norm
Definition Types.hxx:456
Array< Real > s2
Definition Types.hxx:454
Real qtred
Definition Types.hxx:460
Real ltred0
Definition Types.hxx:458
Real x_norm
Definition Types.hxx:448
Array< Real > lI
Definition Types.hxx:455
Real m
Definition Types.hxx:461
Array< Real > r2
Definition Types.hxx:451
Array< Real > r1
Definition Types.hxx:450
Array< Real > lE
Definition Types.hxx:452
Real ltred
Definition Types.hxx:459
Real x_norm_
Definition Types.hxx:449
Vector< Real > x
Definition Types.hxx:447
Real lred0
Definition Types.hxx:457
Array< Real > s1
Definition Types.hxx:453
Direction & operator=(Direction const &)=delete
Delete copy constructor and assignment operator.
Direction()=default
Default constructor.
Direction(Direction const &)=delete
Delete copy constructor and assignment operator.
Vector< Real > l3
Definition Types.hxx:329
Indices I1
Definition Types.hxx:318
Vector< Real > l9
Definition Types.hxx:336
Vector< Real > l5
Definition Types.hxx:331
Integer nV
Definition Types.hxx:348
Input()=default
Default constructor.
std::string name
Definition Types.hxx:317
Integer n3
Definition Types.hxx:341
Integer n4
Definition Types.hxx:342
Indices I7
Definition Types.hxx:324
Integer n5
Definition Types.hxx:343
Integer n1
Definition Types.hxx:339
Integer n6
Definition Types.hxx:344
Integer n9
Definition Types.hxx:347
Indices I3
Definition Types.hxx:320
Vector< Real > u9
Definition Types.hxx:337
Vector< Real > b6
Definition Types.hxx:333
Indices I6
Definition Types.hxx:323
Integer nA
Definition Types.hxx:351
Vector< Real > u5
Definition Types.hxx:332
Integer n0
Definition Types.hxx:338
Input & operator=(Input const &)=delete
Delete copy constructor and assignment operator.
Indices I4
Definition Types.hxx:321
Indices I9
Definition Types.hxx:326
Input(Input const &)=delete
Delete copy constructor and assignment operator.
Indices I2
Definition Types.hxx:319
Indices I8
Definition Types.hxx:325
Integer n7
Definition Types.hxx:345
Integer n8
Definition Types.hxx:346
Integer nI
Definition Types.hxx:349
Vector< Real > x0
Definition Types.hxx:327
Vector< Real > l7
Definition Types.hxx:334
Indices I5
Definition Types.hxx:322
Integer n2
Definition Types.hxx:340
Integer nE
Definition Types.hxx:350
Integer vi
Definition Types.hxx:352
Vector< Real > u4
Definition Types.hxx:330
Vector< Real > b2
Definition Types.hxx:328
Vector< Real > u8
Definition Types.hxx:335
Iterate & operator=(Iterate const &)=delete
Delete copy constructor and assignment operator.
Array< Real > cEs
Definition Types.hxx:414
Real rho
Definition Types.hxx:381
Array< Real > cE
Definition Types.hxx:389
Real v
Definition Types.hxx:401
Integer Hnnz
Definition Types.hxx:400
Real shift
Definition Types.hxx:407
Real mu
Definition Types.hxx:383
Array< Real > lI
Definition Types.hxx:398
SparseMatrix< Real > H
Definition Types.hxx:399
Array< Real > cIu
Definition Types.hxx:417
Real rho_
Definition Types.hxx:382
Array< Real > r1
Definition Types.hxx:387
Vector< Real > x
Definition Types.hxx:380
Real shift22
Definition Types.hxx:419
SparseMatrix< Real > JE
Definition Types.hxx:390
Array< Real > s2
Definition Types.hxx:394
Real fu
Definition Types.hxx:385
Array< Real > lE
Definition Types.hxx:392
Integer JEnnz
Definition Types.hxx:391
Array< Real > cI
Definition Types.hxx:395
Real v0
Definition Types.hxx:403
SparseMatrix< Real > A
Definition Types.hxx:418
Integer Annz
Definition Types.hxx:406
Vector< Real > b
Definition Types.hxx:408
SparseMatrix< Real > JI
Definition Types.hxx:396
Real fs
Definition Types.hxx:413
Real vu
Definition Types.hxx:402
Array< Real > cIs
Definition Types.hxx:416
Integer JInnz
Definition Types.hxx:397
Iterate()=default
Default constructor.
LDLT ldlt
Definition Types.hxx:405
Iterate(Iterate const &)=delete
Delete copy constructor and assignment operator.
Real f
Definition Types.hxx:384
Real v_
Definition Types.hxx:420
Array< Real > cEu
Definition Types.hxx:415
Integer err
Definition Types.hxx:411
Eigen::SimplicialLDLT< SparseMatrix< Real >, Eigen::Lower > LDLT
Definition Types.hxx:378
Vector< Real > kkt_
Definition Types.hxx:410
Vector< Real > kkt
Definition Types.hxx:409
Array< Real > s1
Definition Types.hxx:393
bool cut_
Definition Types.hxx:421
Array< Real > r2
Definition Types.hxx:388
Vector< Real > g
Definition Types.hxx:386
Real phi
Definition Types.hxx:404
Integer bfgs_update_freq
Definition Types.hxx:263
static constexpr Real mu_max_exp0
Definition Types.hxx:253
static constexpr Real rho_min
Definition Types.hxx:244
static constexpr Real ls_thresh
Definition Types.hxx:236
static constexpr Real rho_factor
Definition Types.hxx:245
static constexpr Real grad_max
Definition Types.hxx:231
static constexpr Real shift_factor2
Definition Types.hxx:241
static constexpr Integer mu_trials
Definition Types.hxx:251
Algorithm algorithm
Definition Types.hxx:260
static constexpr Real ls_factor
Definition Types.hxx:235
static constexpr Real nnz_max
Definition Types.hxx:233
static constexpr Real mu_min
Definition Types.hxx:248
static constexpr Real shift_max
Definition Types.hxx:242
Integer iter_max
Definition Types.hxx:259
Real mu_max_exp
Definition Types.hxx:261
static constexpr Real rho_init
Definition Types.hxx:243
static constexpr Real slack_min
Definition Types.hxx:238
Parameter & operator=(Parameter const &)=delete
Delete copy constructor and assignment operator.
static constexpr Real mu_factor_exp
Definition Types.hxx:250
static constexpr Real shift_min
Definition Types.hxx:239
static constexpr Real mu_max
Definition Types.hxx:252
static constexpr Real infeas_max
Definition Types.hxx:232
static constexpr Integer opt_err_mem
Definition Types.hxx:234
static constexpr Real mu_init
Definition Types.hxx:247
Parameter()=default
Default constructor.
static constexpr Real ls_frac
Definition Types.hxx:237
static constexpr Real update_con_1
Definition Types.hxx:254
static constexpr Real rhs_bnd
Definition Types.hxx:230
Parameter(Parameter const &)=delete
Delete copy constructor and assignment operator.
static constexpr Real shift_factor1
Definition Types.hxx:240
static constexpr Real update_con_2
Definition Types.hxx:255
static constexpr Integer rho_trials
Definition Types.hxx:246
Real opt_err_tol
Definition Types.hxx:258
static constexpr Real update_con_3
Definition Types.hxx:256
static constexpr Real mu_factor
Definition Types.hxx:249