My CPMpy page
CPMPy is a high-level constraint modelling language, with solver support for OR-tools CP-SAT and PySAT. Arrays are NumPy arrays so one can use NumPy's operations to create succinct models.
See github.com/tias/cppy for more info about CPMpy.
Documentation: CPMpy: Constraint Programming and Modeling in Python. See Tias Guns' ModRef19 paper Increasing modeling language convenience with a universal n-dimensional array, CPpy as python-embeded example (PDF) for some background about the project.
cpmpy is available via pip.
My CPMPy Models
All these models are also available (or will be available soon) at my GitHub repo: github.com/hakank/hakank/tree/master/cpmpy.
Note: All models imports the utilities package cpmpy_hakank.py which defines the following constraints/utilities:
-
all_different_except_0(a)
-
to_num(a,n,base)
-
increasing(a)
-
increasing_strict(a)
-
decreasing(a)
-
decreasing_strict(a)
-
all_pairs(a)
-
get_different_solution(model,a)
-
flatten_lists(a)
-
ORT_simple_printer(ort.CpSolverSolutionCallback)
: Callback for printing single arrays
-
ORT_arrays_printer
-
ORT_simple_printer_matrix
-
ORT_simple_function_printer
-
ORT_function_printer_arrays
-
print_solution(a)
-
ortools_wrapper(model,var_array,print_solution,num_sols)
-
ortools_wrapper2(model,var_array,print_solution,num_sols)
(no solution count)
-
ortools_wrapper_opt(model,var_array,print_solution,num_sols)
-
base_array(n)
-
scalar_product(a,b)
-
scalar_product1(a)
-
my_circuit(x)
-
my_circuit_path(x,z)
-
count(a,val,c)
-
atmost(a,val,c)
-
atleast(a,val,c)
-
exactly(a,val,c)
-
global_cardinality_count(a,gcc)
-
inverse(x,y)
-
my_cumulative(s, d, r, b)
-
member_of(x, val)
-
regular(x, Q, S, d, q0, F)
-
regular_table(x, Q, S, d, q0, F)
-
lex_less(x,y)
-
lex2(x)
-
lex_greater(x,y)
-
knapsack(values, weights, n)
-
my_abs(x,y,d)
-
my_abs2(x,y)
-
prod(x,res)
-
prod1(x)
-
among(m,x,v)
-
frenicle(x,n)
-
distribute(card, value, base)
-
fill_array(x,x_val)
-
all_different_pairs(a, s)
-
increasing_pairs(a, s)
-
decreasing_pairs(a, s)
-
pairs(a, s)
-
all_min_dist(min_dist, x, n)
-
all_different_on_intersection(x, y)
-
count_a_in_b(ass,bss)
-
all_different_modulo(x, m)
-
all_different_cst(xs, cst)
-
all_different_cst(xs, cst)
-
arith(x, relop, val)
-
arith_relop(a, t, b)
-
diffn(x,y,dx,dy)
-
nvalue(m,x)
-
nvalues(x,op,n)
-
clique(graph,x, card)
-
assignment_model(cost, tasks=None,people=None,print_solution=None,opt="min")
-
latin_square(x)
-
reverse(xfrom,xto)
-
print_model_and_variables(model)
-
argmin(x,p)
-
argmax(x,p)
-
argmin_except_c(x,p,c)
-
argmin_except_0(x,p)
-
argmax_except_c(x,p,c)
-
permutation3(x,p,y)
-
permutation(x,y)
-
get_min_max_domain(x)
-
chain(op,x)
-
minimum_except_c(x,min_val,c,allow_all_c=False)
-
minimum_except_0(x,min_val,allow_all_0s=False)
-
value_precede(s,t,x)
-
value_precede_chain(c,x)
-
sliding_sum(c,x)
-
no_overlap(s1,d1,s2,d2)
-
is_prime(n)
-
primes(limit)
-
all_different_reif(x,b)
-
all_different_reif_m(model,x)
(returns b
)
-
lex_chain_less(x)
-
soft_alldifferent(x,p)
-
among_seq(low,high,seqlen,x)
-
among_range(low,high,x,v)
-
sequence(x,seq_length,lbound,ubound)
-
sort_array(x,y)
-
all_different_consecutive_values(x)
-
all_different_explain(x,d)
Here are the cpmpy models:
- 3_coins.py: 3 coins problem
- 3_jugs_regular.py: 3 jugs problem using
regular
constraint.
- 3sum.py: 3SUM problem
- 12_pack_problem.py: 12 pack problem
- 17b.py: 17x17 Challenge.
- 18_hole_golf.py: 18 Hole golf
- 50_puzzle.py: 50 puzzle (Sam Loyd)
- 5x5_puzzle.py: 5x5 puzzle
- 2012_CMO_problem.py: 2012 problem (from 2012 CMO)
- a_puzzle.py: "A Puzzle", 8809=6,... (from God Plays Dice "A Puzzle"
- a_round_of_golf.py: A round of golf problem (Dell Logic Puzzles)
- abbots_puzzle.py: Abbot's puzzle (Dudeney)
- added_corner.py: Added cordner puzzle
- age_changing.py: Age changing puzzle (Enigma 1224)
- ages_of_the_sons.py: Ages of the sons puzzle
- all_differ_from_at_least_k_pos: Test of global constraint
all_differ_from_at_least_k_pos
- all_different_cst.py: Test of global constraint
all_different_cst
- all_different_consecutive_values.py: Test of global constraint
all_different_consecutive_values
- all_different_reif.py: Test of global constraint
all_different_reif
and all_different_reif_m
(reified version of the all_different
constraint)
- alldifferent_except_0.py: Test of global constraint
all_different_except_0
- all_different_explain.py: Test of global constraint
all_different_explain
- all_different_modulo.py: Test of global constraint
all_different_modulo
- all_different_on_intersection.py: Test of global constraint
all_different_on_intersection
- all_different_pairs.py: Test of global constraint
all_different_pairs
- all_interval.py: All interval problem (CSPLib problem #7)
- all_min_dist.py: Test of global constraint
all_min_dist
- all_partitions.py: All integer partitions
- allergy.py: Allergy logic puzzle
- alphametic.py: General alphametic solver (i.e. problems like "SEND+MORE=MONEY")
- among.py: Test of global constraint
among
- among_seq.py: Test of global constraint
among_seq
- appointment_scheduling.py: Appointment scheduling (matrix and 'set' approach)
- arith.py: Test of global constraint
arith
- archery_puzzle.py: Archery puzzle
- argmax_test.py: Test of global constraints
argmax,argmin,argmin_except_0,argmax_except_c
- assignment.py: Some simple assignment problems, using the fairly general
assignment_model
- arch_friends.py: Arch friends puzzle
- atmost_test.py: Tests of global constraints
atmost
, atleast
, and exactly
- autoref.py: Autoref problem
- averbach_1_2.py: Circular table problem (Averbach, Chein)
- babysitting.py: Babysitting puzzle
- balancing_brackets.py: Balancing brackets
- bales_of_hay.py: Bales of Hay problem
- bananas.py: Bananas puzzle
- best_host.py: Best host problem
- bibd.py: Balanced Incomplete Block Design (BIBD)
- big_bang2.py: Big bang problem
- bin_packing.py: Bin packing problem
- bin_packing_laurent.py: Bin packing problem (port of a model by Laurent Perron)
- birthday_coins.py: Birthday coins puzzle
- book_buy.py: Book buy puzzle
- bowls_and_oranges.py: Bowls and Oranges problem
- breaking_news.py: Breaking news puzzle
- broken_weights.py: Broken weights puzzle
- building_a_house.py: Building a house (scheduling problem)
- building_blocks.py: Building blocks puzzle
- bus_schedule.py: Bus scheduling
- bus_scheduling_csplib.py: Bus driver scheduling problem (prob022 in CSPLib)
Problem instances:
- cabling.py: Cabling problem
- calculs_d_enfer.py: Calculs d'enfer puzzle
- calvin_puzzle.py: Calvin puzzle
- candies.py: Candies problem
- capital_budget.py: Capital budget (Winston)
- car.py: Car sequencing (from Pascal Van Hentenryck "OPL Optimization Programming Language", page 184ff)
- chain_test.py: Test of global constraint
chain
- chess_set.py: Chess set (simple) optimization problem
- circling_squares.py: Circling squares problem
- circuit.py: Decomposition of global constraint
circuit
- circuit_path.py: Decomposition of global constraint
circuit_path
- clique.py: Test of global constraint
clique
- clock_triplets.py: Clock triplets
- coins3.py: Minimum mumber of coins that allows one to pay exactly any amount smaller than one Euro (from the ECLiPSe book)
- coins_grid.py: Tony Hurlimann's coin grid problem
- coloring_ip.py: Map coloring, 0/1 model (GLPK)
- combinatorial_auction.py: Combinatorial auction
- combinatorial_auction2.py: Combinatorial auction
- consecutive_integer_partition_problem.py: Consecutive integer partition problem (Kitzawa)
- contiguity_regular.py: Decomposition of global constraint
global contiguity
(using regular
constraint)
- contracting_costs.py: Contracting costs (Sam Loyd)
- costas_array.py: Costas array problem
- covering_opl.py: Set covering (OPL)
- crew.py: Crew allocation problem
- crossfigure.py: Crossfigure
- crossword2.py: Simple crossword problem
- crypta.py: Crypta, alphametic problem (standard Prolog benchmark)
- crypto.py: Crypto, alphametic problem (standard Prolog benchmark)
- cur_num.py: Curious numbers problem (Dudeney)
- cumulative_tests.py: Some tests of the
my_cumulative
constraint
- curious_set_of_integers.py: Curious set of integers (Martin Gardner)
- debruijn.py: "Classical" and "arbitrary" de Bruijn sequences
- devils_word.py: Devil's word problem
- diet1.py: Simple diet problem (optimization of a diet).
- diamond_free_degree_sequences.py: Diamond-free Degree Sequences
- diffn_test.py: Test of global constraint
diffn
- dinner.py: Dinner problem
- discrete_knapsack_problem.py: Discrete knapsack problem
- discrete_tomography.py: Discrete tomography
Data files:
- distribute.py: Test of global constraint
distribute
- divisible_by_1_through_9.py: Divisible by 1 through 9 puzzle
- divisible_by_9_to_1.py: Divisible by 9 to 1 puzzle
- domino.py: Domino problem
- donald_gerald_robert.py: DONALD+GERALD=ROBERT puzzle
- drive_ya_nuts.py: Drive Ya Nuts puzzle
- dudeney_numbers.py: Dudeney numbers
- einstein_opl.py: Einstein puzzle (OPL)
- einav_puzzle.py: Einav puzzle
- eq10.py: Eq 10, standard benchmark problem
- eq20.py: Eq 20, standard benchmark problem
- equal_sized_groups.py: Equal sized groups
- euler1.py: Project Euler problem #1
- euler2.py: Project Euler problem #2
- euler9.py: Project Euler problem #9
- euler31.py: Project Euler problem #31
- exodus.py: Exodus puzzle
- facility_location_problem.py: Facility location problem
- fancy.py: Mr Greenguest puzzle (a.k.a Fancy dress problem)
- fill_a_pix.py: Fill-a-pix problem.
Data files:
- fill_in_the_square.py: Fill in the squares (Brainjammer)
- finding_celebrities.py: Finding celebrities problem
- five_brigands.py: Five brigands puzzle
- five_floors.py: Five floors puzzle
- five_statements.py: Five statements puzzle
- fixed_charge.py: Fixed charge problem
- flow_free_game.py: Flow Free game
- football.py: Football optimization
- four_islands.py: Four islands puzzle
- four_numbers.py: Four numbers
- fractions_model.py: Fractions puzzle
- frog_circle.py: Frog circle puzzle
- furniture_moving.py: Optimizing furniture moving (Marriott and Stuckey). Contains an experimental decomposition of the global constraint
my_cumulative
- futoshiki.py: Futoshiki puzzle (grid puzzle)
- general_store.py: General store puzzle
- generating_numbers2.py: Permutation set problem.
- giant_cat_army_riddle.py: Giant Cat Army riddle
- global_contiguity.py: Global contiguity constraint using
regular
constraint
- golomb_ruler.py: Golomb ruler
- grocery.py: Grocery problem
- hamming_distance.py: Hamming distance
- handshaking.py: Halmos' handshake problem
- hanging_weights.py: Hanging weights problem
- heterosquare.py: Heterosquare
- hidato.py: Hidato puzzle
- hidato_table.py: Hidato puzzle using global constraint
Table
- huey_dewey_louie.py: Huey, Dewey, and Louie logical puzzle.
- initials_queue_problem.py: Initial queues problem (Chris Smith)
- inverse_test.py: Test of global constraint
inverse
- isbn.py: Some excursion of ISBN13
- jobs_puzzle.py: Jobs puzzle
- just_forgotten.py: Just forgotten puzzle (Enigma 1517)
- k4p2_graceful_graph.py: K4 P2 Graceful graph
- k4p2_graceful_graph2.py: K4 P2 Graceful graph (alternative model)
- kakuro.py: Kakuro puzzle
- kenken2.py: KenKen puzzle
- killer_sudoku.py: Killer Sudoku puzzle
- knapsack.py: Knapsack problem
- knapsack_investments.py: Knapsack investments problem
- knapsack_rosetta_code_01.py: Knapsack problem, 0/1 version (Rosetta code)
- knapsack_rosetta_code_bounded.py: Knapsack problem, bounded version (Rosetta code)
- knights_path.py: Knights path
- knights_tour_circuit.py: Knights tour using the
circuit
constraint
- labeled_dice.py: Labeled dice problem, from Jim Orlin's Colored letters, labeled dice: a logic puzzle (follow up: Update on Logic Puzzle)
- langford.py: Langford's number problem (CSPlib problem 24)
- latin_square_card_puzzle.py: Latin square card puzzle (Greaco-Latin squares)
- least_diff.py: Least Diff problem (alphametic problem)
- lecture_series.py: Lecture series problem
- lectures.py: Lectures, schedule problem from Biggs "Discrete Mathematics"
- lex_test.py: Test of global constraint
lex_less
- liechtenstein_coloring.py: Liechtensteing coloring problem
- linear_combinations.py: Linear combinations. Restoring arrays of numbers from the differences. See Recover_origin_of_pair_differences.pdf (PDF) for more information about this.
- magic_hegagon.py: Magic hexagon
- magic_sequence.py: Magic sequence
- magic_square.py: Magic square problem
- magic_square_water_retention.py: Magic square water retention
- magic_squares_and_cards.py: Magic squares and card problem (Martin Gardner)
- mamas_age.py: Mama's age puzzle
- map.py: Simple map coloring problem.
- map_coloring.py: Simple map coloring problem. Includes symmetry breaking with
value_precede_chain
- marathon2.py: Marathon puzzle (XPress example)
- mastermind_like_problem.py: Mastermind like problem
- matrix_element_test.py: Testing
matrix_element
. However, it is too slow for real use. The version using val == Element(x_flat,i*c+j)
is much faster and thus recommended.
- max_flow_taha.py: Maximum flow problem (Taha)
- max_flow_winston1.py: Maximum flow problem (Winston)
- maximum_density_still_life.py: Maximum density still life
- MCDP1.py: Manufacturing Cell Design Problem (MCDP)
- minesweeper.py: Minesweeper
Data files:
- minimum_except_0.py: Test of global constraint
minimum_except_0
(and minimum_except_c
)
- miss_manners.py: Miss Manners' seating problem
- monks_and_doors.py: Monks and doors puzzle
- mr_smith.py: Mr Smith problem, logic problem (from IF Prolog)
- music_men.py: Music Men puzzle
- nadel.py: Nadel's construction problem
- no_time_to_try.py: No time to try puzzle (Katie Steckles)
- nonogram_regular.py: Nonogram solver using a decomposition of global constraint
regular
. Compare with nonogram_regular_table.py. The program run_nonogram.py is a benchmark using some of these instances.
Problem instances:
- nonogram_regular_table.py: Nonogram solver using
regular_table
. See the Nonogram instances above.
- nontransitive_dice.py: Nontransitive dice
- nqueens_all_solutions.py: N queens problem
- number_lock.py: Number lock puzzle (Crack the code, Mastermind like puzzle)
- number_lock2.py: Number lock puzzle (Crack the code, Mastermind like puzzle), neater version
- number_of_days.py: Number of days problem
- number_puzzle8.py: Number puzzle #8 (2002 AMC 10/12B)
- number_square.py: Number square puzzle (Van Hentenryck)
- nurse_rostering.py: Nurse rostering
- nvalue.py: Test of global constraint
nvalue
- nvalues.py: Test of global constraint
nvalues
- olympic.py: Olympic puzzle (alphametic, standard Prolog benchmark)
- organize_day.py: Organize a day, simple scheduling problem (from ECLiPSe)
- ormat_game.py: Ormat Game
- p_median.py: P-median problem (from OPL)
- pair_divides_the_sum.py: Pair divides the sum
- pandigital_numbers.py: Pandigital numbers in "any" base
- partition_into_subsets_of_equal_values.py: Partition into subsets of equal values
- pentonomies.py: Pentonomies puzzle. The instances are in the file pentonomies_problems.py
- perfect_square_sequence.py: Perfect square sequence
- perfect_squares.py: Perfect squares placements
- permutation3.py: Test of global constraint
permutation3(x,p,y)
- pert.py: Simple PERT model
- photo_problem.py: Photo problem
- picking_teams.py: Picking teams
- pigeon_hole.py: Pigeon hole problem
- ping_pong.py: Ping pong tournament puzzle
- place_number_puzzle.py: Place number puzzle
- polydivisible_numbers.py: Polydivisible numbers
- post_office_problem2.py: Post office problem (Winston)
- pythagoras.py: Pythagoras
- quasigroup_completion.py: Quasigroup completion
Data files:
- queens_diversity.py: Queens diversity
- rabbits.py: Rabbits and pheasants problem
- reduce_overlapping_intervals_cp.py: Reduce overlapping intervals (StackOverflow)
- regexp.py: Simple regular expression: Generating variants of "kjellerstrand" using
regular
constraint.
- regular_test.py: Testing the decomposition of global constraint
regular
constraint.
- regular_table_test.py: Testing the decomposition of global constraint
regular_table
constraint.
- rehearsal.py: Scheduling a rehearsal
- remarkable_sequence.py: Remarkable sequence
- remainders.py: Remainders puzzle (Kordemsky)
- rogo2.py: Rogo puzzle
Instances:
- run_nonogram.py: Benchmark for the nonogram solver comparing nonogram_regular.py and nonogram_regular_table.py
- safe_cracking.py: Safe cracking puzzle (from Oz primer)
- sat.py: Satisfiability problem (GLPK)
- schedule1.py: Simple scheduling problem
- scheduling_speakers.py: Scheduling speakers (Rina Dechter)
- secret_santa.py: Secret santa problem
- secret_santa2.py: Secret santa problem II (optimizing "santa distance")
- send_more_money.py: SEND+MORE=MONEY puzzle
- send_more_money_any_base.py: SEND+MORE=MONEY in "any" base
- send_most_money.py: SEND+MOST=MONEY (maximize MONEY)
- sequence.py: Test of global constriant
sequence
- serial_crack.py: Serial crack problem
- seseman.py: Seseman Convent problem.
- set_covering.py: Set covering, placing of firestations (Winston)
- set_covering2.py: Set covering, number of security telephons on a campus (Taha)
- set_covering3.py: Set covering, senators making a committe (Murty)
- set_covering4.py: Set covering (and set partition), work scheduling (Lundgren, Roennqvist, Vaerbrand "Optimeringslaera")
- set_covering_deployment.py: Set covering deployment
- set_covering_skiena.py: Set covering problem (Steven Skiena)
- set_partition.py: Set partition
- sicherman_dice.py: Sicherman dice
- ski_assignment.py: Ski assignment problem
- skyscraper.py: Skyscraper puzzle
- sliding_sum.py: Test of global constraint
sliding_sum
- smugglers_knapsack.py: Smuggler's knapsack (Marriot, Stuckey)
- social_golfer1.py: Social Golfer problem
- sonet_problem.py: SONET problem
- sort_array.py: Test of constraint
sort_array
- square_root_of_wonderful.py: Square root of WONDERFUL (Martin Gardner)
- stable_marriage.py: Stable Marriage problem (from Van Hentenryck's OPL book and some other problem instances)
- steiner.py: Steiner triplets
- strimko2.py: Strimko puzzle (Latin squares with "streams").
Some data files:
- stuckey_seesaw.py: Simple seesaw problem (Stuckey and Marriott)
- subset_sum.py: Subset sum problem (Katta G. Murty)
- sudoku.py: Sudoku, checking over hundred instances of sizes 9x9, 16x16, and 25x25 (mostly from the Gecode collection). The instances are in sudoku_problems.py
- sudoku_17_hints.py: Checks all 49151 Sudoku instances with 17 hints from Gordon Royle (available in sudoku17.txt)
- survo_puzzle.py: Survo puzzle
Data files:
- talisman_square.py: Talisman square
- temporal_reasoning.py: Temporal reasoning (Apt)
- the_family_puzzle.py: The Family puzzle
- timpkin.py: Mrs Timkpin's age problem (Dudeney)
- to_num_test.py: Converts inter to/from an array of digits using the constraint
to_num
- torn_numbers.py: Torn numbers puzzle (Dudeney)
- tourist_site_competition.py: Tourist site competition
- traffic_lights.py: Traffic lights problem (CSPLib problem 16)
- tsp.py: TSP problem (using a MIP modelling approach, port from GLPK)
- tsp_circuit.py: TSP problem using decomposition of the
circuit
constraint (and circuit_path
)
- tunapalooza.py: Tunapalooza puzzle
- TUCTF2017_future_solver.py: TUCTF 2017 Future solver
- twin_letters.py: Twin letters puzzle
- value_precede_chain_test.py: Test of constraints
value_precede
and value_precede_chain
- volsay.py: Volsay simple optimization problem.
- volsay2.py: Volsay simple optimization problem
- volsay3.py: Volsay simple optimization problem
- warehouse_location.py: Warehouse location problem
- water_buckets1.py: Water buckets problem (generalized)
- wedding_optimal_chart.py: Finding an optimal wedding seating chart
- who_killed_agatha_all_solutions.py: Who killed agatha? (The Dreadsbury Mansion Murder Mystery, an automated reasoning problem)
- word_square.py: Word square
- young_tableaux.py: Young tableaux and partitions
- who_killed_agatha_all_solutions.py
- xkcd.py: xkcd knapsack problem
- zebra.py: Zebra puzzle