00001 #include <iostream>
00002 #include <qpOASES.hpp>
00003 #include <map>
00004
00005 using namespace qpOASES;
00006
00007
00008 double* solve_qpoases_qp_common (double* ret,
00009 double* eval_weight_matrix, double* eval_coeff_vector,
00010 double* state_min_vector, double* state_max_vector,
00011 double* inequality_matrix, double* inequality_min_vector, double* inequality_max_vector,
00012 int state_len, int inequality_len, PrintLevel print_level, double* ret_status,
00013 bool solve_lp) {
00014 real_t* H = new real_t[state_len*state_len];
00015 real_t* A = new real_t[inequality_len*state_len];
00016 real_t* g = new real_t[state_len];
00017 real_t* ub = new real_t[state_len];
00018 real_t* lb = new real_t[state_len];
00019 real_t* ubA = new real_t[inequality_len];
00020 real_t* lbA = new real_t[inequality_len];
00021 if (!solve_lp) {
00022 for (int i = 0; i < state_len*state_len; i++) {
00023 H[i] = eval_weight_matrix[i];
00024 }
00025 } else {
00026 for (int i = 0; i < state_len*state_len; i++) {
00027 H[i] = 0.0;
00028 }
00029 }
00030 for (int i = 0; i < state_len; i++) {
00031 ub[i] = state_max_vector[i];
00032 }
00033 for (int i = 0; i < state_len; i++) {
00034 lb[i] = state_min_vector[i];
00035 }
00036 for (int i = 0; i < state_len; i++) {
00037 g[i] = eval_coeff_vector[i];
00038 }
00039 for (int i = 0; i < inequality_len; i++) {
00040 ubA[i] = inequality_max_vector[i];
00041 }
00042 for (int i = 0; i < inequality_len; i++) {
00043 lbA[i] = inequality_min_vector[i];
00044 }
00045 for (int i = 0; i < inequality_len*state_len; i++) {
00046 A[i] = inequality_matrix[i];
00047 }
00048
00049 QProblem example( state_len,inequality_len, (solve_lp ? HST_ZERO : HST_UNKNOWN));
00050 Options options;
00051 options.printLevel = print_level;
00052 example.setOptions( options );
00053
00054 int nWSR = 1000;
00055 returnValue status = example.init( H,g,A,lb,ub,lbA,ubA, nWSR );
00056
00057 ret_status[0] = getSimpleStatus(status, (print_level != PL_NONE)? BT_TRUE:BT_FALSE);
00058
00059 real_t* xOpt = new real_t[state_len];
00060 example.getPrimalSolution( xOpt );
00061 for (int i = 0; i < state_len; i++) {
00062 ret[i] = xOpt[i];
00063 }
00064 delete[] H;
00065 delete[] A;
00066 delete[] g;
00067 delete[] ub;
00068 delete[] lb;
00069 delete[] ubA;
00070 delete[] lbA;
00071 delete[] xOpt;
00072
00073 return ret;
00074 }
00075
00076
00077
00078
00079
00080 std::map<std::pair<int, int>, QProblem*> qp_map;
00081 double* solve_qpoases_qp_with_hotstart_common (double* ret,
00082 double* eval_weight_matrix, double* eval_coeff_vector,
00083 double* state_min_vector, double* state_max_vector,
00084 double* inequality_matrix, double* inequality_min_vector, double* inequality_max_vector,
00085 int state_len, int inequality_len, PrintLevel print_level, double* ret_status,
00086 bool solve_lp) {
00087 real_t* H = new real_t[state_len*state_len];
00088 real_t* A = new real_t[inequality_len*state_len];
00089 real_t* g = new real_t[state_len];
00090 real_t* ub = new real_t[state_len];
00091 real_t* lb = new real_t[state_len];
00092 real_t* ubA = new real_t[inequality_len];
00093 real_t* lbA = new real_t[inequality_len];
00094 if (!solve_lp) {
00095 for (int i = 0; i < state_len*state_len; i++) {
00096 H[i] = eval_weight_matrix[i];
00097 }
00098 } else {
00099 for (int i = 0; i < state_len*state_len; i++) {
00100 H[i] = 0.0;
00101 }
00102 }
00103 for (int i = 0; i < state_len; i++) {
00104 ub[i] = state_max_vector[i];
00105 }
00106 for (int i = 0; i < state_len; i++) {
00107 lb[i] = state_min_vector[i];
00108 }
00109 for (int i = 0; i < state_len; i++) {
00110 g[i] = eval_coeff_vector[i];
00111 }
00112 for (int i = 0; i < inequality_len; i++) {
00113 ubA[i] = inequality_max_vector[i];
00114 }
00115 for (int i = 0; i < inequality_len; i++) {
00116 lbA[i] = inequality_min_vector[i];
00117 }
00118 for (int i = 0; i < inequality_len*state_len; i++) {
00119 A[i] = inequality_matrix[i];
00120 }
00121
00122 QProblem* example;
00123 std::pair<int, int> tmp_pair(state_len, inequality_len);
00124 std::map<std::pair<int, int>, QProblem*>::iterator it = qp_map.find(tmp_pair);
00125 bool is_initial = (it == qp_map.end());
00126 if (is_initial) {
00127 example = new QProblem ( state_len,inequality_len, (solve_lp ? HST_ZERO : HST_UNKNOWN));
00128 qp_map.insert(std::pair<std::pair<int, int>, QProblem*>(tmp_pair, example));
00129 } else {
00130 example = it->second;
00131 }
00132
00133 Options options;
00134 options.printLevel = print_level;
00135 example->setOptions( options );
00136 int nWSR = 1000;
00137 returnValue status;
00138 if (is_initial) {
00139
00140 status = example->init( H,g,A,lb,ub,lbA,ubA, nWSR );
00141 } else {
00142
00143 status = example->hotstart( g,lb,ub,lbA,ubA, nWSR );
00144 }
00145
00146
00147 ret_status[0] = getSimpleStatus(status, (print_level != PL_NONE)? BT_TRUE:BT_FALSE);
00148 if (example->isSolved() == BT_FALSE && ret_status[0] == 0) ret_status[0] = -100;
00149
00150 real_t* xOpt = new real_t[state_len];
00151 example->getPrimalSolution( xOpt );
00152 for (int i = 0; i < state_len; i++) {
00153 ret[i] = xOpt[i];
00154 }
00155
00156 if ( ret_status[0] != 0 ) {
00157 delete qp_map[tmp_pair];
00158 qp_map.erase(tmp_pair);
00159 }
00160
00161 delete[] H;
00162 delete[] A;
00163 delete[] g;
00164 delete[] ub;
00165 delete[] lb;
00166 delete[] ubA;
00167 delete[] lbA;
00168 delete[] xOpt;
00169
00170 return ret;
00171 }
00172
00173
00174
00175
00176 std::map<std::pair<int, int>, SQProblem*> sqp_map;
00177 double* solve_qpoases_sqp_with_hotstart_common (double* ret,
00178 double* eval_weight_matrix, double* eval_coeff_vector,
00179 double* state_min_vector, double* state_max_vector,
00180 double* inequality_matrix, double* inequality_min_vector, double* inequality_max_vector,
00181 int state_len, int inequality_len, PrintLevel print_level, double* ret_status,
00182 bool solve_lp) {
00183 real_t* H = new real_t[state_len*state_len];
00184 real_t* A = new real_t[inequality_len*state_len];
00185 real_t* g = new real_t[state_len];
00186 real_t* ub = new real_t[state_len];
00187 real_t* lb = new real_t[state_len];
00188 real_t* ubA = new real_t[inequality_len];
00189 real_t* lbA = new real_t[inequality_len];
00190 if (!solve_lp) {
00191 for (int i = 0; i < state_len*state_len; i++) {
00192 H[i] = eval_weight_matrix[i];
00193 }
00194 } else {
00195 for (int i = 0; i < state_len*state_len; i++) {
00196 H[i] = 0.0;
00197 }
00198 }
00199 for (int i = 0; i < state_len; i++) {
00200 ub[i] = state_max_vector[i];
00201 }
00202 for (int i = 0; i < state_len; i++) {
00203 lb[i] = state_min_vector[i];
00204 }
00205 for (int i = 0; i < state_len; i++) {
00206 g[i] = eval_coeff_vector[i];
00207 }
00208 for (int i = 0; i < inequality_len; i++) {
00209 ubA[i] = inequality_max_vector[i];
00210 }
00211 for (int i = 0; i < inequality_len; i++) {
00212 lbA[i] = inequality_min_vector[i];
00213 }
00214 for (int i = 0; i < inequality_len*state_len; i++) {
00215 A[i] = inequality_matrix[i];
00216 }
00217
00218 SQProblem* example;
00219 std::pair<int, int> tmp_pair(state_len, inequality_len);
00220 std::map<std::pair<int, int>, SQProblem*>::iterator it = sqp_map.find(tmp_pair);
00221 bool is_initial = (it == sqp_map.end());
00222 if (is_initial) {
00223 example = new SQProblem ( state_len,inequality_len, (solve_lp ? HST_ZERO : HST_UNKNOWN));
00224 sqp_map.insert(std::pair<std::pair<int, int>, SQProblem*>(tmp_pair, example));
00225 } else {
00226 example = it->second;
00227 }
00228
00229 Options options;
00230 options.printLevel = print_level;
00231 example->setOptions( options );
00232 int nWSR = 1000;
00233 returnValue status;
00234 if (is_initial) {
00235
00236 status = example->init( H,g,A,lb,ub,lbA,ubA, nWSR );
00237 } else {
00238
00239 status = example->hotstart( H,g,A,lb,ub,lbA,ubA, nWSR );
00240 }
00241
00242
00243 ret_status[0] = getSimpleStatus(status, (print_level != PL_NONE)? BT_TRUE:BT_FALSE);
00244 if (example->isSolved() == BT_FALSE && ret_status[0] == 0) ret_status[0] = -100;
00245
00246 real_t* xOpt = new real_t[state_len];
00247 example->getPrimalSolution( xOpt );
00248 for (int i = 0; i < state_len; i++) {
00249 ret[i] = xOpt[i];
00250 }
00251
00252 if ( ret_status[0] != 0 ) {
00253 delete sqp_map[tmp_pair];
00254 sqp_map.erase(tmp_pair);
00255 }
00256
00257 delete[] H;
00258 delete[] A;
00259 delete[] g;
00260 delete[] ub;
00261 delete[] lb;
00262 delete[] ubA;
00263 delete[] lbA;
00264 delete[] xOpt;
00265
00266 return ret;
00267 }
00268
00269 extern "C" {
00270 double* solve_qpoases_qp (double* ret,
00271 double* eval_weight_matrix, double* eval_coeff_vector,
00272 double* state_min_vector, double* state_max_vector,
00273 double* inequality_matrix, double* inequality_min_vector, double* inequality_max_vector,
00274 int state_len, int inequality_len, PrintLevel print_level, double* ret_status) {
00275 solve_qpoases_qp_common(ret,
00276 eval_weight_matrix, eval_coeff_vector,
00277 state_min_vector, state_max_vector,
00278 inequality_matrix, inequality_min_vector, inequality_max_vector,
00279 state_len, inequality_len, print_level, ret_status, false);
00280 };
00281
00282 double* solve_qpoases_lp (double* ret,
00283 double* eval_coeff_vector,
00284 double* state_min_vector, double* state_max_vector,
00285 double* inequality_matrix, double* inequality_min_vector, double* inequality_max_vector,
00286 int state_len, int inequality_len, PrintLevel print_level, double* ret_status) {
00287 solve_qpoases_qp_common(ret,
00288 NULL, eval_coeff_vector,
00289 state_min_vector, state_max_vector,
00290 inequality_matrix, inequality_min_vector, inequality_max_vector,
00291 state_len, inequality_len, print_level, ret_status, true);
00292 };
00293
00294 double* solve_qpoases_qp_with_hotstart (double* ret,
00295 double* eval_weight_matrix, double* eval_coeff_vector,
00296 double* state_min_vector, double* state_max_vector,
00297 double* inequality_matrix, double* inequality_min_vector, double* inequality_max_vector,
00298 int state_len, int inequality_len, PrintLevel print_level, double* ret_status) {
00299 solve_qpoases_qp_with_hotstart_common(ret,
00300 eval_weight_matrix, eval_coeff_vector,
00301 state_min_vector, state_max_vector,
00302 inequality_matrix, inequality_min_vector, inequality_max_vector,
00303 state_len, inequality_len, print_level, ret_status, false);
00304 };
00305
00306 double* solve_qpoases_lp_with_hotstart (double* ret,
00307 double* eval_coeff_vector,
00308 double* state_min_vector, double* state_max_vector,
00309 double* inequality_matrix, double* inequality_min_vector, double* inequality_max_vector,
00310 int state_len, int inequality_len, PrintLevel print_level, double* ret_status) {
00311 solve_qpoases_qp_with_hotstart_common(ret,
00312 NULL, eval_coeff_vector,
00313 state_min_vector, state_max_vector,
00314 inequality_matrix, inequality_min_vector, inequality_max_vector,
00315 state_len, inequality_len, print_level, ret_status, true);
00316 };
00317
00318 double* solve_qpoases_sqp_with_hotstart (double* ret,
00319 double* eval_weight_matrix, double* eval_coeff_vector,
00320 double* state_min_vector, double* state_max_vector,
00321 double* inequality_matrix, double* inequality_min_vector, double* inequality_max_vector,
00322 int state_len, int inequality_len, PrintLevel print_level, double* ret_status) {
00323 solve_qpoases_sqp_with_hotstart_common(ret,
00324 eval_weight_matrix, eval_coeff_vector,
00325 state_min_vector, state_max_vector,
00326 inequality_matrix, inequality_min_vector, inequality_max_vector,
00327 state_len, inequality_len, print_level, ret_status, false);
00328 };
00329
00330 double* solve_qpoases_slp_with_hotstart (double* ret,
00331 double* eval_coeff_vector,
00332 double* state_min_vector, double* state_max_vector,
00333 double* inequality_matrix, double* inequality_min_vector, double* inequality_max_vector,
00334 int state_len, int inequality_len, PrintLevel print_level, double* ret_status) {
00335 solve_qpoases_sqp_with_hotstart_common(ret,
00336 NULL, eval_coeff_vector,
00337 state_min_vector, state_max_vector,
00338 inequality_matrix, inequality_min_vector, inequality_max_vector,
00339 state_len, inequality_len, print_level, ret_status, true);
00340 };
00341 }