Skip to content

Commit

Permalink
NL Writer C API: NLHeader_C #30
Browse files Browse the repository at this point in the history
Extract NLHeader definition into C structs
  • Loading branch information
glebbelov committed Nov 10, 2023
1 parent bcab9a4 commit f0e008c
Show file tree
Hide file tree
Showing 13 changed files with 1,432 additions and 264 deletions.
1 change: 1 addition & 0 deletions nl-writer2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ set(NLW2_LIB_FILES
${NLW2_SRC_PATH}/dtoa.c)
set(NLW2_INC_FILES
${NLW2_INCLUDE_PATH}/mp/nlsol.h
${NLW2_INCLUDE_PATH}/mp/nl-header-c.h
${NLW2_INCLUDE_PATH}/mp/nl-header.h
${NLW2_INCLUDE_PATH}/mp/nl-feeder2.h
${NLW2_INCLUDE_PATH}/mp/nl-writer2.h
Expand Down
127 changes: 127 additions & 0 deletions nl-writer2/examples/c/nlsol_ex_c_nl.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,140 @@

#include "nlsol_ex_c_nl.h"

NLHeader_C CAPI_ex_Header(void* pex_void) {
CAPIExample* pex = (CAPIExample*)pex_void;
NLHeader_C hdr;

hdr.pi.num_vars = pex->n_var;
hdr.pi.num_algebraic_cons = pex->n_con;
hdr.pi.num_objs = pex->n_obj;
hdr.pi.num_ranges = 0;
hdr.pi.num_eqns = 0;
hdr.pi.num_logical_cons = 0;

// Setting some other common data (more may be needed)

/** Total number of nonlinear constraints. */
hdr.pi.num_nl_cons = 0;
hdr.pi.num_nl_objs = 0;
hdr.pi.num_compl_conds = 0;
hdr.pi.num_nl_compl_conds = 0;
hdr.pi.num_compl_dbl_ineqs = 0;
hdr.pi.num_compl_vars_with_nz_lb = 0;

/** Number of nonlinear network constraints. */
hdr.pi.num_nl_net_cons = 0;
hdr.pi.num_linear_net_cons = 0;

/**
Number of nonlinear variables in constraints including nonlinear
variables in both constraints and objectives.
*/
hdr.pi.num_nl_vars_in_cons = 0;

/**
Number of nonlinear variables in objectives including nonlinear
variables in both constraints and objectives.
*/
hdr.pi.num_nl_vars_in_objs = 0;

/** Number of nonlinear variables in both constraints and objectives. */
hdr.pi.num_nl_vars_in_both = 0;

// Miscellaneous
// -------------

/** Number of linear network variables (arcs). */
hdr.pi.num_linear_net_vars = 0;

/** Number of functions. */
hdr.pi.num_funcs = 0;

// Information about discrete variables
// ------------------------------------

/** Number of linear binary variables. */
hdr.pi.num_linear_binary_vars = 0;

/** Number of linear non-binary integer variables. */
hdr.pi.num_linear_integer_vars = pex->n_var_int;

/**
Number of integer nonlinear variables in both constraints and objectives.
*/
hdr.pi.num_nl_integer_vars_in_both = 0;

/** Number of integer nonlinear variables just in constraints. */
hdr.pi.num_nl_integer_vars_in_cons = 0;

/** Number of integer nonlinear variables just in objectives. */
hdr.pi.num_nl_integer_vars_in_objs = 0;

// Information about nonzeros
// --------------------------

/** Number of nonzeros in constraints' Jacobian. */
hdr.pi.num_con_nonzeros = pex->n_con_nz;

/** Number of nonzeros in all objective gradients. */
hdr.pi.num_obj_nonzeros = pex->n_obj_nz;

// Information about names
// -----------------------

/** Length of longest con/obj name if names are available. */
hdr.pi.max_con_name_len = 0; // no need to set

/** Length of longest variable name if names are available. */
hdr.pi.max_var_name_len = 0; // no need to set

// Information about common expressions
// ------------------------------------

/**
Number of common expressions that appear both in constraints
and objectives.
*/
hdr.pi.num_common_exprs_in_both = 0;

/**
Number of common expressions that appear in multiple constraints
and don't appear in objectives.
*/
hdr.pi.num_common_exprs_in_cons = 0;

/**
Number of common expressions that appear in multiple objectives
and don't appear in constraints.
*/
hdr.pi.num_common_exprs_in_objs = 0;

/**
Number of common expressions that only appear in a single constraint
and don't appear in objectives.
*/
hdr.pi.num_common_exprs_in_single_cons = 0;

/**
Number of common expressions that only appear in a single objective
and don't appear in constraints.
*/
hdr.pi.num_common_exprs_in_single_objs = 0;

hdr.nli.prob_name = "c_api_example_model";

return hdr;
}

NLFeeder2_C MakeNLFeeder2_C(
CAPIExample* pex, int binary) {
NLFeeder2_C result;

result.p_user_data_ = pex;
pex->binary_nl = binary;

result.Header = CAPI_ex_Header;

return result;
}

Expand Down
Loading

0 comments on commit f0e008c

Please sign in to comment.