Constraints.py.

Slides:



Advertisements
Similar presentations
Examples for Discrete Constraint Programming
Advertisements

ICS-271:Notes 5: 1 Lecture 5: Constraint Satisfaction Problems ICS 271 Fall 2008.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
Chapter 2 Matrices Finite Mathematics & Its Applications, 11/e by Goldstein/Schneider/Siegel Copyright © 2014 Pearson Education, Inc.
CPSC 322, Lecture 11Slide 1 Constraint Satisfaction Problems (CSPs) Introduction Computer Science cpsc322, Lecture 11 (Textbook Chpt 4.0 – 4.2) January,
Constraint Satisfaction Problems
Goldstein/Schnieder/Lay: Finite Math & Its Applications, 9e 1 of 86 Chapter 2 Matrices.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 8 Multidimensional.
Constraint Satisfaction Problems
MTH 112 Elementary Functions Chapter 6 Trigonometric Identities, Inverse Functions, and Equations Section 5 Solving Trigonometric Equations.
Matrix Mathematics in MATLAB and Excel
Adding and Subtracting Decimals. Essential Question: How do I add and subtract decimals? Always line up decimals Add and subtract like you always do Bring.
Determinants. Determinant - a square array of numbers or variables enclosed between parallel vertical bars. **To find a determinant you must have a SQUARE.
SOLVING SUDOKU WITH MATLAB VERIFICATION FUNCTION correctness verification of the puzzle: checks if the current element appears twice in the same line,
Data Structures Using C++ 2E Chapter 6 Recursion.
Matrices King Saud University. If m and n are positive integers, then an m  n matrix is a rectangular array in which each entry a ij of the matrix is.
CP Summer School Modelling for Constraint Programming Barbara Smith 1.Definitions, Viewpoints, Constraints 2.Implied Constraints, Optimization,
Data Structures Using C++ 2E Chapter 6 Recursion.
Slide 1 Constraint Satisfaction Problems (CSPs) Introduction Jim Little UBC CS 322 – CSP 1 September 27, 2014 Textbook §
Constraint Satisfaction Problems (CSPs) CPSC 322 – CSP 1 Poole & Mackworth textbook: Sections § Lecturer: Alan Mackworth September 28, 2012.
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
Matlab for Engineers Manipulating Matlab Matrices Chapter 4.
Computing Science 1P Large Group Tutorial 20 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Data Structures Using C++ 2E1 Recursion and Backtracking: DFS Depth first search (a way to traverse a tree or graph) Backtracking can be regarded as a.
Sudoku Solver Comparison A comparative analysis of algorithms for solving Sudoku.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 8 Multidimensional Arrays.
CSPs Tamara Berg CS Artificial Intelligence Many slides throughout the course adapted from Svetlana Lazebnik, Dan Klein, Stuart Russell, Andrew.
GRAPHICS MODULE 14 STUDY BOOK. Graphic commands SCREEN - puts the screen into graphics mode WINDOW - allows scaling of the screen LINE - 3 formats –LINE.
Computing Science 1P Large Group Tutorial 13 Simon Gay Department of Computing Science University of Glasgow 2006/07.
1 Arrays of Arrays An array can represent a collection of any type of object - including other arrays! The world is filled with examples Monthly magazine:
Chapter 1 Systems of Linear Equations Linear Algebra.
Section 4.4. a) Factor x 2 + 5x - 36 (x + 9)(x – 4) b) Factor 2x 2 – 20x + 18 Hint: factor out a 2 first 2(x-9)(x-1) c) Factor 2x 2 + x – 6 Why doesn’t.
How Do I Solve This Thing?. You are familiar with Sudoku puzzles. For this variation, place the integers 1 – 9 into each row or column such that the.
N-queens problem Original problem: How to place 8 queens on an 8x8 chessboard so that no two queens attack each other N-queen problem: Generalization for.
An Introduction to Constraint Programming in JChoco.
1 Constraint Satisfaction Problems (CSP). Announcements Second Test Wednesday, April 27.
Math 8H Algebra 1 Glencoe McGraw-Hill JoAnn Evans 8-4 Factoring Trinomials ax 2 + bx + c.
Constraint Satisfaction Problems (CSPs) Introduction
ESC101: Introduction to Computing
high-level operations on pictures
CSP in Python.
CS 270 Math Foundations of CS
Constraint Satisfaction
Constraint Satisfaction Problems (CSPs) Introduction
Sit-In Lab 1 Ob-CHESS-ion
Constraint Satisfaction Problem
Lesson 4.2 Adding and Subtracting Decimals
Lesson 4.2 Adding and Subtracting Decimals
Adding and Subtracting Decimals
Adding and Subtracting Decimals
Karnaugh Maps Topics covered in this presentation: Karnaugh Maps
Constraint Satisfaction
Objective: Today we will investigate the ‘magic’ in magic squares.
Chapter 3: Finite Constraint Domains
Lesson 35 Adding and Subtracting Decimals
Introduction to Programming
Magic Squares   10   X.
Constraint satisfaction problems
Adding and Subtracting Decimals
Introduction to Programming
Adding and Subtracting Decimals
© T Madas.
X values y values. x values y values Domain Range.
Magical Hexagons (Teachers notes):
Constraint Satisfaction Problems
Adding and Subtracting Decimals
Constraint satisfaction problems
Lesson 37 Adding and Subtracting Decimals
Constraint Satisfaction Problems (CSP)
P8.py.
Presentation transcript:

constraints.py

Overview Python_constraint is a simple package for solving CSP problems in Python Installing it Using it Examples Magic Squares Map coloring Sudoku puzzles HW4: Battleships

Installation Get setuptools.py easy_install python-constraint Package management tools using the PyPi software repository PyPi is to Python as CPAN is to Perl See http://pypi.python.org/pypi/setuptools easy_install python-constraint Searches PyPi for current version, gets it, builds it and installs it on your computer Or download/access svn from http://labix.org/python-constraint/

Simple Example >>> from constraint import * >>> p = Problem() >>> p.addVariable("a", [1,2,3]) >>> p.addVariable("b", [4,5,6]) >>> p.getSolutions() [{'a': 3, 'b': 6}, {'a': 3, 'b': 5}, {'a': 3, 'b': 4}, {'a': 2, 'b': 6}, {'a': 2, 'b': 5}, {'a': 2, 'b': 4}, {'a': 1, 'b': 6}, {'a': 1, 'b': 5}, {'a': 1, 'b': 4}] >>> p.addConstraint(lambda a,b: a*2 == b, ("a", "b")) [{'a': 3, 'b': 6}, {'a': 2, 'b': 4}]

Magic Square An NxN array on integers where all of rows, columns and diagonals sum to the same number Given N (e.g. 3) and the magic sum (e.g., 15) find the cell values What are the Variables & their domains Constraints

3x3 Magic Square from constraint import * p = Problem() p.addVariables(range(9), range(1, 10)) p.addConstraint(AllDifferentConstraint(), range(9)) p.addConstraint(ExactSumConstraint(15), [0,4,8]) p.addConstraint(ExactSumConstraint(15), [2,4,6]) for row in range(3): p.addConstraint(ExactSumConstraint(15), [row*3+i for i in range(3)]) for col in range(3): [col+3*i for i in range(3)])

3x3 Magic Square sols = p.getSolutions() print sols for s in sols for row in range(3): for col in range(3): print s[row*3+col], print

3x3 Magic Square > python ms3.py [{0: 6, 1: 7, 2: 2, … 8: 4}, {0: 6, 1: …}, …] 6 7 2 1 5 9 8 3 4 6 1 8 7 5 3 2 9 4 … six more solutions …

Constraints FunctionConstraint() Arguments: Function of N (N>0) arguments Set of N variables Function can be separately defined and referenced by name or defined locally via a lambda expression p.addConstraint(lambda x,y: x == 2 * y, [11,22]) P.addConstraint(dblfn, [11,22] def dblfn (x, y): return x == y * 2

Constraints Constraints on a set of variables: Example: AllDifferentConstraint() AllEqualConstraint() MaxSumConstraint() ExactSumConstraint() MinSumConstraint() Example: p.addConstraint(ExactSumConstraint(100),[11,…19]) p.addConstraint(AllDifferentConstraint(),[11,…19])

Constraints Constraints on a set of possible values InSetConstraint() NotInSetConstraint() SomeInSetConstraint() SomeNotInSetConstraint()

Map Coloring def color (map, colors=['red','green','blue']): (vars, adjoins) = parse_map(map) p = Problem() p.addVariables(vars, colors) for (v1, v2) in adjoins: p.addConstraint(lambda x,y: x!=y, [v1, v2]) solution = p.getSolution() if solution: for v in vars: print "%s:%s " % (v, solution[v]), print else: print 'No solution found :-(’ austrailia = "SA:WA NT Q NSW V; NT:WA Q; NSW: Q V; T:"

Sudoku def sudoku(initValue): p = Problem() # Define a variable for each cell: 11,12,13...21,22,23...98,99 for i in range(1, 10) : p.addVariables(range(i*10+1, i*10+10), range(1, 10)) # Each row has different values p.addConstraint(AllDifferentConstraint(), range(i*10+1, i*10+10)) # Each colum has different values p.addConstraint(AllDifferentConstraint(), range(10+i, 100+i, 10)) # Each 3x3 box has different values p.addConstraint(AllDifferentConstraint(), [11,12,13,21,22,23,31,32,33]) p.addConstraint(AllDifferentConstraint(), [41,42,43,51,52,53,61,62,63]) p.addConstraint(AllDifferentConstraint(), [71,72,73,81,82,83,91,92,93]) p.addConstraint(AllDifferentConstraint(), [14,15,16,24,25,26,34,35,36]) p.addConstraint(AllDifferentConstraint(), [44,45,46,54,55,56,64,65,66]) p.addConstraint(AllDifferentConstraint(), [74,75,76,84,85,86,94,95,96]) p.addConstraint(AllDifferentConstraint(), [17,18,19,27,28,29,37,38,39]) p.addConstraint(AllDifferentConstraint(), [47,48,49,57,58,59,67,68,69]) p.addConstraint(AllDifferentConstraint(), [77,78,79,87,88,89,97,98,99]) # add unary constraints for cells with initial non-zero values for j in range(1, 10): value = initValue[i-1][j-1] if value: p.addConstraint(lambda var, val=value: var == val, (i*10+j,)) return p.getSolution()

Sudoku Input easy = [[0,9,0,7,0,0,8,6,0], [0,3,1,0,0,5,0,2,0], [8,0,6,0,0,0,0,0,0], [0,0,7,0,5,0,0,0,6], [0,0,0,3,0,7,0,0,0], [5,0,0,0,1,0,7,0,0], [0,0,0,0,0,0,1,0,9], [0,2,0,6,0,0,0,5,0], [0,5,4,0,0,8,0,7,0]]

Battleship Puzzle NxN grid Each cell occupied by water or part of a ship Given Ships of varying lengths Row and column sums of number of ship cells What are variables and domains constraints

Battleship Puzzle NxN grid Each cell occupied by water or part of a ship Given Ships of varying lengths Row and column sums of number of ship cells What are variables and domains constraints

Battleship puzzle Resources http://www.conceptispuzzles.com/ http://wikipedia.org/wiki/Battleship_(puzzle) Barbara M. Smith, Constraint Programming Models for Solitaire Battleships, 2006

HW4 problem 4 Write a CSP program to solve 6x6 battleships with 3 subs, 2 destroyers and 1 carrier Given row and column sums and several hints Hints: for a location, specify one of {water, top, bottom, left, right, middle, circle}