RHS 的范围意义的语法
Syntax for ranged sense to RHS
我正在使用 CPLEX Python API 来优化具有多个变量的问题(90 个变量问题)
我已经将我的约束和我的 rhs 设置为:
my_senses = 'GG'
my_rhs = [0.1, 0.1]
但是我想更改我的第一个约束条件(即所有变量的总和必须大于 0 且小于 0.15),以便将大于 0.1 的“G”改为 0 - 0.15 之间的范围
我找不到任何详细说明执行此操作的正确语法的内容。我尝试了以下但没有成功:
my_senses = 'RG'
my_rhs = [[0.0,0.15], 0.1]
L 是 LESS 的字母。
中的小 cplex python 示例
# 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)
我正在使用 CPLEX Python API 来优化具有多个变量的问题(90 个变量问题)
我已经将我的约束和我的 rhs 设置为:
my_senses = 'GG'
my_rhs = [0.1, 0.1]
但是我想更改我的第一个约束条件(即所有变量的总和必须大于 0 且小于 0.15),以便将大于 0.1 的“G”改为 0 - 0.15 之间的范围
我找不到任何详细说明执行此操作的正确语法的内容。我尝试了以下但没有成功:
my_senses = 'RG'
my_rhs = [[0.0,0.15], 0.1]
L 是 LESS 的字母。
中的小 cplex python 示例# 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)