1#ifndef PROXSUITE_PROXQP_UTILS_RANDOM_QP_PROBLEMS_HPP
2#define PROXSUITE_PROXQP_UTILS_RANDOM_QP_PROBLEMS_HPP
5#include <Eigen/SparseCore>
6#include <Eigen/Cholesky>
7#include <Eigen/Eigenvalues>
25template<
typename T, proxqp::Layout L>
32using Vec = Eigen::Matrix<T, Eigen::Dynamic, 1>;
34template<
typename Scalar>
35using SparseMat = Eigen::SparseMatrix<Scalar, Eigen::ColMajor, c_int>;
75std::mt19937 gen(1234);
76std::uniform_real_distribution<> uniform_dist(0.0, 1.0);
77std::normal_distribution<double> normal_dist;
82 double output = double(uniform_dist(gen));
88 static u64 output = gen();
101 return normal_dist(gen);
111 return g_lehmer64_state;
133 return double(a) / double(
u64(1) << 53U);
138 static const double pi2 = std::atan(
static_cast<double>(1)) * 8;
143 double ln = std::log(u1);
144 double sqrt = std::sqrt(-2 * ln);
146 return sqrt * std::cos(pi2 * u2);
150template<
typename Scalar>
156 for (
isize i = 0; i < nrows; ++i) {
162template<
typename Scalar>
168 for (
isize i = 0; i < nrows; ++i) {
169 for (
isize j = 0; j < ncols; ++j) {
178template<
typename Scalar>
183 auto qr = mat.householderQr();
187using Input = std::pair<u128, isize>;
190template<
typename Scalar>
195 static auto cache = std::map<detail::Input, Mat<Scalar, colmajor>>{};
197 auto it = cache.find(input);
198 if (it == cache.end()) {
199 auto res = cache.insert({
208template<
typename Scalar>
218 Scalar diff = log(cond);
219 for (
isize i = 0; i < n; ++i) {
220 d(i) = exp(Scalar(i) / Scalar(n) * diff);
224 return q * d.asDiagonal() * q.transpose();
227template<
typename Scalar>
234 for (
isize i = 0; i < n; ++i) {
236 H.insert(i, i) = random;
239 for (
isize i = 0; i < n; ++i) {
240 for (
isize j = i + 1; j < n; ++j) {
242 if (urandom < p / 2) {
244 H.insert(i, j) = random;
251 H_dense.template selfadjointView<Eigen::Upper>().eigenvalues();
253 Scalar min = eigh.minCoeff();
254 Scalar max = eigh.maxCoeff();
264 Scalar rho = (max - min) / (cond - 1) - min;
269 for (
isize i = 0; i < n; ++i) {
270 H.coeffRef(i, i) += rho;
277template<
typename Scalar>
286 for (
isize i = 0; i < n; ++i) {
287 for (
isize j = i + 1; j < n; ++j) {
291 H.insert(i, j) = random;
297 H_dense.template selfadjointView<Eigen::Upper>().eigenvalues();
298 Scalar min = eigh.minCoeff();
299 for (
isize i = 0; i < n; ++i) {
300 H.coeffRef(i, i) += (rho + abs(min));
307template<
typename Scalar>
315 for (
isize i = 0; i < n; ++i) {
316 for (
isize j = 0; j < n; ++j) {
318 if (urandom < p / 2) {
325 H = ((H + H.transpose()) * 0.5)
329 Vec<Scalar> eigh = H.template selfadjointView<Eigen::Upper>().eigenvalues();
330 Scalar min = eigh.minCoeff();
331 H.diagonal().array() += (rho + abs(min));
336template<
typename Scalar>
342 for (
isize i = 0; i < nrows; ++i) {
343 for (
isize j = 0; j < ncols; ++j) {
353template<
typename Scalar>
360 for (
isize i = 0; i < nrows; ++i) {
361 for (
isize j = 0; j < ncols; ++j) {
386 return lhs.operator*(rhs);
388template<
typename To,
typename From>
392 return from.template cast<To>();
398template<
typename MatLhs,
typename MatRhs,
typename T =
typename MatLhs::Scalar>
402 using Upscaled =
typename std::
403 conditional<std::is_floating_point<T>::value,
long double, T>::type;
410template<
typename MatLhs,
413 typename T =
typename MatLhs::Scalar>
415matmul3(MatLhs
const& a, MatMid
const& b, MatRhs
const& c)
430#if defined(EIGEN_RUNTIME_NO_MALLOC)
431 EigenNoAlloc() noexcept { Eigen::internal::set_is_malloc_allowed(
false); }
432 ~EigenNoAlloc() noexcept { Eigen::internal::set_is_malloc_allowed(
true); }
438template<
typename Scalar>
441 Scalar sparsity_factor,
442 Scalar strong_convexity_factor = Scalar(1e-2))
447 dim, strong_convexity_factor, sparsity_factor);
462template<
typename Scalar>
467 Scalar sparsity_factor,
468 Scalar strong_convexity_factor = Scalar(1e-2))
473 dim, strong_convexity_factor, sparsity_factor);
504template<
typename Scalar>
509 Scalar sparsity_factor)
514 dim, Scalar(0), sparsity_factor);
532 Vec<Scalar> g = -(H * x_sol + C.transpose() * z_sol + A.transpose() * y_sol);
545template<
typename Scalar>
550 Scalar sparsity_factor,
551 Scalar strong_convexity_factor = Scalar(1e-2))
556 dim, strong_convexity_factor, sparsity_factor);
573 C.block(0, 0, n_in, dim) = C_;
574 C.block(n_in, 0, n_in, dim) = C_;
591template<
typename Scalar>
596 Scalar sparsity_factor,
597 Scalar strong_convexity_factor = Scalar(1e-2))
602 dim, strong_convexity_factor, sparsity_factor);
616 C.diagonal().array() += 1;
630template<
typename Scalar>
635 Scalar sparsity_factor,
636 Scalar strong_convexity_factor = Scalar(1e-2))
640 dim, strong_convexity_factor, sparsity_factor);
#define LDLT_EXPLICIT_TPL_DECL(NParams,...)
#define VEG_TAG(Name, Type)
decltype(sizeof(0)) usize
_detail::_meta::make_signed< usize >::Type isize
void ldlt_compute(Eigen::LDLT< T > &out, T const &mat)
void llt_compute(Eigen::LLT< T > &out, T const &mat)
auto to_sparse_sym(Mat< c_float, colmajor > const &mat) -> SparseMat< c_float >
auto to_sparse(Mat< c_float, colmajor > const &mat) -> SparseMat< c_float >
std::pair< u128, isize > Input
auto orthonormal_rand_impl(isize n) -> Mat< Scalar, colmajor >
auto orthonormal_rand(isize n) -> Mat< Scalar, colmajor > const &
auto sparse_matrix_rand_not_compressed(isize nrows, isize ncols, Scalar p) -> Mat< Scalar, colmajor >
auto sparse_positive_definite_rand_compressed(isize n, Scalar rho, Scalar p) -> SparseMat< Scalar >
auto matrix_rand(isize nrows, isize ncols) -> Mat< Scalar, colmajor >
auto sparse_positive_definite_rand(isize n, Scalar cond, Scalar p) -> SparseMat< Scalar >
auto positive_definite_rand(isize n, Scalar cond) -> Mat< Scalar, colmajor >
auto vector_rand(isize nrows) -> Vec< Scalar >
auto sparse_positive_definite_rand_not_compressed(isize n, Scalar rho, Scalar p) -> Mat< Scalar, colmajor >
constexpr u128 lehmer64_constant(0xda942042e4dd58b5)
auto sparse_matrix_rand(isize nrows, isize ncols, Scalar p) -> SparseMat< Scalar >
auto normal_rand() -> double
auto lehmer_global() -> u128 &
auto uniform_rand() -> double
Eigen::Matrix< T, Eigen::Dynamic, 1 > Vec
proxsuite::proxqp::dense::Model< Scalar > dense_strongly_convex_qp(proxqp::isize dim, proxqp::isize n_eq, proxqp::isize n_in, Scalar sparsity_factor, Scalar strong_convexity_factor=Scalar(1e-2))
proxsuite::proxqp::dense::Model< Scalar > dense_box_constrained_qp(proxqp::isize dim, proxqp::isize n_eq, proxqp::isize n_in, Scalar sparsity_factor, Scalar strong_convexity_factor=Scalar(1e-2))
proxsuite::proxqp::dense::Model< Scalar > dense_degenerate_qp(proxqp::isize dim, proxqp::isize n_eq, proxqp::isize n_in, Scalar sparsity_factor, Scalar strong_convexity_factor=Scalar(1e-2))
Eigen::SparseMatrix< Scalar, Eigen::ColMajor, c_int > SparseMat
proxsuite::proxqp::dense::Model< Scalar > dense_unconstrained_qp(proxqp::isize dim, Scalar sparsity_factor, Scalar strong_convexity_factor=Scalar(1e-2))
auto matmul3(MatLhs const &a, MatMid const &b, MatRhs const &c) -> Mat< T, proxqp::colmajor >
auto mat_cast(Mat< From, proxqp::colmajor > const &from) -> Mat< To, proxqp::colmajor >
auto matmul_impl(Mat< T, proxqp::colmajor > const &lhs, Mat< T, proxqp::colmajor > const &rhs) -> Mat< T, proxqp::colmajor >
proxsuite::proxqp::sparse::SparseModel< Scalar > sparse_strongly_convex_qp(proxqp::isize dim, proxqp::isize n_eq, proxqp::isize n_in, Scalar sparsity_factor, Scalar strong_convexity_factor=Scalar(1e-2))
proxsuite::proxqp::dense::Model< Scalar > dense_not_strongly_convex_qp(proxqp::isize dim, proxqp::isize n_eq, proxqp::isize n_in, Scalar sparsity_factor)
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic,(L==proxqp::colmajor) ? Eigen::ColMajor :Eigen::RowMajor > Mat
auto matmul(MatLhs const &a, MatRhs const &b) -> Mat< T, proxqp::colmajor >
constexpr Layout colmajor
This class stores the model of the QP problem.
EigenNoAlloc(EigenNoAlloc const &)=delete
auto operator=(EigenNoAlloc const &) -> EigenNoAlloc &=delete
EigenNoAlloc(EigenNoAlloc &&)=delete
auto operator=(EigenNoAlloc &&) -> EigenNoAlloc &=delete