1
The Issue

With solve minimize I only get one solution, even though there are multiple optimal solutions. I have enabled printout of multiple solutions in the solver configurations. The other optimal solutions are found with solve satisfy, along with non-optimal solutions.

Possible causes

Could it be that the cardinality function card() ranks by enum value where size of two sets are equal? In other words that card(A, B) > card(B, C)? If so, do I have to switch the representation of my vertices?

The Program

I am creating a MiniZinc program for finding the minimum vertex cover of a given graph. The graph in this example is this:

Example graph

With Minimal Vertex Cover solutions being: [{A, B, C, E}, {A, B, E, F}, {A, C, D, E}, {B, C, D, E}, {B, C, D, F}, {B, D, E, F}]. My code only outputs {A, B, C, E}.

Data file:

VERTEX = {A, B, C, D, E, F};
       
edges = [|1, 0, 1, 0, 0, 0, 0, 0, 0
         |1, 1, 0, 1, 1, 0, 0, 0, 0
         |0, 1, 0, 0, 0, 1, 1, 0, 0
         |0, 0, 1, 1, 0, 0, 0, 1, 0
         |0, 0, 0, 0, 1, 1, 0, 1, 1
         |0, 0, 0, 0, 0, 0, 1, 0, 1|];

Solver program:

% Vertices in graph
enum VERTEX;

% Edges between vertices
array[VERTEX, int] of int: edges;

int: num_edges = (length(edges) div card(VERTEX));

% Set of vertices to find
var set of VERTEX: span;

% Number of vertices connected to edge resulting from span
array[1..num_edges] of var 0..num_edges: conn;

% All edges must be connected with at least one vertex from span
constraint forall(i in 1..num_edges)
           (conn[i] >= 1);

% The number of connections to each edge is the number of vertices 
% in span with a connection to that edge
constraint forall(i in 1..num_edges)
           (conn[i] = sum([edges[vert,i]| vert in span]));
           
% Minimize the number of vertices in span
solve minimize card(span);
Community
  • 1
  • 1
Mats
  • 165
  • 1
  • 3
  • 14

2 Answers2

4

solve minimize only show one optimal solution (in some cases, intermediate values might also be shown).

If you want all optimal solutions you must use solve satisfy and add the constraint with the optimal value:

constraint card(span) = 4;

Then the model outputs all the 6 optimal solutions:

card(cpan): 4
span: {A, B, C, E}
conn: [2, 2, 1, 1, 2, 2, 1, 1, 1]
----------
card(cpan): 4
span: {B, C, D, F}
conn: [1, 2, 1, 2, 1, 1, 2, 1, 1]
----------
card(cpan): 4
span: {A, C, D, E}
conn: [1, 1, 2, 1, 1, 2, 1, 2, 1]
----------
card(cpan): 4
span: {B, C, D, E}
conn: [1, 2, 1, 2, 2, 2, 1, 2, 1]
----------
card(cpan): 4
span: {A, B, E, F}
conn: [2, 1, 1, 1, 2, 1, 1, 1, 2]
----------
card(cpan): 4
span: {B, D, E, F}
conn: [1, 1, 1, 2, 2, 1, 1, 2, 2]
----------
==========

Note: I added the output section to show all the values:

output [
   "card(cpan): \(card(span))\n",
   "span: \(span)\n",
   "conn: \(conn)"
];
hakank
  • 6,629
  • 1
  • 17
  • 27
  • Thank you for clearing this up. I am aiming to use this code on a number of examples - would it be possible to dynamically set the `constraint card(span)` to the lowest found number? – Mats May 12 '19 at 19:11
  • 1
    @Matsern Sorry, but no, you have to first run `minimize` and then add the constraint. I suggest that you run this with a wrapper program which do these steps. – hakank May 13 '19 at 05:25
2

An alternative solution is to use OptiMathSAT (v. 1.6.3).

When asking for all solutions in optimization mode, the solver returns all solutions (with respect to the output variables) with the same optimal value.

Example:

~$ mzn2fzn test.mzn test.dzn                                        # your instance
~$ optimathsat -input=fzn -opt.fzn.all_solutions=True < test.fzn 
% allsat model
span = {2, 4, 5, 6};
conn = array1d(1..9, [1, 1, 1, 2, 2, 1, 1, 2, 2]);
----------
% allsat model
span = {1, 3, 4, 5};
conn = array1d(1..9, [1, 1, 2, 1, 1, 2, 1, 2, 1]);
----------
% allsat model
span = {1, 2, 3, 5};
conn = array1d(1..9, [2, 2, 1, 1, 2, 2, 1, 1, 1]);
----------
% allsat model
span = {1, 2, 5, 6};
conn = array1d(1..9, [2, 1, 1, 1, 2, 1, 1, 1, 2]);
----------
% allsat model
span = {2, 3, 4, 5};
conn = array1d(1..9, [1, 2, 1, 2, 2, 2, 1, 2, 1]);
----------
% allsat model
span = {2, 3, 4, 6};
conn = array1d(1..9, [1, 2, 1, 2, 1, 1, 2, 1, 1]);
----------
=========

The main advantage wrt. the approach presented in the accepted answer is that OptiMathSAT is incremental, meaning that the tool searches for other solutions without being restarted, so that it can re-use any useful information that has been previously generated to speed-up the search (e.g. theory lemmas). [CAVEAT: this may not be relevant for small instances; also, other MiniZinc solvers may still be faster depending on the input problem]

Note: please notice that OptiMathSAT does not print the labels of each VERTEX, because the mzn2fzn compiler removes these labels when compiling the file. However, the mapping among numbers and labels should be obvious.


Disclosure: I am one of the developers of this tool.

Patrick Trentin
  • 7,126
  • 3
  • 23
  • 40