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
Related questions
Question
Solve using integer
![# 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](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F96eb680b-bf96-4b4b-a2da-be1777b93ea0%2Fd8127be3-9f02-4636-be99-991c56213337%2F7yfl256_processed.png&w=3840&q=75)
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

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 3 steps with 1 images

Knowledge Booster
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.Recommended textbooks for you

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)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON

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)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON

C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON

Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning

Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education