I am new to SMT solvers. I would like to know that how could I encode a simple TSP problem having 4/6 nodes? I am confused how to set my constraints using the Z3pay APA. Any kind of hint or help would be highly appreciated.
-
You might get more answers if you could post what you've already tried. If the problem is the Z3py API, did you try to encode it using the SMTLib2 standard? Or using a pseudo-code encoding? – Malte Schwerhoff May 23 '13 at 08:35
-
Hi, Actually I am confused how I can encode optimization problem in Z3? I think to solve TSP problems may be pseudo Boolean solvers are the best !! But Z3 isn't a Pseudo Boolean solver, right ? Could you suggest me any paper or document that has implemented pseudo Boolean encoding of TSP problem? I appreciate your advice.. thanks – user1770051 Sep 10 '13 at 19:35
2 Answers
You can phrase your TSP problem as an ILP problem. The question is now how to encode the TSP as an ILP. There are two well known answers: Miller–Tucker–Zemlin and Dantzig–Fulkerson–Johnson.
The basic idea is as follows: Say we have n cities. Let us denote by d_ij the distance between cities i and j and let us denote by x_{ij} the boolean value (0 or 1) whether the TSP contains the edge from i to j. Then finding the smallest tour means
minimize sum_{i,j} x_{ij} d_{ij}
such that the x_{ij} describe a cycle. With those two conditions we get one or more cycles:
sum_{j} x_{ij} = 1 for all i exactly one outgoing edge per city
sum_{i} x_{ij} = 1 for all j exactly one ingoing edge per city
Now we have to exclude the case that the solutions comprises multiple cycles. We can add this exponential number of Dantzig–Fulkerson–Johnson conditions:
sum_{i in S} sum_{j in S} x_{ij} < |S| for all proper subsets S of {1, ..., n}
Note that if our solution contains two cycles then for S being the vertex set of one of the cycles then the x_{ij}-sum will be |S|. On the other hand, if there is only one cycle then the x_{ij}-sum will never reach |S|, e.g., if you remove one vertex from {1, ..., n} then the number of edges remaining is n-2, but |S| = n-1.
Of course, an exponential number of constraints is not what we want, so we look for a more clever way to exclude the subcycle cases. And here is where Miller–Tucker–Zemlin jumps in.
A different approach would be to simply ignore the subcycle problem, compute a solution and check whether the solution comprises subcycles. If it does, exclude the solution by adding it as a lazy constraint and repeat until you get a single-cycle solution. The keyword here is lazy constraint.

- 933
- 13
- 25
There is a nice sample that may be useful for you: http://z3.codeplex.com/SourceControl/changeset/view/1235b3ea24d9#examples/python/hamiltonian/hamiltonian.py

- 8,229
- 14
- 15
-
Hi Bjorner, Thanks for link.. I have already seen your link before. It just check whether a given gap has cycle or not? But a TSP problem is a optimization problem, where sum of selected path's cost must me minimized.. Do you have any other suggestion? Thanks – user1770051 Sep 10 '13 at 19:39