简体   繁体   中英

Pyomo and gurobi solver

Here is my code. As you can see it is a simple liner programming that I am trying so solve via pyomo as an interface for Cplex. I added everything to the path to no avail. I really appreciate your help.

from pyomo.environ import *

model = ConcreteModel()

model.x = Var(domain=NonNegativeReals)
model.y = Var(domain=NonNegativeReals)

model.profit = Objective(expr = 40*model.x + 30*model.y, sense=maximize)

model.demand = Constraint(expr = model.x <= 40)
model.laborA = Constraint(expr = model.x + model.y <= 80)
model.laborB = Constraint(expr = 2*model.x + model.y <= 100)

results = SolverFactory('cplex').solve(model)
results.write()
if results.solver.status:
   model.pprint()

print('\nProfit = ', model.profit())

print('\nDecision Variables')
print('x = ', model.x())
print('y = ', model.y())

print('\nConstraints')
print('Demand  = ', model.demand())
print('Labor A = ', model.laborA())
print('Labor B = ', model.laborB())


sys.path would also return

['/Users/navid/project',
 '/Applications/CPLEX_Studio201/cplex/python/3.8/x86-64_osx',
 '/Users/navid/opt/anaconda3/lib/python38.zip',
 '/Users/navid/opt/anaconda3/lib/python3.8',
 '/Users/navid/opt/anaconda3/lib/python3.8/lib-dynload',
 '',
 '/Users/navid/opt/anaconda3/lib/python3.8/site-packages',
 '/Users/navid/opt/anaconda3/lib/python3.8/site-packages/aeosa',
 '/Users/navid/opt/anaconda3/lib/python3.8/site-packages/chardet-3.0.4-py3.8.egg',
 '/Users/navid/opt/anaconda3/lib/python3.8/site-packages/docloud-1.0.375-py3.8.egg',
 '/Users/navid/opt/anaconda3/lib/python3.8/site-packages/locket-0.2.1-py3.8.egg',
 '/Users/navid/opt/anaconda3/lib/python3.8/site-packages/IPython/extensions',
 '/Users/navid/.ipython']

You can see that Cplex is in the path. I also installed pymo using anaconda. Yet still it returns



WARNING: Could not locate the 'cplex' executable, which is required for solver
    cplex
---------------------------------------------------------------------------
ApplicationError                          Traceback (most recent call last)
/var/folders/w4/q3nldmqx4xz8kc24n903cjc00000gn/T/ipykernel_36345/4155455994.py in <module>
     17 
     18 # solve
---> 19 results = SolverFactory('cplex').solve(model)
     20 results.write()
     21 if results.solver.status:

~/opt/anaconda3/lib/python3.8/site-packages/pyomo/opt/base/solvers.py in solve(self, *args, **kwds)
    510         """ Solve the problem """
    511 
--> 512         self.available(exception_flag=True)
    513         #
    514         # If the inputs are models, then validate that they have been

~/opt/anaconda3/lib/python3.8/site-packages/pyomo/opt/solver/ilmcmd.py in available(self, exception_flag)
     34         if self._assert_available:
     35             return True
---> 36         if not pyomo.opt.solver.shellcmd.SystemCallSolver.available(self, exception_flag):
     37             return False
     38         executable = pyomo.common.Executable("ilmlist")

~/opt/anaconda3/lib/python3.8/site-packages/pyomo/opt/solver/shellcmd.py in available(self, exception_flag)
    123             if exception_flag:
    124                 msg = "No executable found for solver '%s'"
--> 125                 raise ApplicationError(msg % self.name)
    126             return False
    127         return True

ApplicationError: No executable found for solver 'cplex'

What I am doing wrong? Cplex is in the path.

See https://pyomo.readthedocs.io/en/stable/working_models.html#specifying-the-path-to-a-solver for a way to directly specify a path:

Specifying the Path to a Solver Often, the executables for solvers are in the path; however, for situations where they are not, the SolverFactory function accepts the keyword executable, which you can use to set an absolute or relative path to a solver executable. Eg,

opt = pyo.SolverFactory("ipopt", executable="../ipopt")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM