58#pragma GCC diagnostic ignored "-Wshadow"
60#pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
64#include "mp/nl-reader.h"
66#define READER_NAME "nlreader"
67#define READER_DESC "AMPL .nl file reader"
68#define READER_EXTENSION "nl"
72#define SCIP_CALL_THROW(x) \
75 SCIP_RETCODE throw_retcode; \
76 if( ((throw_retcode) = (x)) != SCIP_OKAY ) \
77 throw std::logic_error("Error <" + std::to_string((long long)throw_retcode) + "> in function call"); \
91 int amplopts[mp::MAX_AMPL_OPTIONS];
120 std::vector<SCIP_EXPR*> varexprs;
124 std::vector<std::vector<std::pair<SCIP_Real, SCIP_VAR*> > > nlconslin;
132 std::vector<SCIP_EXPR*> commonexprs;
140 std::vector<SCIP_EXPR*> exprstorelease;
146 std::map<int, std::vector<int> > sosvars;
147 std::vector<int> sosweights;
216 probdata->filenamestublen =
extstart - filename;
218 probdata->filenamestublen =
strlen(filename);
219 assert(probdata->filenamestublen > 0);
221 memcpy(probdata->filenamestub, filename, probdata->filenamestublen);
222 probdata->filenamestub[probdata->filenamestublen] =
'\0';
225 const char* probname =
strrchr(probdata->filenamestub,
'/');
226 if( probname ==
NULL )
227 probname = probdata->filenamestub;
238 probdata->filenamestub[probdata->filenamestublen] =
'.';
239 probdata->filenamestub[probdata->filenamestublen+1] =
'c';
240 probdata->filenamestub[probdata->filenamestublen+2] =
'o';
241 probdata->filenamestub[probdata->filenamestublen+3] =
'l';
242 probdata->filenamestub[probdata->filenamestublen+4] =
'\0';
243 colfile =
new fmt::File(probdata->filenamestub, fmt::File::RDONLY);
245 probdata->filenamestub[probdata->filenamestublen+1] =
'r';
246 probdata->filenamestub[probdata->filenamestublen+3] =
'w';
247 rowfile =
new fmt::File(probdata->filenamestub, fmt::File::RDONLY);
249 catch(
const fmt::SystemError&
e )
254 probdata->filenamestub[probdata->filenamestublen] =
'\0';
267 assert(exprstorelease.empty());
277 const mp::NLHeader&
h
286 probdata->namplopts =
h.num_ampl_options;
292 if( colfile !=
NULL )
298 if( rowfile !=
NULL )
303 probdata->nvars =
h.num_vars;
307 nnlvars =
MAX(
h.num_nl_vars_in_cons,
h.num_nl_vars_in_objs);
312 for(
int i = 0;
i <
h.num_vars; ++
i )
316 if(
i <
h.num_nl_vars_in_both -
h.num_nl_integer_vars_in_both )
318 else if(
i <
h.num_nl_vars_in_both )
321 else if(
i <
h.num_nl_vars_in_cons -
h.num_nl_integer_vars_in_cons )
323 else if(
i <
h.num_nl_vars_in_cons )
326 else if(
i <
h.num_nl_vars_in_objs -
h.num_nl_integer_vars_in_objs )
328 else if(
i <
h.num_nl_vars_in_objs )
331 else if(
i <
h.num_vars -
h.num_linear_binary_vars -
h.num_linear_integer_vars )
333 else if(
i <
h.num_vars -
h.num_linear_integer_vars )
372 probdata->nconss =
h.num_algebraic_cons;
374 nlconslin.resize(
h.num_nl_cons);
380 for(
int i = 0;
i <
h.num_nl_cons; ++
i )
391 for(
int i =
h.num_nl_cons;
i <
h.num_algebraic_cons; ++
i )
398 if(
h.num_nl_cons == 0 &&
h.num_integer_vars() == 0 )
399 probdata->islp =
true;
402 commonexprs.resize(
h.num_common_exprs());
415 exprstorelease.push_back(expr);
444 case mp::expr::MINUS:
467 case mp::expr::LOG10:
495 exprstorelease.push_back(expr);
521 SCIP_Real coefs[2] = { 1.0, -1.0 };
536 case mp::expr::POW_CONST_BASE:
537 case mp::expr::POW_CONST_EXP:
582 exprstorelease.push_back(expr);
593 std::shared_ptr<std::vector<SCIP_EXPR*> >
v;
630 exprstorelease.push_back(expr);
697 amplph.commonexprs[index] = commonexpr;
698 amplph.exprstorelease.push_back(commonexpr);
713 if(
var_index < (
int)amplph.varexprs.size() )
736 assert(index < (
int)commonexprs.size());
748 if( commonexprs[index] !=
NULL )
756 else if( expr !=
NULL )
758 commonexprs[index] = expr;
807 if( index < (
int)nlconslin.size() )
837 if( initsol ==
NULL )
900 case mp::suf::Kind::CON:
901 if(
strncmp(name.data(),
"initial", name.size()) == 0 )
903 suffix = CONSINITIAL;
905 else if(
strncmp(name.data(),
"separate", name.size()) == 0 )
907 suffix = CONSSEPARATE;
909 else if(
strncmp(name.data(),
"enforce", name.size()) == 0 )
911 suffix = CONSENFORCE;
913 else if(
strncmp(name.data(),
"check", name.size()) == 0 )
917 else if(
strncmp(name.data(),
"propagate", name.size()) == 0 )
919 suffix = CONSPROPAGATE;
921 else if(
strncmp(name.data(),
"dynamic", name.size()) == 0 )
923 suffix = CONSDYNAMIC;
925 else if(
strncmp(name.data(),
"removable", name.size()) == 0 )
927 suffix = CONSREMOVABLE;
935 case mp::suf::Kind::VAR:
937 if(
strncmp(name.data(),
"initial", name.size()) == 0 )
941 else if(
strncmp(name.data(),
"removable", name.size()) == 0 )
943 suffix = VARREMOVABLE;
945 else if(
strncmp(name.data(),
"sosno", name.size()) == 0 )
950 else if(
strncmp(name.data(),
"ref", name.size()) == 0 )
954 amplph.sosweights.resize(amplph.probdata->nvars, 0);
962 case mp::suf::Kind::OBJ:
966 case mp::suf::Kind::PROBLEM:
1024 amplph.sosvars[(int)value].
push_back(index);
1029 amplph.sosweights[index] = (int)value;
1038 fmt::StringRef name,
1049 fmt::StringRef name,
1066 int constraintIndex;
1097 if( coefficient == 0.0 )
1100 if( constraintIndex < 0 )
1104 else if( constraintIndex < (
int)amplph.nlconslin.size() )
1106 amplph.nlconslin[constraintIndex].push_back(std::pair<SCIP_Real, SCIP_VAR*>(coefficient, amplph.probdata->vars[
variableIndex]));
1110 SCIP_CONS* lincons = amplph.probdata->conss[constraintIndex];
1134 int constraintIndex,
1151 if( objexpr !=
NULL )
1170 for(
size_t i = 0;
i < nlconslin.size(); ++
i )
1172 for(
size_t j = 0;
j < nlconslin[
i].size(); ++
j )
1179 for(
int i = 0;
i < probdata->nconss; ++
i )
1185 std::vector<SCIP_VAR*>
setvars;
1187 if( !sosvars.empty() )
1189 setvars.resize(probdata->nvars);
1190 probdata->islp =
false;
1192 if( !sosweights.empty() )
1194 for( std::map<
int, std::vector<int> >::iterator
sosit(sosvars.begin());
sosit != sosvars.end(); ++
sosit )
1202 if(
issos2 && sosweights.empty() )
1207 OnUnhandled(
"SOS2 requires variable .ref suffix");
1210 for(
size_t i = 0;
i <
sosit->second.size(); ++
i )
1217 OnUnhandled(
"Missing .ref value for SOS2 variable");
1218 if( !sosweights.empty() )
1239 if( initsol !=
NULL )
1256 if( initsol !=
NULL )
1262 while( !exprstorelease.empty() )
1265 exprstorelease.pop_back();
1269 while( !varexprs.empty() )
1272 varexprs.pop_back();
1290 assert((*probdata)->vars !=
NULL || (*probdata)->nvars == 0);
1291 assert((*probdata)->conss !=
NULL || (*probdata)->conss == 0);
1293 for(
i = 0;
i < (*probdata)->nconss; ++
i )
1299 for(
i = 0;
i < (*probdata)->nvars; ++
i )
1342 mp::ReadNLFile(filename,
handler);
1344 catch(
const mp::UnsupportedError&
e )
1346 SCIPerrorMessage(
"unsupported construct in AMPL .nl file %s: %s\n", filename,
e.what());
1352 catch(
const mp::Error&
e )
1361 catch(
const fmt::SystemError&
e )
1370 catch(
const std::bad_alloc&
e )
1379 catch(
const std::exception&
e )
1427 if( probdata ==
NULL )
1433 probdata->filenamestub[probdata->filenamestublen] =
'.';
1434 probdata->filenamestub[probdata->filenamestublen+1] =
's';
1435 probdata->filenamestub[probdata->filenamestublen+2] =
'o';
1436 probdata->filenamestub[probdata->filenamestublen+3] =
'l';
1437 probdata->filenamestub[probdata->filenamestublen+4] =
'\0';
1442 SCIPerrorMessage(
"could not open file <%s> for writing\n", probdata->filenamestub);
1443 probdata->filenamestub[probdata->filenamestublen] =
'\0';
1447 probdata->filenamestub[probdata->filenamestublen] =
'\0';
1454 for(
int i = 0;
i < probdata->namplopts; ++
i )
1466 for(
int c = 0;
c < probdata->nconss; ++
c )
1487 for(
int i = 0;
i < probdata->nvars; ++
i )
void AddTerm(int var_index, double coef)
receives notification of a term in the linear expression
LinearExprHandler(AMPLProblemHandler &lph_, int index, int num_linear_terms)
constructor
LinearPartHandler(AMPLProblemHandler &lph_)
void AddTerm(int variableIndex, double coefficient)
LinearPartHandler(AMPLProblemHandler &lph_, int constraintIndex_)
NumericArgHandler(int num_args)
constructor
void AddArg(SCIP_EXPR *term)
adds term to sum
std::shared_ptr< std::vector< SCIP_EXPR * > > v
void SetValue(int index, T value)
SuffixHandler(AMPLProblemHandler &lph_, fmt::StringRef name, mp::suf::Kind kind)
constructor
implementation of AMPL/MPs NLHandler that constructs a SCIP problem while a .nl file is read
void EndCommonExpr(int index, SCIP_EXPR *expr, int)
receive notification of the end of a common expression
LinearPartHandler LinearObjHandler
NumericArgHandler BeginSum(int num_args)
receive notification of the beginning of a summation
void OnAlgebraicCon(int constraintIndex, SCIP_EXPR *expr)
receive notification of an algebraic constraint expression
LinearPartHandler OnLinearObjExpr(int objectiveIndex, int)
receive notification of the linear part of an objective
SCIP_EXPR * OnBinary(mp::expr::Kind kind, SCIP_EXPR *firstChild, SCIP_EXPR *secondChild)
receive notification of a binary expression
SCIP_EXPR * OnNumber(double value)
receive notification of a number in a nonlinear expression
SuffixHandler< int > IntSuffixHandler
LinearExprHandler BeginCommonExpr(int index, int num_linear_terms)
receive notification of the beginning of a common expression (defined variable)
AMPLProblemHandler(const AMPLProblemHandler &)=delete
LinearConHandler OnLinearConExpr(int constraintIndex, int)
receive notification of the linear part of a constraint
void OnInitialValue(int var_index, double value)
receive notification of the initial value for a variable
SCIP_EXPR * OnVariableRef(int variableIndex)
receive notification of a variable reference in a nonlinear expression
AMPLProblemHandler(SCIP *scip_, const char *filename)
ColumnSizeHandler OnColumnSizes()
receives notification of Jacobian column sizes
AMPLProblemHandler & operator=(const AMPLProblemHandler &)=delete
LinearPartHandler LinearConHandler
void OnVarBounds(int variableIndex, double variableLB, double variableUB)
receive notification of variable bounds
SCIP_EXPR * OnCommonExprRef(int expr_index)
receive notification of a common expression (defined variable) reference
void OnHeader(const mp::NLHeader &h)
DblSuffixHandler OnDblSuffix(fmt::StringRef name, mp::suf::Kind kind, int)
receive notification of a double suffix
void OnConBounds(int index, double lb, double ub)
receive notification of constraint sides
IntSuffixHandler OnIntSuffix(fmt::StringRef name, mp::suf::Kind kind, int)
receive notification of an integer suffix
SCIP_EXPR * OnUnary(mp::expr::Kind kind, SCIP_EXPR *child)
receive notification of a unary expression
SCIP_EXPR * EndSum(NumericArgHandler handler)
receive notification of the end of a summation
void OnInitialDualValue(int, double)
receives notification of the initial value for a dual variable
SuffixHandler< SCIP_Real > DblSuffixHandler
void OnObj(int objectiveIndex, mp::obj::Type type, SCIP_EXPR *nonlinearExpression)
receive notification of an objective type and the nonlinear part of an objective expression
Constraint handler for linear constraints in their most general form, .
constraint handler for nonlinear constraints specified by algebraic expressions
constraint handler for SOS type 1 constraints
constraint handler for SOS type 2 constraints
absolute expression handler
exponential expression handler
logarithm expression handler
power and signed power expression handlers
product expression handler
handler for sin expressions
constant value expression handler
variable expression handler
SCIP_Real SCIPgetDualsolLinear(SCIP *scip, SCIP_CONS *cons)
SCIP_RETCODE SCIPaddLinearVarNonlinear(SCIP *scip, SCIP_CONS *cons, SCIP_VAR *var, SCIP_Real coef)
SCIP_RETCODE SCIPchgRhsLinear(SCIP *scip, SCIP_CONS *cons, SCIP_Real rhs)
SCIP_RETCODE SCIPaddCoefLinear(SCIP *scip, SCIP_CONS *cons, SCIP_VAR *var, SCIP_Real val)
SCIP_RETCODE SCIPchgLhsNonlinear(SCIP *scip, SCIP_CONS *cons, SCIP_Real lhs)
SCIP_RETCODE SCIPchgRhsNonlinear(SCIP *scip, SCIP_CONS *cons, SCIP_Real rhs)
SCIP_RETCODE SCIPcreateConsBasicSOS1(SCIP *scip, SCIP_CONS **cons, const char *name, int nvars, SCIP_VAR **vars, SCIP_Real *weights)
SCIP_RETCODE SCIPcreateConsBasicLinear(SCIP *scip, SCIP_CONS **cons, const char *name, int nvars, SCIP_VAR **vars, SCIP_Real *vals, SCIP_Real lhs, SCIP_Real rhs)
SCIP_RETCODE SCIPcreateConsBasicNonlinear(SCIP *scip, SCIP_CONS **cons, const char *name, SCIP_EXPR *expr, SCIP_Real lhs, SCIP_Real rhs)
SCIP_RETCODE SCIPchgExprNonlinear(SCIP *scip, SCIP_CONS *cons, SCIP_EXPR *expr)
SCIP_RETCODE SCIPchgLhsLinear(SCIP *scip, SCIP_CONS *cons, SCIP_Real lhs)
SCIP_RETCODE SCIPcreateConsBasicSOS2(SCIP *scip, SCIP_CONS **cons, const char *name, int nvars, SCIP_VAR **vars, SCIP_Real *weights)
SCIP_RETCODE SCIPcreateExprVar(SCIP *scip, SCIP_EXPR **expr, SCIP_VAR *var, SCIP_DECL_EXPR_OWNERCREATE((*ownercreate)), void *ownercreatedata)
SCIP_RETCODE SCIPcreateExprProduct(SCIP *scip, SCIP_EXPR **expr, int nchildren, SCIP_EXPR **children, SCIP_Real coefficient, SCIP_DECL_EXPR_OWNERCREATE((*ownercreate)), void *ownercreatedata)
SCIP_RETCODE SCIPcreateExprSin(SCIP *scip, SCIP_EXPR **expr, SCIP_EXPR *child, SCIP_DECL_EXPR_OWNERCREATE((*ownercreate)), void *ownercreatedata)
SCIP_RETCODE SCIPcreateExprCos(SCIP *scip, SCIP_EXPR **expr, SCIP_EXPR *child, SCIP_DECL_EXPR_OWNERCREATE((*ownercreate)), void *ownercreatedata)
SCIP_RETCODE SCIPcreateExprAbs(SCIP *scip, SCIP_EXPR **expr, SCIP_EXPR *child, SCIP_DECL_EXPR_OWNERCREATE((*ownercreate)), void *ownercreatedata)
SCIP_RETCODE SCIPappendExprSumExpr(SCIP *scip, SCIP_EXPR *expr, SCIP_EXPR *child, SCIP_Real childcoef)
SCIP_RETCODE SCIPcreateExprLog(SCIP *scip, SCIP_EXPR **expr, SCIP_EXPR *child, SCIP_DECL_EXPR_OWNERCREATE((*ownercreate)), void *ownercreatedata)
SCIP_RETCODE SCIPcreateExprExp(SCIP *scip, SCIP_EXPR **expr, SCIP_EXPR *child, SCIP_DECL_EXPR_OWNERCREATE((*ownercreate)), void *ownercreatedata)
SCIP_RETCODE SCIPcreateExprSum(SCIP *scip, SCIP_EXPR **expr, int nchildren, SCIP_EXPR **children, SCIP_Real *coefficients, SCIP_Real constant, SCIP_DECL_EXPR_OWNERCREATE((*ownercreate)), void *ownercreatedata)
SCIP_RETCODE SCIPcreateExprValue(SCIP *scip, SCIP_EXPR **expr, SCIP_Real value, SCIP_DECL_EXPR_OWNERCREATE((*ownercreate)), void *ownercreatedata)
SCIP_RETCODE SCIPcreateExprPow(SCIP *scip, SCIP_EXPR **expr, SCIP_EXPR *child, SCIP_Real exponent, SCIP_DECL_EXPR_OWNERCREATE((*ownercreate)), void *ownercreatedata)
SCIP_Bool SCIPhasPerformedPresolve(SCIP *scip)
SCIP_RETCODE SCIPprintStatus(SCIP *scip, FILE *file)
SCIP_STATUS SCIPgetStatus(SCIP *scip)
SCIP_STAGE SCIPgetStage(SCIP *scip)
SCIP_RETCODE SCIPaddVar(SCIP *scip, SCIP_VAR *var)
SCIP_RETCODE SCIPaddCons(SCIP *scip, SCIP_CONS *cons)
SCIP_PROBDATA * SCIPgetProbData(SCIP *scip)
SCIP_RETCODE SCIPsetObjsense(SCIP *scip, SCIP_OBJSENSE objsense)
SCIP_OBJSENSE SCIPgetObjsense(SCIP *scip)
SCIP_RETCODE SCIPcreateProb(SCIP *scip, const char *name, SCIP_DECL_PROBDELORIG((*probdelorig)), SCIP_DECL_PROBTRANS((*probtrans)), SCIP_DECL_PROBDELTRANS((*probdeltrans)), SCIP_DECL_PROBINITSOL((*probinitsol)), SCIP_DECL_PROBEXITSOL((*probexitsol)), SCIP_DECL_PROBCOPY((*probcopy)), SCIP_PROBDATA *probdata)
void SCIPinfoMessage(SCIP *scip, FILE *file, const char *formatstr,...)
void SCIPverbMessage(SCIP *scip, SCIP_VERBLEVEL msgverblevel, FILE *file, const char *formatstr,...)
const char * SCIPconshdlrGetName(SCIP_CONSHDLR *conshdlr)
SCIP_CONSHDLR * SCIPconsGetHdlr(SCIP_CONS *cons)
SCIP_RETCODE SCIPsetConsSeparated(SCIP *scip, SCIP_CONS *cons, SCIP_Bool separate)
SCIP_RETCODE SCIPsetConsDynamic(SCIP *scip, SCIP_CONS *cons, SCIP_Bool dynamic)
SCIP_RETCODE SCIPsetConsInitial(SCIP *scip, SCIP_CONS *cons, SCIP_Bool initial)
SCIP_RETCODE SCIPsetConsEnforced(SCIP *scip, SCIP_CONS *cons, SCIP_Bool enforce)
SCIP_RETCODE SCIPsetConsRemovable(SCIP *scip, SCIP_CONS *cons, SCIP_Bool removable)
SCIP_RETCODE SCIPgetTransformedCons(SCIP *scip, SCIP_CONS *cons, SCIP_CONS **transcons)
SCIP_RETCODE SCIPreleaseCons(SCIP *scip, SCIP_CONS **cons)
SCIP_RETCODE SCIPsetConsPropagated(SCIP *scip, SCIP_CONS *cons, SCIP_Bool propagate)
SCIP_RETCODE SCIPsetConsChecked(SCIP *scip, SCIP_CONS *cons, SCIP_Bool check)
SCIP_Bool SCIPisExprValue(SCIP *scip, SCIP_EXPR *expr)
SCIP_RETCODE SCIPreleaseExpr(SCIP *scip, SCIP_EXPR **expr)
SCIP_Real SCIPgetValueExprValue(SCIP_EXPR *expr)
SCIP_RETCODE SCIPincludeExternalCodeInformation(SCIP *scip, const char *name, const char *description)
#define SCIPallocClearMemory(scip, ptr)
#define SCIPfreeMemory(scip, ptr)
#define SCIPallocBlockMemoryArray(scip, ptr, num)
#define SCIPfreeBlockMemoryArrayNull(scip, ptr, num)
SCIP_RETCODE SCIPsetReaderCopy(SCIP *scip, SCIP_READER *reader,)
SCIP_RETCODE SCIPincludeReaderBasic(SCIP *scip, SCIP_READER **readerptr, const char *name, const char *desc, const char *extension, SCIP_READERDATA *readerdata)
SCIP_RETCODE SCIPsetReaderRead(SCIP *scip, SCIP_READER *reader,)
SCIP_SOL * SCIPgetBestSol(SCIP *scip)
SCIP_RETCODE SCIPaddSolFree(SCIP *scip, SCIP_SOL **sol, SCIP_Bool *stored)
SCIP_RETCODE SCIPprintSol(SCIP *scip, SCIP_SOL *sol, FILE *file, SCIP_Bool printzeros)
SCIP_RETCODE SCIPsetSolVal(SCIP *scip, SCIP_SOL *sol, SCIP_VAR *var, SCIP_Real val)
SCIP_Real SCIPgetSolVal(SCIP *scip, SCIP_SOL *sol, SCIP_VAR *var)
SCIP_Real SCIPinfinity(SCIP *scip)
SCIP_Bool SCIPisInfinity(SCIP *scip, SCIP_Real val)
SCIP_VARTYPE SCIPvarGetType(SCIP_VAR *var)
SCIP_RETCODE SCIPvarSetInitial(SCIP_VAR *var, SCIP_Bool initial)
SCIP_RETCODE SCIPreleaseVar(SCIP *scip, SCIP_VAR **var)
SCIP_RETCODE SCIPchgVarLbGlobal(SCIP *scip, SCIP_VAR *var, SCIP_Real newbound)
SCIP_RETCODE SCIPvarSetRemovable(SCIP_VAR *var, SCIP_Bool removable)
SCIP_RETCODE SCIPchgVarUbGlobal(SCIP *scip, SCIP_VAR *var, SCIP_Real newbound)
SCIP_RETCODE SCIPcreateVarBasic(SCIP *scip, SCIP_VAR **var, const char *name, SCIP_Real lb, SCIP_Real ub, SCIP_Real obj, SCIP_VARTYPE vartype)
SCIP_RETCODE SCIPchgVarObj(SCIP *scip, SCIP_VAR *var, SCIP_Real newobj)
int SCIPsnprintf(char *t, int len, const char *s,...)
SCIPfreeSol(scip, &heurdata->sol))
SCIPcreateSol(scip, &heurdata->sol, heur))
assert(minobj< SCIPgetCutoffbound(scip))
#define BMScopyMemoryArray(ptr, source, num)
SCIP_RETCODE SCIPincludeReaderNl(SCIP *scip)
#define SCIP_CALL_THROW(x)
SCIP_RETCODE SCIPwriteSolutionNl(SCIP *scip)
struct SCIP_ProbData SCIP_PROBDATA
#define SCIP_DECL_PROBDELORIG(x)
#define SCIP_DECL_READERREAD(x)
#define SCIP_DECL_READERCOPY(x)
enum SCIP_Retcode SCIP_RETCODE
@ SCIP_STATUS_TOTALNODELIMIT
@ SCIP_STATUS_BESTSOLLIMIT
@ SCIP_STATUS_USERINTERRUPT
@ SCIP_STATUS_STALLNODELIMIT
@ SCIP_VARTYPE_CONTINUOUS
enum SCIP_Vartype SCIP_VARTYPE