I am trying to optimize a linear programming problem using CPLEX in Python. I already installed the IBM ILOG CPLEX Studio, as well as the docplex in Python.
When I run the program I'm having the following error:
AttributeError: module 'cplex' has no attribute 'Cplex'
Useful code:
import docplex.mp.model as cpx
import random
import pandas as pd
n = 10
m = 5
set_I = range(1, n+1)
set_J = range(1, m+1)
c = {(i,j): random.normalvariate(0,1) for i in set_I for j in set_J}
a = {(i,j): random.normalvariate(0,5) for i in set_I for j in set_J}
l = {(i,j): random.randint(0,10) for i in set_I for j in set_J}
u = {(i,j): random.randint(10,20) for i in set_I for j in set_J}
b = {j: random.randint(0,30) for j in set_J}
opt_model = cpx.Model(name="MIP Model")
# if x is Binary
x_vars = {(i,j): opt_model.binary_var(name="x_{0}_{1}".format(i,j)) for i in set_I for j in set_J}
# <= constraints
constraints = {j : opt_model.add_constraint(ct=opt_model.sum(a[i,j] * x_vars[i,j] for i in set_I) <= b[j], ctname="constraint_{0}".format(j)) for j in set_J}
objective = opt_model.sum(x_vars[i,j] * c[i,j] for i in set_I for j in set_J)
opt_model.minimize(objective)
opt_model.solve()
opt_df = pd.DataFrame.from_dict(x_vars, orient="index", columns = ["variable_object"])
opt_df.index = pd.MultiIndex.from_tuples(opt_df.index, names=["column_i", "column_j"])
opt_df.reset_index(inplace=True)
opt_df["solution_value"] = opt_df["variable_object"].apply(lambda item: item.solution_value)
print(opt_df)
I extracted this code from: https://medium.com/@m.moarefdoost/optimization-modeling-in-python-pulp-gurobi-and-cplex-7f25acb03d7d
I am a beginner in CPLEX and Python, so I just tried to run this code to verify thatI've installed everything correctly.
Has anyone had the same kind of problem?
I found the error of my code. Actually, I've not installed correctly the setup.py before running my code.
After doing that, everything works well!