reset; ############## Modell ################ param I := 3; # Seitenlänge eines Blocks param erg{1..I^2,1..I^2}; # Raster für einfache Zuordnung set ROWS :={1..I^2}; set COLUMNS :={1..I^2}; set NUMBERS :={1..I^2}; set GRID_ROW :={1..I}; set GRID_COLUMN:={1..I}; param grid {ROWS,COLUMNS}; var x{ROWS,COLUMNS,NUMBERS} binary; var y{i in ROWS,j in COLUMNS} = sum{k in NUMBERS} k*x[i,j,k]; subject to row{i in ROWS, k in NUMBERS}: # unique numbers in each row sum {j in COLUMNS} x[i,j,k] = 1; subject to column {j in COLUMNS, k in NUMBERS}: # unique numbers in each column sum {i in ROWS} x[i,j,k] = 1; subject to square {m in GRID_ROW, n in GRID_COLUMN, k in NUMBERS}: # unique numbers in each block sum {i in GRID_ROW, j in GRID_COLUMN} x[3*m-3+i,3*n-3+j,k] = 1; subject to cell {i in ROWS, j in COLUMNS}: # exactly one number in each cell sum {k in NUMBERS} x[i,j,k] = 1; subject to data {i in ROWS, j in COLUMNS:grid[i,j]>0}: sum {k in NUMBERS} k*x[i,j,k] = grid[i,j]; # vorgegebenen Zahlen werden bei der Berechnung beachtet ############## Sudoku Data ################# data; param grid: 1 2 3 4 5 6 7 8 9:= # 1 7 8 0 6 0 0 0 0 9 # 2 0 0 5 0 7 0 0 0 0 # 3 0 0 0 0 0 9 6 0 2 # 4 0 0 0 4 0 6 8 0 7 # 5 8 0 0 5 0 0 0 0 0 # 6 6 4 1 0 0 0 5 0 0 # 7 0 5 4 0 6 0 0 0 0 # 8 0 0 0 9 0 4 0 3 0 # 9 1 0 0 0 2 0 0 4 0; 1 5 3 0 0 7 0 0 0 0 2 6 0 0 1 9 5 0 0 0 3 0 9 8 0 0 0 0 6 0 4 8 0 0 0 6 0 0 0 3 5 4 0 0 8 0 3 0 0 1 6 7 0 0 0 2 0 0 0 6 7 0 6 0 0 0 0 2 8 0 8 0 0 0 4 1 9 0 0 5 9 0 0 0 0 8 0 0 7 9; model; ############## Ausgabe ################# option solver cplex; solve; # solution display y;