These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace drupol\phpartition\Algorithm; |
||
4 | |||
5 | use drupol\phpartition\BasePartitionAlgorithm; |
||
6 | use drupol\phpartition\Partition; |
||
7 | use drupol\phpartition\PartitionAlgorithmInterface; |
||
8 | use drupol\phpermutations\Generators\Permutations; |
||
9 | use Oefenweb\Statistics\Statistics; |
||
10 | |||
11 | /** |
||
12 | * Class BruteForceCustomA. |
||
13 | * |
||
14 | * @package drupol\phpartition\Algorithm |
||
15 | */ |
||
16 | class BruteForceCustomA extends BasePartitionAlgorithm implements PartitionAlgorithmInterface { |
||
17 | |||
18 | /** |
||
19 | * {@inheritdoc} |
||
20 | */ |
||
21 | 5 | public function getResult() { |
|
22 | // Sort the initial data set ascending. |
||
23 | 5 | $this->getDataPartition()->sortByValue('ASC'); |
|
24 | // Compute the maximum value of the variance. |
||
25 | 5 | $variance = pow(2, $this->getDataPartition()->size()); |
|
26 | // Set a default value for the variable that will contain the solution. |
||
27 | 5 | $solution = NULL; |
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
28 | // Get the number of elements in the input dataset. |
||
29 | 5 | $count = $this->getDataPartition()->size(); |
|
30 | // Compute the size of a chunk. Ceiling the value because it cannot be zero. |
||
31 | 5 | $chunkSize = ceil($count / $this->getSize()); |
|
32 | // Get the rest of the division of the element count by the number of |
||
33 | // partitions. |
||
34 | 5 | $rest = $count % $this->getSize(); |
|
35 | |||
36 | 5 | $permutations = new Permutations($this->getDataPartition()->toArray()); |
|
37 | |||
38 | // Loop through each permutation and |
||
39 | // compute the variance of each subsetchunks. |
||
40 | 5 | $i = 0; |
|
41 | 5 | foreach ($permutations->generator() as $subset) { |
|
42 | 5 | $i++; |
|
43 | // Get the variance of the sums array. |
||
44 | 5 | $varianceCandidate = Statistics::variance( |
|
45 | 5 | array_map(function ($items) { |
|
46 | 5 | $partition = new Partition(); |
|
47 | 5 | $partition->addItems($items); |
|
48 | 5 | return $partition->getWeight(); |
|
49 | 5 | }, array_chunk($subset, $chunkSize) |
|
50 | 5 | ) |
|
51 | 5 | ); |
|
52 | |||
53 | // If we've found a better variance with this subset, store it. |
||
54 | 5 | if ($varianceCandidate < $variance) { |
|
55 | 5 | $variance = $varianceCandidate; |
|
56 | 5 | $solution = $subset; |
|
57 | |||
58 | // If the variance is equal to the size of the set module the number of |
||
59 | // partition that we want, that means that's the best candidate, |
||
60 | // store the value and exit the loop prematurely. |
||
61 | 5 | if ($rest == $varianceCandidate) { |
|
62 | 3 | break; |
|
63 | } |
||
64 | 5 | } |
|
65 | 5 | } |
|
66 | |||
67 | // Store each chunks into a subset in the SubsetContainer. |
||
68 | 5 | foreach (array_chunk($solution, $chunkSize) as $subsetChunks) { |
|
69 | 5 | $this->getPartitionContainer()->insert($this->getPartition()->addItems($subsetChunks)); |
|
70 | 5 | } |
|
71 | |||
72 | 5 | return $this->getPartitionContainer()->getPartitionsItemsArray(); |
|
73 | } |
||
74 | |||
75 | } |
||
76 |