hpipm icon indicating copy to clipboard operation
hpipm copied to clipboard

API improvement proposal: OCP QP size as an array of structs

Open mkatliar opened this issue 6 years ago • 1 comments

Numerous hpipm functions take the arguments that define a size of an OCP QP, for example:

int d_memsize_ocp_qp(int N, int *nx, int *nu, int *nb, int *ng, int *ns);

This creates several inconveniences:

  • At least 5 arrays must be declared and their memory properly (de-)allocated;
  • The functions have 5 arguments of the same type, which must be matched by their order. It is easy to make a mistake by mixing the order of arguments.
  • Every function that needs OCP QP sizes has 5 additional arguments.
  • If the way the size is defined changes (for example, ns is added), it changes the prototype of all functions which take size as an argument.

This can be simplified by putting the sizes corresponding to a single OCP QP stage in a struct:

struct ocp_qp_size
{
    int nx;
    int nu;
    int nb;
    int ng;
    int ns;
};

Then, the prototype of the function above and similar functions will change to

int d_memsize_ocp_qp(int N, ocp_qp_size const * size);

which is much shorter. Furthermore, the chance of confusing different components of the size decreases because of the explicit names in the assignments:

ocp_qp_size size[N];

for (int i = 0; i < N; ++i)
{
    size[i].nx = NX;
    size[i].nu = NU;
    size[i].nb = 1;
    size[i].ng = 0;
    size[i].ns = 0;
}

d_memsize_ocp_qp(N, size);

mkatliar avatar Oct 12 '17 17:10 mkatliar

I implemented a structure that holds the size of an ocp qp. It is a struct of arrays instead of an array of structs, but it makes its job anyway ;)

giaf avatar Nov 02 '17 12:11 giaf