qp_lib.cpp
Go to the documentation of this file.
1 
2 #include <iostream>
3 #include <Eigen/Dense>
4 
5 #include "eiquadprog.hpp"
6 
7 using namespace Eigen;
8 
9 #define print(var) \
10  std::cout<<#var"= "<<std::endl<<var<<std::endl
11 
12 int flag = -1 ;
13 
14 extern "C" {
16  return flag ;
17 }
18 }
19 
20 extern "C" {
21 int check_constraints(double* CE, double* ce0, double* CI, double* ci0,
22  double* x, int x_len, int ce_len, int ci_len, double eqthre, double* ce_err,
23  double* ci_err) {
24  int ret = ce_len + ci_len ;
25  for (int i = 0; i < ce_len; i++) {
26  for (int j = 0; j < x_len; j++) {
27  ce_err[i] += CE[i * x_len + j] * x[j];
28  }
29  ce_err[i] += ce0[i];
30  if ( ce_err[i] < eqthre && ce_err[i] > -eqthre ) ret-- ;
31  }
32  for (int i = 0; i < ci_len; i++) {
33  for (int j = 0; j < x_len; j++) {
34  ci_err[i] += CI[i * x_len + j] * x[j];
35  }
36  ci_err[i] += ci0[i];
37  if ( ci_err[i] > -eqthre ) ret-- ;
38  }
39  return ret;
40 }
41 }
42 
43 
44 extern "C" {
45 double* solve_eiquadprog(double* G, double* g0, double* CE, double* ce0, double* CI,
46  double* ci0, double* x,
47  int x_len, int ce_len, int ci_len,
48  double eqthre,
49  int debug,
50  double* ret_buf, double* ce_err, double* ci_err) {
51  MatrixXd G_buf(x_len, x_len);
52  Eigen::VectorXd g0_buf(x_len);
53  MatrixXd CE_buf(x_len, ce_len);
54  Eigen::VectorXd ce0_buf(ce_len);
55  MatrixXd CI_buf(x_len, ci_len);
56  Eigen::VectorXd ci0_buf(ci_len);
57  Eigen::VectorXd x_buf(x_len);
58 
59  for ( int i=0 ; i<x_len ; i++ ){
60  for ( int j=0 ; j<x_len ; j++ ){
61  G_buf(i,j) = G[i+ x_len*j] ;
62 // std::cout << "(" << i << "," << j << ")" ;
63 // std::cout << " -" << i+x_len*j << "-" ;
64 // std::cout << " -> " << G[i+ x_len*j] << std::endl ;
65  if ( i == 0 ){
66  g0_buf(j) = g0[j] ;
67  x_buf(j) = x[j] ;
68  }
69  }
70  for ( int j=0 ; j<ce_len ; j++ ){
71  CE_buf(i,j) = CE[i+ x_len*j] ;
72  if ( i == 0 ){
73  ce0_buf(j) = ce0[j] ;
74  }
75  }
76  for ( int j=0 ; j<ci_len ; j++ ){
77  CI_buf(i,j) = CI[i+ x_len*j] ;
78  if ( i == 0 ){
79  ci0_buf(j) = ci0[j] ;
80  }
81  }
82  }
83 
84 
85  if (debug>1) {
86  print(G_buf);
87  print(g0_buf);
88  print(CE_buf);
89  print(ce0_buf);
90  print(CI_buf);
91  print(ci0_buf);
92  }
93 
94  ret_buf[0] = solve_quadprog(G_buf, g0_buf, CE_buf, ce0_buf, CI_buf,
95  ci0_buf, x_buf) ;
96  for (int i = 0; i < x_buf.size(); i++)
97  x[i] = x_buf(i) ;
98 
99  flag = check_constraints(CE, ce0, CI, ci0, x, x_len, ce_len, ci_len, eqthre, ce_err, ci_err);
100  //flag = check_constraints(CE, ce0, CI, ci0, x, x_len, eqthre, ce_len, ci_len, ce_err, ci_err);
101  if (debug>0) {
102  std::cout << "[eus-eiquadprog]" << std::endl;
103  std::cout << " :minimized-object "
104  << ret_buf[0] << std::endl;
105  std::cout << " :optimal-state [";
106  for (int i = 0; i < x_buf.size(); i++)
107  std::cout << x[i] << ' ';
108  std::cout << "]" << std::endl;
109 
110  std::cout << " :eq-constraint || " ;
111  for ( int i=0 ; i <ce_len ; i++ ) std::cout << ce_err[i] << " " ;
112  std::cout << "|| < " << eqthre ;
113  std::cout << std::endl ;
114  std::cout << " :iq-constraint [" ;
115  for ( int i=0 ; i <ci_len ; i++ ) std::cout << ci_err[i] << " " ;
116  std::cout << "] > " << -eqthre ;
117  std::cout << std::endl ;
118  std::cout << " :constraint-check " ;
119  std::cout << flag ;
120  std::cout << std::endl ;
121  }
122 
123  return x;
124 }
125 }
double solve_quadprog(MatrixXd &G, VectorXd &g0, const MatrixXd &CE, const VectorXd &ce0, const MatrixXd &CI, const VectorXd &ci0, VectorXd &x)
Definition: eiquadprog.hpp:131
double * solve_eiquadprog(double *G, double *g0, double *CE, double *ce0, double *CI, double *ci0, double *x, int x_len, int ce_len, int ci_len, double eqthre, int debug, double *ret_buf, double *ce_err, double *ci_err)
Definition: qp_lib.cpp:45
#define print(var)
Definition: qp_lib.cpp:9
int flag
Definition: qp_lib.cpp:12
int check_constraints(double *CE, double *ce0, double *CI, double *ci0, double *x, int x_len, int ce_len, int ci_len, double eqthre, double *ce_err, double *ci_err)
Definition: qp_lib.cpp:21
int get_constraints_check_flag()
Definition: qp_lib.cpp:15


eus_qp
Author(s):
autogenerated on Fri May 14 2021 02:51:43