How to get random results from Microsoft Z3?

一个人想着一个人 提交于 2019-12-07 07:58:41

问题


In Microsoft Z3, when we try to solve a formula, Z3 always returns the results in the same sequence, when there are two or more satisfiable solutions.

Is it possible to get random results from Z3 so that for the same input, it will generate different output sequence in different execution.

Please note that, I am using C or C# API. I am not using Z3 using smt2lib. So if you can give me a C or C# API function example that can add randomization, it will be more useful.


回答1:


(set-option :smt.arith.random_initial_value true)
(declare-const x Int)
(declare-const y Int)
(assert (> (+ x y) 0))
(check-sat-using (using-params qflra :random_seed 1))
(get-model)
(check-sat-using (using-params qflra :random_seed 2))
(get-model)
(check-sat-using (using-params qflra :random_seed 3))
(get-model)

Taken from here.




回答2:


You can write a while loop to find all the solutions, and if you want them randomly, what you can do is write one simple constraint that will hide (in real-world 'negate') the solution it has got previously. Like:

int numSolution = 0;
while (true)
{
 do something;.....
 BoolExpr[] args = new BoolExpr[];
 args[i] = your solution variable;
 numSolution++;
 if (numSolution == MAX_NUM_SOLUTION)
  {               
    break;
   }                
 slvr.Assert(z3.MkNot(z3.MkAnd(args)));
}


来源:https://stackoverflow.com/questions/35352624/how-to-get-random-results-from-microsoft-z3

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!