PIPS-NLP
parallelPipsNlp_C_Callback.h
Go to the documentation of this file.
1 /* PIPS-NLP *
2  * Author: Feng Qiang and Cosmin G. Petra *
3  * (C) 2016 Argonne National Laboratory */
4 
5 #ifndef _PARALLELPIPSNLP_C_CALLBACK_H__
6 #define _PARALLELPIPSNLP_C_CALLBACK_H__
7 
8 
9 #include "mpi.h"
10 
11 typedef void * UserDataPtr;
12 
13 /*
14  * prob points to the userdata field of the PipsNlpProblemStruct.
15  * row_node_id and col_node_id are the node ids in the stochastic tree
16  *
17  * The row and col node ids are used to identify the block in problem.
18  * When vectors are requested, the row and col ids should be equal.
19  * eval_f_grad is an exception. See below for details.
20  * When the requested data are sub-matrices, the row and col node ids are
21  * used to identify the block.
22  */
23 typedef struct CallBackData
24 {
28  int typeflag; //typeflag 0 is for traditional bound and jacobian information and type flag 1 is reserved for linking constraints
29 } CallBackData;
30 
32 
33 /*
34  * This function is used to set the initial variable values to PIPS-NLP at the node Id specified in the CallBackData.
35  * x0 should be already allocated for the size of declared variable in this node.
36  * cbd is the call back data pointer.
37  *
38  * note: row and col node ids from the cbd should always be equal.
39  */
40 extern "C" typedef int (*str_init_x0_cb)(double* x0, CallBackDataPtr cbd);
41 
42 /*
43  * This function is used to request the values and sizes of the col and row's lower and upper bound vectors.
44  * The sizes of col and row are represented by n and m, respectively.
45  * To get n and m, set the vectors pointers to NULL.
46  * The vectors of the corresponding node id is given in the CallBackData.
47  *
48  * note: when providing the row_lb and row_ub vectors, it assumes that the equality constraints should be at the top of these vectors.
49  * Therefore, it is wise to order the constraint in a way that always keeps the equality constraints in front of the
50  * inequality constraints to keep everything else consistent.
51  */
52 extern "C" typedef int (*str_prob_info_cb)(int* n, double* col_lb, double* col_up, int* m, double* row_lb, double* row_up, CallBackDataPtr cbd);
53 
54 /*
55  * This function evaluates the value of the objective function declared at the node whose Id is specified in the CallBackData.
56  * x0 and x1 corresponds the values for first stage and second stage variables.
57  * If the objective function on the root node (i.e. the only node in first stage) is requested, x0 and x1 are the same and both point to
58  * the first stage variable values.
59  */
60 extern "C" typedef int (*str_eval_f_cb)(double* x0, double* x1, double *obj, CallBackDataPtr cbd);
61 
62 /*
63  * This function computes the constraint value at current x0 and x1, where x0 and x1 are the first and second stage variable values respectively.
64  * The results should be written in eq_g (for equality constraints) and inq_g (for inequality constraints).
65  * If the constraints to be evaluated are in the root node, x0 and x1 are the same and both point to the first stage variable values.
66  *
67  */
68 extern "C" typedef int (*str_eval_g_cb)(double* x0, double* x1, double* eq_g,double* inq_g, CallBackDataPtr cbd);
69 
70 /*
71  * This function evaluates the objective gradient sub-vector. The objective function to be considered is declared in node with node id equals to row_node_id
72  * from the CallBackData. The gradient sub-vector is the one with respect to the variables declared in node id which equals to col_node_id from the CallBackData.
73  *
74  * Note:
75  * evaluating 1st stage objective gradient, x0 and x1 are the same and both point to the first stage variable values.
76  * evaluating 2st stage objective gradient, x0 and x1 represent the first and second stage variable values respectively.
77  * the objective declared in the second stage node could contains a cross product term of first and second stage variables. This term can contribute
78  * two gradient sub-vectors, where one is with respect to the first stage variables and the other is with respect to the second stage variables.
79  * For example, let us look at the objective function declared in the node 1. By setting (1,0) in the CallBackData for row_node_id and col_node_id,
80  * this function should compute the gradient sub-vector with respect to the root stage variables (where node id is 0).
81  * The gradient sub-vector with respect to the second stage variables should be computed when the row and col node id pair (in the CallBackData)
82  * equals to (1,1).
83  *
84  */
85 extern "C" typedef int (*str_eval_grad_f_cb)(double* x0, double* x1, double* vec_grad_f, CallBackDataPtr cbd);
86 
87 /*
88  * This function evaluates the structures or values of the sub-matrix of the Jacobian in two separate CCS constructs. One is for the
89  * equality constraints and the other is for the inequality constraints.
90  * The Jacobian sub-block is identified by the row_node_id and col_node_id given in the CallBackData.
91  * When setting e_elts and i_elts to NULL, the function should write back the number of non-zeros to e_nz (for equality constraints)
92  * and i_nz (for inequality constraints).
93  * When e_elts or i_elts is not NULL, e_nz and i_nz should have the correct values set already by the solver. The function should
94  * now compute the two Jacobian sub-blocks and write them to the value points triples whose memory is already allocated by the solver.
95  *
96  * (e_elts, e_rowidx, e_colptr) represents the Jacobian block of the equality constraints
97  * (i_elts, i_rowidx, i_colptr) represents the Jacobian block of the inequality constraints
98  */
99 extern "C" typedef int (*str_eval_jac_g_cb)(double* x0, double* x1,
100  int* e_nz, double* e_elts, int* e_rowidx, int *e_colptr,
101  int* i_nz, double* i_elts, int* i_rowidx, int *i_colptr,
102  CallBackDataPtr cbd);
103 
104 /*
105  * This function evaluates the structure or values of the Hessian of the Lagrangian matrix in the CCS format.
106  * Since PIPS-NLP does not support linking constraint, the root stage node only produces the diagonal sub-block for the Hessian
107  * of the Lagrangian matrix.
108  * For example, by setting (0,0) pair for row and col node id in the CallBackData, this function should compute the above mentioned diagonal block.
109  *
110  * Each second stage constraints can produce three sub-blocks for the Hessian of the Lagrangian matrix, i.e. the diagonal block,
111  * the border block and the block that contributes to the root diagonal block.
112  * For example, let us consider the Hessian of the Lagrangian matrix in the following structure (for a two stage n scenario stochastic programming problem),
113  * where H_00 is called the root diagonal block and H_0i is called the border blocks for i \in {1...n}.
114  *
115  * H_00
116  * H_01 H_11
117  * H_02 H_22
118  * H_03 H33
119  * . .
120  * . .
121  * H_0n H_nn
122  *
123  * The diagonal block: this can be requested by setting (1,1), (2,2) etc. in the CallBackData for row and col node id.
124  * The border block : this can be requested by setting (0,1), (0,2) etc. in the CallBackData for row and col node id.
125  * The root diagonal contribution: this can be requested by setting (1,0), (2,0) and etc. in the CallBackData for row and col node id.
126  * Then, PIPS-NLP will use MPI_Allreduce to compute the final H_00 blocks.
127  *
128  * Of course, the nz values of the above mentioned blocks should be computed when setting elts to NULL pointer.
129  */
130 extern "C" typedef int (*str_eval_h_cb)(double* x0, double* x1, double* lamdba,
131  int* nz, double* elts, int* rowidx, int *colptr,
132  CallBackDataPtr cbd);
133 
134 /*
135  * write solution when it is done.
136  */
137 extern "C" typedef int (*str_write_solution_cb)(double* x, double* lam_eq, double* lam_ieq, CallBackDataPtr cbd);
138 
139 
140 extern "C"
141 {
143  {
144  MPI_Comm comm;
145  int nscen;
155  // holders for optimal solution and objective
156  double objective;
157  int nvars;
158  int ncons;
159  };
162  PipsNlpProblemStructPtr
164  int nscen,
174  );
175 
177 
179 
180  /*
181  * Get primal-dual solution corresponding to node Id specified in the CallBackData.
182  * The vector for primal (x) and dual variables (lam_eq and lam_ieq) should be allocated
183  * when calling the function.
184  */
185  int get_x(CallBackDataPtr cbd,double* x, double* lam_eq, double* lam_ieq);
186 
187  /*
188  * Get the objective value
189  */
191 
192  /*
193  * Get the number of total variables
194  */
196 
197  /*
198  * Get the number of total constraints
199  */
201 };
202 #endif
203 
int(* str_eval_grad_f_cb)(double *x0, double *x1, double *vec_grad_f, CallBackDataPtr cbd)
Definition: parallelPipsNlp_C_Callback.h:85
int(* str_write_solution_cb)(double *x, double *lam_eq, double *lam_ieq, CallBackDataPtr cbd)
Definition: parallelPipsNlp_C_Callback.h:137
struct PipsNlpProblemStruct * PipsNlpProblemStructPtr
Definition: parallelPipsNlp_C_Callback.h:160
int typeflag
Definition: parallelPipsNlp_C_Callback.h:28
int(* str_init_x0_cb)(double *x0, CallBackDataPtr cbd)
Definition: parallelPipsNlp_C_Callback.h:40
str_eval_f_cb eval_f
Definition: parallelPipsNlp_C_Callback.h:148
int(* str_eval_f_cb)(double *x0, double *x1, double *obj, CallBackDataPtr cbd)
Definition: parallelPipsNlp_C_Callback.h:60
str_eval_h_cb eval_h
Definition: parallelPipsNlp_C_Callback.h:152
void * UserDataPtr
Definition: NlpInfoCallBack.h:15
UserDataPtr prob
Definition: parallelPipsNlp_C_Callback.h:25
PipsNlpProblemStructPtr CreatePipsNlpProblemStruct(MPI_Comm comm, int nscen, str_init_x0_cb init_x0, str_prob_info_cb prob_info, str_eval_f_cb eval_f, str_eval_g_cb eval_g, str_eval_grad_f_cb eval_grad_f, str_eval_jac_g_cb eval_jac_g, str_eval_h_cb eval_h, str_write_solution_cb write_solution, UserDataPtr userdata)
Definition: parallelPipsNlp_C_Callback.cpp:51
Definition: parallelPipsNlp_C_Callback.h:23
str_init_x0_cb init_x0
Definition: parallelPipsNlp_C_Callback.h:146
int(* str_eval_g_cb)(double *x0, double *x1, double *eq_g, double *inq_g, CallBackDataPtr cbd)
Definition: parallelPipsNlp_C_Callback.h:68
int(* str_prob_info_cb)(int *n, double *col_lb, double *col_up, int *m, double *row_lb, double *row_up, CallBackDataPtr cbd)
Definition: parallelPipsNlp_C_Callback.h:52
double PipsNlpProblemStructGetObjective(PipsNlpProblemStruct *prob)
Definition: parallelPipsNlp_C_Callback.cpp:181
UserDataPtr userdata
Definition: parallelPipsNlp_C_Callback.h:154
int nvars
Definition: parallelPipsNlp_C_Callback.h:157
double objective
Definition: parallelPipsNlp_C_Callback.h:156
Definition: parallelPipsNlp_C_Callback.h:142
int PipsNlpProblemStructGetTotalCons(PipsNlpProblemStruct *prob)
Definition: parallelPipsNlp_C_Callback.cpp:199
CallBackData * CallBackDataPtr
Definition: parallelPipsNlp_C_Callback.h:31
str_eval_g_cb eval_g
Definition: parallelPipsNlp_C_Callback.h:149
int ncons
Definition: parallelPipsNlp_C_Callback.h:158
str_eval_grad_f_cb eval_grad_f
Definition: parallelPipsNlp_C_Callback.h:150
int(* str_eval_jac_g_cb)(double *x0, double *x1, int *e_nz, double *e_elts, int *e_rowidx, int *e_colptr, int *i_nz, double *i_elts, int *i_rowidx, int *i_colptr, CallBackDataPtr cbd)
Definition: parallelPipsNlp_C_Callback.h:99
int(* str_eval_h_cb)(double *x0, double *x1, double *lamdba, int *nz, double *elts, int *rowidx, int *colptr, CallBackDataPtr cbd)
Definition: parallelPipsNlp_C_Callback.h:130
int row_node_id
Definition: parallelPipsNlp_C_Callback.h:26
int PipsNlpSolveStruct(PipsNlpProblemStruct *prob)
Definition: parallelPipsNlp_C_Callback.cpp:100
str_write_solution_cb write_solution
Definition: parallelPipsNlp_C_Callback.h:153
int nscen
Definition: parallelPipsNlp_C_Callback.h:145
str_eval_jac_g_cb eval_jac_g
Definition: parallelPipsNlp_C_Callback.h:151
struct CallBackData CallBackData
int col_node_id
Definition: parallelPipsNlp_C_Callback.h:27
void FreePipsNlpProblemStruct(PipsNlpProblemStruct *prob)
Definition: parallelPipsNlp_C_Callback.cpp:168
str_prob_info_cb prob_info
Definition: parallelPipsNlp_C_Callback.h:147
void * UserDataPtr
Definition: parallelPipsNlp_C_Callback.h:11
int PipsNlpProblemStructGetTotalVars(PipsNlpProblemStruct *prob)
Definition: parallelPipsNlp_C_Callback.cpp:190
MPI_Comm comm
Definition: parallelPipsNlp_C_Callback.h:144
int get_x(CallBackDataPtr cbd, double *x, double *lam_eq, double *lam_ieq)
Definition: parallelPipsNlp_C_Callback.cpp:174