diff --git a/App/a7.php b/App/a7.php new file mode 100644 index 0000000..1d6b933 --- /dev/null +++ b/App/a7.php @@ -0,0 +1,106 @@ +<?php +require_once 'Work-Cell-Scheduler/App/tdd.php'; + +$hours = NULL; +$worker = array(); +$product = array(); +$cell = array(); + +$hours = rand(1,3); +$numWorker = 5; +$numProduct = 6; +$numCell = 4; +$numProductProduced = 15; +$workersTrained = 20; + +$i=NULL; + + +for ($i=1; $i<=5; $i++){ + $worker[]="worker-$i"; +} + +for ($i=1; $i<=4; $i++){ + $cell[]="cell-$i"; +} + +for ($i=1; $i<=6; $i++){ + $product[]="product-$i"; +} + +//print_r($worker); +//print_r($cell); +//print_r($product); + +assertEquals($worker[1],"worker-2"); +assertEquals($cell[1],"cell-2"); +assertEquals($product[1],"product-2"); + +$randomWorker = rand(0,$numWorker-1); +//print_r($worker[$randomWorker]); + +class Demand{ + + public $product; + public $cell; + public $hours; + + function __construct($p, $c, $h){ + $this->hours=$h; + $this->cell=$c; + $this->product=$p; + } + +} + +//create demand +for($i=0;$i<$numProductProduced;$i++){ + $newProduct=array_rand($product,1); + $newCell=array_rand($cell,1); + $demand[]=new Demand($newProduct,$newCell,$hours); +} + +//print_r($demand); + +class training{ + + public $attribute; + + function __get($p){ + return $this->$p; + } + + function __set($p, $prod){ + $this->$p=$prod; + } +} +$t = new training(); +$t->attribute = .80; + +//populate Training Matrix with random productivity +//for($i=0;$i<$workersTrained;$i++){ + //$newWorker=array_rand($worker,1); + //$newCell=array_rand($cell,1); + //$setKey = "${newWorker}_${newCell}"; + //$randomProductivity = rand (50,100)/100; + //$trainingMatrix[$setKey]= $randomProductivity; +//} + +foreach ($worker as $newWorker){ + $newWorker=array_rand($worker,1); + $newCell=array_rand($cell,1); + $setKey = "${newWorker}_${newCell}"; + $randomProductivity = rand (50,100)/100; + $trainingMatrix[$setKey]= $randomProductivity; +} + +print_r($trainingMatrix); + + +//C + + + + + +?> \ No newline at end of file diff --git a/App/examB.php b/App/examB.php new file mode 100644 index 0000000..6e83445 --- /dev/null +++ b/App/examB.php @@ -0,0 +1,87 @@ +<?php +require_once 'Work-Cell-Scheduler/WCS/os.php'; +require_once 'tdd.php'; + +function examB(){ + $os=New WebIS\OS; + /*$os->addVariable('x11'); + $os->addObjCoef('x11','2'); + $os->addVariable('x12'); + $os->addObjCoef('x12','5'); + $os->addVariable('x13'); + $os->addObjCoef('x13','3'); + $os->addVariable('x14'); + $os->addObjCoef('x14','3'); + $os->addVariable('x21'); + $os->addObjCoef('x21','3'); + $os->addVariable('x22'); + $os->addObjCoef('x22','2'); + $os->addVariable('x23'); + $os->addObjCoef('x23','2'); + $os->addVariable('x24'); + $os->addObjCoef('x24','2'); + $os->addVariable('x31'); + $os->addObjCoef('x31','3'); + $os->addVariable('x32'); + $os->addObjCoef('x32','4'); + $os->addVariable('x33'); + $os->addObjCoef('x33','8'); + $os->addVariable('x34'); + $os->addObjCoef('x34','4'); + $os->addVariable('x41'); + $os->addObjCoef('x41','3'); + $os->addVariable('x42'); + $os->addObjCoef('x42','4'); + $os->addVariable('x43'); + $os->addObjCoef('x43','2'); + $os->addVariable('x44'); + $os->addObjCoef('x44','2'); + $os->addVariable('x51'); + $os->addObjCoef('x51','3'); + $os->addVariable('x52'); + $os->addObjCoef('x52','2'); + $os->addVariable('x53'); + $os->addObjCoef('x53','2'); + $os->addVariable('x54'); + $os->addObjCoef('x54','2'); + */ + + $supplier = array(1,2,3,4); + $department = array(1,2,3,4,5); + $shipping = array( + array(2,5,3,3), + array(3,2,2,2), + array(3,4,8,4), + array(3,4,2,2), + array(3,2,2,2), + ); + $capacity = array(600,200,300,500); + $profit = array(20,30,40,25,25); + $demand = array(600,200,300,100,300); + $prodCosts = array(10,14,40,11); + + //print_r($shipping); + //random shipping&suppliers + foreach ($supplier as $newSupplier){ + $newSupplier=array_rand($supplier,1); + $newDepartment=array_rand($department,1); + $setKey = "${newSupplier}_${newDepartment}"; + $shippingMatrix[$setKey]= $shipping; + } + //print_r($shippingMatrix); + + foreach(array_combine($department, $demand) as $dp => $dm){ + $os->addConstraint(NULL, $dp); + + } + foreach(array_combine($supplier, $capacity) as $sup => $cap){ + $os->addConstraint(NULL, $sup); + + } + + //$os->solve(); +} + +examB(); + +?> \ No newline at end of file diff --git a/App/osbasics.php b/App/osbasics.php new file mode 100644 index 0000000..6ebabb3 --- /dev/null +++ b/App/osbasics.php @@ -0,0 +1,161 @@ +<?php +echo "osbasics"; + +// problem 1 +echo "1"; + +assert(TRUE); + +// problem 2 +echo "2"; + +function tddStringEquals($a,$b){ + if (!($a===$b)){ + return FALSE; + } + return TRUE; +} +assert(tddStringEquals("One","One")); + +// problem 3 +echo "3"; + +function assertEquals($expected,$result){ + if ($expected!==$result){ + throw new Exception("Expected string does not match result"); + } + return TRUE; +} + +function assertNotEquals($expected, $result){ + if ($expected===$result){ + throw new Exception("Expected string matches result"); + } + return TRUE; +} + +assertEquals("One","One"); +assertNotEquals("One","Two"); + + +// To trigger Exceptions + +//assertEquals("One","Two"); +//assertNotEquals("One","One"); + + +//problem 4 +echo "4"; + +try { + assertEquals("One", "Two"); +} +catch (Exception $e){ + $failed=TRUE; +} +assert($failed); + +try { + assertNotEquals("One", "One"); +} +catch (Exception $e){ + $failed=TRUE; +} +assert($failed); + + +// problem 5 + +echo "5"; + +$xml = <<<XML +<?xml version="1.0"?> +<osil><instanceHeader></osil> +XML; + + +function osil(){ + $osil = new simpleXMLElement ("<osil/>"); + $osil -> addChild ('instanceHeader'); + return $osil -> asXML(); + +} +//assertEquals($xml,osil()); + + +// problem 6a +echo "6"; + +function assertContainsString($needle, $haystack){ + if(strpos($haystack, $needle)===FALSE){ + throw new Exception("assertContainsString: |$needle |$haystack"); + } + return TRUE; +} + +assertContainsString("one", "one two three"); +//assertContainsString("zero", "one two three"); + +// problem 6b +function assertContains($needle, $hay){ + foreach ($hay as $h) + if(strpos($h, $needle)===FALSE){ + throw new Exception("assertContains: |$needle |$hay"); + } + return TRUE; +} + +assertContains("needle", array("needle hay", "hay needle")); + + +// problem 7 +echo "7"; + +exec("\\WebIS\\bin\\OSsolverservice.exe -h", $output, $result); +print_r($output); + + +assertEquals(0,$result); + +// problem 8 +echo "8"; + +function write($file){ + $osil = new simpleXMLElement ("<osil/>"); + $osil -> addChild ('instanceHeader'); + return $osil -> asXML($file); +} +write("test.xml"); +//read file +$xml = file_get_contents("test.xml"); +assertEquals($xml,osil()); + +// problem 9 +echo "9"; + +function solve(){ + exec("\\WebIS\\bin\\OSsolverservice.exe -osil test.xml -osrl solution.xml", $output, $result); + if($result !==0){ + throw new Exception("Solve function failed!\n".implode("\n",$output)); + } + return TRUE; +} +$osil = new simpleXMLElement ('<osil/>'); +$osil -> addChild("instanceHeader"); +$osil -> addChild("instanceData") -> addChild ("objectives") -> addChild("obj") -> addAttribute("objCoeffiencts", 0); +$osil -> asXML('test.xml'); +assert(solve()); + +//problem 10 +echo "10"; + +function solution(){ + $xml = file_get_contents(asXML('solution.xml')); + $osrl = new simpleXMLElement($xml); + $result = (string)$osrl -> Optimization -> solution -> status -> attributes() -> type(); + $value = (double)$osrl -> Optimization -> solution -> objectives -> values -> obj; + return $value; +} +echo "11"; + +?> \ No newline at end of file diff --git a/App/personAppA_3.php b/App/personAppA_3.php new file mode 100644 index 0000000..836bd70 --- /dev/null +++ b/App/personAppA_3.php @@ -0,0 +1,219 @@ +<?php +namespace WCS; + +class PersonApp { + private $person=NULL; + + function add(Person $person){ + $this->person=$person; + return TRUE; + } + + function process($page){ + $this->load(); + $this->save(); + echo $this->edit($page); + } + + function get(){ + if($this->person===NULL){ + $this->person=new Person(); + } + if(!$this->person->setPerson($_REQUEST['person'])){ + //print ":PersonApp.process: unable to set person |".$_REQUEST['person']."|"); + return FALSE; + } + if(isset($_REQUEST['name']) and !$this->person->setName($_REQUEST['name'])){ + //print ":PersonApp.process: unable to set person |".$_REQUEST['name']."|"); + return FALSE; + } + } + + function load(){ + if(!isset($_REQUEST['action'])){ + return FALSE; + } + if($_REQUEST['action']!='Load'){ + return FALSE; + } + $this->get(); + if($this->person->read()===FALSE){ + return FALSE; + } + return TRUE; + } + + function save(){ + if($this->person===NULL){ + $this->person=new Person(); + } + if(!isset($_REQUEST['action'])){ + return FALSE; + } + if($_REQUEST['action']!='Update'){ + return FALSE; + } + $this->get(); + if($this->person->delete()===FALSE){ + print ":PersonApp.save: unable to delete()"; + return FALSE; + } + if($this->person->write()===FALSE){ + print ":PersonApp.save: unable to write()"; + return FALSE; + } + return TRUE; + } + + function edit($action){ + $person=htmlspecialchars($this->person->getPerson()); + $name=htmlspecialchars($this->person->getName()); + return <<<HTML + <form action="$action" method="GET"> + <table border="1"> + <tr><td>Person</td><td><input type="text" name="person" value="$person"></td></tr> + <tr><td>Name</td> <td><input type="text" name="name" value="$name"></td></tr> + </table> + <input type="submit" name="action" value="Update"> + <input type="submit" name="action" value="Load"> + </form> +HTML; + } +} + + +class Person { + private $person=NULL; + private $name=NULL; + private $rate=NULL; + + /** + * Database Handle + * @var \mysqli + */ + static $db=NULL; + + function __construct(){ + if(!is_null(self::$db)){ + return; + } + self::$db = @new \mysqli(\WCS\Config::$dbhost,\WCS\Config::$dbuser,\WCS\Config::$dbpassword,\WCS\Config::$dbdatabase); + if(self::$db->connect_error){ + throw new \Exception("Error unable to connect to database: ".self::$db->connect_error); + } + } + + function display(){ + $str="{person: $this->person"; + if(!is_null($this->name)){ + $str.=" name: $this->name"; + } + return $str.'}'; + } + + /** + * Set person + * @param string $person Alphanumeric username [a-zA-Z0-9] + * @return bool person set. + */ + function setPerson($person){ + //print ":Person.setPerson: |$person|".gettype($person); + if(preg_match('/^[a-zA-Z0-9]+$/',$person)){ + $this->person=$person; + return TRUE; + } + return FALSE; + } + + /** + * Set Person name + * @param string $name of person. + */ + function setName($name){ + if(preg_match('/^\s*$/',$name)){ + return FALSE; + } + $this->name=$name; + return TRUE; + } + + function getName(){ + return $this->name; + } + + function getPerson(){ + return $this->person; + } + + function write(){ + $stmt=self::$db->prepare("INSERT INTO Person (person,name) VALUES (?,?)"); + if($stmt===FALSE){ + die("Person.write: unable to create statement " . self::$db->error); + return FALSE; + } + if($stmt->bind_param("ss",$this->person,$this->name)===FALSE){ + die("Person.write: unable to bind " . self::$db->error); + } + if($stmt->execute()===FALSE){ + if($stmt->errno==1062){ // Duplicate Entry + $stmt->close(); + self::$db->close(); + return FALSE; + } + die("Person.write: unable to execute self::$db->errno self::$db->error"); + return FALSE; + } + $stmt->close(); + return TRUE; + } + + /** + * Remove Person + * @return bool TRUE on success (even if record did not exist); + */ + function delete(){ + $stmt=self::$db->prepare("DELETE FROM Person WHERE person=?"); + if($stmt===FALSE){ + die("WCS/Person.delete> stmt:".self::$db->error); + return FALSE; + } + if($stmt->bind_param('s',$this->person)===FALSE){ + die("WCS/Person.delete> bind_param:".self::$db->error); + return FALSE; + } + if($stmt->execute()===FALSE){ + die("WCS/Person.delete> execute:".self::$db->errno." ".self::$db->error); + return FALSE; + } + return TRUE; + } + + function read() { + $stmt=self::$db->prepare("SELECT name,rate FROM Person WHERE person=?"); + if($stmt===FALSE){ + die("Person.get: unable to create statement " . self::$db->error); + return FALSE; + } + if($stmt->bind_param("s",$this->person)===FALSE){ + die("Person.get: unable to bind_param " . self::$db->error); + return FALSE; + } + if($stmt->bind_result($this->name,$this->rate)===FALSE){ + die("Person.get: unable to bind_result " . self::$db->error); + return FALSE; + } + if($stmt->execute()===FALSE){ + die("Person.get: unable to execute self::$db->errno self::$db->error"); + return FALSE; + } + if($stmt->fetch()==FALSE){ + $stmt->close(); + return FALSE; + } + // print "Person.get: ".$this->display(); + return TRUE; + } + +} + +?> diff --git a/App/personTestA_3.php b/App/personTestA_3.php new file mode 100644 index 0000000..de79bd4 --- /dev/null +++ b/App/personTestA_3.php @@ -0,0 +1,9 @@ +<?php + + + + + + + +?> \ No newline at end of file diff --git a/App/solution b/App/solution new file mode 100644 index 0000000..63983fd --- /dev/null +++ b/App/solution @@ -0,0 +1,12 @@ +<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="http://www.coin-or.org/OS/stylesheets/OSrL.xslt"?> +<osrl xmlns="os.optimizationservices.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="os.optimizationservices.org http://www.optimizationservices.org/schemas/2.0/OSrL.xsd" > +<general> +<generalStatus type="error"> +</generalStatus> +<message>PARSER ERROR: Input is either not valid or well formed: improperly formed <instanceData> element +Here are 20 characters currently being pointed to in the input string: /osil> + +See line number: 1 +</message> +</general> +</osrl> diff --git a/App/solution.php b/App/solution.php new file mode 100644 index 0000000..6a54063 --- /dev/null +++ b/App/solution.php @@ -0,0 +1,24 @@ +<!DOCTYPE html> +<html> +<body> + +<tr> + <td>300</td> + <td>100</td> + <td>200</td> +</tr> +<tr> + <td>0</td> + <td>200</td> + <td>0</td> +</tr> +<tr> + <td>300</td> + <td>0</td> + <td>0</td> +</tr> +</table> + +</body> +</html> +<?php diff --git a/App/tdd.php b/App/tdd.php new file mode 100644 index 0000000..da1c5a4 --- /dev/null +++ b/App/tdd.php @@ -0,0 +1,91 @@ +<?php +// problem 1 +//echo "1"; + +assert(TRUE); + +// problem 2 +//echo "2"; + +function tddStringEquals($a,$b){ + if (!($a===$b)){ + return FALSE; + } + return TRUE; +} +assert(tddStringEquals("One","One")); + +// problem 3 +//echo "3"; + +function assertEquals($expected,$result){ + if ($expected!==$result){ + throw new Exception("Expected string does not match result"); + } + return TRUE; +} + +function assertNotEquals($expected, $result){ + if ($expected===$result){ + throw new Exception("Expected string matches result"); + } + return TRUE; +} + +assertEquals("One","One"); +assertNotEquals("One","Two"); + + +// To trigger Exceptions + +//assertEquals("One","Two"); +//assertNotEquals("One","One"); + + +//problem 4 +//echo "4"; + +try { + assertEquals("One", "Two"); +} +catch (Exception $e){ + $failed=TRUE; +} +assert($failed); + +try { + assertNotEquals("One", "One"); +} +catch (Exception $e){ + $failed=TRUE; +} +assert($failed); + +// problem 6a +//echo "6"; + +function assertContainsString($needle, $haystack){ + if(strpos($haystack, $needle)===FALSE){ + throw new Exception("assertContainsString: |$needle |$haystack"); + } + return TRUE; +} + +assertContainsString("one", "one two three"); +//assertContainsString("zero", "one two three"); + +// problem 6b +function assertContains($needle, $hay){ + foreach ($hay as $h) + if(strpos($h, $needle)===FALSE){ + throw new Exception("assertContains: |$needle |$hay"); + } + return TRUE; +} + +assertContains("needle", array("needle hay", "hay needle")); + + + + +?> \ No newline at end of file diff --git a/App/test.xml b/App/test.xml new file mode 100644 index 0000000..f7c72a6 --- /dev/null +++ b/App/test.xml @@ -0,0 +1,2 @@ +<?xml version="1.0"?> +<osil><instanceHeader/><instanceData><objectives><obj objCoeffiencts="0"/></objectives></instanceData></osil> diff --git a/App/testxml b/App/testxml new file mode 100644 index 0000000..8efc3ef --- /dev/null +++ b/App/testxml @@ -0,0 +1,2 @@ +<?xml version="1.0"?> +<osil><instanceHeader/></osil> diff --git a/Web/A4osrl.xml b/Web/A4osrl.xml new file mode 100644 index 0000000..b994f53 --- /dev/null +++ b/Web/A4osrl.xml @@ -0,0 +1 @@ +<?xml version="1.0" encoding="UTF-8"?> \ No newline at end of file diff --git a/Web/exampleProblem.php b/Web/exampleProblem.php new file mode 100644 index 0000000..ea6c72b --- /dev/null +++ b/Web/exampleProblem.php @@ -0,0 +1,85 @@ +<?php +// Optimization Services Test Copyright 2014 by WebIS Spring 2014 License Apache 2.0 +require_once 'Work-Cell-Scheduler/TDD/validator.php'; +require_once 'Work-Cell-Scheduler/WCS/os.php'; +include 'Work-Cell-Scheduler/Config/local.php'; + + +class OsTestCase extends WebIS\Validator { + + protected static $__CLASS__=__CLASS__; + + // Basic OSSolverServices test. + function testOS() { + exec(WebIS\OS::$solver." --version",$output,$return); + $this->assertEquals(0,$return); + $this->assertContains("OS Version: 2.",$output[5]); + } + + function testSolver(){ + $os=New WebIS\OS; + //$this->assertEquals(0.0,$os->solve(),"Solve empty problem"); + //$this->assertContains(date('Y-m-d'),$os->getName(),"Solved today"); + $os->addVariable('x11'); + //$this->assertEquals(0.0,$os->solve(),"Solve problem with only one variable"); + //$this->assertEquals(0.0,$os->getVariable('x11'),"x11 is zero"); + $os->addObjCoef('x11', '3'); + //$this->assertEquals(0.0,$os->solve()); + $os->addVariable('x12'); + $os->addObjCoef('x12', '2'); + $os->addVariable('x21'); + $os->addObjCoef('x21', '1'); + $os->addVariable('x22'); + $os->addObjCoef('x22', '5'); + $os->addVariable('x31'); + $os->addObjCoef('x31', '5'); + $os->addVariable('x32'); + $os->addObjCoef('x32', '4'); + + $os->addConstraint(NULL, 45); + $os->addConstraintCoef('x11',1); + $os->addConstraintCoef('x12',1); + + $os->addConstraint(NULL, 60); + $os->addConstraintCoef('x21',1); + $os->addConstraintCoef('x22',1); + + $os->addConstraint(NULL, 35); + $os->addConstraintCoef('x31',1); + $os->addConstraintCoef('x32',1); + + $os->addConstraint(50); + $os->addConstraintCoef('x11',1); + $os->addConstraintCoef('x21',1); + $os->addConstraintCoef('x31',1); + + $os->addConstraint(60); + $os->addConstraintCoef('x12',1); + $os->addConstraintCoef('x22',1); + $os->addConstraintCoef('x32',1); + + //$this->assertEquals(-80.0,$os->solve()); + //$this->assertEquals(0,$os->getVariable('x1')); + //$this->assertEquals(40,$os->getVariable('x2')); + + //$this->assertEquals(350,$os->solve()); + //$this->assertEquals(20,$os->getVariable('x11')); + //$this->assertEquals(20,$os->getVariable('x12')); + //$this->assertEquals(20,$os->getVariable('x21')); + //$this->assertEquals(20,$os->getVariable('x22')); + //$this->assertEquals(10,$os->getVariable('x31')); + //$this->assertEquals(20,$os->getVariable('x32')); + + + + print_r($os->solve()); + + } +} + +if (!defined('PHPUnit_MAIN_METHOD')) { + OsTestCase::main(); +} + +?> +