PHP Classes

PHP Probability Selector: Decide which item to select based on probabilities

Recommend this page to a friend!
  Info   View files Documentation   View files View files (14)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 70 All time: 10,227 This week: 136Up
Version License PHP version Categories
probability-selector 1.0MIT/X Consortium ...7.4Algorithms, PHP 5, Statistics
Description 

Author

This class can decide which item to select based on probabilities.

It can take a parameter, a list of items with identifiers, probability weight, and usage count.

The class can calculate probabilities based on the item weight and usage count and decide the next item to use. The identifier of the selected item is returned.

Innovation Award
PHP Programming Innovation award nominee
February 2023
Number 6
Some applications need to select items from a list automatically. For instance, applications that need to assign jobs to a list of servers.

This class implements an algorithm that considers the number of times the items on a list was used and probability weights assigned to each item to decide which item should be used next time automatically.

Manuel Lemos
Picture of Smoren  Freelight
  Performance   Level  
Name: Smoren Freelight <contact>
Classes: 35 packages by
Country: Russian Federation Russian Federation
Age: 34
All time rank: 291979 in Russian Federation Russian Federation
Week rank: 9 Up1 in Russian Federation Russian Federation Up
Innovation award
Innovation award
Nominee: 15x

Documentation

PHP Probability Selector

Packagist PHP Version Support Scrutinizer Code Quality Coverage Status Build and test License: MIT

Selection manager for choosing next elements to use from data source based on uniform distribution of selections.

Infinite iteration

use Smoren\ProbabilitySelector\ProbabilitySelector;

$ps = new ProbabilitySelector([
    // data     // weight  // initial usage counter
    ['first',   1,         0],
    ['second',  2,         0],
    ['third',   3,         4],
]);

foreach ($ps as $datum) {
    echo "{$datum}, ";
}
// second, second, first, second, third, third, second, first, third, second, third, third, second, first, third, ...

Iteration limit and export

use Smoren\ProbabilitySelector\ProbabilitySelector;

$ps = new ProbabilitySelector([
    // data     // weight
    ['first',   1],
    ['second',  2],
]);
foreach ($ps->getIterator(6) as $datum) {
    echo "{$datum}, ";
}
// second, second, first, second, second, first

print_r($ps->export());
/*
[
    ['first',  1, 2],
    ['second', 2, 4],
]
 */

Single decision

use Smoren\ProbabilitySelector\ProbabilitySelector;

$ps = new ProbabilitySelector([
    // data     // weight
    ['first',   1],
    ['second',  2],
]);
$ps->decide(); // second
$ps->decide(); // second
$ps->decide(); // first

Unit testing

composer install
composer test-init
composer test

Standards

PHP Probability Selector conforms to the following standards:

License

PHP Probability Selector is licensed under the MIT License.


  Files folder image Files  
File Role Description
Files folder image.github (1 directory)
Files folder imagesrc (1 file)
Files folder imagetests (3 files, 2 directories)
Accessible without login Plain text file .scrutinizer.yml Data Auxiliary data
Accessible without login Plain text file codeception.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpcs.xml Data Auxiliary data
Accessible without login Plain text file phpstan.neon Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  .github  
File Role Description
Files folder imageworkflows (1 file)

  Files folder image Files  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file test_master.yml Data Auxiliary data

  Files folder image Files  /  src  
File Role Description
  Plain text file ProbabilitySelector.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imageunit (1 file)
Files folder image_support (1 file)
  Accessible without login Plain text file coding_standard.xml Data Auxiliary data
  Accessible without login Plain text file unit.suite.yml Data Auxiliary data
  Accessible without login Plain text file _bootstrap.php Aux. Auxiliary script

  Files folder image Files  /  tests  /  unit  
File Role Description
  Plain text file ProbabilitySelectorTest.php Class Class source

  Files folder image Files  /  tests  /  _support  
File Role Description
  Plain text file UnitTester.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:70
This week:0
All time:10,227
This week:136Up