Skip to content

Commit

Permalink
NLW2 C API example: suffix read #30
Browse files Browse the repository at this point in the history
  • Loading branch information
glebbelov committed Nov 29, 2023
1 parent e911157 commit 5983dee
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions nl-writer2/examples/c/nlsol_ex_c_sol.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "nlsol_ex_c_sol.h"

Expand Down Expand Up @@ -29,6 +31,65 @@ void OnSolveCode(void* p_user_data, int sc) {
pex->solve_code_ = sc;
}

void OnIntSuffix(
void* p_user_data, NLW2_SuffixInfo_C si, void* p_api_data) {
CAPIExample* pex = (CAPIExample*)p_user_data;
int nitems_max[4] = {pex->n_var, pex->n_con, pex->n_obj, 1};
int kind = si.kind_;
int nmax = nitems_max[kind & 3]; // {vars, cons, objs, 1}
const char* name = si.name_;
const char* table = si.table_;
printf("SUFFIX '%s': kind=%d\n", name, kind);
if (table && strlen(table))
printf("SUFFIX TABLE:\n%s\n", table);
int i;
int v;
printf("%s", "NON-ZERO VALUES:");
while (NLW2_IntSuffixNNZ(p_api_data)) {
NLW2_ReadIntSuffixEntry(p_api_data, &i, &v);
printf(" (%d, %d)", i, v);
if (i<0 || i>=nmax) {
NLW2_ReportIntSuffixError(
p_api_data, "bad suffix element index");
return;
}
}
if (NLW2_IntSuffixReadOK(p_api_data)) // Can check
printf(" ...%s\n", "OK");
else
printf(" ...%s\n", "FAILURE");
}

void OnDblSuffix(
void* p_user_data, NLW2_SuffixInfo_C si, void* p_api_data) {
CAPIExample* pex = (CAPIExample*)p_user_data;
int nitems_max[4] = {pex->n_var, pex->n_con, pex->n_obj, 1};
int kind = si.kind_;
int nmax = nitems_max[kind & 3]; // {vars, cons, objs, 1}
const char* name = si.name_;
const char* table = si.table_;
printf("SUFFIX '%s': kind=%d\n", name, kind);
if (table && strlen(table))
printf("SUFFIX TABLE:\n%s\n", table);
int i;
double v;
printf("%s", "NON-ZERO VALUES:");
while (NLW2_DblSuffixNNZ(p_api_data)) {
NLW2_ReadDblSuffixEntry(p_api_data, &i, &v);
printf(" (%d, %g)", i, v);
if (i<0 || i>=nmax) {
NLW2_ReportDblSuffixError(
p_api_data, "bad suffix element index");
return;
}
}
if (NLW2_DblSuffixReadOK(p_api_data)) // Can check
printf(" ...%s\n", "OK");
else
printf(" ...%s\n", "FAILURE");
}


NLW2_SOLHandler2_C MakeSOLHandler2_C(CAPIExample* pex) {
NLW2_SOLHandler2_C result
= NLW2_MakeSOLHandler2_C_Default();
Expand All @@ -42,6 +103,8 @@ NLW2_SOLHandler2_C MakeSOLHandler2_C(CAPIExample* pex) {
result.OnPrimalSolution = OnPrimalSolution;
result.OnObjno = OnObjno;
result.OnSolveCode = OnSolveCode;
result.OnIntSuffix = OnIntSuffix;
result.OnDblSuffix = OnDblSuffix;

return result;
}
Expand Down

0 comments on commit 5983dee

Please sign in to comment.