mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-01-13 18:48:40 +00:00
Implement tests for knapsack and job scheduling
This commit is contained in:
parent
df838d1dee
commit
87975e16f1
35
tests/Algorithm/JobScheduling/JobTest.php
Normal file
35
tests/Algorithm/JobScheduling/JobTest.php
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
/**
|
||||
* Orange Management
|
||||
*
|
||||
* PHP Version 7.4
|
||||
*
|
||||
* @package tests
|
||||
* @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;
|
||||
|
||||
use phpOMS\Algorithm\JobScheduling\Job;
|
||||
|
||||
/**
|
||||
* @testdox phpOMS\Algorithm\JobScheduling\Job: Test the job for the JobScheduling implementations
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
class JobTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function testDefault() : void
|
||||
{
|
||||
$item = new Job(3.0, new \DateTime('now'), null, 'abc');
|
||||
|
||||
self::assertEquals(3.0, $item->getValue());
|
||||
self::assertEquals((new \DateTime('now'))->format('Y-m-d'), $item->getStart()->format('Y-m-d'));
|
||||
self::assertEquals(null, $item->getEnd());
|
||||
self::assertEquals('abc', $item->getName());
|
||||
}
|
||||
}
|
||||
52
tests/Algorithm/JobScheduling/WeightedTest.php
Normal file
52
tests/Algorithm/JobScheduling/WeightedTest.php
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
<?php
|
||||
/**
|
||||
* Orange Management
|
||||
*
|
||||
* PHP Version 7.4
|
||||
*
|
||||
* @package tests
|
||||
* @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;
|
||||
|
||||
use phpOMS\Algorithm\JobScheduling\Weighted;
|
||||
|
||||
/**
|
||||
* @testdox phpOMS\Algorithm\JobScheduling\Weighted: Test the job for the JobScheduling implementations
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
class WeightedTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function testDefault() : void
|
||||
{
|
||||
$jobs = [
|
||||
new Job(20, new \DateTime('2003-01-01'), new \DateTime('2010-01-01'), 'A'),
|
||||
new Job(50, new \DateTime('2001-01-01'), new \DateTime('2002-01-01'), 'B'),
|
||||
new Job(100, new \DateTime('2006-01-01'), new \DateTime('2019-01-01'), 'C'),
|
||||
new Job(200, new \DateTime('2002-01-01'), new \DateTime('2100-01-01'), 'D'),
|
||||
];
|
||||
|
||||
$filtered = WeighteD::solve($jobs);
|
||||
|
||||
$value = 0;
|
||||
$names = [];
|
||||
|
||||
foreach ($filtered as $job) {
|
||||
$value += $job->getValue();
|
||||
$names[] = $job->getName();
|
||||
}
|
||||
|
||||
self::assertEqualsWithDelta(250, $value, 0.01);
|
||||
|
||||
self::assertTrue(
|
||||
\in_array('B', $names)
|
||||
&& \in_array('D', $names)
|
||||
);
|
||||
}
|
||||
}
|
||||
47
tests/Algorithm/Knapsack/BackpackTest.php
Normal file
47
tests/Algorithm/Knapsack/BackpackTest.php
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
<?php
|
||||
/**
|
||||
* Orange Management
|
||||
*
|
||||
* PHP Version 7.4
|
||||
*
|
||||
* @package tests
|
||||
* @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;
|
||||
|
||||
use phpOMS\Algorithm\Knapsack\Backpack;
|
||||
|
||||
/**
|
||||
* @testdox phpOMS\Algorithm\Knapsack\Backpack: Test the backpack for the Knapsack implementations
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
class BackpackTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function testDefault() : void
|
||||
{
|
||||
$backpack = new Backpack(3.0);
|
||||
|
||||
self::assertEquals(3.0, $backpack->getMaxCost());
|
||||
self::assertEquals(0.0, $backpack->getValue());
|
||||
self::assertEquals(0.0, $backpack->getCost());
|
||||
self::assertEquals([], $backpack->getItems());
|
||||
}
|
||||
|
||||
public function testGetSet() : void
|
||||
{
|
||||
$backpack = new Backpack(3.0);
|
||||
$backpack->addItem(new Item(2, 1), 2);
|
||||
$backpack->addItem(new Item(2, 1), 1);
|
||||
|
||||
self::assertEquals(3.0, $backpack->getMaxCost());
|
||||
self::assertEquals(6.0, $backpack->getValue());
|
||||
self::assertEquals(3.0, $backpack->getCost());
|
||||
self::assertEquals(2, \count($backpack->getItems()));
|
||||
}
|
||||
}
|
||||
81
tests/Algorithm/Knapsack/BoundedTest.php
Normal file
81
tests/Algorithm/Knapsack/BoundedTest.php
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
<?php
|
||||
/**
|
||||
* Orange Management
|
||||
*
|
||||
* PHP Version 7.4
|
||||
*
|
||||
* @package tests
|
||||
* @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;
|
||||
|
||||
use phpOMS\Algorithm\Knapsack\Bounded;
|
||||
|
||||
/**
|
||||
* @testdox phpOMS\Algorithm\Knapsack\Bounded: Test the continuous Knapsack implementations
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
class BoundedTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function testBackpacking() : void
|
||||
{
|
||||
$items = [
|
||||
['item' => new Item(150, 9, 'map'), 'quantity' => 1],
|
||||
['item' => new Item(35, 13, 'compass'), 'quantity' => 1],
|
||||
['item' => new Item(200, 153, 'water'), 'quantity' => 2],
|
||||
['item' => new Item(60, 50, 'sandwich'), 'quantity' => 2],
|
||||
['item' => new Item(60, 15, 'glucose'), 'quantity' => 2],
|
||||
['item' => new Item(45, 68, 'tin'), 'quantity' => 3],
|
||||
['item' => new Item(60, 27, 'banana'), 'quantity' => 3],
|
||||
['item' => new Item(40, 39, 'apple'), 'quantity' => 3],
|
||||
['item' => new Item(30, 23, 'cheese'), 'quantity' => 1],
|
||||
['item' => new Item(10, 52, 'beer'), 'quantity' => 3],
|
||||
['item' => new Item(70, 11, 'suntan_cream'), 'quantity' => 1],
|
||||
['item' => new Item(30, 32, 'camera'), 'quantity' => 1],
|
||||
['item' => new Item(15, 24, 'Tshirt'), 'quantity' => 2],
|
||||
['item' => new Item(10, 48, 'trousers'), 'quantity' => 2],
|
||||
['item' => new Item(40, 73, 'umbrella'), 'quantity' => 1],
|
||||
['item' => new Item(70, 42, 'waterproof_trousers'), 'quantity' => 1],
|
||||
['item' => new Item(75, 43, 'waterproof_overclothes'), 'quantity' => 1],
|
||||
['item' => new Item(80, 22, 'note_case'), 'quantity' => 1],
|
||||
['item' => new Item(20, 7, 'sunglasses'), 'quantity' => 1],
|
||||
['item' => new Item(12, 18, 'towel'), 'quantity' => 2],
|
||||
['item' => new Item(50, 4, 'socks'), 'quantity' => 1],
|
||||
['item' => new Item(10, 30, 'book'), 'quantity' => 2],
|
||||
];
|
||||
|
||||
$backpack = new Backpack(400.0);
|
||||
$modifiedBackpack = Bounded::solve($items, $backpack);
|
||||
|
||||
self::assertEquals(1010, $modifiedBackpack->getValue());
|
||||
self::assertEquals(396, $modifiedBackpack->getCost());
|
||||
|
||||
$backpackItems = $modifiedBackpack->getItems();
|
||||
self::assertEquals(11, \count($backpackItems));
|
||||
|
||||
$names = [];
|
||||
foreach ($backpackItems as $backpackItem) {
|
||||
$names[] = $backpackItem['item']->getName();
|
||||
}
|
||||
|
||||
self::assertTrue(
|
||||
\in_array('map', $names)
|
||||
&& \in_array('compass', $names)
|
||||
&& \in_array('water', $names)
|
||||
&& \in_array('glucose', $names)
|
||||
&& \in_array('banana', $names)
|
||||
&& \in_array('cheese', $names)
|
||||
&& \in_array('suntan_cream', $names)
|
||||
&& \in_array('waterproof_overclothes', $names)
|
||||
&& \in_array('note_case', $names)
|
||||
&& \in_array('sunglasses', $names)
|
||||
&& \in_array('socks', $names)
|
||||
);
|
||||
}
|
||||
}
|
||||
99
tests/Algorithm/Knapsack/ContinuousTest.php
Normal file
99
tests/Algorithm/Knapsack/ContinuousTest.php
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
<?php
|
||||
/**
|
||||
* Orange Management
|
||||
*
|
||||
* PHP Version 7.4
|
||||
*
|
||||
* @package tests
|
||||
* @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;
|
||||
|
||||
use phpOMS\Algorithm\Knapsack\Continuous;
|
||||
|
||||
/**
|
||||
* @testdox phpOMS\Algorithm\Knapsack\Continuous: Test the continuous Knapsack implementations
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
class ContinuousTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function testBackpacking() : void
|
||||
{
|
||||
$items = [
|
||||
['item' => new Item(36.0, 3.8, 'beef'), 'quantity' => 1],
|
||||
['item' => new Item(43.0, 5.4, 'pork'), 'quantity' => 1],
|
||||
['item' => new Item(90.0, 3.6, 'ham'), 'quantity' => 1],
|
||||
['item' => new Item(45.0, 2.4, 'greaves'), 'quantity' => 1],
|
||||
['item' => new Item(30.0, 4.0, 'flitch'), 'quantity' => 1],
|
||||
['item' => new Item(56.0, 2.5, 'brawn'), 'quantity' => 1],
|
||||
['item' => new Item(67.0, 3.7, 'welt'), 'quantity' => 1],
|
||||
['item' => new Item(95.0, 3.0, 'salami'), 'quantity' => 1],
|
||||
['item' => new Item(98.0, 5.9, 'sausage'), 'quantity' => 1],
|
||||
];
|
||||
|
||||
$backpack = new Backpack(15.0);
|
||||
$modifiedBackpack = Continuous::solve($items, $backpack);
|
||||
|
||||
self::assertEquals(95 + 90 + 56 + 45 + 3.5 / 3.7 * 67, $modifiedBackpack->getValue());
|
||||
self::assertEquals(3.0 + 3.6 + 2.5 + 2.4 + 3.5, $modifiedBackpack->getCost());
|
||||
|
||||
$backpackItems = $modifiedBackpack->getItems();
|
||||
self::assertEquals(5, \count($backpackItems));
|
||||
|
||||
$names = [];
|
||||
foreach ($backpackItems as $backpackItem) {
|
||||
$names[] = $backpackItem['item']->getName();
|
||||
}
|
||||
|
||||
self::assertTrue(
|
||||
\in_array('salami', $names)
|
||||
&& \in_array('ham', $names)
|
||||
&& \in_array('brawn', $names)
|
||||
&& \in_array('greaves', $names)
|
||||
&& \in_array('welt', $names)
|
||||
);
|
||||
}
|
||||
|
||||
public function testBackpackingAlternative() : void
|
||||
{
|
||||
$items = [
|
||||
['item' => new Item(36.0 / 3.8, 1, 'beef'), 'quantity' => 3.8],
|
||||
['item' => new Item(43.0 / 5.4, 1, 'pork'), 'quantity' => 5.4],
|
||||
['item' => new Item(90.0 / 3.6, 1, 'ham'), 'quantity' => 3.6],
|
||||
['item' => new Item(45.0 / 2.4, 1, 'greaves'), 'quantity' => 2.4],
|
||||
['item' => new Item(30.0 / 4.0, 1, 'flitch'), 'quantity' => 4.0],
|
||||
['item' => new Item(56.0 / 2.5, 1, 'brawn'), 'quantity' => 2.5],
|
||||
['item' => new Item(67.0 / 3.7, 1, 'welt'), 'quantity' => 3.7],
|
||||
['item' => new Item(95.0 / 3.0, 1, 'salami'), 'quantity' => 3.0],
|
||||
['item' => new Item(98.0 / 5.9, 1, 'sausage'), 'quantity' => 5.9],
|
||||
];
|
||||
|
||||
$backpack = new Backpack(15.0);
|
||||
$modifiedBackpack = Continuous::solve($items, $backpack);
|
||||
|
||||
self::assertEquals(95 + 90 + 56 + 45 + 3.5 / 3.7 * 67, $modifiedBackpack->getValue());
|
||||
self::assertEquals(3.0 + 3.6 + 2.5 + 2.4 + 3.5, $modifiedBackpack->getCost());
|
||||
|
||||
$backpackItems = $modifiedBackpack->getItems();
|
||||
self::assertEquals(5, \count($backpackItems));
|
||||
|
||||
$names = [];
|
||||
foreach ($backpackItems as $backpackItem) {
|
||||
$names[] = $backpackItem['item']->getName();
|
||||
}
|
||||
|
||||
self::assertTrue(
|
||||
\in_array('salami', $names)
|
||||
&& \in_array('ham', $names)
|
||||
&& \in_array('brawn', $names)
|
||||
&& \in_array('greaves', $names)
|
||||
&& \in_array('welt', $names)
|
||||
);
|
||||
}
|
||||
}
|
||||
34
tests/Algorithm/Knapsack/ItemTest.php
Normal file
34
tests/Algorithm/Knapsack/ItemTest.php
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
/**
|
||||
* Orange Management
|
||||
*
|
||||
* PHP Version 7.4
|
||||
*
|
||||
* @package tests
|
||||
* @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;
|
||||
|
||||
use phpOMS\Algorithm\Knapsack\Item;
|
||||
|
||||
/**
|
||||
* @testdox phpOMS\Algorithm\Knapsack\Item: Test the item for the Knapsack implementations
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
class ItemTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function testDefault() : void
|
||||
{
|
||||
$item = new Item(3.0, 2.0, 'abc');
|
||||
|
||||
self::assertEquals(3.0, $item->getValue());
|
||||
self::assertEquals(2.0, $item->getCost());
|
||||
self::assertEquals('abc', $item->getName());
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user