有问题欢迎大家评论区评论
记录一下自己的学习过程吧!
未来更新模型案例
直接代码
# 导入
from docplex.mp.model import Model
# 创建一个模型
opt_mod = Model(name="Linear Program")
# 添加决策变量
x = opt_mod.continuous_var(name="x", lb=0)
y = opt_mod.continuous_var(name="y", lb=0)
# 添加约束条件(constraints)
c1 = opt_mod.add_constraint(x + y >= 8, ctname="c1")
c2 = opt_mod.add_constraint(2 * x + y >= 10, ctname="c2")
c3 = opt_mod.add_constraint(x + 4 * y >= 11, ctname="c3")
# 定义objective function(目标函数)
obj_fn = 5 * x + 4 * y # 目标函数表达式
opt_mod.set_objective("min", obj_fn) # 定义目标函数
opt_mod.print_information() # 输出opt_mod的模型信息
# 求解
opt_mod.solve() # 求解opt_mod
opt_mod.print_solution() # 输出结果
最简单的线性规划问题,虽然用numpy,scipy,pandas也能解决,没有但是了