Windows: I can't use ampl to call solver via pyomo

I use pyomo to formulate my problem and try to use ampl to call ipopt. According to documents, I have installed coin module via: python -m amplpy.modules install coin.
However when I use :pyo.SolverFactory(modules.find(f"{self.solver}"), solve_io=“nl”),
it reported that:
Failed to create solver with name ‘D’: Failed to set executable for
solver asl. File with name=D either does not exist or it is not executable. To
skip this validation, call set_executable with validate=False.
The SolverFactory was unable to create the solver “D”
and returned an UnknownSolver object. This error is raised at the point
where the UnknownSolver object was used as if it were valid (by calling
method “solve”).

The original solver was created with the following parameters:
executable: D
solve_io: nl
solver: \anaconda\envs\power\Lib\site-packages\ampl_module_coin\bin\ipopt.exe
type: D
_args: ()
options: {}

I have checked the value of modules.find(f"{self.solver}"), it shows :‘d:\anaconda\envs\power\Lib\site-packages\ampl_module_coin\bin\ipopt.exe’
Which I think it is correct. So I wonder why it didn’t work?

Hi @Zhang,

Could you please try with modules.find(self.solver) instead of modules.find(f"{self.solver}")? Since there are forward slashes in the path, it may be causing trouble to have them in a f-string.

It doesn’t work either, with following outputs:

here, self.solver=‘ipopt’

Hi @Zhang,

The issue is that Pyomo parses the executable path in a way that does not work well on Windows. In this case it assumes that the solver name is “D” because that is the beginning of the path.

The following should fix the issue by specifying the executable path:

from amplpy import modules
import pyomo.environ as pyo
solver_name = "ipopt"
solver = pyo.SolverFactory(solver_name+"nl", executable=modules.find(solver_name), solve_io="nl")

We will update the instructions at AMPL Modules for Python — AMPL Resources to help people that may be facing the same issue.