Question

In: Math

Consider a 9 × 9 Sudoku, where each number appears exactly once in each row and...

Consider a 9 × 9 Sudoku, where each number appears exactly once in each row and
exactly once in each column. Assume that a list of numbers already appear in some
cells are given.
(a) (20pts) Write an optimization model for Sudoku.
(b) (10pts) Create your own initial list of numbers (1 through 9) which appear in
some cells of the table and use this as your input data to solve the optimization
problem in Part (a) using an optimization solver.

Solutions

Expert Solution

a)

Here is a data matrix B of clues. The first row, B(1,2,2), means row 1, column 2 has a clue 2. The second row, B(1,5,3), means row 1, column 5 has a clue 3. Here is the entire matrix B.

B = [1,2,2;
    1,5,3;
    1,8,4;
    2,1,6;
    2,9,3;
    3,3,4;
    3,7,5;
    4,4,8;
    4,6,6;
    5,1,8;
    5,5,1;
    5,9,6;
    6,4,7;
    6,6,5;
    7,3,7;
    7,7,6;
    8,1,4;
    8,9,8;
    9,2,3;
    9,5,4;
    9,8,2];

drawSudoku(B) % For the listing of this program, see the end of this example.

b)

step 1: Create an optimization variable x that is binary and of size 9-by-9-by-9.

x = optimvar('x',9,9,9,'Type','integer','LowerBound',0,'UpperBound',1);

step 2 : Create an optimization problem with a rather arbitrary objective function. The objective function can help the solver by destroying the inherent symmetry of the problem.

sudpuzzle = optimproblem;
mul = ones(1,1,9);
mul = cumsum(mul,3);
sudpuzzle.Objective = sum(sum(sum(x,1),2).*mul);

step 3 : Represent the constraints that the sums of x in each coordinate direction are one

sudpuzzle.Constraints.consx = sum(x,1) == 1;
sudpuzzle.Constraints.consy = sum(x,2) == 1;
sudpuzzle.Constraints.consz = sum(x,3) == 1;

step 4 : Create the constraints that the sums of the major grids sum to one as well.

majorg = optimconstr(3,3,9);

for u = 1:3
    for v = 1:3
        arr = x(3*(u-1)+1:3*(u-1)+3,3*(v-1)+1:3*(v-1)+3,:);
        majorg(u,v,:) = sum(sum(arr,1),2) == ones(1,1,9);
    end
end
sudpuzzle.Constraints.majorg = majorg;

step 5 : Include the initial clues by setting lower bounds of 1 at the clue entries. This setting fixes the value of the corresponding entry to be 1, and so sets the solution at each clued value to be the clue entry.

for u = 1:size(B,1)
    x.LowerBound(B(u,1),B(u,2),B(u,3)) = 1;
end

Solve the Sudoku puzzle.

sudsoln = solve(sudpuzzle)

Round the solution to ensure that all entries are integers, and display the solution.

sudsoln.x = round(sudsoln.x);

y = ones(size(sudsoln.x));
for k = 2:9
    y(:,:,k) = k; % multiplier for each depth k
end
S = sudsoln.x.*y; % multiply each entry by its depth
S = sum(S,3); % S is 9-by-9 and holds the solved puzzle
drawSudoku(S)


Related Solutions

Find the number of passwords that use each of the digits 3,4,5,6,7,8,9 exactly once. IN how...
Find the number of passwords that use each of the digits 3,4,5,6,7,8,9 exactly once. IN how many of the passwords: 1. are the four odd digits consecutive? 2. are no two odd digits consecutive? 3. are the first three digits even? 4. are the three even digits consecutive?
Where does the need for at-least-once and at-most-once semantics come from? Why can’t we have exactly-once...
Where does the need for at-least-once and at-most-once semantics come from? Why can’t we have exactly-once semantics? – Justify.
Consider a market for used cars where each buyer wants to purchase exactly one used car...
Consider a market for used cars where each buyer wants to purchase exactly one used car if at all. In case he does not buy a car, his utility is zero. There are two kinds of used cars, good quality, called peaches, and bad quality, called lemons. Let there be 100 lemons with valuation 0 for both buyers and sellers, whereas there are 100 peaches with valuations of 100 for the buyers, and 60 for the sellers. There are 500...
1 (a) A pair of dice is rolled, and the number that appears uppermost on each...
1 (a) A pair of dice is rolled, and the number that appears uppermost on each die is observed. Refer to this experiment and find the probability of the given event. (Enter your answer as a fraction.) The sum of the numbers is either 7 or 11. (b) An experiment consists of selecting a card at random from a 52-card deck. Refer to this experiment and find the probability of the event. (Enter your answer as a fraction.) A face...
The first row of the table below shows the number of individuals of each of three...
The first row of the table below shows the number of individuals of each of three genotypes in a given population in the summer of 2015. The second row shows the relative probability that each of these genotypes survives over the winter (the relative fitness based on survival). If you resurvey the population in the spring of 2016, how many SS individuals would you expect to find? Genotype TT TS SS Number of individuals (Fall) 582 400 1645 Probability of...
write a function declaration for a 2d array where each row size is 8 and the...
write a function declaration for a 2d array where each row size is 8 and the function does not return anything.
Write a function declaration for a function that sums each row of a 2D array, where...
Write a function declaration for a function that sums each row of a 2D array, where each row size is 10. The function does not return a result. IN C Program.
Write a function declaration for a function that sums each row of a 2D array, where...
Write a function declaration for a function that sums each row of a 2D array, where each row size is 10. The function does not return a result. Need it in 10 minutes, please.
This is very important. You must follow my instructions exactly. There are 9 questions. Each question...
This is very important. You must follow my instructions exactly. There are 9 questions. Each question requires you to answer TRUE or FALSE followed by an explanation. If you do not do this I will interpret your answer as I see it, so the word TRUE or FALSE is critical as a start to each question. The second important thing is to put each answer below the relevant question, not all at the end of the exam. So, you will...
26. Match (by number) the following terms with their definitions. Each letter is used only once....
26. Match (by number) the following terms with their definitions. Each letter is used only once. Angel 1. Shares held by investors 2. Shares receive priority for future dividends, if dividends are not paid in a given year 3. Shareholders can lose no more than the amount they invested in the company 4. The corporation's own stock that it reacquired 5. The amount invested by stockholders 6. The earnings not paid out in dividends 7. Shares can be returned to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT