Implement interfaces for algorithms

This commit is contained in:
Dennis Eichhorn 2019-10-17 16:08:43 +02:00
parent 7cb47ecea7
commit f5208e95fe
9 changed files with 224 additions and 83 deletions

View File

@ -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
{

View File

@ -0,0 +1,63 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package phpOMS\Algorithm\JobScheduling
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\Algorithm\JobScheduling;
/**
* Job interface.
*
* @package phpOMS\Algorithm\JobScheduling;
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
interface JobInterface
{
/**
* Get value of the job
*
* @return float
*
* @since 1.0.0
*/
public function getValue() : float;
/**
* Get start time of the job
*
* @return \DateTime
*
* @since 1.0.0
*/
public function getStart() : \DateTime;
/**
* Get end time of the job
*
* @return \DateTime
*
* @since 1.0.0
*/
public function getEnd() : ?\DateTime;
/**
* Get the name of the job
*
* @return string
*
* @since 1.0.0
*/
public function getName() : string;
}

View File

@ -37,14 +37,14 @@ final class Weighted
/**
* Sort jobs by end date.
*
* @param Jobs $j1 Job 1
* @param Jobs $j2 Job 2
* @param JobInterface $j1 Job 1
* @param JobInterface $j2 Job 2
*
* @return int
*
* @since 1.0.0
*/
private static function sortByEnd(Job $j1, Job $j2) : int
private static function sortByEnd(JobInterface $j1, JobInterface $j2) : int
{
if ($j1->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
*/

View File

@ -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;

View File

@ -0,0 +1,75 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package phpOMS\Algorithm\Knapsack
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\Algorithm\Knapsack;
/**
* Backpack interface.
*
* @package phpOMS\Algorithm\Knapsack;
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
interface BackpackInterface
{
/**
* Get the value of the stored items
*
* @return float
*
* @since 1.0.0
*/
public function getValue() : float;
/**
* Get the cost of the stored items
*
* @return float
*
* @since 1.0.0
*/
public function getCost() : float;
/**
* Get the max allowed costs for the items
*
* @return float
*
* @since 1.0.0
*/
public function getMaxCost() : float;
/**
* Get items
*
* @return array
*
* @since 1.0.0
*/
public function getItems() : array;
/**
* Add item to backpack
*
* @param ItemInterface $item Item
* @param mixed $quantity Quantity of the item
*
* @return void
*
* @since 1.0.0
*/
public function addItem(ItemInterface $item, $quantity = 1) : void;
}

View File

@ -42,14 +42,14 @@ final class Bounded
*
* This algorithm only works for integer cost, values and quantities!
*
* @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
{
$n = \count($items);
$maxCost = (int) $backpack->getMaxCost();

View File

@ -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();

View File

@ -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
{

View File

@ -0,0 +1,54 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package phpOMS\Algorithm\Knapsack
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\Algorithm\Knapsack;
/**
* Item interface.
*
* @package phpOMS\Algorithm\Knapsack;
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
interface ItemInterface
{
/**
* Get value of the item
*
* @return float
*
* @since 1.0.0
*/
public function getValue() : float;
/**
* Get value of the item
*
* @return float
*
* @since 1.0.0
*/
public function getCost() : float;
/**
* Get the name of the item
*
* @return string
*
* @since 1.0.0
*/
public function getName() : string;
}