Giter Club home page Giter Club logo

a9bsp's Introduction

a9bsp

The name "a9bsp" is short for Accessible Boolean Satisfiability Problem: the ultimate goal of this library is to make satisfiability problems more accessible to individuals without strong backgrounds in logical theory or mathematics. Built on top of pycosat, the library has straightforward, descriptive functions that generate equivalent boolean satisfiability rules.

Examples

N-Queens Puzzle

The N-queens puzzle is a puzzle in which the player attempts to place a number of queens, often 8, on a board in such a way that none of the queens are in each other's line of attack. Queens can attack pieces that are on the same row, column or diagonal.

Here is a programmatic description of the problem using a9bsp:

from __future__ import print_function, division

import itertools

import a9bsp

n_queens = a9bsp.AccessibleBSP()

# Number of queens and board size; NxN board with N queens.
queen_count = 8

# Generate a chess board with each cell represented by an X and Y position.
chess_board = itertools.product(range(queen_count), range(queen_count))

# Define the conditions of the problem. Iterate through every square on the
# board and compare it with every other square on the board.
for (ax, ay), (bx, by) in itertools.combinations(chess_board, 2):

                                            # Two queens cannot:
    if ((ax == bx) or                       # - Be in the same column
        (ay == by) or                       # - Be in the same row
        (abs((ay - by) / (ax - bx)) == 1)): # - Be on the same diagonal

        # Therefore, two queens cannot be in any pairs of cells meeting
        # these conditions.
        n_queens.mutually_excludes([(ax, ay), (bx, by)])

# One queen per column
for row in range(queen_count):
    squares_in_column = [(n, row) for n in range(queen_count)]
    n_queens.includes_any(squares_in_column)

# One queen per row
for column in range(queen_count):
    squares_in_row = [(column, n) for n in range(queen_count)]
    n_queens.includes_any(squares_in_row)

# Print a board with the positions of each queen
for n, solution in enumerate(n_queens.solutions, 1):
    print("Solution %d:" % (n,))
    for y in range(queen_count):
        for x in range(queen_count):
            if (x, y) in solution:
                print("Q ", end="")
            else:
                print(". ", end="")
        print("")

The first solution of 92 produced by the script:

Solution 1:
. . Q . . . . .
. . . . Q . . .
. Q . . . . . .
. . . . . . . Q
. . . . . Q . .
. . . Q . . . .
. . . . . . Q .
Q . . . . . . .

a9bsp's People

Contributors

ericpruitt avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.