From f5208e95fe2fb58602e7589de874f3c07a6598bb Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Thu, 17 Oct 2019 16:08:43 +0200 Subject: [PATCH] Implement interfaces for algorithms --- Algorithm/JobScheduling/Job.php | 26 ++------ Algorithm/JobScheduling/JobInterface.php | 63 ++++++++++++++++++++ Algorithm/JobScheduling/Weighted.php | 14 ++--- Algorithm/Knapsack/Backpack.php | 39 +++--------- Algorithm/Knapsack/BackpackInterface.php | 75 ++++++++++++++++++++++++ Algorithm/Knapsack/Bounded.php | 8 +-- Algorithm/Knapsack/Continuous.php | 8 +-- Algorithm/Knapsack/Item.php | 20 ++----- Algorithm/Knapsack/ItemInterface.php | 54 +++++++++++++++++ 9 files changed, 224 insertions(+), 83 deletions(-) create mode 100644 Algorithm/JobScheduling/JobInterface.php create mode 100644 Algorithm/Knapsack/BackpackInterface.php create mode 100644 Algorithm/Knapsack/ItemInterface.php diff --git a/Algorithm/JobScheduling/Job.php b/Algorithm/JobScheduling/Job.php index 73fda98de..63ff7473e 100644 --- a/Algorithm/JobScheduling/Job.php +++ b/Algorithm/JobScheduling/Job.php @@ -22,7 +22,7 @@ namespace phpOMS\Algorithm\JobScheduling; * @link https://orange-management.org * @since 1.0.0 */ -class Job +class Job implements JobInterface { /** * Value of the job @@ -74,11 +74,7 @@ class Job } /** - * Get value of the job - * - * @return float - * - * @since 1.0.0 + * {@inheritdoc} */ public function getValue() : float { @@ -86,11 +82,7 @@ class Job } /** - * Get start time of the job - * - * @return \DateTime - * - * @since 1.0.0 + * {@inheritdoc} */ public function getStart() : \DateTime { @@ -98,11 +90,7 @@ class Job } /** - * Get end time of the job - * - * @return \DateTime - * - * @since 1.0.0 + * {@inheritdoc} */ public function getEnd() : ?\DateTime { @@ -110,11 +98,7 @@ class Job } /** - * Get the name of the job - * - * @return string - * - * @since 1.0.0 + * {@inheritdoc} */ public function getName() : string { diff --git a/Algorithm/JobScheduling/JobInterface.php b/Algorithm/JobScheduling/JobInterface.php new file mode 100644 index 000000000..a6e13d31e --- /dev/null +++ b/Algorithm/JobScheduling/JobInterface.php @@ -0,0 +1,63 @@ +getEnd() === null && $j2->getEnd() !== null) { return 1; @@ -64,8 +64,8 @@ final class Weighted /** * Search for a none-conflicting job that comes befor a defined job * - * @param Job[] $jobs List of jobs - * @param int $pivot Job to find the previous job to + * @param JobInterface[] $jobs List of jobs + * @param int $pivot Job to find the previous job to * * @return int * @@ -100,9 +100,9 @@ final class Weighted /** * Maximize the value of the job execution without overlapping jobs * - * @param Job[] $jobs Jobs to filter + * @param JobInterface[] $jobs Jobs to filter * - * @return Job[] + * @return JobInterface[] * * @since 1.0.0 */ diff --git a/Algorithm/Knapsack/Backpack.php b/Algorithm/Knapsack/Backpack.php index ec2faa859..f65c808e8 100644 --- a/Algorithm/Knapsack/Backpack.php +++ b/Algorithm/Knapsack/Backpack.php @@ -23,7 +23,7 @@ namespace phpOMS\Algorithm\Knapsack; * @link https://orange-management.org * @since 1.0.0 */ -class Backpack +class Backpack implements BackpackInterface { /** * Maximum amount of cost this backpack can hold @@ -52,7 +52,7 @@ class Backpack /** * Items inside the backpack * - * @var Item[] + * @var ItemInterface[] * @since 1.0.0 */ private array $items = []; @@ -70,11 +70,7 @@ class Backpack } /** - * Get the value of the stored items - * - * @return float - * - * @since 1.0.0 + * {@inheritdoc} */ public function getValue() : float { @@ -82,11 +78,7 @@ class Backpack } /** - * Get the cost of the stored items - * - * @return float - * - * @since 1.0.0 + * {@inheritdoc} */ public function getCost() : float { @@ -94,11 +86,7 @@ class Backpack } /** - * Get the max allowed costs for the items - * - * @return float - * - * @since 1.0.0 + * {@inheritdoc} */ public function getMaxCost() : float { @@ -106,11 +94,7 @@ class Backpack } /** - * Get items - * - * @return array - * - * @since 1.0.0 + * {@inheritdoc} */ public function getItems() : array { @@ -118,16 +102,9 @@ class Backpack } /** - * Add item to backpack - * - * @param Item $item Item - * @param mixed $quantity Quantity of the item - * - * @return void - * - * @since 1.0.0 + * {@inheritdoc} */ - public function addItem(Item $item, $quantity = 1) : void + public function addItem(ItemInterface $item, $quantity = 1) : void { $this->items[] = ['item' => $item, 'quantity' => $quantity]; $this->value += $item->getValue() * $quantity; diff --git a/Algorithm/Knapsack/BackpackInterface.php b/Algorithm/Knapsack/BackpackInterface.php new file mode 100644 index 000000000..eba401541 --- /dev/null +++ b/Algorithm/Knapsack/BackpackInterface.php @@ -0,0 +1,75 @@ + Item, 'quantity' => ?] - * @param Backpack $backpack Backpack to fill + * @param array $items Items to fill the backpack with ['item' => Item, 'quantity' => ?] + * @param BackpackInterface $backpack Backpack to fill * - * @return Backpack + * @return BackpackInterface * * @since 1.0.0 */ - public static function solve(array $items, Backpack $backpack) : Backpack + public static function solve(array $items, BackpackInterface $backpack) : BackpackInterface { $n = \count($items); $maxCost = (int) $backpack->getMaxCost(); diff --git a/Algorithm/Knapsack/Continuous.php b/Algorithm/Knapsack/Continuous.php index 4d0d7d68d..f66666df8 100644 --- a/Algorithm/Knapsack/Continuous.php +++ b/Algorithm/Knapsack/Continuous.php @@ -38,14 +38,14 @@ final class Continuous /** * Fill the backpack with items * - * @param array $items Items to fill the backpack with ['item' => Item, 'quantity' => ?] - * @param Backpack $backpack Backpack to fill + * @param array $items Items to fill the backpack with ['item' => Item, 'quantity' => ?] + * @param BackpackInterface $backpack Backpack to fill * - * @return Backpack + * @return BackpackInterface * * @since 1.0.0 */ - public static function solve(array $items, Backpack $backpack) : Backpack + public static function solve(array $items, BackpackInterface $backpack) : BackpackInterface { usort($items, function($a, $b) { return $a['item']->getValue() / $a['item']->getCost() < $b['item']->getValue() / $b['item']->getCost(); diff --git a/Algorithm/Knapsack/Item.php b/Algorithm/Knapsack/Item.php index 7aff3d41c..411c8bde4 100644 --- a/Algorithm/Knapsack/Item.php +++ b/Algorithm/Knapsack/Item.php @@ -22,7 +22,7 @@ namespace phpOMS\Algorithm\Knapsack; * @link https://orange-management.org * @since 1.0.0 */ -class Item +class Item implements ItemInterface { /** * Value of the item @@ -64,11 +64,7 @@ class Item } /** - * Get value of the item - * - * @return float - * - * @since 1.0.0 + * {@inheritdoc} */ public function getValue() : float { @@ -76,11 +72,7 @@ class Item } /** - * Get value of the item - * - * @return float - * - * @since 1.0.0 + * {@inheritdoc} */ public function getCost() : float { @@ -88,11 +80,7 @@ class Item } /** - * Get the name of the item - * - * @return string - * - * @since 1.0.0 + * {@inheritdoc} */ public function getName() : string { diff --git a/Algorithm/Knapsack/ItemInterface.php b/Algorithm/Knapsack/ItemInterface.php new file mode 100644 index 000000000..fc2bd5c30 --- /dev/null +++ b/Algorithm/Knapsack/ItemInterface.php @@ -0,0 +1,54 @@ +