Sudoku.java


Sudoku.java


/* Copyright 2021, 狗万app足彩Gurobi Optimization, LLC */ /*数独示例。数独板是一个9x9的网格,它被进一步分成一个3x3的网格。网格中的每个单元格必须取从0到9的值。在同一行、列或3x3子网格中的两个网格单元格不能取相同的值。在MIP公式中,二进制变量x[i,j,v]表示cell 是否取值'v'。约束条件如下:1.单击“确定”。每个cell必须取一个值(sum_v x[i,j,v] = 1) 2。每个值在每行中使用一次(sum_i x[i,j,v] = 1) 3。每个值在每个列(sum_j x[i,j,v] = 1)中使用一次。每个值在3x3的subgrid (sum_grid x[i,j,v] = 1)中精确使用一次(sum_grid x[i,j,v] = 1)。 */ import gurobi.*; import java.io.*; public class Sudoku { public static void main(String[] args) { int n = 9; int s = 3; if (args.length < 1) { System.out.println("Usage: java Sudoku filename"); System.exit(1); } try { GRBEnv env = new GRBEnv(); GRBModel model = new GRBModel(env); // Create 3-D array of model variables GRBVar[][][] vars = new GRBVar[n][n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int v = 0; v < n; v++) { String st = "G_" + String.valueOf(i) + "_" + String.valueOf(j) + "_" + String.valueOf(v); vars[i][j][v] = model.addVar(0.0, 1.0, 0.0, GRB.BINARY, st); } } } // Add constraints GRBLinExpr expr; // Each cell must take one value for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { expr = new GRBLinExpr(); expr.addTerms(null, vars[i][j]); String st = "V_" + String.valueOf(i) + "_" + String.valueOf(j); model.addConstr(expr, GRB.EQUAL, 1.0, st); } } // Each value appears once per row for (int i = 0; i < n; i++) { for (int v = 0; v < n; v++) { expr = new GRBLinExpr(); for (int j = 0; j < n; j++) expr.addTerm(1.0, vars[i][j][v]); String st = "R_" + String.valueOf(i) + "_" + String.valueOf(v); model.addConstr(expr, GRB.EQUAL, 1.0, st); } } // Each value appears once per column for (int j = 0; j < n; j++) { for (int v = 0; v < n; v++) { expr = new GRBLinExpr(); for (int i = 0; i < n; i++) expr.addTerm(1.0, vars[i][j][v]); String st = "C_" + String.valueOf(j) + "_" + String.valueOf(v); model.addConstr(expr, GRB.EQUAL, 1.0, st); } } // Each value appears once per sub-grid for (int v = 0; v < n; v++) { for (int i0 = 0; i0 < s; i0++) { for (int j0 = 0; j0 < s; j0++) { expr = new GRBLinExpr(); for (int i1 = 0; i1 < s; i1++) { for (int j1 = 0; j1 < s; j1++) { expr.addTerm(1.0, vars[i0*s+i1][j0*s+j1][v]); } } String st = "Sub_" + String.valueOf(v) + "_" + String.valueOf(i0) + "_" + String.valueOf(j0); model.addConstr(expr, GRB.EQUAL, 1.0, st); } } } // Fix variables associated with pre-specified cells File file = new File(args[0]); FileInputStream fis = new FileInputStream(file); byte[] input = new byte[n]; for (int i = 0; i < n; i++) { fis.read(input); for (int j = 0; j < n; j++) { int val = (int) input[j] - 48 - 1; // 0-based if (val >= 0) vars[i][j][val].set(GRB.DoubleAttr.LB, 1.0); } // read the endline byte fis.read(); } // Optimize model model.optimize(); // Write model to file model.write("sudoku.lp"); double[][][] x = model.get(GRB.DoubleAttr.X, vars); System.out.println(); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int v = 0; v < n; v++) { if (x[i][j][v] > 0.5) { System.out.print(v+1); } } } System.out.println(); } // Dispose of model and environment model.dispose(); env.dispose(); } catch (GRBException e) { System.out.println("Error code: " + e.getErrorCode() + ". " + e.getMessage()); } catch (IOException e) { System.out.println("IO Error"); } } }