GCC Code Coverage Report


Directory: ./
File: src/solver_LP_abstract.cpp
Date: 2025-03-17 04:04:52
Exec Total Coverage
Lines: 41 58 70.7%
Branches: 39 126 31.0%

Line Branch Exec Source
1 /*
2 * Copyright 2015, LAAS-CNRS
3 * Author: Andrea Del Prete
4 */
5
6 #include <hpp/centroidal-dynamics/logger.hh>
7 #include <hpp/centroidal-dynamics/solver_LP_abstract.hh>
8 #include <hpp/centroidal-dynamics/solver_LP_qpoases.hh>
9 #include <iostream>
10
11 #ifdef CLP_FOUND
12 #include <hpp/centroidal-dynamics/solver_LP_clp.hh>
13 #endif
14
15 using namespace std;
16
17 namespace centroidal_dynamics {
18
19 15 Solver_LP_abstract *Solver_LP_abstract::getNewSolver(SolverLP solverType) {
20
2/4
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✓ Branch 4 taken 15 times.
✗ Branch 5 not taken.
15 if (solverType == SOLVER_LP_QPOASES) return new Solver_LP_qpoases();
21
22 #ifdef CLP_FOUND
23 if (solverType == SOLVER_LP_CLP) return new Solver_LP_clp();
24 #endif
25
26 SEND_ERROR_MSG("Specified solver type not recognized: " +
27 toString(solverType));
28 return NULL;
29 }
30
31 1 bool Solver_LP_abstract::writeLpToFile(const std::string &filename,
32 Cref_vectorX c, Cref_vectorX lb,
33 Cref_vectorX ub, Cref_matrixXX A,
34 Cref_vectorX Alb, Cref_vectorX Aub) {
35 1 MatrixXX::Index n = c.size(), m = A.rows();
36
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert(lb.size() == n);
37
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert(ub.size() == n);
38
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert(A.cols() == n);
39
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert(Alb.size() == m);
40
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert(Aub.size() == m);
41
42 std::ofstream out(filename.c_str(),
43
1/2
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 std::ios::out | std::ios::binary | std::ios::trunc);
44
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 out.write((char *)(&n), sizeof(typename MatrixXX::Index));
45
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 out.write((char *)(&m), sizeof(typename MatrixXX::Index));
46
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 out.write((const char *)c.data(), n * sizeof(typename MatrixXX::Scalar));
47
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 out.write((const char *)lb.data(), n * sizeof(typename MatrixXX::Scalar));
48
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 out.write((const char *)ub.data(), n * sizeof(typename MatrixXX::Scalar));
49
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 out.write((const char *)A.data(), m * n * sizeof(typename MatrixXX::Scalar));
50
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 out.write((const char *)Alb.data(), m * sizeof(typename MatrixXX::Scalar));
51
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 out.write((const char *)Aub.data(), m * sizeof(typename MatrixXX::Scalar));
52
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 out.close();
53 1 return true;
54 1 }
55
56 15 bool Solver_LP_abstract::readLpFromFile(const std::string &filename, VectorX &c,
57 VectorX &lb, VectorX &ub, MatrixXX &A,
58 VectorX &Alb, VectorX &Aub) {
59
1/2
✓ Branch 3 taken 15 times.
✗ Branch 4 not taken.
15 std::ifstream in(filename.c_str(), std::ios::in | std::ios::binary);
60 15 typename MatrixXX::Index n = 0, m = 0;
61
1/2
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
15 in.read((char *)(&n), sizeof(typename MatrixXX::Index));
62
1/2
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
15 in.read((char *)(&m), sizeof(typename MatrixXX::Index));
63
1/2
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
15 c.resize(n);
64
1/2
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
15 lb.resize(n);
65
1/2
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
15 ub.resize(n);
66
1/2
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
15 A.resize(m, n);
67
1/2
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
15 Alb.resize(m);
68
1/2
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
15 Aub.resize(m);
69
2/4
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 15 times.
✗ Branch 5 not taken.
15 in.read((char *)c.data(), n * sizeof(typename MatrixXX::Scalar));
70
2/4
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 15 times.
✗ Branch 5 not taken.
15 in.read((char *)lb.data(), n * sizeof(typename MatrixXX::Scalar));
71
2/4
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 15 times.
✗ Branch 5 not taken.
15 in.read((char *)ub.data(), n * sizeof(typename MatrixXX::Scalar));
72
2/4
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 15 times.
✗ Branch 5 not taken.
15 in.read((char *)A.data(), m * n * sizeof(typename MatrixXX::Scalar));
73
2/4
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 15 times.
✗ Branch 5 not taken.
15 in.read((char *)Alb.data(), m * sizeof(typename MatrixXX::Scalar));
74
2/4
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 15 times.
✗ Branch 5 not taken.
15 in.read((char *)Aub.data(), m * sizeof(typename MatrixXX::Scalar));
75
1/2
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
15 in.close();
76 15 return true;
77 15 }
78
79 LP_status Solver_LP_abstract::solve(const std::string &filename,
80 Ref_vectorX sol) {
81 VectorX c, lb, ub, Alb, Aub;
82 MatrixXX A;
83 if (!readLpFromFile(filename, c, lb, ub, A, Alb, Aub)) {
84 SEND_ERROR_MSG("Error while reading LP from file " + string(filename));
85 return LP_STATUS_ERROR;
86 }
87 return solve(c, lb, ub, A, Alb, Aub, sol);
88 }
89
90 bool Solver_LP_abstract::setMaximumIterations(unsigned int maxIter) {
91 if (maxIter == 0) return false;
92 m_maxIter = maxIter;
93 return true;
94 }
95
96 bool Solver_LP_abstract::setMaximumTime(double seconds) {
97 if (seconds <= 0.0) return false;
98 m_maxTime = seconds;
99 return true;
100 }
101
102 } // end namespace centroidal_dynamics
103