Basic Numberjack Tutorial Adapted from Hebrard et al.s AAAI 2010 - - PowerPoint PPT Presentation

basic numberjack tutorial
SMART_READER_LITE
LIVE PREVIEW

Basic Numberjack Tutorial Adapted from Hebrard et al.s AAAI 2010 - - PowerPoint PPT Presentation

Outline Introduction Intro to Python Modeling in Numberjack Examples Conclusion Basic Numberjack Tutorial Adapted from Hebrard et al.s AAAI 2010 tutorial and parts of the Numberjack website CS 175 April 5, 2011 CS 175 Basic Numberjack


slide-1
SLIDE 1

Outline Introduction Intro to Python Modeling in Numberjack Examples Conclusion

Basic Numberjack Tutorial

Adapted from Hebrard et al.’s AAAI 2010 tutorial and parts of the Numberjack website CS 175 April 5, 2011

CS 175 Basic Numberjack Tutorial

slide-2
SLIDE 2

Outline Introduction Intro to Python Modeling in Numberjack Examples Conclusion

1 Introduction 2 Intro to Python 3 Modeling in Numberjack 4 Examples

N-Queens Problem Magic Squares Combinatorial Auctions

5 Conclusion

CS 175 Basic Numberjack Tutorial

slide-3
SLIDE 3

Outline Introduction Intro to Python Modeling in Numberjack Examples Conclusion

1 Introduction 2 Intro to Python 3 Modeling in Numberjack 4 Examples

N-Queens Problem Magic Squares Combinatorial Auctions

5 Conclusion

CS 175 Basic Numberjack Tutorial

slide-4
SLIDE 4

Outline Introduction Intro to Python Modeling in Numberjack Examples Conclusion

What is Numberjack?

A platform for constraints Written in Python - a front-end to C++-based solvers Excellent for rapidly trying out models

CS 175 Basic Numberjack Tutorial

slide-5
SLIDE 5

Outline Introduction Intro to Python Modeling in Numberjack Examples Conclusion

What is Numberjack?

A platform for constraints Written in Python - a front-end to C++-based solvers Excellent for rapidly trying out models ”Cuts your exponential search tree into logs”

CS 175 Basic Numberjack Tutorial

slide-6
SLIDE 6

Outline Introduction Intro to Python Modeling in Numberjack Examples Conclusion

1 Introduction 2 Intro to Python 3 Modeling in Numberjack 4 Examples

N-Queens Problem Magic Squares Combinatorial Auctions

5 Conclusion

CS 175 Basic Numberjack Tutorial

slide-7
SLIDE 7

Outline Introduction Intro to Python Modeling in Numberjack Examples Conclusion

Overview of Python

Scripting language Supports classes, objects, etc. Duck-typing

CS 175 Basic Numberjack Tutorial

slide-8
SLIDE 8

Outline Introduction Intro to Python Modeling in Numberjack Examples Conclusion

Variables and Functions

Variables a = 2 No need to declare the variable Variables are untyped Functions def double(a): return a * 2 Functions are also not typed Indentations based on whitespace and are part of the syntax

CS 175 Basic Numberjack Tutorial

slide-9
SLIDE 9

Outline Introduction Intro to Python Modeling in Numberjack Examples Conclusion

Lists and Tuples

Lists foo = [1, 4, 5, 10, 2] bar = ["this", "is", "a", "list"] Tuples triplet = (1, 2, 3) course = ("CS", 175) Again, types don’t matter even within lists and tuples

CS 175 Basic Numberjack Tutorial

slide-10
SLIDE 10

Outline Introduction Intro to Python Modeling in Numberjack Examples Conclusion

Control

if <boolean_exp>: do_stuff() while <boolean_exp>: do_stuff()

CS 175 Basic Numberjack Tutorial

slide-11
SLIDE 11

Outline Introduction Intro to Python Modeling in Numberjack Examples Conclusion

For Loops

For loops in C/C++/Java for (int i = 0; i < n; ++i) { do_stuff(i) } For loops in Python for i in range(n): do_stuff(i)

CS 175 Basic Numberjack Tutorial

slide-12
SLIDE 12

Outline Introduction Intro to Python Modeling in Numberjack Examples Conclusion

More Fun with For Loops

for element in list: do_stuff_with(element) teamProjects = [(0, "Asteroid Simulation"), (1, "Scrabble"), (2, "Poker")] for teamNumber, project in teamProjects: print "Team", teamNumber, ":", project Team 0 : Asteroid Simulation Team 1 : Scrabble Team 2 : Poker

CS 175 Basic Numberjack Tutorial

slide-13
SLIDE 13

Outline Introduction Intro to Python Modeling in Numberjack Examples Conclusion

List Comprehensions

A very useful feature! >>> range(4) [0, 1, 2, 3] >>> [x * 2 for x in range(4)] [0, 2, 4, 6] >>> [x * 2 for x in range(4) if x >= 2] [4, 6] Generally, [<expression> for x in <Iterable> (if <condition>)]

CS 175 Basic Numberjack Tutorial

slide-14
SLIDE 14

Outline Introduction Intro to Python Modeling in Numberjack Examples Conclusion

1 Introduction 2 Intro to Python 3 Modeling in Numberjack 4 Examples

N-Queens Problem Magic Squares Combinatorial Auctions

5 Conclusion

CS 175 Basic Numberjack Tutorial

slide-15
SLIDE 15

Outline Introduction Intro to Python Modeling in Numberjack Examples Conclusion

Overview

Constructs

Variables Constraints Model

A common API to interface with back-end solvers

CS 175 Basic Numberjack Tutorial

slide-16
SLIDE 16

Outline Introduction Intro to Python Modeling in Numberjack Examples Conclusion

Variables

# binary variable Variable() # domain from 0 to N−1 Variable(N) # domain from L to U Variable(L, U) # domain specified by a list Variable(list) Useful method (used after a solution has been found) get_value()

CS 175 Basic Numberjack Tutorial

slide-17
SLIDE 17

Outline Introduction Intro to Python Modeling in Numberjack Examples Conclusion

Variables

More constructors: # create a list of N binary variables VarArray(N) # create a list of N variables with domains from 0 to D−1 VarArray(N, D) # create a list of N variables with domains from L to U VarArray(N, L, U)

CS 175 Basic Numberjack Tutorial

slide-18
SLIDE 18

Outline Introduction Intro to Python Modeling in Numberjack Examples Conclusion

Variables

...and even more constructors: # create a matrix of M x N binary variables m = Matrix(M, N) # create a matrix of M x N variables with domains from L to U m = Matrix(M, N, L, U) Special operators # Return a VarArray containing all of the elements of the Matrix m.flat # Return a list of VarArrays corresponding to each row m.row # Return a list of VarArrays corresponding to each column m.col

CS 175 Basic Numberjack Tutorial

slide-19
SLIDE 19

Outline Introduction Intro to Python Modeling in Numberjack Examples Conclusion

Constraints

Arithmetic operators on variables x > y x == y + 2 a[1] > b[2] m[1][4] != n[4][3] Global constructors AllDiff([a, b, c, d, e]) AllDiff(myVarArray) AllDiff(myMatrix) Sum([a, b, c, d]) >= e

CS 175 Basic Numberjack Tutorial

slide-20
SLIDE 20

Outline Introduction Intro to Python Modeling in Numberjack Examples Conclusion

Model

Used to collect the constraints together to define a problem Constructors # empty model model = Model() # model with constraints model = Model(constraints,...) Adding more constraints model.add(constraints) #or model += constraints

CS 175 Basic Numberjack Tutorial

slide-21
SLIDE 21

Outline Introduction Intro to Python Modeling in Numberjack Examples Conclusion

Using a Solver

Different solvers available (Mistral, MiniSat, Walksat) Methods # Get a solver to solve the given problem specified # by the model, solver = model.load(’nameOfSolver’) # attempts to solve the problem solver.solve() # for search−based solvers only (to generate multiple solutions) solver.startNewSearch() while solver.getNewSolution(): # do something with solution Results are stored in the Variable objects

CS 175 Basic Numberjack Tutorial

slide-22
SLIDE 22

Outline Introduction Intro to Python Modeling in Numberjack Examples Conclusion

Outline of Usage

Specify variables Specify constraints over those variables Construct a model with the constraints Construct the solver using that model Call solve() and extract results from Variables using get value()

CS 175 Basic Numberjack Tutorial

slide-23
SLIDE 23

Outline Introduction Intro to Python Modeling in Numberjack Examples Conclusion

Outline of Usage

Specify variables Specify constraints over those variables Construct a model with the constraints Construct the solver using that model Call solve() and extract results from Variables using get value() Can alternatively use the print statement on Variables directly to output their values

CS 175 Basic Numberjack Tutorial

slide-24
SLIDE 24

Outline Introduction Intro to Python Modeling in Numberjack Examples Conclusion N-Queens Problem

1 Introduction 2 Intro to Python 3 Modeling in Numberjack 4 Examples

N-Queens Problem Magic Squares Combinatorial Auctions

5 Conclusion

CS 175 Basic Numberjack Tutorial

slide-25
SLIDE 25

Outline Introduction Intro to Python Modeling in Numberjack Examples Conclusion N-Queens Problem

Problem Definition

Place queens on the chessboard such that no two queens are attacking each other

CS 175 Basic Numberjack Tutorial

slide-26
SLIDE 26

Outline Introduction Intro to Python Modeling in Numberjack Examples Conclusion N-Queens Problem

Modeling

What are the variables/domains of variables for the 4-queens problem presented?

CS 175 Basic Numberjack Tutorial

slide-27
SLIDE 27

Outline Introduction Intro to Python Modeling in Numberjack Examples Conclusion N-Queens Problem

Modeling

What are the variables/domains of variables for the 4-queens problem presented? How about in general for the N-queens problem?

CS 175 Basic Numberjack Tutorial

slide-28
SLIDE 28

Outline Introduction Intro to Python Modeling in Numberjack Examples Conclusion N-Queens Problem

Modeling

What are the variables/domains of variables for the 4-queens problem presented? How about in general for the N-queens problem? What constraints do we need?

CS 175 Basic Numberjack Tutorial

slide-29
SLIDE 29

Outline Introduction Intro to Python Modeling in Numberjack Examples Conclusion Magic Squares

1 Introduction 2 Intro to Python 3 Modeling in Numberjack 4 Examples

N-Queens Problem Magic Squares Combinatorial Auctions

5 Conclusion

CS 175 Basic Numberjack Tutorial

slide-30
SLIDE 30

Outline Introduction Intro to Python Modeling in Numberjack Examples Conclusion Magic Squares

Problem Definition

Given an N × N square, place numbers ranging from 1 to N2 such that each row, column, and diagonal has the same sum

CS 175 Basic Numberjack Tutorial

slide-31
SLIDE 31

Outline Introduction Intro to Python Modeling in Numberjack Examples Conclusion Magic Squares

Modeling

Same questions as before...(variables, domains, constraints?)

CS 175 Basic Numberjack Tutorial

slide-32
SLIDE 32

Outline Introduction Intro to Python Modeling in Numberjack Examples Conclusion Magic Squares

Modeling

Same questions as before...(variables, domains, constraints?) Which Numberjack Variable constructor seems appropriate for this?

CS 175 Basic Numberjack Tutorial

slide-33
SLIDE 33

Outline Introduction Intro to Python Modeling in Numberjack Examples Conclusion Combinatorial Auctions

1 Introduction 2 Intro to Python 3 Modeling in Numberjack 4 Examples

N-Queens Problem Magic Squares Combinatorial Auctions

5 Conclusion

CS 175 Basic Numberjack Tutorial

slide-34
SLIDE 34

Outline Introduction Intro to Python Modeling in Numberjack Examples Conclusion Combinatorial Auctions

Problem Definition

Items Bid Amount Variable A, B 10 x0 A, C 20 x1 B, D 20 x2 B, C, D 25 x3 A 14 x4 Choose bids such that sets of items across bids are disjoint

CS 175 Basic Numberjack Tutorial

slide-35
SLIDE 35

Outline Introduction Intro to Python Modeling in Numberjack Examples Conclusion Combinatorial Auctions

Problem Definition

Items Bid Amount Variable A, B 10 x0 A, C 20 x1 B, D 20 x2 B, C, D 25 x3 A 14 x4 Choose bids such that sets of items across bids are disjoint ....such that the selection maximizes the revenue

CS 175 Basic Numberjack Tutorial

slide-36
SLIDE 36

Outline Introduction Intro to Python Modeling in Numberjack Examples Conclusion Combinatorial Auctions

Problem Definition

Items Bid Amount Variable A, B 10 x0 A, C 20 x1 B, D 20 x2 B, C, D 25 x3 A 14 x4 Choose bids such that sets of items across bids are disjoint ....such that the selection maximizes the revenue Different from constraint satisfaction...known as constraint

  • ptimization

In addition to constraints, we need to specify an objective function

CS 175 Basic Numberjack Tutorial

slide-37
SLIDE 37

Outline Introduction Intro to Python Modeling in Numberjack Examples Conclusion Combinatorial Auctions

Modeling

The number of variables is given this time, but what are the domains? What is the objective function? What are the constraints?

CS 175 Basic Numberjack Tutorial

slide-38
SLIDE 38

Outline Introduction Intro to Python Modeling in Numberjack Examples Conclusion

Conclusion

Rapid prototyping of problems Easy to test out different solvers Numberjack website: http://numberjack.ucc.ie (also linked from the course page)

CS 175 Basic Numberjack Tutorial