测试代码1:
# -*- 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
from cplex.exceptions import CplexError
# data common to all populateby functions
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表示等于
def populatebyrow(prob):
prob.objective.set_sense(prob.objective.sense.maximize) # 求最大值 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"], [1.0, -3.0, 1.0]],
[["x2", "x4"], [1.0, -3.5]]]# 设置约束的系数
prob.linear_constraints.add(lin_expr=rows, senses=my_sense,
rhs=my_rhs, names=my_rownames)# 填充线性参数进模型
try:
my_prob = cplex.Cplex()
handle = populatebyrow(my_prob) # 调用函数填充模型
my_prob.solve()
except CplexError as exc:
print(exc)
print()
# solution.get_status() returns an integer code
print("Solution status = ", my_prob.solution.get_status(), ":", end=' ')
# the following line prints the corresponding string
print(my_prob.solution.status[my_prob.solution.get_status()])
print("Solution value = ", my_prob.solution.get_objective_value()) # 获取最优解的值
numcols = my_prob.variables.get_num()
numrows = my_prob.linear_constraints.get_num()#
slack = my_prob.solution.get_linear_slacks()
x = my_prob.solution.get_values() # 获取取得最优解的变量值
print('x: ')
print(x)
测试代码2:
```python
execfile("cplexpypath.py")
import cplex
from cplex.exceptions import CplexError
import sys
# data common to all populateby functions
my_obj = [1.0, 2.0, 3.0]
my_ub = [40.0, cplex.infinity, cplex.infinity]
my_colnames = ["x1", "x2", "x3"]
my_rhs = [20.0, 30.0]
my_rownames = ["c1", "c2"]
my_sense = "LL"
def populatebyrow(prob):
prob.objective.set_sense(prob.objective.sense.maximize)
# since lower bounds are all 0.0 (the default), lb is omitted here
prob.variables.add(obj = my_obj, ub = my_ub, names = my_colnames)
# can query variables like the following bounds and names:
# lbs is a list of all the lower bounds
lbs = prob.variables.get_lower_bounds()
# ub1 is just the first lower bound
ub1 = prob.variables.get_upper_bounds(0)
# names is ["x1", "x3"]
names = prob.variables.get_names([0, 2])
rows = [[[0,"x2","x3"],[-1.0, 1.0,1.0]],
[["x1",1,2],[ 1.0,-3.0,1.0]]]
prob.linear_constraints.add(lin_expr = rows, senses = my_sense,
rhs = my_rhs, names = my_rownames)
# because there are two arguments, they are taken to specify a range
# thus, cols is the entire constraint matrix as a list of column vectors
cols = prob.variables.get_cols("x1", "x3")
def populatebycolumn(prob):
prob.objective.set_sense(prob.objective.sense.maximize)
prob.linear_constraints.add(rhs = my_rhs, senses = my_sense,
names = my_rownames)
c = [[[0,1],[-1.0, 1.0]],
[["c1",1],[ 1.0,-3.0]],
[[0,"c2"],[ 1.0, 1.0]]]
prob.variables.add(obj = my_obj, ub = my_ub, names = my_colnames,
columns = c)
def populatebynonzero(prob):
prob.objective.set_sense(prob.objective.sense.maximize)
prob.linear_constraints.add(rhs = my_rhs, senses = my_sense,
names = my_rownames)
prob.variables.add(obj = my_obj, ub = my_ub, names = my_colnames)
rows = [0,0,0,1,1,1]
cols = [0,1,2,0,1,2]
vals = [-1.0,1.0,1.0,1.0,-3.0,1.0]
prob.linear_constraints.set_coefficients(zip(rows, cols, vals))
# can also change one coefficient at a time
# prob.linear_constraints.set_coefficients(1,1,-3.0)
# or pass in a list of triples
# prob.linear_constraints.set_coefficients([(0,1,1.0), (1,1,-3.0)])
def lpex1(pop_method):
try:
my_prob = cplex.Cplex()
if pop_method == "r":
handle = populatebyrow(my_prob)
if pop_method == "c":
handle = populatebycolumn(my_prob)
if pop_method == "n":
handle = populatebynonzero(my_prob)
my_prob.solve()
except CplexError, exc:
print exc
return
numrows = my_prob.linear_constraints.get_num()
numcols = my_prob.variables.get_num()
print
# solution.get_status() returns an integer code
print "Solution status = " , my_prob.solution.get_status(), ":",
# the following line prints the corresponding string
print my_prob.solution.status[my_prob.solution.get_status()]
print "Solution value = ", my_prob.solution.get_objective_value()
slack = my_prob.solution.get_linear_slacks()
pi = my_prob.solution.get_dual_values()
x = my_prob.solution.get_values()
dj = my_prob.solution.get_reduced_costs()
for i in range(numrows):
print "Row %d: Slack = %10f Pi = %10f" % (i, slack[i], pi[i])
for j in range(numcols):
print "Column %d: Value = %10f Reduced cost = %10f" % (j, x[j], dj[j])
my_prob.write("lpex1.lp")
if __name__ == "__main__":
if len(sys.argv) != 2 or sys.argv[1] not in ["-r", "-c", "-n"]:
print "Usage: lpex1.py -X"
print " where X is one of the following options:"
print " r generate problem by row"
print " c generate problem by column"
print " n generate problem by nonzero"
print " Exiting..."
sys.exit(-1)
lpex1(sys.argv[1][1])
else:
prompt = """Enter the letter indicating how the problem data should be populated:
r : populate by rows
c : populate by columns
n : populate by nonzeros\n ? > """
r = ’r’
c = ’c’
n = ’n’
lpex1(input(prompt))