[AMPL 24856] Fixing this code

set ROWS := 1…9;

set COLS := 1…9;

set BLOCKS := 1…9;

param s: 1 2 3 4 5 6 7 8 9 :=

1 9 4 3 7 1

2 3 8 7 1

3 2 7 3 1 5

4 5 6 4

5 7 8 6 9

6 4 7 3

7 9 4 7 3

8 4

9 6

set COLS;

set BLOCKS;

set ROWS := 1…9;

param s{ROWS,COLS};

var x{i in ROWS, j in COLS, v in 1…9} binary;

minimize dummy_obj:

sum {i in ROWS, j in COLS, v in 1…9} x[i,j,v];

subject to row_constr {i in ROWS, v in 1…9}:

sum {j in COLS} x[i,j,v] = 1;

subject to col_constr {j in COLS, v in 1…9}:

sum {i in ROWS} x[i,j,v] = 1;

subject to block_constr {k in BLOCKS, v in 1…9}:

sum {i in ROWS, j in COLS: (i,j) in BLOCKS[k]} x[i,j,v] = 1;

…other constraints

option solver cplex;

data SAHC-hw5;

solve;

Not getting a feasible solution. the sodoku code is supposed to

Find an assignment of the numbers 1, 2, …, 9 to the empty cells of this
matrix such that
In each row all numbers appear exactly once.
In each column all numbers appear exactly once.
In each 3 × 3 block all numbers appear exactly once.

Please help me fix the errors

You can’t write a set-expression like 1…9 in your data. This appears to be accepted, but in fact it gives a set with just one member: the string “1…9”. (There’s a similar issue described in https://discuss.ampl.com/t/why-does-set-s-50-70-give-me-a-set-of-only-3-members/63.)

For Sudoku, just put these statements in the model,

set ROWS := 1…9;
set COLS := 1…9;
set BLOCKS := 1…9;

and do not put them in the data at all.

syntax error

context: >>> set <<< ROWS := 1…9;

ampl:

i implemented your feedback of taking it out of the data, and putting it into the model file only. I am still getting a syntax error

Please let me know what I am doing wrong

Your statement “set ROWS := 1…9;” is written correctly. The AMPL error message,

syntax error
context: >>> set <<< ROWS := 1…9;

indicates an error at the beginning of the statement. When the error is at the beginning, very often the problem is with the statement before – for example, a missing ; at the end of the previous statement.

To get more help, attach the latest versions of your files – so that the full context of this error can be seen.

I have attached the requirements of the code as well as my code. Please help me fix it, because I do not know what is going wrong

SAHC-hw5.dat (143 Bytes)

SAHC-hw5.mod (556 Bytes)

Your statement “set ROWS := 1…9;” is written correctly. The AMPL error message,

sahc-hw5.mod, line 2 (offset 6):
        syntax error
context:   >>> set  <<< ROWS := 1..9;

indicates an error at the beginning of the statement. When the error is at the beginning, very often the problem is with the statement before – for example, a missing ; at the end of the previous statement.

Indeed, in this case your model file is missing a ; at the end of the “reset” statement that comes before the “set” statement:

reset
set ROWS := 1..9;

When you change this to “reset;” then the error is fixed.