RHS的范围意义语法



我正在使用CPLEX Python API来优化一个有多个变量的问题(90个变量问题)

我已经在我的感官和我的rhs中设置了约束:

my_senses = 'GG'
my_rhs = [0.1, 0.1]

然而,我想改变我的第一个约束(这是所有变量的总和必须大于0小于0.15),所以,而不是大"G">

我找不到任何详细说明正确语法的东西。我尝试了以下操作,但没有成功:

my_senses = 'RG'
my_rhs = [[0.0,0.15], 0.1]

L是LESS的字母

小型复杂python示例out of zoo示例

# Import packages.
import cplex
my_prob = cplex.Cplex()
my_obj = [500, 400]
my_ub = [cplex.infinity, cplex.infinity]
my_lb = [0.0, 0.0]
my_ctype = "II"
my_colnames = ["nbBus40", "nbBus30"]
my_rhs = [290, 285]
my_rownames = ["nbKids","nbKids2"]
my_sense = ["L","G"]
my_prob.objective.set_sense(my_prob.objective.sense.minimize)
my_prob.variables.add(obj=my_obj, lb=my_lb, ub=my_ub, types=my_ctype,
names=my_colnames)
rows = [[["nbBus40", "nbBus30"], [40,30]],[["nbBus40", "nbBus30"], [40,30]]]
my_prob.linear_constraints.add(lin_expr=rows, senses=my_sense,
rhs=my_rhs, names=my_rownames)
my_prob.solve()
print("cost  = ", my_prob.solution.get_objective_value())
numcols = my_prob.variables.get_num()

sol = my_prob.solution.get_values()
for j in range(numcols):
print(my_colnames[j],"  = ", sol[j])

或使用"R"范围:

# Import packages.
import cplex
my_prob = cplex.Cplex()
my_obj = [500, 400]
my_ub = [cplex.infinity, cplex.infinity]
my_lb = [0.0, 0.0]
my_ctype = "II"
my_colnames = ["nbBus40", "nbBus30"]
my_rhs = [285]
my_range = [5]
my_rownames = ["nbKids"]
my_sense = ["R"]
my_prob.objective.set_sense(my_prob.objective.sense.minimize)
my_prob.variables.add(obj=my_obj, lb=my_lb, ub=my_ub, types=my_ctype,
names=my_colnames)
rows = [[["nbBus40", "nbBus30"], [40,30]]]
my_prob.linear_constraints.add(lin_expr=rows, senses=my_sense,
rhs=my_rhs,range_values=my_range, names=my_rownames)
my_prob.solve()
print("cost  = ", my_prob.solution.get_objective_value())
numcols = my_prob.variables.get_num()

sol = my_prob.solution.get_values()
for j in range(numcols):
print(my_colnames[j],"  = ", sol[j])

但是我会推荐docplex api,它也是python的,但是级别更高

然后写

from docplex.mp.model import Model
mdl = Model(name='buses')
nbbus40 = mdl.integer_var(name='nbBus40')
nbbus30 = mdl.integer_var(name='nbBus30')
mdl.add_range(285,(nbbus40*40 + nbbus30*30),290,"kids")
mdl.minimize(nbbus40*500 + nbbus30*400)
mdl.solve(log_output=True,)
mdl.export("c:\temp\buses.lp")
for v in mdl.iter_integer_vars():
print(v," = ",v.solution_value)

相关内容

  • 没有找到相关文章

最新更新