1. Go to this page and download the library: Download ozdemir/subset-finder library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ozdemir / subset-finder example snippets
use Ozdemir\SubsetFinder\Subset;
use Ozdemir\SubsetFinder\SubsetCollection;
use Ozdemir\SubsetFinder\SubsetFinder;
use Ozdemir\SubsetFinder\SubsetFinderConfig;
// Define your collection and subset criteria
$collection = collect([
new Product(id: 1, quantity: 11, price: 15),
new Product(id: 2, quantity: 6, price: 5),
new Product(id: 3, quantity: 6, price: 5),
]);
$subsetCollection = new SubsetCollection([
Subset::of([1, 2])->take(5), // Find 5 items from products 1 and 2
Subset::of([3])->take(2), // Find 2 items from product 3
]);
// Create and configure SubsetFinder
$config = new SubsetFinderConfig(
idField: 'id',
quantityField: 'quantity',
sortField: 'price', // Sort by price (ascending)
sortDescending: false
);
$subsetFinder = new SubsetFinder($collection, $subsetCollection, $config);
$subsetFinder->solve();
// Get results
$foundSubsets = $subsetFinder->getFoundSubsets();
$remaining = $subsetFinder->getRemaining();
$maxSubsets = $subsetFinder->getSubsetQuantity();
// For large datasets (512MB memory, lazy evaluation enabled)
$subsetFinder = new SubsetFinder(
$collection,
$subsetCollection,
SubsetFinderConfig::forLargeDatasets()
);
// For performance (64MB memory, lazy evaluation disabled)
$subsetFinder = new SubsetFinder(
$collection,
$subsetCollection,
SubsetFinderConfig::forPerformance()
);
// For balanced approach (256MB memory, lazy evaluation enabled)
$subsetFinder = new SubsetFinder(
$collection,
$subsetCollection,
SubsetFinderConfig::forBalanced()
);
use Ozdemir\SubsetFinder\Facades\SubsetFinder;
// Create with default configuration
$subsetFinder = SubsetFinder::create($collection, $subsetCollection);
// Create with specific profile
$subsetFinder = SubsetFinder::forLargeDatasets($collection, $subsetCollection);
$subsetFinder = SubsetFinder::forPerformance($collection, $subsetCollection);
use Ozdemir\SubsetFinder\Traits\HasSubsetOperations;
class ProductCollection extends Collection
{
use HasSubsetOperations;
}
$products = new ProductCollection([...]);
// Find subsets directly on the collection
$subsetFinder = $products->findSubsets($subsetCollection);
// Use profiles
$subsetFinder = $products->findSubsetsWithProfile($subsetCollection, 'large_datasets');
// Check feasibility
if ($products->canSatisfySubsets($subsetCollection)) {
// Proceed with subset creation
}
$products = collect([
new Product(id: 1, quantity: 100, price: 10), // T-shirt
new Product(id: 2, quantity: 50, price: 5), // Socks
new Product(id: 3, quantity: 25, price: 20), // Hat
]);
$bundles = new SubsetCollection([
Subset::of([1, 2])->take(2), // T-shirt + Socks bundle
Subset::of([1, 3])->take(1), // T-shirt + Hat bundle
]);
$subsetFinder = new SubsetFinder($products, $bundles);
$subsetFinder->solve();
// Create 25 T-shirt + Socks bundles
// Create 25 T-shirt + Hat bundles
// Remaining: 25 T-shirts, 0 socks, 0 hats
$inventory = collect([
new Item(id: 'A', quantity: 100, category: 'electronics'),
new Item(id: 'B', quantity: 200, category: 'clothing'),
new Item(id: 'C', quantity: 150, category: 'books'),
]);
$orders = new SubsetCollection([
Subset::of(['A', 'B'])->take(10), // Electronics + Clothing order
Subset::of(['C'])->take(5), // Books order
]);
$subsetFinder = new SubsetFinder($inventory, $orders);
$subsetFinder->solve();
use Psr\Log\LoggerInterface;
class CustomLogger implements LoggerInterface
{
// Implement logger methods
}
$subsetFinder = new SubsetFinder(
$collection,
$subsetCollection,
$config,
new CustomLogger()
);
// Check memory before processing
if (memory_get_usage(true) > $config->maxMemoryUsage) {
throw new \Exception('Insufficient memory for processing');
}
// Process in batches for very large datasets
$batchSize = 1000;
foreach ($collection->chunk($batchSize) as $batch) {
// Process batch
}