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 # Grid size for standard Sudoku
valuesrange range N
rowsrange rangeN
colsrange rangeN
# Decision variable: xr c n is if number n is in cell r c otherwise
model.x pyo.Varrowsrange, colsrange, valuesrange, domainpyo.Binary
model.constraints pyo.ConstraintList
# Constraint : Each cell contains exactly one number
for r in rowsrange:
for c in colsrange:
model.constraints.addsummodelxr c n for n in valuesrange
# Constraint : Each number appears exactly once in each row
for r in rowsrange:
for n in valuesrange:
model.constraints.addsummodelxr c n for c in colsrange
# Constraint : Each number appears exactly once in each column
for c in colsrange:
for n in valuesrange:
model.constraints.addsummodelxr c n for r in rowsrange
# Constraint : Each number appears exactly once in each x block
sqrtN
for n in valuesrange:
for br in range N sqrtN:
for bc in range N sqrtN:
model.constraints.add
summodelxbr j bc i n
for i in rangesqrtN
for j in rangesqrtN
# Constraint : Prefilled cells
prefilledcells
# Enforce prefilled cells by setting the specific cell variables to
for r c n in prefilledcells:
model.constraints.addmodelxr c n
# Objective function no specific objective, just find a feasible solution
model.obj pyo.Objectiveexpr sensepyo.maximize
# Solve the model
solver pyo.SolverFactoryglpk
result solver.solvemodel teeTrue
# Output the solved values for only the missing cells, ordered from to
if result.solver.terminationcondition pyo.TerminationCondition.optimal:
printSolved Sudoku Puzzle only missing cells:
for r in rowsrange:
for c in colsrange:
for n in valuesrange:
# Print only missing cells, skip prefilled
if model.xr c nvalue and r c n not in prefilledcells:
printfrcn
else:
printNo optimal solution found. Solver termination condition:", result.solver.terminationcondition
MY OUTPUT:runfileC:Userseddyuntitledpy wdirC:Userseddy GLPSOL: GLPK LPMIP Solver, v Parameters specified in the command line: write C:UserseddyAppDataLocalTemptmpwlmkoglpkraw wglp C:UserseddyAppDataLocalTemptmppwjjmglpkglp cpxlp C:UserseddyAppDataLocalTemptmppbshezicpyomo.lp Reading problem data from C:UserseddyAppDataLocalTemptmppbshezicpyomo.lp C:UserseddyAppDataLocalTemptmppbshezicpyomo.lp:: warning: lower bound of variable x redefined C:UserseddyAppDataLocalTemptmppbshezicpyomo.lp:: warning: upper bound of variable x redefined rows, columns, nonzeros integer variables, all of which are binary lines were read Writing problem data to C:UserseddyAppDataLocalTemptmppwjjmglpkglp lines were written GLPK Integer Optimizer, v rows, columns, nonzeros integer variables, all of which are binary Preprocessing... rows, columns, nonzeros integer variables, all of which are binary Scaling... A: minaije maxaije ratio e Problem data seem to be well scaled Constructing initial basis... Size of triangular part is Solving LP relaxation... GLPK Simplex Optimizer, v rows, columns, nonzeros : obj e inf e: obj e inf e OPTIMAL LP SOLUTION FOUND Integer optimization begins... Longstep dual simplex will be used : mip not found yet inf ; : ee; : mip e tree is empty ; INTEGER OPTIMAL SOLUTION FOUND Time used: secs Memory used: Mb bytes Writing MIP solution to C:UserseddyAppDataLocalTemptmpwlmkoglpkraw'... lines were written Solved Sudoku Puzzle only missing cells:
