Question: please help fix my pyomo code to solve my sudoku problem i almost have it but my output is giving me the wrong values:import pyomo.environ

please help fix my pyomo code to solve my sudoku problem i almost have it but my output is giving me the wrong values:import pyomo.environ as pyo
# Define the model and grid parameters
model = pyo.ConcreteModel()
N =9 # Grid size for standard Sudoku
values_range = range(1, N +1)
rows_range = range(N)
cols_range = range(N)
# Decision variable: x[r, c, n] is 1 if number n is in cell (r, c),0 otherwise
model.x = pyo.Var(rows_range, cols_range, values_range, domain=pyo.Binary)
model.constraints = pyo.ConstraintList()
# Constraint 1: Each cell contains exactly one number
for r in rows_range:
for c in cols_range:
model.constraints.add(sum(model.x[r, c, n] for n in values_range)==1)
# Constraint 2: Each number appears exactly once in each row
for r in rows_range:
for n in values_range:
model.constraints.add(sum(model.x[r, c, n] for c in cols_range)==1)
# Constraint 3: Each number appears exactly once in each column
for c in cols_range:
for n in values_range:
model.constraints.add(sum(model.x[r, c, n] for r in rows_range)==1)
# Constraint 4: Each number appears exactly once in each 3x3 block
sqrtN =3
for n in values_range:
for br in range(0, N, sqrtN):
for bc in range(0, N, sqrtN):
model.constraints.add(
sum(model.x[br + j, bc + i, n]
for i in range(sqrtN)
for j in range(sqrtN)
)==1
)
# Constraint 5: Pre-filled cells
prefilled_cells ={
(0,0,8),(1,2,3),(1,3,6),
(2,1,7),(2,4,9),(2,6,2),
(3,0,1),(3,1,5),(3,5,7),
(4,4,4),(4,5,5),(4,6,7),
(5,3,1),(5,7,3),
(6,3,9),(6,7,6),(6,8,8),
(7,2,8),(7,3,5),(7,7,1),
(8,1,9),(8,6,4)
}
# Enforce pre-filled cells by setting the specific cell variables to 1
for (r, c, n) in prefilled_cells:
model.constraints.add(model.x[r, c, n]==1)
# Objective function (no specific objective, just find a feasible solution)
model.obj = pyo.Objective(expr=0, sense=pyo.maximize)
# Solve the model
solver = pyo.SolverFactory("glpk")
result = solver.solve(model, tee=True)
# Output the solved values for only the missing cells, ordered from (0,1) to (8,8)
if result.solver.termination_condition == pyo.TerminationCondition.optimal:
print("Solved Sudoku Puzzle (only missing cells):")
for r in rows_range:
for c in cols_range:
for n in values_range:
# Print only missing cells, skip prefilled
if model.x[r, c, n].value ==1 and (r, c, n) not in prefilled_cells:
print(f"({r},{c},{n})")
else:
print("No optimal solution found. Solver termination condition:", result.solver.termination_condition)
MY OUTPUT:runfile('C:/Users/eddy_/untitled12.py', wdir='C:/Users/eddy_') GLPSOL: GLPK LP/MIP Solver, v4.65 Parameter(s) specified in the command line: --write C:\Users\eddy_\AppData\Local\Temp\tmpwl93mk5o.glpk.raw --wglp C:\Users\eddy_\AppData\Local\Temp\tmppwj677jm.glpk.glp --cpxlp C:\Users\eddy_\AppData\Local\Temp\tmppbshezic.pyomo.lp Reading problem data from 'C:\Users\eddy_\AppData\Local\Temp\tmppbshezic.pyomo.lp'... C:\Users\eddy_\AppData\Local\Temp\tmppbshezic.pyomo.lp:4717: warning: lower bound of variable 'x4' redefined C:\Users\eddy_\AppData\Local\Temp\tmppbshezic.pyomo.lp:4717: warning: upper bound of variable 'x4' redefined 346 rows, 730 columns, 2938 non-zeros 729 integer variables, all of which are binary 5446 lines were read Writing problem data to 'C:\Users\eddy_\AppData\Local\Temp\tmppwj677jm.glpk.glp'...4364 lines were written GLPK Integer Optimizer, v4.65346 rows, 730 columns, 2938 non-zeros 729 integer variables, all of which are binary Preprocessing... 228 rows, 228 columns, 912 non-zeros 228 integer variables, all of which are binary Scaling... A: min|aij|=1.000e+00 max|aij|=1.000e+00 ratio =1.000e+00 Problem data seem to be well scaled Constructing initial basis... Size of triangular part is 155 Solving LP relaxation... GLPK Simplex Optimizer, v4.65228 rows, 228 columns, 912 non-zeros 0: obj =-0.000000000e+00 inf =5.400e+01(40)58: obj =-0.000000000e+00 inf =4.554e-14(0) OPTIMAL LP SOLUTION FOUND Integer optimization begins... Long-step dual simplex will be used +58: mip = not found yet <=+inf (1; 0)+102: >>>>>0.000000000e+00<=0.000000000e+000.0%(4; 0)+102: mip =0.000000000e+00<= tree is empty 0.0%(0; 7) INTEGER OPTIMAL SOLUTION FOUND Time used: 0.0 secs Memory used: 0.7 Mb (747476 bytes) Writing MIP solution to 'C:\Users\eddy_\AppData\Local\Temp\tmpwl93mk5o.glpk.raw'... 1085 lines were written Solved Sudoku Puzzle (only missing cells): (0,1,4)(0,2,6)(0,3,7)(0,4,1)(0,5,2)(0,6,3)(0,7,5)(0,8,9)(1,0,9)(1,1,2)(1,4,5)(1,5,8)(1,6,1)(1,7,4)(1,8,7)(2,0,5)(2,2,1)(2,3,4)(2,5,3)(2,7,8)(2,8,6)(3,2,9)(3,3,3)(3,4,

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!