Coder Social home page Coder Social logo

valenpe7 / nonogram_solver Goto Github PK

View Code? Open in Web Editor NEW
4.0 2.0 0.0 249 KB

simple Java implementation of nonogram solver based on constraint satisfaction programming using several heuristics

Java 100.00%
nonogram solver constraint-satisfaction-problem combinatorial-optimization arc-consistency forward-checking

nonogram_solver's Introduction

Nonogram solver

This program is part of the assessment work of the course "B4B36ZUI - Introduction to Artificial Intelligence" lectured at FEE CTU in Prague.

Nonograms (http://en.wikipedia.org/wiki/Nonogram) are brain teasers, defined by a legend linked to a grid used to draw a picture. Each number in the legend sets the number of the following boxes filled with color which belongs to the given number. The following rules hold:

  • there is always at least one empty box between two blocks of the boxes filled with the same color
  • there does not have to be an empty box between the boxes filled with different color
  • the order of the numbers in the legend corresponds to the order of the blocks of the boxes (from left to right, from top to bottom)

Input:

The program reads the file from the standard input that has the following format:

NUMBER_OF_ROWS,NUMBER_OF_COLUMNS
CONSTRAINTS_FOR_ROW_1
CONSTRAINTS_FOR_ROW_2
...
CONSTRAINTS_FOR_ROW_M
CONSTRAINTS_FOR_COLUMN_1
CONSTRAINTS_FOR_COLUMN_2
...
CONSTRAINTS_FOR_COLUMN_N

while each constraint has the format:

COLOR_1,NUMBER_1,COLOR_2,NUMBER_2,...,NUMBER_K

where the field "color" applies to the following number and is in a format of the character that represents the color you will use for drawing (for example "#"). The field "number" sets the size of the given block.

The input examples can be found in /nonogram_solver/input directory.

Output:

  • The solution is drawn line-by-line to the standard output
  • The colored box is marked by the sign of the given color, the empty box is marked as "_"
  • If multiple pictures satisfy the constraints, all of them are drawn and separated with an empty line
  • If the solution does not exist, the output is "null"

Implementation details:

The problem is formalized as follows:

  • Variables - individual rows and columns of given task
  • Domains - each variable has its own domain determined by all possible combinations of blocks placing (independently of other variables)
  • Constraints - j-th value of i-th row must be equal to i-th value of j-th column

The following CSP algorithms/techniques are used to speed up the computation:

  • Recursive backtracking - searching for all solutions of given task
  • Forward checking - reducing variable domains after each assignment and checking whether no domain is empty
  • Arc consistency (AC-3) - runs after each assignment to identify further relationships between updated variable domains and potentially reduce their size even more
  • Heuristics for variables - most constrained variable heuristics (the least number of values remaining in its domain)

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.