Python Z3 和 concurrent.futures
Python Z3 and concurrent.futures
我想并行解决一组包含的问题,然后添加附加信息以解决新问题。
下面是用于解决问题的程序结构示例:
from z3 import *
import concurrent.futures
# solver test function
def add(a, b, solver, index):
solver.append(a > b)
assert solver.check()
model = solver.model()
return {
'solver': solver,
'av': model[a],
'a': a,
'b': b,
'bv': model[b],
'index': index
}
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
# start solving the problems
to_compute = []
for i in range(3):
sol = z3.Solver()
to_compute.append(executor.submit(
add,
Int('a{}'.format(i)),
Int('b{}'.format(i)),
sol,
i
))
# wait for the solution to the computations
next_to_solve = []
for result_futures in concurrent.futures.as_completed(to_compute):
results = result_futures.result()
print(results)
sol = results['solver']
sol.append(results['a'] > results['av'])
next_to_solve.append(
executor.submit(
add,
results['a'],
results['b'],
sol,
results['index']
)
)
每次程序的结果都不一样运行,结果包括:
- Z3Exception 'invalid dec_ref command'
- python 崩溃
- 没有错误
我需要做什么才能使程序更可靠?
你看到这个例子了吗:http://github.com/Z3Prover/z3/blob/master/examples/python/parallel.py
我不是 z3py 中并发特性的专家,但似乎您需要非常小心地在您作为 运行 求解器的同一上下文中创建变量。该文件中有一些提示。
我想并行解决一组包含的问题,然后添加附加信息以解决新问题。
下面是用于解决问题的程序结构示例:
from z3 import *
import concurrent.futures
# solver test function
def add(a, b, solver, index):
solver.append(a > b)
assert solver.check()
model = solver.model()
return {
'solver': solver,
'av': model[a],
'a': a,
'b': b,
'bv': model[b],
'index': index
}
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
# start solving the problems
to_compute = []
for i in range(3):
sol = z3.Solver()
to_compute.append(executor.submit(
add,
Int('a{}'.format(i)),
Int('b{}'.format(i)),
sol,
i
))
# wait for the solution to the computations
next_to_solve = []
for result_futures in concurrent.futures.as_completed(to_compute):
results = result_futures.result()
print(results)
sol = results['solver']
sol.append(results['a'] > results['av'])
next_to_solve.append(
executor.submit(
add,
results['a'],
results['b'],
sol,
results['index']
)
)
每次程序的结果都不一样运行,结果包括:
- Z3Exception 'invalid dec_ref command'
- python 崩溃
- 没有错误
我需要做什么才能使程序更可靠?
你看到这个例子了吗:http://github.com/Z3Prover/z3/blob/master/examples/python/parallel.py
我不是 z3py 中并发特性的专家,但似乎您需要非常小心地在您作为 运行 求解器的同一上下文中创建变量。该文件中有一些提示。