matlab_and_gams:interfacing_optimization_and_visualization_software_via_the_gdxmrw_utilities

This shows you the differences between two versions of the page.

Both sides previous revision Previous revision Next revision | Previous revision | ||

matlab_and_gams:interfacing_optimization_and_visualization_software_via_the_gdxmrw_utilities [2015/02/10 17:37] tlastusilta [Are there any video tutorials available?] |
matlab_and_gams:interfacing_optimization_and_visualization_software_via_the_gdxmrw_utilities [2021/06/30 21:06] (current) Atharv Bhosekar [Is there anything to extract the parameter matrix (LP) from GAMS to Matlab?] |
||
---|---|---|---|

Line 4: | Line 4: | ||

**Note:** The gdxmrw utilities have been developed by [[sdirkse@gams.com|Steven P. Dirkse (GAMS Development)]] and [[ferris@cs.wisc.edu|Michael C. Ferris (University of Wisconsin Madiscon]] | **Note:** The gdxmrw utilities have been developed by [[sdirkse@gams.com|Steven P. Dirkse (GAMS Development)]] and [[ferris@cs.wisc.edu|Michael C. Ferris (University of Wisconsin Madiscon]] | ||

- | =====New Address===== | ||

- | All the GAMS/Matlab utilities (rgdx, wgdx, and gams) previously documented and made available here are fully integrated into the GAMS distribution, so no separate downloads are necessary. We recommend that you use the supported version of these utilities integrated into and documented in the GAMS distributions available for [[http://www.gams.com/download|download]]. | ||

- | |||

- | The previous versions, along with their documentation, can be found [[http://www.gams.com/~steve/gdxmrwOld.html|here]], but be aware that these older versions are unsupported and differ in syntax and functionality from the supported versions. | ||

=====FAQ===== | =====FAQ===== | ||

- | ====Are there any video tutorials available?==== | ||

- | Yes, from the Youtube Channel "GAMS Lessons" you can find the following GAMS and Matlab videos: \\ [[https://www.youtube.com/watch?v=HPn_q8nlktE|Setup and Introduction to GDXMRW]] \\ [[https://www.youtube.com/watch?v=QBOXB3foG8Y|GDXMRW tools IRGDX and IWGDX]] \\ [[https://www.youtube.com/watch?v=s755kr8MH0Y|GDXMRW tools RGDX and WGDX]] | ||

- | |||

- | ====Which platforms are currently supported?==== | ||

- | gdxmrw is available for Windows 32-bit, Windows 64-bit, Linux 64-bit, and Mac OS X 64-bit. Note that, we are not planning to support platforms that are not supported by MathWorks, see [[http://www.mathworks.com/support/sysreq/roadmap.html|Platform Road Map for the MATLAB]]. | ||

- | Please also note that a 32-bit version of Matlab requires a 32-bit version of GAMS, and the 64-bit version of Matlab will only work with the 64-bit version of GAMS. | ||

====How can I construct an interface between GAMS and Matlab?==== | ====How can I construct an interface between GAMS and Matlab?==== | ||

Line 30: | Line 20: | ||

An example application in two variations is available in the datalib as [[http://www.gams.com/datalib/libhtml/gdxmrw_tr1.htm|gdxmrw_tr1]] and [[http://www.gams.com/datalib/libhtml/gdxmrw_tr2.htm|gdxmrw_tr2]]. The .gms files contain comments as well as GAMS source. The .m files should be run from Matlab. | An example application in two variations is available in the datalib as [[http://www.gams.com/datalib/libhtml/gdxmrw_tr1.htm|gdxmrw_tr1]] and [[http://www.gams.com/datalib/libhtml/gdxmrw_tr2.htm|gdxmrw_tr2]]. The .gms files contain comments as well as GAMS source. The .m files should be run from Matlab. | ||

- | ====The initial tests don't work. What should I do?==== | ||

- | |||

- | Extended installation debugging and support instructions are [[interfaces:gdxmrwSupport|available here]]. | ||

====Error "Matlab cannot save changes to the path" when setting the Matlab Path on Windows==== | ====Error "Matlab cannot save changes to the path" when setting the Matlab Path on Windows==== | ||

Line 69: | Line 56: | ||

The error message indicates that you did not set the path to GAMS in Matlab or that you run an old GAMS version which does not include gams.m and the mex library files. | The error message indicates that you did not set the path to GAMS in Matlab or that you run an old GAMS version which does not include gams.m and the mex library files. | ||

- | ==== Error message: Attempt to execute SCRIPT gams as a function ==== | ||

- | The full text of the error message is: | ||

- | <code> | ||

- | last error message: Attempt to execute SCRIPT gams as a function: | ||

- | C:\Program Files (x86)\GAMS23.7\gams.m | ||

- | last error identifier: MATLAB:scriptNotAFunction | ||

- | See testinst log file testinstlog.txt for details | ||

- | ??? Error using ==> testinst at 127 | ||

- | Error in testinst: terminating prematurely | ||

- | </code> | ||

- | |||

- | and the file testinstlog.txt reads something like: | ||

- | <code> | ||

- | Date = 15-Jul-2011 | ||

- | Matlab version = 7.12.0.635 (R2011a) | ||

- | Error in testinst: terminating prematurely | ||

- | last error message: Attempt to execute SCRIPT gams as a function: | ||

- | C:\Program Files (x86)\GAMS23.7\gams.m | ||

- | last error identifier: MATLAB:scriptNotAFunction | ||

- | </code> | ||

- | |||

- | |||

- | The problem here is that the version of GAMS and the version of Matlab are built for different architectures, so the wrapper-script ''gams.m'' was not able to find a ''gams'' Mex-file built for the same architecture as the running Matlab. Most likely you are trying to run a 32-bit version of GAMS with a 64-bit version of Matlab (or vice versa). Your GAMS license will work equally well with the 32-bit and 64-bit versions of GAMS so you should install a GAMS system built for the same platform/architecture as the Matlab system you have installed. To check the Matlab architecture, use Matlab's ''computer'' function. The ''mexext'' function gives a clue about what Mex-files need to be in the GAMS sysdir as well. | ||

- | <code> | ||

- | >> computer | ||

- | |||

- | ans = | ||

- | |||

- | PCWIN64 | ||

- | |||

- | >> computer('arch') | ||

- | |||

- | ans = | ||

- | |||

- | win64 | ||

- | |||

- | >> mexext | ||

- | |||

- | mexw64 | ||

- | |||

- | </code> | ||

==== Error message: Invalid MEX-file 'wgdx.mexw64': The specified module could not be found. ==== | ==== Error message: Invalid MEX-file 'wgdx.mexw64': The specified module could not be found. ==== | ||

Line 115: | Line 61: | ||

Matlab fails to load binary MEX-files if it cannot find **all** .dll files referenced by the MEX-file. Thus the message above is somewhat misleading: it gives the impression that the specified Mex-file is invalid or not found, instead of pointing out the missing dependencies. More information on this error, including suggested fixes, can be found on the Matlab Web site [[http://www.mathworks.com/help/matlab/matlab_external/invalid-mex-file-error.html|here]]. | Matlab fails to load binary MEX-files if it cannot find **all** .dll files referenced by the MEX-file. Thus the message above is somewhat misleading: it gives the impression that the specified Mex-file is invalid or not found, instead of pointing out the missing dependencies. More information on this error, including suggested fixes, can be found on the Matlab Web site [[http://www.mathworks.com/help/matlab/matlab_external/invalid-mex-file-error.html|here]]. | ||

- | We have found that users getting this error message are sometimes missing the Microsoft C++ Redistributable Runtime Libraries. These can be installed via Windows Update. | + | We have found that users getting this error message are sometimes missing the Microsoft Visual C++ Redistributable runtime libraries. These can be installed via Windows Update. Note that many versions of these libraries are available. The version of the Microsoft Visual C++ Redistributable that is required depends on what version of MSVC++ was used to build the MEX-file. Most likely, the 2012 or 2013 version is the one needed, and it is safe to install both of them. |

- | ==== Error running gams() function in a Matlab loop ==== | + | If you must, you can peek into the MEX-files for the string MSVCR to learn more. A dependency on MSVCR110.dll is satisfied by the Microsoft Visual C++ 2012 Redistributable. A dependency on MSVCR120.dll is satisfied by the Microsoft Visual C++ 2013 Redistributable. At this time (Jan 2017) we don't use newer compilers, and nothing older can be actively supported. |

- | | + | |

- | Some users have reported errors when calling gams() from within a Matlab loop. The symptoms vary - some users report incorrect values being returned to Matlab, while others report that gams.exe has crashed. | + | |

- | | + | |

- | There are three workarounds or solutions to the problem. | + | |

- | | + | |

- | * **This problem has been fixed.** If you have a GAMS Distribution 23.9.3 (Sep 2012) or later it should have the updated mex-files. If you don't have a version as recent as this, the solution is to update your GAMS system to use distribution 23.9.3 or later. To check the version info of your mex-files: | + | |

- | <code> | + | |

- | >> wgdx('?') | + | |

- | GDXMRW::wgdx : rev32781 2012-04-26 18:00:27Z sdirkse | + | |

- | </code> | + | |

- | This is the earliest revision with the fix. If you have a lower revision number than 32781 or a last-source-change date earlier than 26 April 2012, you should update to a newer GAMS distribution. Don't try to just slide some newer mex-files into an older GAMS distribution: components are designed and tested to work with the distribution they belong to. | + | |

- | | + | |

- | * Another approach is to use rgdx() and wgdx() to transfer data between Matlab and GAMS and to use Matlab's system() call to execute the GAMS job. This solution has some other benefits as well, as described [[matlab_and_gams:interfacing_optimization_and_visualization_software_via_the_gdxmrw_utilities#how_can_i_construct_an_interface_between_gams_and_matlab|above]]. For example: | + | |

- | <code> | + | |

- | for i=1:365 | + | |

- | wgdx(input,...); | + | |

- | % sometimes the mex-function gams fails when run in a loop | + | |

- | % gams(model); | + | |

- | % but running gams in a subshell via the system() function is OK | + | |

- | system (['gams model lo=2 --TRIP=', int2str(i)]); | + | |

- | rgdx(results); | + | |

- | end | + | |

- | </code> | + | |

- | * The most painful and least recommended approach, but one that allows you to keep using the gams() mex-function, is to copy the mex-function and use the copy in your Matlab code. When making the copy, do not copy or modify the gams.m script - this only contains documentation for the mex-function. The mex-function extension varies by platform - use the Matlab routine ''mexext'' to get the extension. | + | |

- | <code> | + | |

- | >> mexext | + | |

- | | + | |

- | mexw64 | + | |

- | | + | |

- | >> which gams | + | |

- | C:\gams_64\23.7.2\gams.mexw64 | + | |

- | >> cd \gams_64\23.7.2\ | + | |

- | >> system ('copy gams.mexw64 rgams.mexw64') | + | |

- | 1 file(s) copied. | + | |

- | | + | |

- | ans = | + | |

- | | + | |

- | 0 | + | |

- | | + | |

- | >> which rgams | + | |

- | C:\gams_64\23.7.2\rgams.mexw64 | + | |

- | >> | + | |

- | </code> | + | |

- | | + | |

- | Once you have made the copy, you can use the new name as necessary to avoid problems with running gams() in a loop. If you are surprised that such a trivial change is enough to work around the problem, you are not alone. | + | |

- | ==== Is there anything to extract the parameter matrix (LP) from GAMS to Matlab? ==== | + | |

- | | + | |

- | There are two ways to do that. One way is to use the GAMS/Convert tool to write out the LP data in GDX form: The option file ''convert.opt'' looks like | + | |

- | <code>jacobian jac.gdx </code> | + | |

- | | + | |

- | and then you can use the GDXMRW utilities to read the GDX data in Matlab. This will be quite efficient for large data and you should get the data with full double precision. | + | |

- | | + | |

- | Another option is to use the MPECDUMP solver and the ''matlab'' option. This dumps the data to text files that can be written in Matlab, and also writes a .m file to do it for you. Try this by creating the option file ''mpecdump.opt'' with | + | |

- | <code>matlab xxx</code> | + | |

- | | + | |

- | and run | + | |

- | <code> | + | |

- | gamslib trnsport | + | |

- | gams trnsport lp mpecdump optfile 1 | + | |

- | </code> | + | |

- | and you'll get ''xxx.m'' that reads lots of data into Matlab. | ||

IMPRESSUM / LEGAL NOTICE
PRIVACY POLICY
matlab_and_gams/interfacing_optimization_and_visualization_software_via_the_gdxmrw_utilities.1423586278.txt.gz · Last modified: 2015/02/10 17:37 by tlastusilta