Skip to content

Commit

Permalink
Return PyDict in getSolutionDict (workaround for #2)
Browse files Browse the repository at this point in the history
  • Loading branch information
fdabrandao committed Jul 6, 2021
1 parent 69b010c commit c31be04
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 74 deletions.
22 changes: 12 additions & 10 deletions python/common-python-extensions.i
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
%extend ampls::AMPLModel{

std::map<std::string, double> _getSolutionDict() {

%extend ampls::AMPLModel{
PyObject* getSolutionDict() {
PyObject* res = PyDict_New();
std::vector<double> sol = self->getSolutionVector();
std::map<int, std::string> map = self->getVarMapInverse();
std::map<std::string, double> res;
std::map<int, std::string>::const_iterator it;
for (it = map.begin(); it != map.end(); it++)
res[it->second] = sol[it->first];
for (it = map.begin(); it != map.end(); ++it) {
PyDict_SetItem(res, PyString_FromString(it->second.c_str()), PyFloat_FromDouble(sol[it->first]));
}
return res;
}
}

%extend ampls::impl::BaseCallback{

std::map<std::string, double> _getSolutionDict() {
PyObject* getSolutionDict() {
PyObject* res = PyDict_New();
std::vector<double> sol = self->getSolutionVector();
std::map<int, std::string> map = self->getVarMapInverse();
std::map<std::string, double> res;
std::map<int, std::string>::const_iterator it;
for (it = map.begin(); it != map.end(); it++)
res[it->second] = sol[it->first];
for (it = map.begin(); it != map.end(); ++it) {
PyDict_SetItem(res, PyString_FromString(it->second.c_str()), PyFloat_FromDouble(sol[it->first]));
}
return res;
}
}
10 changes: 0 additions & 10 deletions python/cplex/amplpy_cplex/patch.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import types


def getSolutionDict(self):
return dict(self._getSolutionDict())


def setCallback(self, cb):
def run(self):
try:
Expand All @@ -15,8 +11,6 @@ def run(self):

cb._run = cb.run
cb.run = types.MethodType(run, cb)
# Workaround for issue #2
cb.getSolutionDict = types.MethodType(getSolutionDict, cb)
super(type(cb), cb).__init__()
self._setCallback(cb)

Expand All @@ -42,8 +36,6 @@ def exportCplexModel(self, options=None):
os.remove(fname + '.nl')
model._setCallback = model.setCallback
model.setCallback = types.MethodType(setCallback, model)
# Workaround for issue #2
model.getSolutionDict = types.MethodType(getSolutionDict, model)
return model
except:
shutil.rmtree(tmp)
Expand Down Expand Up @@ -76,8 +68,6 @@ def exportGurobiModel(self, options=None):
os.remove(fname + '.nl')
model._setCallback = model.setCallback
model.setCallback = types.MethodType(setCallback, model)
# Workaround for issue #2
model.getSolutionDict = types.MethodType(getSolutionDict, model)
return model
except:
shutil.rmtree(tmp)
Expand Down
46 changes: 24 additions & 22 deletions python/cplex/amplpy_cplex/swig/amplpy_cplex_swig_wrap.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -7158,13 +7158,14 @@ SWIG_FromCharPtr(const char *cptr)
return SWIG_FromCharPtrAndSize(cptr, (cptr ? strlen(cptr) : 0));
}

SWIGINTERN std::map< std::string,double,std::less< std::string >,std::allocator< std::pair< std::string const,double > > > ampls_impl_BaseCallback__getSolutionDict(ampls::impl::BaseCallback *self){
SWIGINTERN PyObject *ampls_impl_BaseCallback_getSolutionDict(ampls::impl::BaseCallback *self){
PyObject* res = PyDict_New();
std::vector<double> sol = self->getSolutionVector();
std::map<int, std::string> map = self->getVarMapInverse();
std::map<std::string, double> res;
std::map<int, std::string>::const_iterator it;
for (it = map.begin(); it != map.end(); it++)
res[it->second] = sol[it->first];
for (it = map.begin(); it != map.end(); ++it) {
PyDict_SetItem(res, PyString_FromString(it->second.c_str()), PyFloat_FromDouble(sol[it->first]));
}
return res;
}

Expand All @@ -7181,13 +7182,14 @@ SWIG_AsVal_bool (PyObject *obj, bool *val)
return SWIG_OK;
}

SWIGINTERN std::map< std::string,double,std::less< std::string >,std::allocator< std::pair< std::string const,double > > > ampls_AMPLModel__getSolutionDict(ampls::AMPLModel *self){
SWIGINTERN PyObject *ampls_AMPLModel_getSolutionDict(ampls::AMPLModel *self){
PyObject* res = PyDict_New();
std::vector<double> sol = self->getSolutionVector();
std::map<int, std::string> map = self->getVarMapInverse();
std::map<std::string, double> res;
std::map<int, std::string>::const_iterator it;
for (it = map.begin(); it != map.end(); it++)
res[it->second] = sol[it->first];
for (it = map.begin(); it != map.end(); ++it) {
PyDict_SetItem(res, PyString_FromString(it->second.c_str()), PyFloat_FromDouble(sol[it->first]));
}
return res;
}

Expand Down Expand Up @@ -20159,31 +20161,31 @@ SWIGINTERN PyObject *_wrap_BaseCallback_getValue(PyObject *self, PyObject *args)
}


SWIGINTERN PyObject *_wrap_BaseCallback__getSolutionDict(PyObject *self, PyObject *args) {
SWIGINTERN PyObject *_wrap_BaseCallback_getSolutionDict(PyObject *self, PyObject *args) {
PyObject *resultobj = 0;
ampls::impl::BaseCallback *arg1 = (ampls::impl::BaseCallback *) 0 ;
void *argp1 = 0 ;
int res1 = 0 ;
PyObject *swig_obj[1] ;
std::map< std::string,double,std::less< std::string >,std::allocator< std::pair< std::string const,double > > > result;
PyObject *result = 0 ;

if (!SWIG_Python_UnpackTuple(args, "BaseCallback__getSolutionDict", 0, 0, 0)) SWIG_fail;
if (!SWIG_Python_UnpackTuple(args, "BaseCallback_getSolutionDict", 0, 0, 0)) SWIG_fail;
res1 = SWIG_ConvertPtr(self, &argp1,SWIGTYPE_p_ampls__impl__BaseCallback, 0 | 0 );
if (!SWIG_IsOK(res1)) {
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "BaseCallback__getSolutionDict" "', argument " "1"" of type '" "ampls::impl::BaseCallback *""'");
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "BaseCallback_getSolutionDict" "', argument " "1"" of type '" "ampls::impl::BaseCallback *""'");
}
arg1 = reinterpret_cast< ampls::impl::BaseCallback * >(argp1);
{
try {
result = ampls_impl_BaseCallback__getSolutionDict(arg1);
result = (PyObject *)ampls_impl_BaseCallback_getSolutionDict(arg1);
} catch(const ampls::AMPLSolverException &e) {
SWIG_exception(SWIG_RuntimeError, e.what());
}
catch(...) {
SWIG_exception(SWIG_RuntimeError, "Unknown exception");
}
}
resultobj = swig::from(static_cast< std::map< std::string,double,std::less< std::string >,std::allocator< std::pair< std::string const,double > > > >(result));
resultobj = result;
return resultobj;
fail:
return NULL;
Expand Down Expand Up @@ -21531,31 +21533,31 @@ SWIGINTERN PyObject *_wrap_AMPLModel_getAMPLsDoubleParameter(PyObject *self, PyO
}


SWIGINTERN PyObject *_wrap_AMPLModel__getSolutionDict(PyObject *self, PyObject *args) {
SWIGINTERN PyObject *_wrap_AMPLModel_getSolutionDict(PyObject *self, PyObject *args) {
PyObject *resultobj = 0;
ampls::AMPLModel *arg1 = (ampls::AMPLModel *) 0 ;
void *argp1 = 0 ;
int res1 = 0 ;
PyObject *swig_obj[1] ;
std::map< std::string,double,std::less< std::string >,std::allocator< std::pair< std::string const,double > > > result;
PyObject *result = 0 ;

if (!SWIG_Python_UnpackTuple(args, "AMPLModel__getSolutionDict", 0, 0, 0)) SWIG_fail;
if (!SWIG_Python_UnpackTuple(args, "AMPLModel_getSolutionDict", 0, 0, 0)) SWIG_fail;
res1 = SWIG_ConvertPtr(self, &argp1,SWIGTYPE_p_ampls__AMPLModel, 0 | 0 );
if (!SWIG_IsOK(res1)) {
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "AMPLModel__getSolutionDict" "', argument " "1"" of type '" "ampls::AMPLModel *""'");
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "AMPLModel_getSolutionDict" "', argument " "1"" of type '" "ampls::AMPLModel *""'");
}
arg1 = reinterpret_cast< ampls::AMPLModel * >(argp1);
{
try {
result = ampls_AMPLModel__getSolutionDict(arg1);
result = (PyObject *)ampls_AMPLModel_getSolutionDict(arg1);
} catch(const ampls::AMPLSolverException &e) {
SWIG_exception(SWIG_RuntimeError, e.what());
}
catch(...) {
SWIG_exception(SWIG_RuntimeError, "Unknown exception");
}
}
resultobj = swig::from(static_cast< std::map< std::string,double,std::less< std::string >,std::allocator< std::pair< std::string const,double > > > >(result));
resultobj = result;
return resultobj;
fail:
return NULL;
Expand Down Expand Up @@ -66659,7 +66661,7 @@ SWIGINTERN PyMethodDef SwigPyBuiltin__ampls__impl__BaseCallback_methods[] = {
{ "getMessage", _wrap_BaseCallback_getMessage, METH_NOARGS, "" },
{ "getAMPLWhere", _wrap_BaseCallback_getAMPLWhere, METH_NOARGS, "" },
{ "getValue", _wrap_BaseCallback_getValue, METH_O, "" },
{ "_getSolutionDict", _wrap_BaseCallback__getSolutionDict, METH_NOARGS, "" },
{ "getSolutionDict", _wrap_BaseCallback_getSolutionDict, METH_NOARGS, "" },
{ NULL, NULL, 0, NULL } /* Sentinel */
};

Expand Down Expand Up @@ -67109,7 +67111,7 @@ SWIGINTERN PyMethodDef SwigPyBuiltin__ampls__AMPLModel_methods[] = {
{ "setAMPLsParameter", _wrap_AMPLModel_setAMPLsParameter, METH_VARARGS, "" },
{ "getAMPLsIntParameter", _wrap_AMPLModel_getAMPLsIntParameter, METH_O, "" },
{ "getAMPLsDoubleParameter", _wrap_AMPLModel_getAMPLsDoubleParameter, METH_O, "" },
{ "_getSolutionDict", _wrap_AMPLModel__getSolutionDict, METH_NOARGS, "" },
{ "getSolutionDict", _wrap_AMPLModel_getSolutionDict, METH_NOARGS, "" },
{ NULL, NULL, 0, NULL } /* Sentinel */
};

Expand Down
10 changes: 0 additions & 10 deletions python/gurobi/amplpy_gurobi/patch.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import types


def getSolutionDict(self):
return dict(self._getSolutionDict())


def setCallback(self, cb):
def run(self):
try:
Expand All @@ -15,8 +11,6 @@ def run(self):

cb._run = cb.run
cb.run = types.MethodType(run, cb)
# Workaround for issue #2
cb.getSolutionDict = types.MethodType(getSolutionDict, cb)
super(type(cb), cb).__init__()
self._setCallback(cb)

Expand All @@ -42,8 +36,6 @@ def exportCplexModel(self, options=None):
os.remove(fname + '.nl')
model._setCallback = model.setCallback
model.setCallback = types.MethodType(setCallback, model)
# Workaround for issue #2
model.getSolutionDict = types.MethodType(getSolutionDict, model)
return model
except:
shutil.rmtree(tmp)
Expand Down Expand Up @@ -76,8 +68,6 @@ def exportGurobiModel(self, options=None):
os.remove(fname + '.nl')
model._setCallback = model.setCallback
model.setCallback = types.MethodType(setCallback, model)
# Workaround for issue #2
model.getSolutionDict = types.MethodType(getSolutionDict, model)
return model
except:
shutil.rmtree(tmp)
Expand Down
46 changes: 24 additions & 22 deletions python/gurobi/amplpy_gurobi/swig/amplpy_gurobi_swig_wrap.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -7050,13 +7050,14 @@ SWIG_FromCharPtr(const char *cptr)
return SWIG_FromCharPtrAndSize(cptr, (cptr ? strlen(cptr) : 0));
}

SWIGINTERN std::map< std::string,double,std::less< std::string >,std::allocator< std::pair< std::string const,double > > > ampls_impl_BaseCallback__getSolutionDict(ampls::impl::BaseCallback *self){
SWIGINTERN PyObject *ampls_impl_BaseCallback_getSolutionDict(ampls::impl::BaseCallback *self){
PyObject* res = PyDict_New();
std::vector<double> sol = self->getSolutionVector();
std::map<int, std::string> map = self->getVarMapInverse();
std::map<std::string, double> res;
std::map<int, std::string>::const_iterator it;
for (it = map.begin(); it != map.end(); it++)
res[it->second] = sol[it->first];
for (it = map.begin(); it != map.end(); ++it) {
PyDict_SetItem(res, PyString_FromString(it->second.c_str()), PyFloat_FromDouble(sol[it->first]));
}
return res;
}

Expand All @@ -7073,13 +7074,14 @@ SWIG_AsVal_bool (PyObject *obj, bool *val)
return SWIG_OK;
}

SWIGINTERN std::map< std::string,double,std::less< std::string >,std::allocator< std::pair< std::string const,double > > > ampls_AMPLModel__getSolutionDict(ampls::AMPLModel *self){
SWIGINTERN PyObject *ampls_AMPLModel_getSolutionDict(ampls::AMPLModel *self){
PyObject* res = PyDict_New();
std::vector<double> sol = self->getSolutionVector();
std::map<int, std::string> map = self->getVarMapInverse();
std::map<std::string, double> res;
std::map<int, std::string>::const_iterator it;
for (it = map.begin(); it != map.end(); it++)
res[it->second] = sol[it->first];
for (it = map.begin(); it != map.end(); ++it) {
PyDict_SetItem(res, PyString_FromString(it->second.c_str()), PyFloat_FromDouble(sol[it->first]));
}
return res;
}

Expand Down Expand Up @@ -20095,31 +20097,31 @@ SWIGINTERN PyObject *_wrap_BaseCallback_getValue(PyObject *self, PyObject *args)
}


SWIGINTERN PyObject *_wrap_BaseCallback__getSolutionDict(PyObject *self, PyObject *args) {
SWIGINTERN PyObject *_wrap_BaseCallback_getSolutionDict(PyObject *self, PyObject *args) {
PyObject *resultobj = 0;
ampls::impl::BaseCallback *arg1 = (ampls::impl::BaseCallback *) 0 ;
void *argp1 = 0 ;
int res1 = 0 ;
PyObject *swig_obj[1] ;
std::map< std::string,double,std::less< std::string >,std::allocator< std::pair< std::string const,double > > > result;
PyObject *result = 0 ;

if (!SWIG_Python_UnpackTuple(args, "BaseCallback__getSolutionDict", 0, 0, 0)) SWIG_fail;
if (!SWIG_Python_UnpackTuple(args, "BaseCallback_getSolutionDict", 0, 0, 0)) SWIG_fail;
res1 = SWIG_ConvertPtr(self, &argp1,SWIGTYPE_p_ampls__impl__BaseCallback, 0 | 0 );
if (!SWIG_IsOK(res1)) {
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "BaseCallback__getSolutionDict" "', argument " "1"" of type '" "ampls::impl::BaseCallback *""'");
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "BaseCallback_getSolutionDict" "', argument " "1"" of type '" "ampls::impl::BaseCallback *""'");
}
arg1 = reinterpret_cast< ampls::impl::BaseCallback * >(argp1);
{
try {
result = ampls_impl_BaseCallback__getSolutionDict(arg1);
result = (PyObject *)ampls_impl_BaseCallback_getSolutionDict(arg1);
} catch(const ampls::AMPLSolverException &e) {
SWIG_exception(SWIG_RuntimeError, e.what());
}
catch(...) {
SWIG_exception(SWIG_RuntimeError, "Unknown exception");
}
}
resultobj = swig::from(static_cast< std::map< std::string,double,std::less< std::string >,std::allocator< std::pair< std::string const,double > > > >(result));
resultobj = result;
return resultobj;
fail:
return NULL;
Expand Down Expand Up @@ -21467,31 +21469,31 @@ SWIGINTERN PyObject *_wrap_AMPLModel_getAMPLsDoubleParameter(PyObject *self, PyO
}


SWIGINTERN PyObject *_wrap_AMPLModel__getSolutionDict(PyObject *self, PyObject *args) {
SWIGINTERN PyObject *_wrap_AMPLModel_getSolutionDict(PyObject *self, PyObject *args) {
PyObject *resultobj = 0;
ampls::AMPLModel *arg1 = (ampls::AMPLModel *) 0 ;
void *argp1 = 0 ;
int res1 = 0 ;
PyObject *swig_obj[1] ;
std::map< std::string,double,std::less< std::string >,std::allocator< std::pair< std::string const,double > > > result;
PyObject *result = 0 ;

if (!SWIG_Python_UnpackTuple(args, "AMPLModel__getSolutionDict", 0, 0, 0)) SWIG_fail;
if (!SWIG_Python_UnpackTuple(args, "AMPLModel_getSolutionDict", 0, 0, 0)) SWIG_fail;
res1 = SWIG_ConvertPtr(self, &argp1,SWIGTYPE_p_ampls__AMPLModel, 0 | 0 );
if (!SWIG_IsOK(res1)) {
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "AMPLModel__getSolutionDict" "', argument " "1"" of type '" "ampls::AMPLModel *""'");
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "AMPLModel_getSolutionDict" "', argument " "1"" of type '" "ampls::AMPLModel *""'");
}
arg1 = reinterpret_cast< ampls::AMPLModel * >(argp1);
{
try {
result = ampls_AMPLModel__getSolutionDict(arg1);
result = (PyObject *)ampls_AMPLModel_getSolutionDict(arg1);
} catch(const ampls::AMPLSolverException &e) {
SWIG_exception(SWIG_RuntimeError, e.what());
}
catch(...) {
SWIG_exception(SWIG_RuntimeError, "Unknown exception");
}
}
resultobj = swig::from(static_cast< std::map< std::string,double,std::less< std::string >,std::allocator< std::pair< std::string const,double > > > >(result));
resultobj = result;
return resultobj;
fail:
return NULL;
Expand Down Expand Up @@ -41240,7 +41242,7 @@ SWIGINTERN PyMethodDef SwigPyBuiltin__ampls__impl__BaseCallback_methods[] = {
{ "getMessage", _wrap_BaseCallback_getMessage, METH_NOARGS, "" },
{ "getAMPLWhere", _wrap_BaseCallback_getAMPLWhere, METH_NOARGS, "" },
{ "getValue", _wrap_BaseCallback_getValue, METH_O, "" },
{ "_getSolutionDict", _wrap_BaseCallback__getSolutionDict, METH_NOARGS, "" },
{ "getSolutionDict", _wrap_BaseCallback_getSolutionDict, METH_NOARGS, "" },
{ NULL, NULL, 0, NULL } /* Sentinel */
};

Expand Down Expand Up @@ -41690,7 +41692,7 @@ SWIGINTERN PyMethodDef SwigPyBuiltin__ampls__AMPLModel_methods[] = {
{ "setAMPLsParameter", _wrap_AMPLModel_setAMPLsParameter, METH_VARARGS, "" },
{ "getAMPLsIntParameter", _wrap_AMPLModel_getAMPLsIntParameter, METH_O, "" },
{ "getAMPLsDoubleParameter", _wrap_AMPLModel_getAMPLsDoubleParameter, METH_O, "" },
{ "_getSolutionDict", _wrap_AMPLModel__getSolutionDict, METH_NOARGS, "" },
{ "getSolutionDict", _wrap_AMPLModel_getSolutionDict, METH_NOARGS, "" },
{ NULL, NULL, 0, NULL } /* Sentinel */
};

Expand Down

0 comments on commit c31be04

Please sign in to comment.