我是在Anaconda和Python3.10环境上安装的
conda create -p xxx/cplex python=3.10
pip install cplex -i https://pypi.doubanio.com/simple
pip install docplex -i https://pypi.doubanio.com/simple
代码来自(181条消息) anaconda环境下Cplex及Docplex安装流程-CSDN博客
# 导入库
import time
import random
import docplex.mp.model as cpx
import pandas as pd
start_time = time.time()
# 创建模型
model = cpx.Model()
# 创建变量列表
X = model.continuous_var_list([i for i in range(0, 2)], lb=0, name='X')
# 设定目标函数
model.minimize(2 * X[0] + 3 * X[1])
# 添加约束条件
model.add_constraint(3 * X[0] + X[1] >= 30)
model.add_constraint(X[0] - X[1] <= 10)
model.add_constraint(X[1] >= 1)
# 求解模型
sol = model.solve()
# 打印结果
print(sol)
end_time = time.time()
print(round(end_time - start_time, 4))
目录
docplex的安装
docplex的初级使用:
docplex的高级建模:
参考:
‘’代码主要来自python运筹优化(一):Cplex for python使用简介 - 知乎 (zhihu.com)
# 导入库
import time
import random
import docplex.mp.model as cpx
import pandas as pd
start_time = time.time()
n = 10
m = 5
n1 = 6
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")
# 创建变量-way-1
# if x is Continuous
x_vars = {(i, j): opt_model.continuous_var(lb=l[i, j], ub=u[i, j], name="x_{0}_{1}".format(i, j)) for i in set_I for j
in set_J}
# print(x_vars)
# 创建变量-way-2 一次性添加一组变量
# xb = {(i, j) for i in set_I for j in set_J} # 建立起下标的集合
# x_vars = opt_model.continuous_var_dict(xb, lb=l, ub=u, name="x")
# print(x_vars)
# 添加约束条件
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)
# 求解模型:solving with local cplex
sol = opt_model.solve()
# # 打印结果
# print(sol)
# a = sol.to_string()
if sol is not None:
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)
# CPLEX
opt_df["solution_value"] = opt_df["variable_object"].apply(lambda item: item.solution_value)
opt_df.drop(columns=["variable_object"], inplace=True)
opt_df.to_csv("./optimization_solution.csv")
else:
print("solve failed")
end_time = time.time()
print(round(end_time - start_time, 4))
"""
防止约束条件是随机生成的,不存在满足约束的解,导致报错Model did not solve successfully,
解决方法: 写到了while里面,当输出不为None时, break
"""
# 导入库
import time
import random
import docplex.mp.model as cpx
import pandas as pd
start_time = time.time()
sol = None
while True:
n = 10
m = 5
n1 = 6
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 Continuous
x_vars = {(i, j): opt_model.continuous_var(lb=l[i, j], ub=u[i, j], name="x_{0}_{1}".format(i, j)) for i in set_I for
j in set_J}
# print(x_vars)
# # 建立起下标的集合
# xb = {(i, j) for i in set_I for j in set_J}
# y_vars = opt_model.continuous_var_dict(xb, lb=l, ub=u, name="y")
# print(y_vars)
# 添加约束条件
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)
# 求解模型:solving with local cplex
sol = opt_model.solve()
if sol is not None:
break
# # 打印结果
# print(sol)
# a = sol.to_string()
# if sol is not None:
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)
# CPLEX
opt_df["solution_value"] = opt_df["variable_object"].apply(lambda item: item.solution_value)
opt_df.drop(columns=["variable_object"], inplace=True)
opt_df.to_csv("./optimizastion_solution.csv")
end_time = time.time()
print(round(end_time - start_time, 4))
DOcplex介绍及安装(1) - 知乎 (zhihu.com)
(181条消息) anaconda环境下Cplex及Docplex安装流程-CSDN博客
python运筹优化(一):Cplex for python使用简介 - 知乎 (zhihu.com)