% ROME Example: Solving Sudoku using LP
%
%
% Modified by:
% 1. Melvyn & Joel   (Created 1 Aug 2009)

A_incomplete = [0 0 0 0 2 0 9 1 6
      0 0 7 4 8 9 0 0 0
      9 2 5 0 0 0 0 0 8
      0 0 0 1 0 0 4 2 3
      0 9 0 5 3 4 0 7 0
      4 3 1 0 0 2 0 0 0
      8 0 0 0 0 0 2 9 7
      0 0 0 2 6 7 3 0 0
      2 7 3 0 4 0 0 0 0];

rome_begin;
%rome_solver('MOSEK');
h = rome_model('Sudoku');

newvar x(9,9,9) nonneg
%newvar x(9,9,9) bin    % Binary support for CPLEX


% Line Constraints
for ii = 1:9
    for jj = 1:9
        rome_constraint(sum(x(ii, jj, :)) == 1);
        rome_constraint(sum(x(ii, :, jj)) == 1);
        rome_constraint(sum(x(:, ii, jj)) == 1);
    end
end

% Box Constraints
for ii = 1:9
    for jj = 1:3
        for kk = 1:3
            y = x(3*(jj-1)+1:3*jj, 3*(kk-1)+1:3*kk, ii);
            rome_constraint(sum(y(:)) == 1);
        end
    end
end

% Data Constraints
for ii = 1:9
    y = x(:, :, ii);
    ind = find(A_incomplete == ii);
    if(isempty(ind)) 
        continue; 
    end
    rome_constraint(y(ind) == 1);
end

rome_minimize(sum(x(:)));
h.solve;
xsol = h.eval(x);

A = zeros(9);
for ii = 1:9
    A(find(xsol(:,:,ii))) = ii;
end

rome_end;

disp('Incomplete'); 
disp(A_incomplete);
disp('Completed')
disp(A);

% ROME: Copyright (C) 2009 by Joel Goh and Melvyn Sim
% See the file COPYING.txt for full copyright information.