Python:退出代码的后台计时器(在Windows上)

2024-06-07 03:11:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在完成我在Python上的第一个约束编程任务。在一些大规模的问题上,代码会被卡住并计算出长期的最佳解决方案,因此我想创建一个后台计时器,它将退出程序(很可能是一个例外),并选择以前的最佳解决方案。我以前试过“信号”模块-在Windows上不起作用

这一个也不起作用,因为我认为它实际上没有脱离循环来检查条件

start = time.time()
while time.time - start < 60

以下是代码,您希望从以下位置退出: 在某个点上,它被卡在了status = solver.Solve(model),所以我想我需要某种计时器,从这个循环外部将我从代码中拉出来。也许是异步的


...

    with open(file_location, 'r') as input_data_file:
    input_data = input_data_file.read()
# Modify this code to run your optimization algorithm

    # parse the input
    lines = input_data.split('\n')

    first_line = lines[0].split()
    node_count = int(first_line[0])
    edge_count = int(first_line[1])

    edges = []

    for i in range(1, edge_count + 1):
        line = lines[i]
        parts = line.split()
        edges.append((int(parts[0]), int(parts[1])))

    # build a trivial solution
    # every node has its own color
    
    k = 20
   
    while True:
        
        model = cp_model.CpModel()
        nodes = []
        for i in range(0, node_count):
            i = model.NewIntVar(0,k, "node %i" % i)
            nodes.append(i)
        for i, conn in enumerate(edges):
            model.Add(nodes[conn[0]] != nodes[conn[1]])

        solver = cp_model.CpSolver()
        solver.parameters.max_time_in_seconds = 8
        finish_time = time.time() + solver.parameters.max_time_in_seconds
        solver.parameters.num_search_workers = 8
        
        solution_printer = VarArraySolutionPrinter(nodes)
        status = solver.SolveWithSolutionCallback(model, solution_printer)
        solution = []

        if time.time() >= finish_time:
            print("We die!")
            break

        if status == cp_model.FEASIBLE:
            print('Solution is FEASIBLE \n\n\n')
            print("Colors Used: %i" % k )
            k = k - 1
            continue

        if status == cp_model.OPTIMAL:
            print('Solution is OPTIMAL ')
            print("Colors Used: %i \n\n\n" % k )
            k = k - 1
            continue

        #CATCHING THE FINAL SOLUTION
        if status == cp_model.INFEASIBLE:
            k = k + 1
            model = cp_model.CpModel()
            nodes = []

            for i in range(0, node_count):
                i = model.NewIntVar(0,k, "node %i" % i)
                nodes.append(i)

            for i, conn in enumerate(edges):
                model.Add(nodes[conn[0]] != nodes[conn[1]])

            solver = cp_model.CpSolver()
            solution_printer = VarArraySolutionPrinter(nodes)
            status = solver.SolveWithSolutionCallback(model, solution_printer)
            solution = []
            checker_var = 1

            print("This is optimal solution." + " Number of colors: %i" % k)
            for i in nodes:
                i = solver.Value(i)
                solution.append(i)
            break
...

刚刚用parameters编辑了代码,由于某种原因仍然卡住了

谢谢你的帮助,它终于起作用了,把我从这个循环中抛了出去


Tags: innodeforinputmodeltimestatuscount