承接 stratadox/puzzle-solver 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

stratadox/puzzle-solver

Composer 安装命令:

composer require stratadox/puzzle-solver

包简介

A generic puzzle solving package, capable of solving a wide variety of puzzles.

README 文档

README

Github Action codecov Scrutinizer Code Quality Maintainability Latest Stable Version License

A generic puzzle solving package, capable of solving a wide variety of puzzles.

Examples

Some examples of puzzles it can solve:

Motivation

People kept asking me to implement a solver for this puzzle or that puzzle. This time, instead of just solving one puzzle, I figured I'd make a universal puzzle solver that would simply solve all of those puzzles and all puzzles to come, in one swift strike. Plus, this way, the algorithms can be re-used for different purposes, such as AI for video games or a solver for some real-life problem. Who knows what the future brings!

Basic usage

There are two "ways" to use this package: the Universal Solver, an informed "swiss army knife" approach, or the Brute Solver... what's in a name.

Universal Solver

The Universal Solver is like a swiss army knife for puzzle solvers.

Feed it with some information on what you're looking for, and receive a solver for your puzzle. All the puzzles that are implemented as examples, as well as many puzzles that don't have an example implementation, can be given efficient solvers using this approach.

5-queens problem with universal solver

Solving a 5-queens problem:

<?php
use Stratadox\PuzzleSolver\Find;
use Stratadox\PuzzleSolver\Puzzle\NQueens\NQueensPuzzle;
use Stratadox\PuzzleSolver\UniversalSolver;

$solver = UniversalSolver::forAPuzzle()
    ->withGoalTo(Find::allBestSolutions())
    ->whereMovesEventuallyRunOut()
    ->select();


$solutions = $solver->solve(NQueensPuzzle::forQueens(5));

assert(count($solutions) === 10);

Sliding puzzle with universal solver

Solving a sliding puzzle:

<?php
use Stratadox\PuzzleSolver\Find;
use Stratadox\PuzzleSolver\Puzzle\SlidingPuzzle\LevenshteinHeuristic;
use Stratadox\PuzzleSolver\Puzzle\SlidingPuzzle\SlidingPuzzle;
use Stratadox\PuzzleSolver\UniversalSolver;

$solver = UniversalSolver::aimingTo(Find::aBestSolution())
    ->withHeuristic(new LevenshteinHeuristic())
    ->select();

$puzzle = SlidingPuzzle::withPieces(
    [2, 4, 1],
    [8, 5, 7],
    [3, 0, 6]
);

$solution = $solver->solve($puzzle)[0];

assert(count($solution->moves()) === 25);

Brute Solver

On the to-do list.

Available Algorithms

Rather than providing a set number of algorithms, this package provides a composable algorithm.

The solver itself can be of one of two flavours, with one of three basic search strategies which in turn can be refined with a number of decorators.

Flavours

The basic solver comes in two flavours: eager or lazy.

Eager

Eager puzzle solvers will search for the first valid solution. Eager solvers will stop after finding a single solution, making them neat for puzzles that can have only one possible solution, or require only a single solution.

Lazy

Lazy puzzle solvers will continue to look for solutions until they've found all valid solutions. Lazy solvers can be used for puzzles that require more than one solution.

Search Strategies

There are three main search strategies available: depth-first, breadth-first and best-first.

Depth-first

Depth-first searches continuously follow the first option, until it turns out the branch does not lead to a solution, at which point it tracks back to the first next available option.

Breadth-first

Breadth-first searches continuously explore all available options, before passing into the options available after those.

Best-first

Best-first searches enqueue newly encountered nodes by order of quality. The move that is most likely to lead to the desired outcome is considered first. In order to determine the quality of a move, the best-first strategy can be provisioned with a heuristic.

Decorators

In order to optimise the search, and/or to not get stuck in eternal loops, a search strategy will typically be wrapped in at least one decorator.

Visited Node Skipper

Visited Node Skipper keeps a list of puzzle states that have already been encountered during the search to skip candidates when they are about to be considered for a second time.

Visited Node Cost Checker

Visited Node Cost Checker keeps a record of the cost of reaching each considered candidate. When a candidate is encountered that already has a recorded cost, the cost of the newly discovered path to reach the same state as before is compared with the previously recorded cost. If the cost is lower than before, the record gets updated and the new candidate is considered. In case the cost is equal or higher than previous paths to the same goal, the candidate is discarded.

Worse-Than-Best Solution Skipper

The Worse-Than-Best Solution Skipper keeps track of the lowest cost of the solutions that have been found during the search. Each time a node is encountered of which the path cost is more than the cost of the cheapest solution that has already been found, the node is skipped.

Duplicate Node Skipper

Duplicate Node Skipper prevents paths from getting into loops. Before considering a new candidate, all previous puzzle states are compared with the new state. If the new puzzle state was already reached on this very path, the candidate is considered a loop and gets rejected.

Iteration Limiter

Iteration Limiter aborts the search when the amount of considered candidates exceeds a set limit.

Debug Logger

Debug Logger is a visualiser for the puzzle solver. Each time a new candidate is taken under consideration, the debug logger logs it to the output stream.

To-do

  • A "Brute Solver": given an iteration limit, try all* configurations of solvers.

    • Start with the lazy searches, then move to eager.
    • Try a solver, catch OutOfIterations and attempt the next.

    (*) Maybe not all, but a bunch of sensible ones..

  • A neat (and extensible) console app to solve puzzles with. The current samples directory is a bit primitive.

    • PuzzleFactory interface and a factory per puzzle, to streamline loading puzzle input and enhance extensibility.
    • SolutionRenderer
    • Symfony console Application? (To decide: do we want that dependency in a utility repo)
  • Website version (Or rather, the link to a future repository for that)

    • Easy-to-use way to add new puzzles? (Can that be done?)

stratadox/puzzle-solver 适用场景与选型建议

stratadox/puzzle-solver 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8 次下载、GitHub Stars 达 3, 最近一次更新时间为 2021 年 01 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 stratadox/puzzle-solver 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 stratadox/puzzle-solver 我们能提供哪些服务?
定制开发 / 二次开发

基于 stratadox/puzzle-solver 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 8
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 6
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 2
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-01-26