Solve using integer programming

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Solve using integer programming

# Integer Programming: Sudoku
# We have provided most of an IP implementation.
# Again, you just need to implement the constraints. Note however, unlike in the CSP version,
# we have not already "prefilled" the squares for you. You'll need to add those constraints yourself.
A helper function to visualize ouput. You do not need to change this ***
""" binary: the output of your solver ***
""" psize: the problem size (e.g. 3 for a 3x3 grid of 3x3 squares in a standard Sudoku) ***
def sudokuIPToGrid(binary,psize):
if binary is None:
return None
dim = psize**2
x = np.zeros((dim,dim),dtype=int)
for i in range (dim):
for j in range (dim):
for k in range(dim):
return x
if binary[dim*i+j][k] >= 0.99:
x[i][j] = k+1
*** Implementation for a IP Sudoku Solver ***
positions: list of (row,column, value) triples representing the already filled in cells"""
""" psize: the problem size (e.g. 3 for a 3x3 grid of 3x3 squares in a standard Sudoku) ***
"*" the library does not support 3D variables, so M[i][j] should be your indicator variable ***
""" for the ith square having value j where in a 4x4 grid i ranges from 0 to 15 ***
def sudokuIP (positions, psize):
# Define the variables - see comment above about interpretation
dim = psize**2
M = cp.Variable((dim**2,dim),integer=True) #Sadly we cannot do 3D Variables
print(positions)
constraints = []
#
# Your code
# It should define the constraints needed
# We've given you one to get you started
constraints.extend([0 <= M[x][k] for x in range(dim**2) for k in range (dim)])
constraints.extend([1 >= M[x][k] for x in range(dim**2) for k in range (dim)])
# End your code
#
# Form dummy objective - we only care about feasibility
obj = cp.Minimize (M[0][0])
# Form and solve problem.
prob = cp.Problem(obj, constraints)
prob.solve()
#Uncomment the version below instead if you want more detailed information from the solver to see what
might be going wrong
#Please leave commented out when submitting.
#prob.solve(verbose=True)
#For debugging you may want to look at some of the information contained in prob before returning
# See the example file
return M.value
Transcribed Image Text:# Integer Programming: Sudoku # We have provided most of an IP implementation. # Again, you just need to implement the constraints. Note however, unlike in the CSP version, # we have not already "prefilled" the squares for you. You'll need to add those constraints yourself. A helper function to visualize ouput. You do not need to change this *** """ binary: the output of your solver *** """ psize: the problem size (e.g. 3 for a 3x3 grid of 3x3 squares in a standard Sudoku) *** def sudokuIPToGrid(binary,psize): if binary is None: return None dim = psize**2 x = np.zeros((dim,dim),dtype=int) for i in range (dim): for j in range (dim): for k in range(dim): return x if binary[dim*i+j][k] >= 0.99: x[i][j] = k+1 *** Implementation for a IP Sudoku Solver *** positions: list of (row,column, value) triples representing the already filled in cells""" """ psize: the problem size (e.g. 3 for a 3x3 grid of 3x3 squares in a standard Sudoku) *** "*" the library does not support 3D variables, so M[i][j] should be your indicator variable *** """ for the ith square having value j where in a 4x4 grid i ranges from 0 to 15 *** def sudokuIP (positions, psize): # Define the variables - see comment above about interpretation dim = psize**2 M = cp.Variable((dim**2,dim),integer=True) #Sadly we cannot do 3D Variables print(positions) constraints = [] # # Your code # It should define the constraints needed # We've given you one to get you started constraints.extend([0 <= M[x][k] for x in range(dim**2) for k in range (dim)]) constraints.extend([1 >= M[x][k] for x in range(dim**2) for k in range (dim)]) # End your code # # Form dummy objective - we only care about feasibility obj = cp.Minimize (M[0][0]) # Form and solve problem. prob = cp.Problem(obj, constraints) prob.solve() #Uncomment the version below instead if you want more detailed information from the solver to see what might be going wrong #Please leave commented out when submitting. #prob.solve(verbose=True) #For debugging you may want to look at some of the information contained in prob before returning # See the example file return M.value
Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Recurrence Relation
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education