PHPackages                             zomat/php-genetics - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. zomat/php-genetics

ActiveLibrary

zomat/php-genetics
==================

03PHP

Since Sep 11Pushed 2y ago1 watchersCompare

[ Source](https://github.com/Zomat/php-genetics)[ Packagist](https://packagist.org/packages/zomat/php-genetics)[ RSS](/packages/zomat-php-genetics/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (2)Used By (0)

PHP Genetics Algorithm for Knapsack Problem
===========================================

[](#php-genetics-algorithm-for-knapsack-problem)

This repository contains a PHP implementation of a genetic algorithm for solving the Knapsack Problem. The Knapsack Problem is a classic optimization problem where given a set of items with associated weights and values, the goal is to select a subset of items that maximizes the total value while staying within a given weight limit.

Getting Started
---------------

[](#getting-started)

These instructions will help you understand the code and set it up for your own use.

### Prerequisites

[](#prerequisites)

- PHP 8.1 or higher
- Composer for dependency management

### Installation

[](#installation)

1. Clone the repository to your local machine:

```
git clone https://github.com/Zomat/php-genetics.git
```

2. Navigate to the repository directory:

```
cd php-genetics
```

3. Install the required dependencies using Composer:

```
composer install
```

### Usage

[](#usage)

In the knapsack.php file, you will find an example configuration of the genetic algorithm. You can customize the algorithm parameters and the items placed in the knapsack by modifying the relevant values in the code. Here's an example configuration:

```
$gaBuilder = new GeneticsAlgorithmBuilder;

$gaBuilder->setGenerationLimit(1000)
->setPopulationSize(10)
->setMutationLimit(1)
->setMutationProbability(0.5)
->setElitism(true)
->setWeightLimit(3000);

$gaBuilder->setItems(
    new Item('Laptop', 500, 2200),
    new Item('Headphones', 150, 160),
    // Add more items here...
);

try {
    $ga = $gaBuilder->build();
} catch (\Exception $e) {
    exit("Can't build algorithm: {$e->getMessage()}" . PHP_EOL);
}

$result = $ga->run();

echo "Result population: " . PHP_EOL;
echo $result->itemNames . PHP_EOL;
echo " => Fitness: " . $result->fitness . PHP_EOL;
echo " => Generation: " . $result->generation . PHP_EOL;
```

```
php knapsack.php
```

Contributing
------------

[](#contributing)

Feel free to contribute by opening issues or submitting pull requests. Your contributions are highly appreciated!

License
-------

[](#license)

This project is licensed under the MIT License.

###  Health Score

14

—

LowBetter than 2% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity3

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity23

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/7cd55ae3866ae68a8d93b74397e7c090a97d88bcc48200ce877494dab263d158?d=identicon)[Zomat](/maintainers/Zomat)

---

Top Contributors

[![Zomat](https://avatars.githubusercontent.com/u/20731551?v=4)](https://github.com/Zomat "Zomat (11 commits)")

### Embed Badge

![Health badge](/badges/zomat-php-genetics/health.svg)

```
[![Health](https://phpackages.com/badges/zomat-php-genetics/health.svg)](https://phpackages.com/packages/zomat-php-genetics)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
