# ------------------------------------------------------------------------------ # Licensed Materials - Property of IBM # 5725-A06 5725-A29 5724-Y48 5724-Y49 5724-Y54 5724-Y55 5655-Y21 # Copyright IBM Corporation 2008, 2019. All Rights Reserved. # # US Government Users Restricted Rights - Use, duplication or # disclosure restricted by GSA ADP Schedule Contract with # IBM Corp. # ------------------------------------------------------------------------------ """Imports the shared library on supported platforms."""
import platform
from sys import version_info
ERROR_STRING = "CPLEX 12.10.0.0 is not compatible with this version of Python."
if platform.system() in ('Darwin', 'Linux', 'AIX', 'Windows', 'Microsoft'): if version_info < (3, 6, 0): raise Exception(ERROR_STRING) elif version_info < (3, 7, 0): from cplex._internal.py36_cplex12100 import * elif version_info < (3, 9, 0): from cplex._internal.py37_cplex12100 import * else: raise Exception(ERROR_STRING) else: raise Exception("The CPLEX Python API is not supported on this platform.")
后续安装docplex
1
pip install DOcplex
0x04 测试
来简单的测试一下,求解这种问题
求解模型
1 2 3 4 5 6 7 8 9 10 11 12 13
from docplex.mp.model import Model #导出库,只用这一个就够了 model = Model() #创建模型 var_list = [i for i in range(0, 7)] #创建列表 X = model.binary_var_list(var_list, lb=0, name='X') #创建变量列表 #设定目标函数 model.maximize(11* X[0] + 9 * X[1] + 29 * X[2]+9* X[3]+21*X[4]+31*X[5]+22*X[6]) #添加约束条件 model.add_constraint(X[0]+X[1]+X[2] <= 2) model.add_constraint(X[3] + X[4] >=1) model.add_constraint(X[5] + X[6] >=1) model.add_constraint(10* X[0] + 8* X[1] + 20 * X[2]+5* X[3]+13*X[4]+22*X[5]+10*X[6] <=60) sol = model.solve() #求解模型 print(sol) #打印结果