[英]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.
如您所见,这是一个简单的线性编程,我正在尝试通过 pyomo 作为 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 sys.path 也会返回
['/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.您可以看到 Cplex 在路径中。 I also installed pymo using anaconda.
我还使用 anaconda 安装了 pymo。 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.
Cplex 在路径中。
See https://pyomo.readthedocs.io/en/stable/working_models.html#specifying-the-path-to-a-solver for a way to directly specify a path:有关直接指定路径的方法,请参阅https://pyomo.readthedocs.io/en/stable/working_models.html#specifying-the-path-to-a-solver :
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.
但是,对于它们不是的情况,SolverFactory function 接受关键字可执行文件,您可以使用它来设置求解器可执行文件的绝对或相对路径。 Eg,
例如,
opt = pyo.SolverFactory("ipopt", executable="../ipopt")
opt = pyo.SolverFactory("ipopt", executable="../ipopt")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.