0

So I have a rather comlicated matlab function (it calls a simulation that in turn calls an external optimisation suite (cplex or gurobi)). And for certain settings and inputs the MATLAB function and the python function called from Matlab give the same result but for others they differ (correct answer is ~4500) python sometimes returns 0.015... or 162381, so widely varying results I can't spot a pattern or correlation for.

my guess would be either something with int/ float / double variable conversions, or some form of memory problem. The result comes straight from CPLEX so I'm little confused as to why it changes.

On a side note, if I return a structure that contains a structure of arrays, that kills the python kernel. That makes debugging from python a little harder (I have pymatbridge and metakernel installed)

Has anyone, had similar issues of unreliable matlab functions in python? Solution ideas other than, executing matlab from the console and reading in a results file?

ic_fl2
  • 831
  • 9
  • 29

1 Answers1

0

So turns out, that on occasion the code will run if some variables aren't specified as double, and in other cases it integer division or what not, results false results. I have no idea how this correlates to the input as it really shouldn't but I just went and specified all variables in the relevant section of code to be doubles and that fixed it. So

tl;dr: Even if it runs, just enforce all variables are doubles and the problem is solved.

Really something Mathworks should fix in their python api

ic_fl2
  • 831
  • 9
  • 29
  • I have not idea if this would help in your case, but if you're not already aware of it, you may want to take a look at http://stackoverflow.com/questions/183853/in-python-what-is-the-difference-between-and-when-used-for-division. In particular, using `from __future__ import division` can safeguard against issues like this in Python >= 2.2 (in your code, perhaps not in this Mathworks package). – rkersh Jun 30 '16 at 20:38
  • I already use `from __future__ import division` as I suspected this would cause issues. As stated, just plastering all variables in double() in Matlab fixed the issue, even though if executed in MATLAB the variables were all doubles already and the code ran in Python. I suspect MATLAB handles this differently internally and this causes issues with the python interface. Once I can reproduce it I'll post an example and file a bug report if needed. – ic_fl2 Jul 01 '16 at 18:39