Skip to content

Commit

Permalink
NL Writer C API example structure #30
Browse files Browse the repository at this point in the history
Finished high-level example code
  • Loading branch information
glebbelov committed Nov 10, 2023
1 parent 7ac64ac commit f274b8f
Show file tree
Hide file tree
Showing 16 changed files with 164 additions and 35 deletions.
3 changes: 3 additions & 0 deletions nl-writer2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ add_library(${NLW2_C_API_LIB_NAME} STATIC
${NLW2_C_API_LIB_FILES} ${NLW2_C_API_INC_FILES})
target_include_directories(
${NLW2_C_API_LIB_NAME} PUBLIC ${NLW2_INCLUDE_PATH})
target_link_libraries(${NLW2_C_API_LIB_NAME} ${NLW2_LIB_NAME})

######################################################
# Examples
Expand All @@ -86,6 +87,8 @@ set(NLSOL_C_EX_FILES
${NLW2_C_EXAMPLE_PATH}/nlsol_ex_c_nl.c
${NLW2_C_EXAMPLE_PATH}/nlsol_ex_c_sol.h
${NLW2_C_EXAMPLE_PATH}/nlsol_ex_c_sol.c
${NLW2_C_EXAMPLE_PATH}/nlsol_ex_c_nlutils.h
${NLW2_C_EXAMPLE_PATH}/nlsol_ex_c_nlutils.c
)

option(BUILD_EXAMPLES_NLW2
Expand Down
26 changes: 20 additions & 6 deletions nl-writer2/examples/c/nlsol_ex_c.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "nlsol_ex_c_model.h"
#include "nlsol_ex_c_nl.h"
#include "nlsol_ex_c_sol.h"
#include "api/c/nl-writer2-misc-c.h"
#include "nlsol_ex_c_nlutils.h"
#include "api/c/nlsol-c.h"

/// main()
Expand All @@ -34,17 +34,31 @@ int main(int argc, const char* const* argv) {
int binary = (argc<=3) || strcmp("text", argv[3]);
const char* stub = (argc>4) ? argv[4] : "stub";

// Create custom interface
CAPIExample example = MakeCAPIExample_Linear_01();
NLFeeder2_C feeder = MakeNLFeeder2_C();
SOLHandler2_C handler = MakeSOLHandler2_C();
NLUtils_C utils = NLW2_MakeNLUtils_C_Default();
NLFeeder2_C feeder = MakeNLFeeder2_C(&example, binary);
SOLHandler2_C solhnd = MakeSOLHandler2_C(&example);
NLUtils_C utils = MakeNLUtils_C();

NLSOL_C nlsol = NLW2_MakeNLSOL_C(&feeder, &handler, &utils);
// Create NLSOL_C
NLSOL_C nlsol = NLW2_MakeNLSOL_C(&feeder, &solhnd, &utils);

// Solve
NLW2_SetSolver_C(&nlsol, solver);
NLW2_SetSolverOptions_C(&nlsol, sopts);
if (0==NLW2_Solve_C(&nlsol, stub)) {
printf("%s\n", NLW2_GetErrorMessage_C(&nlsol));
exit(EXIT_FAILURE);
}
PrintSolution_C(&example, stub);

// Destroy API-owned objects
NLW2_DestroyNLSOL_C(&nlsol);

// Destroy our example data
// Destroy our custom interface and example data
DestroyNLUtils_C(&utils);
DestroySOLHandler2_C(&solhnd);
DestroyNLFeeder2_C(&feeder);
DestroyCAPIExample_Linear_01(&example);

return 0;
Expand Down
5 changes: 5 additions & 0 deletions nl-writer2/examples/c/nlsol_ex_c_model.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <stdlib.h>
#include <math.h>
#include <assert.h>

#include "nlsol_ex_c_model.h"

Expand Down Expand Up @@ -70,3 +71,7 @@ void DestroyCAPIExample_Linear_01(CAPIExample* pEx) {

// ...
}

void PrintSolution_C(CAPIExample* pex, const char* stub) {
assert(0);
}
6 changes: 6 additions & 0 deletions nl-writer2/examples/c/nlsol_ex_c_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ typedef struct CAPIExample {
int n_obj_nz;
const char* obj_name;

/// Some technical stuff
int binary_nl;

} CAPIExample;

/// Create linear example data
Expand All @@ -105,4 +108,7 @@ CAPIExample MakeCAPIExample_Linear_01();
/// Destroy linear example data
void DestroyCAPIExample_Linear_01(CAPIExample* );

/// Print solution
void PrintSolution_C(CAPIExample* pex, const char* stub);

#endif // NLSOL_EX_C_MODEL_H
12 changes: 11 additions & 1 deletion nl-writer2/examples/c/nlsol_ex_c_nl.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
#include <stdlib.h>

#include "nlsol_ex_c_nl.h"

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

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

return result;
}

void DestroyNLFeeder2_C(NLFeeder2_C* pf) {
pf->p_user_data_ = NULL;
}
7 changes: 6 additions & 1 deletion nl-writer2/examples/c/nlsol_ex_c_nl.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@

#include "api/c/nl-feeder2-c.h"

#include "nlsol_ex_c_model.h"

/// Fill an NLFeeder2_C for the C API example
NLFeeder2_C MakeNLFeeder2_C();
NLFeeder2_C MakeNLFeeder2_C(CAPIExample* , int binary);

/// Destroy custom NLFeeder2_C
void DestroyNLFeeder2_C(NLFeeder2_C* );

#endif // NLSOL_EX_C_NL_H
10 changes: 10 additions & 0 deletions nl-writer2/examples/c/nlsol_ex_c_nlutils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "nlsol_ex_c_nlutils.h"

NLUtils_C MakeNLUtils_C() {
// Just return the API's default config:
return NLW2_MakeNLUtils_C_Default();
}

void DestroyNLUtils_C(NLUtils_C* p) {
NLW2_DestroyNLUtils_C_Default(p);
}
16 changes: 16 additions & 0 deletions nl-writer2/examples/c/nlsol_ex_c_nlutils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Custom NLUtils_C for the C API example
*
*/
#ifndef NLSOL_EX_C_NLUTILS_H
#define NLSOL_EX_C_NLUTILS_H

#include "api/c/nl-writer2-misc-c.h"

/// Fill NLUtils_C for the C API example
NLUtils_C MakeNLUtils_C();

/// Destroy custom NLUtils_C
void DestroyNLUtils_C(NLUtils_C* );

#endif // NLSOL_EX_C_NLUTILS_H
10 changes: 9 additions & 1 deletion nl-writer2/examples/c/nlsol_ex_c_sol.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
#include <stdlib.h>

#include "nlsol_ex_c_sol.h"

SOLHandler2_C MakeSOLHandler2_C() {
SOLHandler2_C MakeSOLHandler2_C(CAPIExample* pex) {
SOLHandler2_C result;

result.p_user_data_ = pex;

return result;
}

void DestroySOLHandler2_C(SOLHandler2_C* p) {
p->p_user_data_ = NULL;
}
7 changes: 6 additions & 1 deletion nl-writer2/examples/c/nlsol_ex_c_sol.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@

#include "api/c/sol-handler2-c.h"

#include "nlsol_ex_c_model.h"

/// Fill a SOLHandler2_C for the C API example
SOLHandler2_C MakeSOLHandler2_C();
SOLHandler2_C MakeSOLHandler2_C(CAPIExample* );

/// Destroy custom SOLHandler2_C
void DestroySOLHandler2_C(SOLHandler2_C* );

#endif // NLSOL_EX_C_SOL_H
20 changes: 18 additions & 2 deletions nl-writer2/include/api/c/nl-feeder2-c.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,25 @@
extern "C" {
#endif

/// Wrap mp::NLFeeder2
/** Wrap mp::NLFeeder2 for C API.
NLFeeder2_C: writes model details on request
via provided callback objects.
See the examples folder.
For the NL format, variables and constraints must have certain order.
**Variable ordering:**
first continuous, then integer.
Some solvers might require more elaborate ordering, see NLHeader_C.
**Constraint ordering:**
first algebraic (including complementarity), then logical.
Some solvers might require nonlinear constraints first.
*/
typedef struct NLFeeder2_C {
void* p_;
/// User data
void* p_user_data_;

} NLFeeder2_C;

Expand Down
16 changes: 12 additions & 4 deletions nl-writer2/include/api/c/nl-writer2-misc-c.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,30 @@
extern "C" {
#endif

/// Wrap mp::NLUtils.
/// Wrap mp::NLUtils for C API.
///
/// NL writer and SOL reader utilities.
/// It provides default facilities for logging
/// and error handling.
/// The default error handler exit()s.
typedef struct NLUtils_C {
void* p_;

/// Use this pointer if you need to store your data
void* p_user_data_;

/// Used by the API
/// with NLW2_MakeNLUtils_C_Default() /
/// NLW2_DestroyNLUtils_C_Default().
void* p_api_data_;
} NLUtils_C;

/// Create a default NLUtils_C wrapper object.
/// User application might want to customize
/// using the below functions.
/// User application might change some methods
/// and use the p_user_data_ pointer.
NLUtils_C NLW2_MakeNLUtils_C_Default();

/// Destroy the NLUtils_C object created by the API
void NLW2_DestroyNLUtils_C_Default(NLUtils_C*);

#ifdef __cplusplus
} // extern "C"
Expand Down
17 changes: 9 additions & 8 deletions nl-writer2/include/api/c/nlsol-c.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ extern "C" {
///
/// Usage: see the C API example and the below API.
typedef struct NLSOL_C {
void* p_;
/// Internal data
void* p_api_data_;

} NLSOL_C;

Expand All @@ -37,31 +38,31 @@ NLSOL_C NLW2_MakeNLSOL_C(NLFeeder2_C* , SOLHandler2_C* , NLUtils_C* );
void NLW2_DestroyNLSOL_C(NLSOL_C* );

/// Set solver, such as "gurobi", "highs", "ipopt"
void NLSOL_C_SetSolver(NLSOL_C* , const char* solver);
void NLW2_SetSolver_C(NLSOL_C* , const char* solver);

/// Set solver options, such as "outlev=1 lim:time=500"
void NLSOL_C_SetSolverOptions(NLSOL_C* , const char* sopts);
void NLW2_SetSolverOptions_C(NLSOL_C* , const char* sopts);

/// Solve.
/// @param filestub: filename stub to be used
/// for input files (.nl, .col., .row, etc.),
/// and output files (.sol).
/// @return true if all ok.
int NLSOL_C_Solve(NLSOL_C* , const char* filestub);
int NLW2_Solve_C(NLSOL_C* , const char* filestub);

/// Get error message.
const char* NLSOL_C_GetErrorMessage(NLSOL_C* );
const char* NLW2_GetErrorMessage_C(NLSOL_C* );

/// Substep: write NL and any accompanying files.
int NLSOL_C_WriteNLFile(NLSOL_C* , const char* filestub);
int NLW2_WriteNLFile_C(NLSOL_C* , const char* filestub);

/// Substep: invoke chosen solver for \a filestub.
int NLSOL_C_InvokeSolver(NLSOL_C* , const char* filestub);
int NLW2_InvokeSolver_C(NLSOL_C* , const char* filestub);

/// Substep: read solution.
/// @param filename: complete file name,
/// normally (stub).sol.
int NLSOL_C_ReadSolution(NLSOL_C* , const char* filename);
int NLW2_ReadSolution_C(NLSOL_C* , const char* filename);


#ifdef __cplusplus
Expand Down
9 changes: 7 additions & 2 deletions nl-writer2/include/api/c/sol-handler2-c.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@
extern "C" {
#endif

/// Wrap mp::SOLHandler2
/// Wrap mp::SOLHandler2 for C API.
///
/// SOLHandler2_C: reads solution details on request
/// via provided callback objects.
/// See the examples folder.
typedef struct SOLHandler2_C {
void* p_;
/// User data
void* p_user_data_;

} SOLHandler2_C;

Expand Down
3 changes: 2 additions & 1 deletion nl-writer2/include/mp/nl-feeder2.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ namespace mp {
For the NL format, variables and constraints must have certain order.
**Variable ordering:** first continuous, then integer.
**Variable ordering:**
first continuous, then integer.
Some solvers might require more elaborate ordering, see NLHeader.
**Constraint ordering:**
Expand Down
Loading

0 comments on commit f274b8f

Please sign in to comment.