ecos_bb.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * ECOS - Embedded Conic Solver.
  3. * Copyright (C) 2012-2015 A. Domahidi [domahidi@embotech.com],
  4. * Automatic Control Lab, ETH Zurich & embotech GmbH, Zurich, Switzerland.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /*
  20. * The branch and bound module is (c) Han Wang, Stanford University,
  21. * [hanwang2@stanford.edu]
  22. *
  23. * Extended with improved branching rules by Pascal Lüscher, student of FHNW
  24. * [luescherpascal@gmail.com]
  25. */
  26. #ifndef __ecos_bb_H__
  27. #define __ecos_bb_H__
  28. #include "ecos.h"
  29. #include "spla.h"
  30. #include "glblopts.h"
  31. /* Print verbosity */
  32. #define MI_PRINTLEVEL (1)
  33. /* ecos_bb configuration settings */
  34. #define MI_ABS_EPS (1E-6)
  35. #define MI_REL_EPS (1E-3)
  36. #define MI_MAXITER (1000)
  37. #define MI_INT_TOL (FTOL_INACC)
  38. /* Flags */
  39. #define MI_SOLVED_NON_BRANCHABLE (3)
  40. #define MI_SOLVED_BRANCHABLE (2)
  41. #define MI_NOT_SOLVED (1)
  42. #define MI_FREE (0)
  43. #define MI_ONE (1)
  44. #define MI_ZERO (0)
  45. #define MI_STAR (-1)
  46. /*** Exit flags ***/
  47. /*ECOS_BB found optimal solution*/
  48. #define MI_OPTIMAL_SOLN (ECOS_OPTIMAL)
  49. /*ECOS_BB proved problem is infeasible*/
  50. #define MI_INFEASIBLE (ECOS_PINF)
  51. /*ECOS_BB proved problem is unbounded*/
  52. #define MI_UNBOUNDED (ECOS_DINF)
  53. /*ECOS_BB hit maximum iterations but a feasible solution was found and the best seen feasible solution was returned*/
  54. #define MI_MAXITER_FEASIBLE_SOLN (ECOS_OPTIMAL + ECOS_INACC_OFFSET)
  55. /*ECOS_BB hit maximum iterations without finding a feasible solution*/
  56. #define MI_MAXITER_NO_SOLN (ECOS_PINF + ECOS_INACC_OFFSET)
  57. /*ECOS_BB hit maximum iterations without finding a feasible solution that was unbounded*/
  58. #define MI_MAXITER_UNBOUNDED (ECOS_DINF + ECOS_INACC_OFFSET)
  59. /* Max integer and all smaller integer representable by single precision */
  60. #define MAX_FLOAT_INT (8388608)
  61. #ifdef __cplusplus
  62. extern "C"
  63. {
  64. #endif
  65. enum BRANCHING_STRATEGY
  66. {
  67. BRANCHING_STRATEGY_MOST_INFEASIBLE = 0,
  68. BRANCHING_STRATEGY_STRONG_BRANCHING = 1,
  69. BRANCHING_STRATEGY_PSEUDOCOST_BRANCHING = 2,
  70. BRANCHING_STRATEGY_RELIABILITY = 3,
  71. BRANCHING_STRATEGY_RANDOM = 4
  72. };
  73. enum NODE_SELECTION_METHOD
  74. {
  75. BREADTH_FIRST = 0,
  76. DIVE_LOWER_NODE = 1,
  77. DIVE_UPPER_NODE = 2,
  78. };
  79. typedef struct settings_bb
  80. {
  81. idxint maxit; /* maximum number of iterations */
  82. idxint verbose; /* verbosity bool for PRINTLEVEL < 3 */
  83. pfloat abs_tol_gap; /* termination criteria |U-L| */
  84. pfloat rel_tol_gap; /* termination criteria for |U-L|/|L| < 3 */
  85. pfloat integer_tol; /* integer rounding tolerance */
  86. enum BRANCHING_STRATEGY branching_strategy;
  87. idxint reliable_eta; /* number of pseudocost values needed for costs to be reliable */
  88. enum NODE_SELECTION_METHOD node_selection_method;
  89. } settings_bb;
  90. typedef struct node
  91. {
  92. char status;
  93. pfloat L;
  94. pfloat U;
  95. pfloat relaxation;
  96. idxint split_idx;
  97. pfloat split_val;
  98. idxint prev_split_idx;
  99. pfloat prev_split_val;
  100. pfloat prev_relaxation;
  101. int up_branch_node;
  102. } node;
  103. /* Wrapper for mixed integer module */
  104. typedef struct ecos_bb_pwork
  105. {
  106. /* Mixed integer data */
  107. idxint num_bool_vars;
  108. idxint num_int_vars;
  109. node *nodes;
  110. char *bool_node_ids;
  111. pfloat *int_node_ids;
  112. idxint *bool_vars_idx;
  113. idxint *int_vars_idx;
  114. /* ECOS data */
  115. pwork *ecos_prob;
  116. /* Modified pointers to ecos internals */
  117. /* Use these to edit or reset the h variables */
  118. spmat *A;
  119. spmat *G;
  120. pfloat *c;
  121. pfloat *b;
  122. pfloat *h;
  123. /* best iterate seen so far */
  124. /* variables */
  125. pfloat *x; /* primal variables */
  126. pfloat *y; /* multipliers for equality constaints */
  127. pfloat *z; /* multipliers for conic inequalities */
  128. pfloat *s; /* slacks for conic inequalities */
  129. pfloat kap; /* kappa (homogeneous embedding) */
  130. pfloat tau; /* tau (homogeneous embedding) */
  131. stats *info; /* info of best iterate */
  132. pfloat global_U;
  133. pfloat global_L;
  134. /* Tmp data */
  135. char *tmp_bool_node_id;
  136. pfloat *tmp_int_node_id;
  137. idxint iter;
  138. idxint dive_node_id;
  139. /* Tmp nodes used for strong branching */
  140. char *tmp_branching_bool_node_id;
  141. pfloat *tmp_branching_int_node_id;
  142. /* Pseudocost branching values */
  143. pfloat *pseudocost_bin_sum;
  144. pfloat *pseudocost_int_sum;
  145. idxint *pseudocost_bin_cnt;
  146. idxint *pseudocost_int_cnt;
  147. /* Stored pointers to prevent memory leaks */
  148. pfloat *Gpr_new;
  149. idxint *Gjc_new;
  150. idxint *Gir_new;
  151. pfloat *h_new;
  152. /* settings struct */
  153. settings *ecos_stgs;
  154. settings_bb *stgs;
  155. idxint default_settings;
  156. } ecos_bb_pwork;
  157. ecos_bb_pwork *ECOS_BB_setup(
  158. idxint n, idxint m, idxint p,
  159. idxint l, idxint ncones, idxint *q, idxint nex,
  160. pfloat *Gpr, idxint *Gjc, idxint *Gir,
  161. pfloat *Apr, idxint *Ajc, idxint *Air,
  162. pfloat *c, pfloat *h, pfloat *b,
  163. idxint num_bool_vars, idxint *bool_vars_idx,
  164. idxint num_int_vars, idxint *int_vars_idx,
  165. settings_bb *stgs);
  166. idxint ECOS_BB_solve(ecos_bb_pwork *prob);
  167. void ECOS_BB_cleanup(ecos_bb_pwork *prob, idxint num_vars_keep);
  168. void updateDataEntry_h(ecos_bb_pwork *w, idxint idx, pfloat value);
  169. void updateDataEntry_c(ecos_bb_pwork *w, idxint idx, pfloat value);
  170. settings_bb *get_default_ECOS_BB_settings();
  171. /* Calculate the offset into the node_id array */
  172. static char *get_bool_node_id(idxint idx, ecos_bb_pwork *prob)
  173. {
  174. return &prob->bool_node_ids[prob->num_bool_vars * idx];
  175. }
  176. static pfloat *get_int_node_id(idxint idx, ecos_bb_pwork *prob)
  177. {
  178. return &prob->int_node_ids[prob->num_int_vars * idx * 2];
  179. }
  180. static pfloat abs_2(pfloat number)
  181. {
  182. return number < 0.0 ? -number : number;
  183. }
  184. static pfloat pfloat_round(pfloat number)
  185. {
  186. return (number >= 0) ? (int)(number + 0.5) : (int)(number - 0.5);
  187. }
  188. static pfloat pfloat_ceil(pfloat number, pfloat integer_tol)
  189. {
  190. return (pfloat)(number < 0 ? (int)number : (int)(number + (1 - integer_tol)));
  191. }
  192. static pfloat pfloat_floor(pfloat number, pfloat integer_tol)
  193. {
  194. return (pfloat)(number < 0 ? (int)(number - (1 - integer_tol)) : (int)number);
  195. }
  196. static idxint float_eqls(pfloat a, pfloat b, pfloat integer_tol)
  197. {
  198. return abs_2(a - b) < integer_tol;
  199. }
  200. #ifdef __cplusplus
  201. }
  202. #endif
  203. #endif