|
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
|
|
|
/** |
|
20
|
|
|
* {@inheritdoc} |
|
21
|
|
|
*/ |
|
22
|
5 |
|
public function getResult() |
|
23
|
|
|
{ |
|
24
|
|
|
// Sort the initial data set ascending. |
|
25
|
5 |
|
$this->getDataPartition()->sortByValue('ASC'); |
|
26
|
|
|
// Compute the maximum value of the variance. |
|
27
|
5 |
|
$variance = pow(2, $this->getDataPartition()->size()); |
|
28
|
|
|
// Set a default value for the variable that will contain the solution. |
|
29
|
5 |
|
$solution = null; |
|
30
|
|
|
// Get the number of elements in the input dataset. |
|
31
|
5 |
|
$count = $this->getDataPartition()->size(); |
|
32
|
|
|
// Compute the size of a chunk. Ceiling the value because it cannot be zero. |
|
33
|
5 |
|
$chunkSize = ceil($count / $this->getSize()); |
|
34
|
|
|
// Get the rest of the division of the element count by the number of |
|
35
|
|
|
// partitions. |
|
36
|
5 |
|
$rest = $count % $this->getSize(); |
|
37
|
|
|
|
|
38
|
5 |
|
$permutations = new Permutations($this->getDataPartition()->toArray()); |
|
39
|
|
|
|
|
40
|
|
|
// Loop through each permutation and |
|
41
|
|
|
// compute the variance of each subsetchunks. |
|
42
|
5 |
|
$i = 0; |
|
43
|
5 |
|
foreach ($permutations->generator() as $subset) { |
|
44
|
5 |
|
$i++; |
|
45
|
|
|
// Get the variance of the sums array. |
|
46
|
5 |
|
$varianceCandidate = Statistics::variance( |
|
47
|
5 |
|
array_map(function ($items) { |
|
48
|
5 |
|
$partition = new Partition(); |
|
49
|
5 |
|
$partition->addItems($items); |
|
50
|
5 |
|
return $partition->getWeight(); |
|
51
|
5 |
|
}, array_chunk($subset, $chunkSize)) |
|
52
|
5 |
|
); |
|
53
|
|
|
|
|
54
|
|
|
// If we've found a better variance with this subset, store it. |
|
55
|
5 |
|
if ($varianceCandidate < $variance) { |
|
56
|
5 |
|
$variance = $varianceCandidate; |
|
57
|
5 |
|
$solution = $subset; |
|
58
|
|
|
|
|
59
|
|
|
// If the variance is equal to the size of the set module the number of |
|
60
|
|
|
// partition that we want, that means that's the best candidate, |
|
61
|
|
|
// store the value and exit the loop prematurely. |
|
62
|
5 |
|
if ($rest == $varianceCandidate) { |
|
63
|
3 |
|
break; |
|
64
|
|
|
} |
|
65
|
5 |
|
} |
|
66
|
5 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
// Store each chunks into a subset in the SubsetContainer. |
|
69
|
5 |
|
foreach (array_chunk($solution, $chunkSize) as $subsetChunks) { |
|
70
|
5 |
|
$this->getPartitionContainer()->insert($this->getPartition()->addItems($subsetChunks)); |
|
71
|
5 |
|
} |
|
72
|
|
|
|
|
73
|
5 |
|
return $this->getPartitionContainer()->getPartitionsItemsArray(); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|