Skip to content

Commit fee61ed

Browse files
Updated nomad mex interface for 2022b
1 parent 5565511 commit fee61ed

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

Solvers/Source/nomadmex.cpp

+12-14
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,10 @@ class matlabEval : public NOMAD::Evaluator {
136136
{
137137
char errstr[1024];
138138
bool stop = false;
139-
int i, j, n = x.size(), status;
139+
int i, j, n = x.size();
140140
double *xm, *fvals;
141141
mxLogical *sur;
142+
mxArray *status = nullptr;
142143
count_eval = true; //mexErrMsgTxt will kill MEX
143144

144145
//Check for Ctrl-C
@@ -162,7 +163,7 @@ class matlabEval : public NOMAD::Evaluator {
162163
}
163164
//Call MATLAB Objective
164165
try {
165-
status = mexCallMATLAB(1, fun->plhs, fun->nrhs, fun->prhs, fun->f);
166+
status = mexCallMATLABWithTrap(1, fun->plhs, fun->nrhs, fun->prhs, fun->f);
166167
}
167168
//Note if these errors occur it is due to errors in MATLAB code, no way to recover?
168169
catch(exception &e) {
@@ -206,7 +207,7 @@ class matlabEval : public NOMAD::Evaluator {
206207
xm[i] = x[i].value();
207208
//Call MATLAB Constraint
208209
try {
209-
status = mexCallMATLAB(1, con->plhs, con->nrhs, con->prhs, con->f);
210+
status = mexCallMATLABWithTrap(1, con->plhs, con->nrhs, con->prhs, con->f);
210211
}
211212
catch(...)
212213
{
@@ -254,7 +255,7 @@ class matlabEval : public NOMAD::Evaluator {
254255
memcpy(mxGetPr(iterF->prhs[2]), fvals, sizeof(double));
255256
memcpy(mxGetPr(iterF->prhs[3]), xm, n * sizeof(double));
256257
try {
257-
status = mexCallMATLAB(1, iterF->plhs, 4, iterF->prhs, iterF->f);
258+
status = mexCallMATLABWithTrap(1, iterF->plhs, 4, iterF->prhs, iterF->f);
258259
}
259260
catch (...)
260261
{
@@ -668,9 +669,7 @@ void mexFunction (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
668669
mexPrintf("------------------------------------------------------------------\n");
669670
mexEvalString("drawnow;"); //flush draw buffer
670671
}
671-
//Let this file sort out errors
672-
mexSetTrapFlag(1);
673-
672+
674673
//Reset tags and bbe (C.Tribes 3/14)
675674
NOMAD::Eval_Point::reset_tags_and_bbes();
676675

@@ -771,8 +770,6 @@ void mexFunction (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
771770

772771
//Return cout to initial buffer
773772
std::cout.rdbuf(cout_sbuf);
774-
//Return error control to default
775-
mexSetTrapFlag(0);
776773

777774
//Free Memory
778775
if(mSEval) delete mSEval; mSEval = NULL;
@@ -784,14 +781,15 @@ void mexFunction (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
784781
//Determine Variable Return Types + # Objectives + #NL Constraints (only OPTI Version)
785782
vector<NOMAD::bb_output_type> detRetTypes(usrFcn *fun, mxArray *out_types, int *nobj, usrFcn *con, int *ncon, double *x0, size_t n)
786783
{
787-
int i, j, stat;
784+
int i, j;
785+
mxArray *status = nullptr;
788786

789787
//Test Blackbox / Objective Evaluation
790788
fun->plhs[0] = NULL;
791789
memcpy(mxGetPr(fun->prhs[fun->xrhs]), x0, n * sizeof(double));
792790
//Call MATLAB Objective
793-
stat = mexCallMATLAB(1, fun->plhs, fun->nrhs, fun->prhs, fun->f);
794-
if(stat)
791+
status = mexCallMATLABWithTrap(1, fun->plhs, fun->nrhs, fun->prhs, fun->f);
792+
if(status)
795793
mexErrMsgTxt("Error calling Objective Function!");
796794
//Ensure we have a real column
797795
if(mxGetN(fun->plhs[0]) > mxGetM(fun->plhs[0]))
@@ -811,8 +809,8 @@ vector<NOMAD::bb_output_type> detRetTypes(usrFcn *fun, mxArray *out_types, int *
811809
con->plhs[0] = NULL;
812810
memcpy(mxGetPr(con->prhs[con->xrhs]), x0, n * sizeof(double));
813811
//Call MATLAB Objective
814-
stat = mexCallMATLAB(1, con->plhs, con->nrhs, con->prhs, con->f);
815-
if(stat)
812+
status = mexCallMATLABWithTrap(1, con->plhs, con->nrhs, con->prhs, con->f);
813+
if(status)
816814
mexErrMsgTxt("Error calling Constraint Function!");
817815
//Ensure we have a real column
818816
if(mxGetN(con->plhs[0]) > mxGetM(con->plhs[0]))

0 commit comments

Comments
 (0)