Skip to content

Commit

Permalink
fix(poule): toNrOfPlaces
Browse files Browse the repository at this point in the history
  • Loading branch information
thepercival committed Aug 12, 2024
1 parent b299d6b commit 626fb0b
Show file tree
Hide file tree
Showing 36 changed files with 830 additions and 796 deletions.
164 changes: 86 additions & 78 deletions composer.lock

Large diffs are not rendered by default.

33 changes: 20 additions & 13 deletions domain/Combinations/HomeAwayBalancer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use SportsPlanning\Combinations\PlaceCombination;
use SportsPlanning\Counters\CounterForPlace;
use SportsPlanning\Counters\Maps\PlaceCounterMap;
use SportsPlanning\Counters\Maps\Schedule\HomeCounterMap;
use SportsPlanning\Counters\Maps\Schedule\RangedPlaceCombinationCounterMap;
use SportsPlanning\Counters\Maps\Schedule\RangedPlaceCounterMap;
use SportsPlanning\Counters\Maps\Schedule\SideCounterMap;
Expand Down Expand Up @@ -172,6 +171,7 @@ private function addHomeAwaysToExisting(
$newSportHomeAways[] = $bestSSportHomeAway;
$homeCounterMap->addHomeAway($bestSSportHomeAway);
$awayCounterMap->addHomeAway($bestSSportHomeAway);
(new HomeAwayOutput())->outputHomeAways($newSportHomeAways, " while loop");
}
// $homeCounterMap->output($this->logger, '', 'ha assigned home totals');
// $awayCounterMap->output($this->logger, '', 'ha assigned away totals');
Expand Down Expand Up @@ -570,6 +570,7 @@ protected function getBestHomeAway(
foreach( $sportHomeAwaysToAssign as $homeAway) {

$homeCount = $this->countPlaceCombination($assignedHomeCounterMap, $homeAway->getHome());
$highestHomeCountSinglePlace = $this->calculateHighestSinglePlaceCount($assignedHomeCounterMap, $homeAway->getHome());
$count = $homeCount + $this->countPlaceCombination($assignedAwayCounterMap, $homeAway->getHome());

if( $bestHomeAway === null || $homeCount < $lowestHomeCount) {
Expand All @@ -583,14 +584,20 @@ protected function getBestHomeAway(

$swappedHomeAway = $homeAway->swap();
$swappedHomeCount = $this->countPlaceCombination($assignedHomeCounterMap, $swappedHomeAway->getHome());
$swappedHighestHomeCountSinglePlace = $this->calculateHighestSinglePlaceCount($assignedHomeCounterMap, $swappedHomeAway->getHome());
$swappedCount = $swappedHomeCount + $this->countPlaceCombination($assignedAwayCounterMap, $swappedHomeAway->getHome());

if( $swappedHomeCount < $lowestHomeCount) {
$lowestHomeCount = $swappedHomeCount;
$highestCount = $swappedCount;
$bestHomeAway = $homeAway;
$swapped = true;
} else if( $swappedHomeCount === $lowestHomeCount && $swappedHomeCount > $highestCount) {
} else if( $swappedHomeCount === $lowestHomeCount && $swappedCount > $highestCount) {
$highestCount = $swappedHomeCount;
$bestHomeAway = $homeAway;
$swapped = true;
} else if( $swappedHomeCount === $lowestHomeCount && $swappedCount === $highestCount
&& $highestHomeCountSinglePlace > $swappedHighestHomeCountSinglePlace) {
$highestCount = $swappedHomeCount;
$bestHomeAway = $homeAway;
$swapped = true;
Expand Down Expand Up @@ -632,17 +639,17 @@ protected function countPlaceCombination(PlaceCounterMap $placeCounterMap, Place
// return $lowestCount;
// }

// protected function getHighestPlaceCount(PlaceCounterMap $placeCounterMap, PlaceCombination $placeCombination): int {
//
// $highestCount = 0;
// foreach( $placeCombination->getPlaces() as $place) {
// $count = $placeCounterMap->count($place);
// if( $highestCount === 0 || $count > $highestCount ) {
// $highestCount = $count;
// }
// }
// return $highestCount;
// }
protected function calculateHighestSinglePlaceCount(PlaceCounterMap $placeCounterMap, PlaceCombination $placeCombination): int {

$highestCount = 0;
foreach( $placeCombination->getPlaces() as $place) {
$count = $placeCounterMap->count($place);
if( $highestCount === 0 || $count > $highestCount ) {
$highestCount = $count;
}
}
return $highestCount;
}

private function getHomeDifference(PlaceCounterMap $homePlaceCounterMap, HomeAway $sportHomeAway): int {
$homeDiff = $this->countPlaceCombination($homePlaceCounterMap, $sportHomeAway->getHome())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
/**
* @implements Iterator<string|int, PlaceCombination>
*/
class PlaceCombinationIterator implements Iterator
class PlaceNrCombinationIterator implements Iterator
{
/**
* @var list<PlaceIterator>
* @var list<PlaceNrIterator>
*/
protected array $placeIterators;
protected array $placeNrIterators;
protected int $nrOfIncrements = 1;

/**
Expand All @@ -27,21 +27,21 @@ class PlaceCombinationIterator implements Iterator
*/
public function __construct(Poule $poule, array $startPlaces, protected int $maxNrOfIncrements)
{
$this->placeIterators = array_map(fn (Place $place) => new PlaceIterator($poule, $place->getPlaceNr()), $startPlaces);
$this->placeNrIterators = array_map(fn (Place $place) => new PlaceNrIterator($poule, $place->getPlaceNr()), $startPlaces);
}

public function current(): PlaceCombination
{
$places = array_map(fn (PlaceIterator $placeIterator) => $placeIterator->current(), $this->placeIterators);
$placeNrs = array_map(fn (PlaceNrIterator $placeIterator) => $placeIterator->current(), $this->placeNrIterators);
return new PlaceCombination($places);
}

public function next(): void
{
$this->nrOfIncrements++;
foreach ($this->placeIterators as $placeIterator) {
foreach ($this->placeNrIterators as $placeNrIterator) {
// for( $i = 0 ; $i < $this->delta ;$i++) {
$placeIterator->next();
$placeNrIterator->next();
// }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/**
* @implements Iterator<int, Place>
*/
class PlaceIterator implements Iterator
class PlaceNrIterator implements Iterator
{
private int $current;

Expand Down
33 changes: 17 additions & 16 deletions domain/Combinations/StatisticsCalculator/Against/GamesPerPlace.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
use SportsScheduler\Combinations\StatisticsCalculator;
use SportsScheduler\Combinations\StatisticsCalculator\Against\GamesPerPlace as GppStatisticsCalculator;
use SportsPlanning\Place;
use SportsPlanning\SportVariant\WithPoule\Against\GamesPerPlace as AgainstGppWithPoule;
// use SportsPlanning\SportVariant\WithPoule\Against\GamesPerPlace as AgainstGppWithPoule;
use SportsHelpers\Sport\Variant\WithNrOfPlaces\Against\GamesPerPlace as AgainstGppWithNrOfPlaces;

class GamesPerPlace extends StatisticsCalculator
{
protected bool $checkOnWith;

public function __construct(
protected AgainstGppWithPoule $againstGppWithPoule,
protected AgainstGppWithNrOfPlaces $againstGppWithNrOfPlaces,
RangedPlaceCounterMap $rangedHomeCounterMap,
int $nrOfHomeAwaysAssigned,
protected RangedPlaceCounterMap $rangedAmountCounterMap,
Expand All @@ -30,11 +31,11 @@ public function __construct(
)
{
parent::__construct($rangedHomeCounterMap,$nrOfHomeAwaysAssigned, $logger);
$this->checkOnWith = $againstGppWithPoule->getSportVariant()->hasMultipleSidePlaces();
$this->checkOnWith = $againstGppWithNrOfPlaces->getSportVariant()->hasMultipleSidePlaces();
}

public function getNrOfGamesToGo(): int {
return $this->againstGppWithPoule->getTotalNrOfGames() - $this->getNrOfHomeAwaysAssigned();
return $this->againstGppWithNrOfPlaces->getTotalNrOfGames() - $this->getNrOfHomeAwaysAssigned();
}

public function addHomeAway(HomeAway $homeAway): self
Expand All @@ -52,7 +53,7 @@ public function addHomeAway(HomeAway $homeAway): self
$rangedHomeCounterMap->addHomeAway($homeAway);

return new self(
$this->againstGppWithPoule,
$this->againstGppWithNrOfPlaces,
$rangedHomeCounterMap,
$this->nrOfHomeAwaysAssigned + 1,
$rangedAmountCounterMap,
Expand All @@ -64,7 +65,7 @@ public function addHomeAway(HomeAway $homeAway): self

public function allAssigned(): bool
{
if ($this->nrOfHomeAwaysAssigned < $this->againstGppWithPoule->getTotalNrOfGames()) {
if ($this->nrOfHomeAwaysAssigned < $this->againstGppWithNrOfPlaces->getTotalNrOfGames()) {
return false;
}

Expand Down Expand Up @@ -130,7 +131,7 @@ private function amountWithinMarginHelper(int|null $minimalAllowedDifference = n
}

$nrOfGamesToGo = $this->getNrOfGamesToGo();
$nrOfPlacesGo = $nrOfGamesToGo * $this->againstGppWithPoule->getSportVariant()->getNrOfGamePlaces();
$nrOfPlacesGo = $nrOfGamesToGo * $this->againstGppWithNrOfPlaces->getSportVariant()->getNrOfGamePlaces();
if( $this->rangedAmountCounterMap->withinRange($nrOfPlacesGo) ) {
return true;
}
Expand Down Expand Up @@ -164,7 +165,7 @@ private function againstWithinMarginHelper(int|null $minimalAllowedDifference =
}

$nrOfGamesToGo = $this->getNrOfGamesToGo();
$nrOfPlaceCombinationsToGo = $nrOfGamesToGo * $this->againstGppWithPoule->getSportVariant()->getNrOfAgainstCombinationsPerGame();
$nrOfPlaceCombinationsToGo = $nrOfGamesToGo * $this->againstGppWithNrOfPlaces->getSportVariant()->getNrOfAgainstCombinationsPerGame();
if( $this->rangedAgainstCounterMap->withinRange($nrOfPlaceCombinationsToGo) ) {
return true;
}
Expand Down Expand Up @@ -418,19 +419,19 @@ public function outputAgainstTotals(string $prefix, bool $withDetails): void {
if( !$withDetails ) {
return;
}
foreach( $this->againstGppWithPoule->getPoule()->getPlaces() as $place ) {
$this->outputAgainstPlaceTotals($place, $prefix . ' ');
for( $placeNr = 1 ; $placeNr <= $this->againstGppWithNrOfPlaces->getNrOfPlaces() ; $placeNr++ ) {
$this->outputAgainstPlaceTotals($placeNr, $prefix . ' ');
}
}

private function outputAgainstPlaceTotals(Place $place, string $prefix): void {
$placeNr = $place->getPlaceNr() < 10 ? '0' . $place->getPlaceNr() : $place->getPlaceNr();
$out = $placeNr . " => ";
foreach( $this->againstGppWithPoule->getPoule()->getPlaces() as $opponent ) {
if( $opponent->getPlaceNr() <= $place->getPlaceNr() ) {
private function outputAgainstPlaceTotals(int $placeNr, string $prefix): void {
$placeNrOutput = $placeNr < 10 ? '0' . $placeNr : $placeNr;
$out = $placeNrOutput . " => ";
for( $opponentNr = 1 ; $opponentNr <= $this->againstGppWithNrOfPlaces->getNrOfPlaces() ; $opponentNr++ ) {
if( $opponentNr <= $placeNr) {
$out .= ' ,';
} else {
$opponentNr = $opponent->getPlaceNr() < 10 ? '0' . $opponent->getPlaceNr() : $opponent->getPlaceNr();
$opponentNrOutput = $opponentNr < 10 ? '0' . $opponentNr : $opponentNr;
$placeCombination = new PlaceCombination([$place, $opponent]);
$out .= '' . $opponentNr . ':' . $this->getOutputAmount($placeCombination) . ',';
}
Expand Down
8 changes: 4 additions & 4 deletions domain/Game/PreAssignSorter.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,12 @@ protected function initMultiplierMap(Input $input): int
$this->muliplierMap = [];
foreach ($input->getSports() as $sport) {
$sportVariant = $sport->createVariant();
$sportVariantWithLargestPoule = (new VariantCreator())->createWithPoule($maxNrOfPlaces, $sportVariant);
$maxNrOfGamePlacesPerBatch = $sportVariantWithLargestPoule->getNrOfGamePlacesPerBatch();
$sportVariantWithLargestNrOfPlaces = (new VariantCreator())->createWithNrOfPlaces($maxNrOfPlaces, $sportVariant);
$maxNrOfGamePlacesPerBatch = $sportVariantWithLargestNrOfPlaces->getNrOfGamePlacesPerBatch();
$this->muliplierMap[$sport->getNumber()] = [];
foreach ($input->getPoules() as $poule) {
$variantWithPoule = (new VariantCreator())->createWithPoule(count($poule->getPlaces()), $sportVariant);
$nrOfGamePlacesPerBatch = $variantWithPoule->getNrOfGamePlacesPerBatch();
$variantWithNrOfPlaces = (new VariantCreator())->createWithNrOfPlaces(count($poule->getPlaces()), $sportVariant);
$nrOfGamePlacesPerBatch = $variantWithNrOfPlaces->getNrOfGamePlacesPerBatch();
// $nrOfGameRoundsPoule = $sportVariant->getNrOfGameRounds($poule->getPlaces()->count());
$this->muliplierMap[$sport->getNumber()][$poule->getNumber()] = $maxNrOfGamePlacesPerBatch / $nrOfGamePlacesPerBatch;
}
Expand Down
39 changes: 18 additions & 21 deletions domain/GameRound/Creator/Against/GamesPerPlace.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Psr\Log\LoggerInterface;
use SportsHelpers\Against\Side;
use SportsHelpers\Sport\Variant\Against\GamesPerPlace as AgainstGpp;
use SportsHelpers\Sport\Variant\WithPoule\Against\EquallyAssignCalculator;
use SportsHelpers\Sport\Variant\WithNrOfPlaces\Against\EquallyAssignCalculator;
use SportsPlanning\Combinations\Amount\Range as AmountRange;
use SportsPlanning\Combinations\CombinationMapper;
use SportsPlanning\Combinations\HomeAway;
Expand All @@ -25,7 +25,7 @@
use SportsScheduler\GameRound\Creator\Against as AgainstCreator;
use SportsPlanning\Counters\CounterForPlace;
use SportsPlanning\Poule;
use SportsPlanning\SportVariant\WithPoule\Against\GamesPerPlace as AgainstGppWithPoule;
use SportsHelpers\Sport\Variant\WithNrOfPlaces\Against\GamesPerPlace as AgainstGppWithNrOfPlaces;

class GamesPerPlace extends AgainstCreator
{
Expand All @@ -40,7 +40,7 @@ public function __construct(LoggerInterface $logger)
}

public function createGameRound(
Poule $poule,
int $nrOfPlaces,
AgainstGpp $againstGpp,
GppHomeAwayCreator $homeAwayCreator,
AllScheduleMaps $allScheduleMaps,
Expand All @@ -53,26 +53,26 @@ public function createGameRound(
if( $nrOfSecondsBeforeTimeout > 0 ) {
$this->timeoutDateTime = (new \DateTimeImmutable())->add(new \DateInterval('PT' . $nrOfSecondsBeforeTimeout . 'S'));
}
$variantWithPoule = new AgainstGppWithPoule($poule, $againstGpp);
$againstGppWithNrOfPlaces = new AgainstGppWithNrOfPlaces($nrOfPlaces, $againstGpp);
$gameRound = new AgainstGameRound();
$this->highestGameRoundNumberCompleted = 0;
$this->nrOfGamesPerGameRound = $variantWithPoule->getNrOfGamesSimultaneously();
$this->nrOfGamesPerGameRound = $againstGppWithNrOfPlaces->getNrOfGamesSimultaneously();

$homeAways = $this->createHomeAways($homeAwayCreator, $poule, $againstGpp);
$homeAways = $this->createHomeAways($homeAwayCreator, $nrOfPlaces, $againstGpp);
$homeAways = $this->initHomeAways($homeAways);

$calculator = new EquallyAssignCalculator();
if( $calculator->assignAgainstSportsEqually( count($poule->getPlaces()), [$againstGpp] ) ) {

}
// $calculator = new EquallyAssignCalculator();
// if( $calculator->assignAgainstSportsEqually( count($poule->getPlaces()), [$againstGpp] ) ) {
//
// }
$rangedAmountCounterMap = new RangedPlaceCounterMap($allScheduleMaps->getAmountCounterMap(),$amountRange);
$rangedWithCounterMap = new RangedPlaceCombinationCounterMap($allScheduleMaps->getWithCounterMap(),$withAmountRange);
$rangedAgainstCounterMap = new RangedPlaceCombinationCounterMap($allScheduleMaps->getAgainstCounterMap(),$againstAmountRange);
$rangedHomeCounterMap = new RangedPlaceCounterMap($allScheduleMaps->getHomeCounterMap(), $homeAmountRange);
$rangedAwayCounterMap = new RangedPlaceCounterMap($allScheduleMaps->getAwayCounterMap(), $homeAmountRange);

$statisticsCalculator = new GppStatisticsCalculator(
$variantWithPoule,
$againstGppWithNrOfPlaces,
$rangedHomeCounterMap,
0,
$rangedAmountCounterMap,
Expand All @@ -94,7 +94,7 @@ public function createGameRound(
$statisticsCalculator,
$gameRound
) === false) {
throw new NoSolutionException('creation of homeaway can not be false', E_ERROR);
throw new NoSolutionException('gameRounds could not created, all possibilities tried', E_ERROR);
}
$homeAwayBalancer = new HomeAwayBalancer($this->logger);

Expand All @@ -111,7 +111,7 @@ public function createGameRound(
}

/**
* @param AgainstGppWithPoule $againstWithPoule
* @param AgainstGppWithNrOfPlaces $againstWithNrOfPlaces
* @param list<HomeAway> $homeAwaysForGameRound
* @param list<HomeAway> $homeAways
* @param GppStatisticsCalculator $statisticsCalculator
Expand All @@ -120,7 +120,7 @@ public function createGameRound(
* @return bool
*/
protected function assignGameRound(
AgainstGppWithPoule $againstWithPoule,
AgainstGppWithNrOfPlaces $againstWithNrOfPlaces,
array $homeAwaysForGameRound,
array $homeAways,
GppStatisticsCalculator $statisticsCalculator,
Expand Down Expand Up @@ -279,20 +279,17 @@ function (HomeAway $homeAway) use ($gameRound): bool {

/**
* @param GppHomeAwayCreator $homeAwayCreator
* @param Poule $poule
* @param AgainstGpp $sportVariant
* @param AgainstGppWithNrOfPlaces $againstGppWithNrOfPlaces
* @return list<HomeAway>
*/
protected function createHomeAways(
GppHomeAwayCreator $homeAwayCreator,
Poule $poule,
AgainstGpp $sportVariant): array
AgainstGppWithNrOfPlaces $againstGppWithNrOfPlaces): array
{
$variantWithPoule = (new AgainstGppWithPoule($poule, $sportVariant));
$totalNrOfGames = $variantWithPoule->getTotalNrOfGames();
$totalNrOfGames = $againstGppWithNrOfPlaces->getTotalNrOfGames();
$homeAways = [];
while ( count($homeAways) < $totalNrOfGames ) {
$homeAways = array_merge($homeAways, $homeAwayCreator->create($variantWithPoule));
$homeAways = array_merge($homeAways, $homeAwayCreator->create($againstGppWithNrOfPlaces));
}
return $homeAways;
}
Expand Down
Loading

0 comments on commit 626fb0b

Please sign in to comment.