1

I have done a program which works fine when using "glpk" solver and I am able to get the expected results, hence I am confident that the program works. Following is the code.

import pandas as pd
from pyomo.environ import *
import numpy as np


rawfile = "C:/Users/User/Downloads/chickenwings2.csv"

df_raw = pd.read_csv(rawfile, index_col='Name')
print (df_raw)

Set = df_raw.index.tolist()
count = dict(zip(df_raw.index,df_raw['count']))
price = dict(zip(df_raw.index,df_raw['price']))

#print (count,price)

model = ConcreteModel()
model.x = Var(Set, within=NonNegativeIntegers)

model.obj = Objective(expr= sum(price[i]*model.x[i] for i in Set), sense=minimize)

model.count_con = Constraint(expr=sum(count[i]*model.x[i] for i in Set) == 200)

opt = SolverFactory("glpk") 
opt_success = opt.solve(model)

total_count = sum(count[i]*value(model.x[i]) for i in Set)
print('Total Count:', total_count)
print('Total Price:', value(model.obj))

print('%5s %5s %12s' % ('Set','Count', 'Order Count'))
print('=========================')

for i in Set:
    if value(model.x[i]>0):
        print ('%5s %5s %5s' % (i,count[i], value(model.x[i])))

print('=========================')

However, when I tried using gurobi solver by altering the code as shown below, I am not able to get any results.

opt = SolverFactory("gurobi", solver_io="python") 

or

opt = SolverFactory("gurobi")

The error:

Traceback (most recent call last):
  File "D:/Python learning/ProjektX/chicken wings.py", line 26, in <module>
    opt_success = opt.solve(model)
  File "D:\EngineeringSoftware\Anaconda\lib\site-packages\pyomo\solvers\plugins\solvers\direct_solver.py", line 68, in solve
    self.available(exception_flag=True)
  File "D:\EngineeringSoftware\Anaconda\lib\site-packages\pyomo\solvers\plugins\solvers\direct_or_persistent_solver.py", line 301, in available
    "plugin").format(type(self)))
pyutilib.common._exceptions.ApplicationError: No Python bindings available for <class 'pyomo.solvers.plugins.solvers.gurobi_direct.GurobiDirect'> solver plugin

or

WARNING: Could not locate the 'gurobi' executable, which is required for
    solver gurobi

For Installation of gurobi, I have followed the gurobi installation procedure using anaconda

conda config --add channels http://conda.anaconda.org/gurobi
conda install gurobi

I have created a environment variable under the name GRB_LICENSE_FILE and directed the variable value to the location of the .lic file.

I have exhausted all option that I can find from the internet, hence I am requesting help from the pyomo community here to enlighten me on this issue.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Dlearn
  • 13
  • 1
  • 3

3 Answers3

3

Regarding that you try to use the Python interface for Gurobi with this line:

opt = SolverFactory("gurobi", solver_io="python")

You might get the

Traceback (most recent call last):
  File "D:/Python learning/ProjektX/chicken wings.py", line 26, in <module>
    opt_success = opt.solve(model)
  File "D:\EngineeringSoftware\Anaconda\lib\site-packages\pyomo\solvers\plugins\solvers\direct_solver.py", line 68, in solve
    self.available(exception_flag=True)
  File "D:\EngineeringSoftware\Anaconda\lib\site-packages\pyomo\solvers\plugins\solvers\direct_or_persistent_solver.py", line 301, in available
    "plugin").format(type(self)))
pyutilib.common._exceptions.ApplicationError: No Python bindings available for <class 'pyomo.solvers.plugins.solvers.gurobi_direct.GurobiDirect'> solver plugin

error. One reason that can lead to this kind of error is that your Gurobi's Python binding has to be installed. This binding comes with your Gurobi installation, but is not installed by default. Provided that you already have a valide Gurobi license, I suggest you to visit

http://www.gurobi.com/documentation/8.1/quickstart_mac/the_gurobi_python_interfac.html

To summarize the support page,

  1. Open a command prompt with admin privileges;
  2. Navigate to the folder where your Gurobi installation is;
  3. Type python setup.py install to run the installation of the Python binding.

You can check if it is installed by entering the line import gurobipy in your Python console.

V. Brunelle
  • 1,038
  • 11
  • 29
  • If you're using conda, you can follow the instructions here: https://www.gurobi.com/documentation/current/quickstart_mac/cs_anaconda_and_grb_conda_.html – rocarvaj Jul 26 '23 at 18:13
2

Sounds like you need to add the folder containing the gurobi executable to your Windows and Anaconda paths.

See this stackoverflow post for a step-by-step guide on how to do that: How to add a folder to `Path` environment variable in Windows 10 (with screenshots)

See this post for adding a folder to your conda path: How to add folder to search path for a given Anaconda environment?

Bethany Nicholson
  • 2,723
  • 1
  • 11
  • 18
0

I have tried adding the folder to windows and anaconda path(error code still present) and I tried installing gurobipy (No setup.py file in the Gurobi package).

However, I was using Pycharm to run the code, but the user guide explicitly mentions to use Spyder IDE or Jupyter, so I imported the code into Spyder and I can get the code to work. It even works using either ["gurobi"] or ["gurobi", solver_io="python"] for the solver.

It looks like there are some issues with Pycharm + gurobi.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Dlearn
  • 13
  • 1
  • 3
  • It sounds like you had a problem with the gurobi path not being available in pycharm. Is the problem with Pycharm still within the scope of this question? If not, please consider closing the question. – Romeo Valentin Jan 25 '19 at 02:22
  • Hi Romeo Valentin, i think the title will be misleading/insufficient if I would add the Pycharm issue into the scope. I will close it. – Dlearn Jan 25 '19 at 05:59
  • At least today, the user guide explicitly says that one *can* use PyCharm with gurobi - it is just that the installation procedure shown focuses on Spyder, as it comes by default with Anaconda. – desertnaut Nov 01 '20 at 23:29