Details
PHP Probability Selector
![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)
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.
![PHP Programming Innovation award nominee PHP Programming Innovation award nominee](/award/innovation/nominee.gif) February 2023
Nominee
Vote |
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 |
|
Applications that use this package |
|
No pages of applications that use this class were specified.
If you know an application of this package, send a message to the author to add a link here.
|
Files |
|
|
Files |
|