3

I am trying to solve a Sudoku program that I created. This is the Objective Function

IloNumExpr numExpr = cplex.linearNumExpr();
cplex.addMaximize(numExpr);

What I am trying to do is to add to this either a constraint or new objective function that will allow for the four corner points in the 9x9 Sudoku matrix have special preference in the order of 5 > 7 > 4 > 6 > 8 > 2 > 3 > 9 > 1

Any ideas on what mathematical formulation is needed to complete this?

Ali
  • 56,466
  • 29
  • 168
  • 265

1 Answers1

1

You'd impose the preference order by adding elements to your Objective Function.

Let's say you have variables of the type

choose[row][col][digit]

If choose[2][3][8] = 1 it means that the square (2,3) has the value 8.

The four corners are: 1,1; 1,9; 9,1; 9,9

Essentially, you need to add the following to your existing objective function.

9 x choose[1][1][5] + 8 x choose[1][1][7] + 7 x choose[1][1][4] + ... + 2 x choose[1][1][9] + 1 x choose[1][1][1]
9 x choose[1][9][5] + 8 x choose[1][9][7] + 7 x choose[1][9][4] + ... + 2 x choose[1][9][9] + 1 x choose[1][9][1]
9 x choose[9][1][5] + 8 x choose[9][1][7] + 7 x choose[9][1][4] + ... + 2 x choose[9][1][9] + 1 x choose[9][1][1]
9 x choose[9][9][5] + 8 x choose[9][9][7] + 7 x choose[9][9][4] + ... + 2 x choose[9][1][9] + 1 x choose[9][9][1]

In CPLEX

// Preference order: 5 > 7 > 4 > 6 > 8 > 2 > 3 > 9 > 1

  int[]  preferenceOrder;
  preferenceOrder[1] = 9;
  preferenceOrder[2] = 8;  
  preferenceOrder[8] = 2; 
  preferenceOrder[9] = 1;
  cplex.addMaximize(cplex.scalProd(preferenceOrder, choose));

Why this works?

The CPLEX Solver tries to maximize the value of the obj function. All else being equal, it will first try to make choose[1][1][5] to be 1, and will next try to make it 7 and so on.

Ram Narasimhan
  • 22,341
  • 5
  • 49
  • 55
  • Thank You so much for the help. I am really trying to get different functions for sudoku as a beginning programmer. – user3055708 Dec 03 '13 at 03:40