Skip to content

Commit

Permalink
NLW2_NLSuffix_C, NLW2_NLSolution_C: insert 'NL' #30
Browse files Browse the repository at this point in the history
For conformance with C++ and Python
  • Loading branch information
glebbelov committed Feb 22, 2024
1 parent 7385be7 commit b4d3137
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 17 deletions.
4 changes: 2 additions & 2 deletions nl-writer2/examples/c/easyAPI_1_MILP/nlsol_ex_easy_api_c.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ static int ApproxEqual(double n, double m) {

/// Check solution
int NLW2_CheckSolution_C(
NLW2_Solution_C* p_sol, NLW2_SolCheckData_C* p_sol_ref) {
NLW2_NLSolution_C* p_sol, NLW2_SolCheckData_C* p_sol_ref) {
if (!ApproxEqual(p_sol->obj_val_, p_sol_ref->obj_val_ref_)) {
printf("MILP 1: wrong obj val (%.17g !~ %.17g)\n",
p_sol->obj_val_, p_sol_ref->obj_val_ref_);
Expand Down Expand Up @@ -113,7 +113,7 @@ int SolveAndCheck(const char* solver, const char* sopts,
NLW2_NLSolver_C nlse = NLW2_MakeNLSolver_C(NULL);
NLW2_SetNLOptions_C(&nlse, nlopts);
NLW2_SetFileStub_C(&nlse, stub);
NLW2_Solution_C sol = NLW2_SolveNLModel_C(&nlse, &nlme, solver, sopts);
NLW2_NLSolution_C sol = NLW2_SolveNLModel_C(&nlse, &nlme, solver, sopts);
if (sol.solve_result_ >= -1) {
if (!NLW2_CheckSolution_C(&sol, &solchkdata)) {
printf("Solution check failed.\n");
Expand Down
12 changes: 6 additions & 6 deletions nl-writer2/include/api/c/nl-model-c.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ void NLW2_SetDualWarmstart_C(NLW2_NLModel_C* ,


/// NL suffix type
typedef struct NLW2_Suffix_C {
typedef struct NLW2_NLSuffix_C {
/// Name
const char* name_;
/// Suffix table
Expand All @@ -155,14 +155,14 @@ typedef struct NLW2_Suffix_C {
int numval_;
/// Values. Always double precision.
const double* values_;
} NLW2_Suffix_C;
} NLW2_NLSuffix_C;

/// Add suffix, e.g., basis statuses.
/// @return true iff new suffix added (vs replaced.)
/// @note SOS constraints can be modeled as suffixes
/// for some AMPL solvers.
int NLW2_AddSuffix_C(NLW2_NLModel_C* ,
NLW2_Suffix_C suf_c);
NLW2_NLSuffix_C suf_c);


/// Compute objective value
Expand Down Expand Up @@ -207,7 +207,7 @@ const char* NLW2_ObjName_C(NLW2_NLModel_C* );


/// NL solution
typedef struct NLW2_Solution_C {
typedef struct NLW2_NLSolution_C {
/**
Solve result.
If >-2, solver interaction successful. Then:
Expand Down Expand Up @@ -249,8 +249,8 @@ typedef struct NLW2_Solution_C {
/// Num suffixes
int nsuf_;
/// Suffixes
const NLW2_Suffix_C* suffixes_;
} NLW2_Solution_C;
const NLW2_NLSuffix_C* suffixes_;
} NLW2_NLSolution_C;


#ifdef __cplusplus
Expand Down
4 changes: 2 additions & 2 deletions nl-writer2/include/api/c/nl-solver-c.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ const char* NLW2_GetErrorMessage_C(NLW2_NLSolver_C* );
///
/// @see NLW2_LoadNLModel_C(), NLW2_RunSolver_C(),
/// NLW2_ReadSolution_C() for details.
NLW2_Solution_C NLW2_SolveNLModel_C(NLW2_NLSolver_C* ,
NLW2_NLSolution_C NLW2_SolveNLModel_C(NLW2_NLSolver_C* ,
NLW2_NLModel_C* ,
const char* solver,
const char* solver_opts);
Expand Down Expand Up @@ -156,7 +156,7 @@ int NLW2_RunSolver_C(NLW2_NLSolver_C* ,
/// @note To compute objective value,
/// execute NLW2_ComputeObjValue_C()
/// if x_ available.
NLW2_Solution_C NLW2_ReadSolution_C(NLW2_NLSolver_C* );
NLW2_NLSolution_C NLW2_ReadSolution_C(NLW2_NLSolver_C* );

/// Read solution to SOLHandler.
///
Expand Down
2 changes: 1 addition & 1 deletion nl-writer2/src/nl-model-c.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ void NLW2_SetDualWarmstart_C(NLW2_NLModel_C* nlme,
{ CastNZ<mp::NLModel>(nlme->p_data_)->SetWarmstart(ini_y); }

int NLW2_AddSuffix_C(NLW2_NLModel_C* nlme,
NLW2_Suffix_C suf_c) {
NLW2_NLSuffix_C suf_c) {
mp::NLSuffix suf{
suf_c.name_, suf_c.table_, suf_c.kind_,
{suf_c.values_, suf_c.values_+suf_c.numval_}
Expand Down
12 changes: 6 additions & 6 deletions nl-writer2/src/nl-solver-c.cc
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ using NLSOL_C_Impl
/// Storage for NLSE_Solution_C data
struct NLW2_Solution_C_Data {
mp::NLSolution sol_;
std::vector<NLW2_Suffix_C> suffixes_;
std::vector<NLW2_NLSuffix_C> suffixes_;
};

} // namespace mp
Expand Down Expand Up @@ -743,15 +743,15 @@ const char* NLW2_GetErrorMessage_C(NLW2_NLSolver_C* pnls) {

/// Add solution data to NLW2_NLSolver_C
/// and return its C wrapper, NLW2_Solution_C.
static NLW2_Solution_C NLW2_WrapNLSOL_Solution_C
static NLW2_NLSolution_C NLW2_WrapNLSOL_Solution_C
(NLW2_NLSolver_C* nlse, mp::NLSolution sol) {
// Store the C++ data
if (!nlse->p_sol_)
nlse->p_sol_ = new mp::NLW2_Solution_C_Data;
auto& sol_data = *CastNZ<mp::NLW2_Solution_C_Data>(nlse->p_sol_);
sol_data.sol_ = std::move(sol);

NLW2_Solution_C result;
NLW2_NLSolution_C result;
{
const auto& sol=sol_data.sol_;
result.nbs_ = sol.nbs_;
Expand All @@ -763,7 +763,7 @@ static NLW2_Solution_C NLW2_WrapNLSOL_Solution_C
sol_data.suffixes_.reserve(sol.suffixes_.size());
result.suffixes_ = sol_data.suffixes_.data();
for (const auto& suf: sol.suffixes_) {
NLW2_Suffix_C suf_c;
NLW2_NLSuffix_C suf_c;
suf_c.kind_ = suf.kind_;
suf_c.name_ = suf.name_.c_str();
suf_c.table_ = suf.table_.c_str();
Expand All @@ -779,7 +779,7 @@ static NLW2_Solution_C NLW2_WrapNLSOL_Solution_C
return result;
}

NLW2_Solution_C NLW2_SolveNLModel_C(NLW2_NLSolver_C* nlse,
NLW2_NLSolution_C NLW2_SolveNLModel_C(NLW2_NLSolver_C* nlse,
NLW2_NLModel_C* nlme,
const char* solver,
const char* solver_opts) {
Expand Down Expand Up @@ -821,7 +821,7 @@ int NLW2_RunSolver_C(NLW2_NLSolver_C* pnls,
->Solve(solver, solver_opts);
}

NLW2_Solution_C NLW2_ReadSolution_C(NLW2_NLSolver_C* nlse) {
NLW2_NLSolution_C NLW2_ReadSolution_C(NLW2_NLSolver_C* nlse) {
auto sol
= CastNZ<mp::NLSolver>(nlse->p_nlsol_)->ReadSolution();
return NLW2_WrapNLSOL_Solution_C(nlse, std::move(sol));
Expand Down

0 comments on commit b4d3137

Please sign in to comment.