Coder Social home page Coder Social logo

antnatb / k-knights Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 278 KB

Project for Artificial Intelligence @ Unifi - Implementation of two solvers based on Backtracking and Maintaining-Arc-Consistency for the K Knights Problem

Python 53.57% TeX 46.43%
ac-3 backtracking chessboard csp-solver

k-knights's Introduction

README

Overview

This repository contains two implementations for solving the problem of placing k knights on an n x n chessboard such that no two knights threaten each other. The implementations utilize Constraint Satisfaction Problems (CSP) and backtracking algorithms to find a solution. Additionally, the repository provides functionality for visualizing the solution on a chessboard. A PDF report is present to better understand the problem and analyze the results.

File Descriptions

  1. k_knights_1.py:

    • This file contains the first implementation of the CSP solver for the knights problem, where the variables are the knights.
    • Classes:
      • CSP_Knights: Represents the CSP for placing knights on the board. It defines the variables, domains, and constraints.
      • Solver: Implements the backtracking algorithm.
      • Drawer: Provides methods for visualizing the chessboard and the solution.
  2. k_knights_2.py:

    • This file contains the second implementation of the CSP solver for the knights problem, where the variables are the squares of the chessboard. The classes have the same names and functionality as the first implementation
  3. main.py:

    • The main script for running tests on the two implementations.
    • Functions:
      • custom_test(): Allows the user to run custom tests by specifying the number of knights and the board size.
      • default_test(): Runs a series of default test cases to compare the performance of the two implementations.
      • test(k, n): Runs a single test case with ( k ) knights on an ( n \times n ) board.
  4. test.py:

    • A script that contains utility functions for running the tests. It is used by main.py to execute the custom and default tests.

How to Run

Running Custom Tests

Follow these steps:

  1. Clone the repository:

    git clone <repository_url>
    cd <repository_name>
  2. Run the main.py script:

    python main.py
  3. When prompted, enter custom to run custom tests or default to run the default tests.

  4. If you're running a custom test, follow the prompts to enter the number of knights and the board size.

Results

The default tests will run the series of cases described in the relation, that is cases where k=c for a given n, measuring the time taken by each implementation to find a solution. The results will be displayed in the console, and the solutions will be visualized on a chessboard. For n>8, implementation 2 will take a very long time.

Notes

  • The implementations may take a significant amount of time for larger board sizes and higher numbers of knights due to the complexity of the problem.
  • Ensure you have the required libraries installed before running the scripts:
    pip install matplotlib

k-knights's People

Contributors

antnatb avatar

Watchers

Kostas Georgiou 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.