In reply to sharatk:
I was thinking of solving this exact example problem, but never made it past the mental exercise phase.
Assuming you have a class that models your sudoku puzzle, you’ll need an encapsulating class that contains two sudoku instances. In both instances you set the values of the fixed cells and turn rand_mode off on those cells. Then randomize one instance, which should generate a solution.
Then the encapsulating class provides a constraint to enforce the 2 instances are not identical. i.e.
class unique_solution;
sodoku a = new;
rand sodoku b = new;
constraint {
a.cell != b.cell;
}
function set( int x, y, val );
a.cell[x][y] == val; a.cell[x][y].rand_mode(0);
b.cell[x][y] == val; b.cell[x][y].rand_mode(0);
endfunction
function void solve();
// set fixed cells
set( 1, 4, 3 );
set( 6, 8, 2 );
...
// find a solution
if ( a.randomize() ) begin
$display(" found a solution ");
if ( this.randomize() ) begin
$display(" found another solution, first was not unique ");
end
else begin
$display(" no other solution found, the first one was unique ");
end
end
else begin
$display(" your puzzle has no solution ");
end
endclass
Now, how well solvers handle this could be another issue.
*edited