Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Windows version of gams from WSL #3147

Open
frankpiedra opened this issue Feb 19, 2024 · 5 comments
Open

Using Windows version of gams from WSL #3147

frankpiedra opened this issue Feb 19, 2024 · 5 comments

Comments

@frankpiedra
Copy link

Summary

I want to use gams as a solver with:

solver = SolverFactory('gams')

The problem is that I work from a Windows machine using WSL for the python-side, but gams is installed on the Windows-side.
pyomo doesn't seems capable of finding gams executable, even when adding gams folder to my PATH or symlinking it to my ~/.local/bin folder. I can run gams from the WSL terminal with no problem though

Steps to reproduce the issue

from pyomo.opt import SolverFactory
solver = SolverFactory('gams')
solver.available()

Error Message

NameError                                 Traceback (most recent call last)
Cell In[32], [line 1](vscode-notebook-cell:?execution_count=32&line=1)
----> [1](vscode-notebook-cell:?execution_count=32&line=1) solver.available()

File [~/.virtualenvs/forest_problem/lib/python3.10/site-packages/pyomo/solvers/plugins/solvers/GAMS.py:667](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/mnt/d/Papers_propios/Paper3/Pyomo_Code_Debug/~/.virtualenvs/forest_problem/lib/python3.10/site-packages/pyomo/solvers/plugins/solvers/GAMS.py:667), in GAMSShell.available(self, exception_flag)
    [665](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/mnt/d/Papers_propios/Paper3/Pyomo_Code_Debug/~/.virtualenvs/forest_problem/lib/python3.10/site-packages/pyomo/solvers/plugins/solvers/GAMS.py:665) avail = self._run_simple_model(1)
    [666](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/mnt/d/Papers_propios/Paper3/Pyomo_Code_Debug/~/.virtualenvs/forest_problem/lib/python3.10/site-packages/pyomo/solvers/plugins/solvers/GAMS.py:666) if not avail and exception_flag:
--> [667](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/mnt/d/Papers_propios/Paper3/Pyomo_Code_Debug/~/.virtualenvs/forest_problem/lib/python3.10/site-packages/pyomo/solvers/plugins/solvers/GAMS.py:667)     raise NameError(
    [668](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/mnt/d/Papers_propios/Paper3/Pyomo_Code_Debug/~/.virtualenvs/forest_problem/lib/python3.10/site-packages/pyomo/solvers/plugins/solvers/GAMS.py:668)         "'gams' command failed to solve a simple model - "
    [669](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/mnt/d/Papers_propios/Paper3/Pyomo_Code_Debug/~/.virtualenvs/forest_problem/lib/python3.10/site-packages/pyomo/solvers/plugins/solvers/GAMS.py:669)         "GAMS shell solver functionality is not available."
    [670](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/mnt/d/Papers_propios/Paper3/Pyomo_Code_Debug/~/.virtualenvs/forest_problem/lib/python3.10/site-packages/pyomo/solvers/plugins/solvers/GAMS.py:670)     )
    [671](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/mnt/d/Papers_propios/Paper3/Pyomo_Code_Debug/~/.virtualenvs/forest_problem/lib/python3.10/site-packages/pyomo/solvers/plugins/solvers/GAMS.py:671) return avail

NameError: 'gams' command failed to solve a simple model - GAMS shell solver functionality is not available.

Information on your system

Pyomo version:6.7.0
Python version:3.10.12
Operating system: Windows 10 with Ubuntu WSL
How Pyomo was installed (PyPI, conda, source): PyPI
Solver (if applicable): gams CPLEX

@mrmundt
Copy link
Contributor

mrmundt commented Feb 19, 2024

This may seem like a silly question, but are you importing all of pyomo.environ as well? I am on a Mac, and when I run:

In [1]: from pyomo.opt import SolverFactory

In [2]: solver = SolverFactory('gams')

I get a warning:

WARNING: Failed to create solver with name 'gams': The solver plugin was not
registered. Please confirm that the 'pyomo.environ' package has been imported.
Traceback (most recent call last):
  File "/pyomo/opt/base/solvers.py", line 161, in __call__
    raise RuntimeError(
RuntimeError:   The solver plugin was not registered.
  Please confirm that the 'pyomo.environ' package has been imported.

@frankpiedra
Copy link
Author

I'm not doing that import, should it be done when using gams? I didn't have problems on Windows

While debugging I replicated the command that's generated inside solver.available() (removing lo=0 to see the output) and I get a 145 code error:

$ /mnt/c/GAMS/45/gams /tmp/tmp595n4d7p/test.gms curdir=/tmp/tmp595n4d7p                        

--- Job test.gms Start 02/20/24 10:05:15 45.4.0 19dc3313 WEX-WEI x86 64bit/MS Windows                                   
*** Unable to open input file (RC=2) /tmp/tmp595n4d7p/test.gms                                                          
*** SysMsg: No such file or directory                                                                                   
--- Applying:                                                                                                               
C:\GAMS\45\gmsprmNT.txt                                                                                                 C:\Users\Usuario\Documents\GAMS\gamsconfig.yaml                                                                     
--- GAMS Parameters defined                                                                                                 
Input /tmp/tmp595n4d7p/test.gms                                                                                         
ScrDir \\wsl.localhost\Ubuntu\tmp\tmp595n4d7p\225a\                                                                     
SysDir C:\GAMS\45\                                                                                                     
CurDir \\wsl.localhost\Ubuntu\tmp\tmp595n4d7p\
*** Status: Terminated due to parameter errors 
--- Job test.gms Stop 02/20/24 10:05:15 elapsed 0:00:00.006

@frankpiedra
Copy link
Author

Moving that tmp file to the Windows filesystem seems to make it solve correctly though

@frankpiedra
Copy link
Author

Seems to be related to GAMS Windows executable not being able to read into WSL directories.

Steps to make a dirty fix

  • symlink gams.exe into some PATH directory, in my case ln -s /mnt/c/GAMS/45/gams.exe ~/.local/bin/gams
  • When setting up the solver, use a tmp directory on the Windows side with tmpdir:
SolverFactory('gams').solve(
        model, solver='CPLEX', 
        tee=True,
        add_options=['option reslim=30; option optcr = 0.0;'], 
        tmpdir="wsl.localhost/Ubuntu/mnt/d/tmpmodels",
        keepfiles=True
    )
  • Skip the check (this is the dirty part) of the executable availability, this is because this checks uses the WSL tmp directory and can't be setted up
! GAMS.py lines 677-695
 def _run_simple_model(self, n):
        solver_exec = self.executable()
        if solver_exec is None:
            return False
        tmpdir = mkdtemp()
        try:
            test = os.path.join(tmpdir, 'test.gms')
            with open(test, 'w') as FILE:
                FILE.write(self._simple_model(n))
            result = subprocess.run(
                [solver_exec, test, "curdir=" + tmpdir, 'lo=0'],
                stdout=subprocess.DEVNULL,
                stderr=subprocess.DEVNULL,
            )
            # This little hack:
            return True 
            # return not result.returncode
        finally:
            shutil.rmtree(tmpdir)
        return False 

@mrmundt
Copy link
Contributor

mrmundt commented Feb 20, 2024

@frankpiedra I'm glad it's working for you now. We are actively in the process of redesigning the solver interfaces (see #3137 and #1030 ). I'm going to add this to the very long list of "solver-related issues" on that PEP issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants