【问题标题】:Summary of knapsack solve in CPLEXCPLEX 中的背包求解总结
【发布时间】:2019-05-30 17:57:45
【问题描述】:

所以我在 CPLEX 的 python 版本中遇到了麻烦。我认为很容易得到求解器所做工作的总结,即分支数量等。

有人知道怎么做吗?

import cplex
from cplex.exceptions import CplexError
class knapsack:
    def __init__(self,N,g,square_list):
        self.N = N
        self.square_list= square_list
        self.g = g
    def solve_problem(self):
        try:
            my_prob = cplex.Cplex()
            prob =my_prob
            prob.set_log_stream(None)
            prob.set_error_stream(None)
            prob.set_warning_stream(None)
            prob.set_results_stream(None)
            my_obj = self.g
            my_ctype = "B"
            number_of_one = self.square_list.count(1.0)
            my_ctype = my_ctype*len(self.square_list)
            val = self.N  -number_of_one
            rhs=[val]
            my_sense="L"
            my_rownames = ["r1"]

            counter =0
            variable_list=[]
            coiff_list=[]
            for i in self.square_list:
                if i==0:
                    coiff_list.append(1.0)
                else:
                    coiff_list.append(-1.0)
                variable_list.append("w" + str(counter))
                counter+=1

            rows = [[variable_list, coiff_list]]
            prob.objective.set_sense(prob.objective.sense.minimize)

            prob.variables.add(obj=my_obj, types=my_ctype,
                        names=variable_list)
            prob.linear_constraints.add(lin_expr=rows, senses=my_sense,
                                    rhs=rhs)
            my_prob.solve()
            x = my_prob.solution.get_values()
            print(my_prob.solution.get_status())
            print("---")
            print(my_prob.solution.status())
            return x
        except CplexError as exc:
            print(exc)
            return

当我查看与 my_prob 和 myprob.solution 关联的方法时,我看到了

['MIP_starts', 'SOS', '_Cplex__copy_init', '__class__', '__del__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_aborter', '_disposed', '_env', '_env_lp_ptr', '_invoke_generic_callback', '_is_MIP', '_is_special_filetype', '_lp', 'advanced', 'cleanup', 'conflict', 'copy_vmconfig', 'del_vmconfig', 'double_annotations', 'end', 'feasopt', 'get_aborter', 'get_dettime', 'get_num_cores', 'get_problem_name', 'get_problem_type', 'get_stats', 'get_time', 'get_version', 'get_versionnumber', 'has_vmconfig', 'indicator_constraints', 'linear_constraints', 'long_annotations', 'objective', 'order', 'parameters', 'populate_solution_pool', 'presolve', 'problem_type', 'pwl_constraints', 'quadratic_constraints', 'read', 'read_annotations', 'read_copy_vmconfig', 'register_callback', 'remove_aborter', 'runseeds', 'set_callback', 'set_error_stream', 'set_log_stream', 'set_problem_name', 'set_problem_type', 'set_results_stream', 'set_warning_stream', 'solution', 'solve', 'start', 'unregister_callback', 'use_aborter', 'variables', 'write', 'write_annotations', 'write_benders_annotation']
['MIP', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_add_iter', '_add_single', '_conv', '_cplex', '_env', '_get_index', '_get_index_function', 'advanced', 'basis', 'get_activity_levels', 'get_dual_values', 'get_float_quality', 'get_indicator_slacks', 'get_indices', 'get_integer_quality', 'get_linear_slacks', 'get_method', 'get_objective_value', 'get_quadratic_activity_levels', 'get_quadratic_dualslack', 'get_quadratic_slacks', 'get_quality_metrics', 'get_reduced_costs', 'get_solution_type', 'get_status', 'get_status_string', 'get_values', 'infeasibility', 'is_dual_feasible', 'is_primal_feasible', 'method', 'pool', 'progress', 'quality_metric', 'sensitivity', 'status', 'type', 'write']

【问题讨论】:

  • 文档here 有帮助吗?
  • 非常感谢。我找到了一些东西 - 我现在就回答我的问题。

标签: python cplex


【解决方案1】:

发现解决方案对象上有写函数

my_prob.solution.write("myanswer")

这包含有关 CPLEX 运行的所有相关信息。

【讨论】:

  • 确实,您可以阅读 SOL 文件here
【解决方案2】:

如果您熟悉 CPLEX 交互,您可能会习惯在优化后看到类似以下内容的摘要:

MIP - Integer optimal, tolerance (0.0001/1e-06):  Objective = -2.0183208990e+02
Current MIP best bound = -2.0181209207e+02 (gap = 0.0199978, 0.01%)
Solution time =    1.43 sec.  Iterations = 25361  Nodes = 4335 (21)
Deterministic time = 686.22 ticks  (479.17 ticks/sec)

正如 cmets 部分所建议的,如果您查看文档 here,则可以查询所有这些信息。正如你所建议的,它大部分来自Cplex.solution 接口。

例如,考虑以下交互式会话:

>>> c.problem_type[c.get_problem_type()]
'MILP'
>>> c.solution.get_status_string()
'integer optimal, tolerance'
>>> c.parameters.mip.tolerances.mipgap.get()
0.0001
>>> c.parameters.mip.tolerances.absmipgap.get()
1e-06
>>> c.solution.get_objective_value()
-201.83208990000034
>>> c.solution.MIP.get_best_objective()
-201.8120920681663
>>> c.solution.MIP.get_mip_relative_gap()
9.908152783804216e-05
>>> print(c.solution.get_quality_metrics())
Incumbent solution:
MILP objective                                -2.0183208990e+02
MILP solution norm |x| (Total, Max)            4.65432e+02  2.02051e+02
MILP solution error (Ax=b) (Total, Max)        5.24512e-11  2.34035e-12
MILP x bound error (Total, Max)                0.00000e+00  0.00000e+00
MILP x integrality error (Total, Max)          0.00000e+00  0.00000e+00
MILP slack bound error (Total, Max)            4.54747e-13  4.54747e-13
>>> c.solution.MIP.get_incumbent_node()
4266
>>> c.solution.MIP.get_num_cuts(c.solution.MIP.cut_type.GUB_cover)
3

【讨论】:

  • 嘿只是好奇-我使用 c.solution.MIP.get_incumbent_node() 认为这会告诉我在编写解决方案之前搜索的分支和边界节点的数量,但它总是返回 0 - 即使输出解决方案与输入不同。建议?
  • 在底层,Cplex.solution.MIP.get_incumbent_node() 使用来自 C API 的 CPXXgetnodeint。如果不查看引擎日志或不了解有关您的模型的任何其他详细信息,很难说出您为什么会出现这种行为。如果您可以提供一个小示例来重现该行为,那么这将值得作为 stackoverflow 上的一个新问题提出。或者,如果您希望对此保密,您可以通过 rkersh 给我发送电子邮件,地址为我们 dot ibm dot com。
  • 您好,rkersh,感谢您的回复。我创建了一个新线程,因为我认为这是一个解决 IP 并使用您的 python 版本的人会发现有用的问题。 stackoverflow.com/questions/60623801/…
猜你喜欢
  • 1970-01-01
  • 2021-08-14
  • 2018-03-01
  • 1970-01-01
  • 2017-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-04
相关资源
最近更新 更多