cplex求解1

python环境下cplex安装

发现了一个简单的方法,直接使用 Anaconda 安装 cplex 包,因为 cplex 把自己最新的 python 包都发到 Anaconda 云里面了。
(1)打开 dos 命令行窗口,输入:

conda install -c IBMDecisionOptimization docplex cplex

或者用 pip 安装:

pip install cplex

就能直接将 cplex 的库安装好
(2) 在 ipython 里面输入 import cplex
若加载成功,则证明 cplex 包已经成功添加了

cplex编程步骤

在这里插入图片描述在这里插入图片描述

在这里插入图片描述

简单例子

# -*- coding: utf-8 -*-
# The MIP problem solved in this example is:
# 问题描述
#   Maximize  x1 + 2 x2 + 3 x3 + x4
#   Subject to
#      - x1 +   x2 + x3 + 10 x4 <= 20
#        x1 - 3 x2 + x3         <= 30
#               x2      - 3.5x4  = 0
#   Bounds
#        0 <= x1 <= 40
#        0 <= x2
#        0 <= x3
#        2 <= x4 <= 3
#   Integers
#       x4


import cplex

my_obj = [1.0, 2.0, 3.0, 1.0]   # 系数
my_ub = [40.0, cplex.infinity, cplex.infinity, 3.0] #变量上界
my_lb =[0.0, 0.0, 0.0, 2.0]   #变量下界
my_ctype = "CCCI"    #变量类型 I 表示Integer

my_colnames = ["x1", "x2", "x3", "x4"]   # 变量名
my_rhs = [20.0, 30.0, 0.0]      # 约束右端的值
my_rownames=["r1", "r2", "r3"]  # 约束名
my_sense = "LLE"       # 约束的属性:L表示小于,E表示等于

prob = cplex.Cplex()
prob.objective.set_sense(prob.objective.sense.maximize)
prob.variables.add(obj=my_obj, lb=my_lb, ub=my_ub, types=my_ctype, names=my_colnames)

rows=[[["x1", "x2", "x3", "x4"], [-1.0, 1.0, 1.0, 10.0]],
      [["x1", "x2", "x3", "x4"], [1.0, -3.0, 1.0, 0.0]],
      [["x1", "x2", "x3", "x4"], [0.0, 1.0, 0.0, -3.5]]]  # 设置约束的系数

prob.linear_constraints.add(lin_expr=rows, senses=my_sense, rhs=my_rhs, names=my_rownames)

solution = prob.solve()


print("Solution status = ", prob.solution.get_status(), ":", end=' ')
# the following line prints the corresponding string
print(prob.solution.status[prob.solution.get_status()])
print("Solution value  = ", prob.solution.get_objective_value())

numcols = prob.variables.get_num()
numrows = prob.linear_constraints.get_num()

slack = prob.solution.get_linear_slacks()
x = prob.solution.get_values()

print('x: ')
print(x)

输出结果

C:\ProgramData\Miniconda3\python.exe F:/optimize/cplex/cplex01.py
Version identifier: 20.1.0.1 | 2021-12-07 | 9dfdf6686
CPXPARAM_Read_DataCheck                          1
Found incumbent of value 46.000000 after 0.00 sec. (0.00 ticks)
Tried aggregator 2 times.
Aggregator did 1 substitutions.
Reduced MIP has 2 rows, 3 columns, and 6 nonzeros.
Reduced MIP has 0 binaries, 1 generals, 0 SOSs, and 0 indicators.
Presolve time = 0.00 sec. (0.01 ticks)
Tried aggregator 1 time.
Reduced MIP has 2 rows, 3 columns, and 6 nonzeros.
Reduced MIP has 0 binaries, 1 generals, 0 SOSs, and 0 indicators.
Presolve time = 0.00 sec. (0.00 ticks)
MIP emphasis: balance optimality and feasibility.
MIP search method: dynamic search.
Parallel mode: deterministic, using up to 8 threads.
Root relaxation solution time = 0.00 sec. (0.00 ticks)

        Nodes                                         Cuts/
   Node  Left     Objective  IInf  Best Integer    Best Bound    ItCnt     Gap

*     0+    0                           46.0000      163.0000           254.35%
*     0+    0                          122.5000      163.0000            33.06%
      0     0      125.2083     1      122.5000      125.2083        3    2.21%
      0     0        cutoff            122.5000                      3     --- 
Elapsed time = 0.02 sec. (0.03 ticks, tree = 0.01 MB, solutions = 2)

Root node processing (before b&c):
  Real time             =    0.02 sec. (0.03 ticks)
Parallel b&c, 8 threads:
  Real time             =    0.00 sec. (0.00 ticks)
  Sync time (average)   =    0.00 sec.
  Wait time (average)   =    0.00 sec.
                          ------------
Total (root+branch&cut) =    0.02 sec. (0.03 ticks)
Solution status =  101 : MIP_optimal
Solution value  =  122.5
x: 
[40.0, 10.5, 19.5, 3.0]

Process finished with exit code 0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值