Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A7 #44

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
200 changes: 200 additions & 0 deletions App/osbasics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
<?php

echo "osbasics\n";

//problem 1

assert(TRUE);

//problem 2

function tddStringEquals($expected,$result){
if (!($expected===$result)){
return FALSE;
}
return TRUE;
}

assert(tddStringEquals("one","one"));


//assert(tddAssertStringEquals("one","two"));


//problem 3

function AssertEquals($expected,$result){
if($expected!==$result){
throw new Exception("Assert Equals failed");
}
return TRUE;
}

function AssertNotEquals($expected,$result){
if($expected===$result){
throw new Exception("Assert Not Equals Failed");
}
return TRUE;
}

AssertEquals("one","one");
//AssertEquals("one","two");

AssertNotEquals("one","two");
//AssertNotEquals("one","one");

//problem 4

$failed=FALSE;
try{
AssertEquals("one","two");
} catch (Exception $e){
$failed=TRUE;
}
assert($failed);

$failed=FALSE;
try{
AssertNotEquals("one","one");
} catch (Exception $e){
$failed=TRUE;
}
assert($failed);

//problem 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

function AssertContainsString($needle,$haystack){
if (strpos($haystack,$needle)===FALSE){
throw new Exception("AssertContainsString failed");
}
}
AssertContainsString("needle","haystack needle");
//AssertContainsString("needle","nada");

//AssertContainsString("needle","nada");
$failed=FALSE;
try{
//AssertContainsString("needle","needle");
AssertContainsString("needle","HAY");
} catch (Exception $e){
$failed=TRUE;
}
assert($failed);

//echo "passed\n";

//Problem 6b

function AssertContains($needle,$hay){
foreach($hay as $h)
if (strpos($h,$needle)!==FALSE){
return TRUE;
}
throw new Exception("AssertContains failed");
}
AssertContains("needle",array("needle hay","hay needle"));
//AssertContains("needle",array("hay hay","hay hay hay"));

$failed=FALSE;
try{
AssertContains("needle",array("hay","hay no sharp objects"));
} catch (Exception $e){
$failed=TRUE;
}
assert($failed);

//Problem 7

exec("\\WebIS\\bin\\OSSolverService.exe -h",$output,$result);

AssertEquals(0,$result);

//print_r ($output);

$failed=FALSE;
try{
AssertContains("OS Version X.Y.Z",$output);
//AssertContains("OS Version: 2.",$output);
} catch (Exception $e){
$failed=TRUE;
}
assert($failed);

AssertContains("OS Version: 2.",$output);

//problem 8

function write($file){
$osil=new SimpleXMLElement('<osil/>');
$osil -> addchild('instanceHeader');
return $osil-> asXML($file);
}

write('test.xml');
$xxml=file_get_contents('test.xml');

//print_r($xxml);
//print_r(osil());
AssertEquals($xxml,osil());


//Problem 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('numberOfObjCoef',0);
$osil -> asXML('test.xml');

assert(solve());


//problem 10


function solution(){
$xml=file_get_contents('solution.xml');
//print_r($xml);
$osrl=new simpleXMLElement($xml);
//print_r($osrl);
$result=(string)$osrl -> optimization->solution->status->attributes()->type();
//print_r($result);
$value=(double)$osrl->optimization->solution->objectives->values->obj;
//print_r($value);
return $value;
//AssertEquals("optimal",$result);
//problem 11
//AssertContains("PARSER ERROR",$xml);
}
//solution();
//assertEquals(solution(),0);

//problem 12



echo "done";

?>
2 changes: 2 additions & 0 deletions App/test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0"?>
<osil><instanceHeader/><instanceData><objectives><obj numberOfObjCoef="0"/></objectives></instanceData></osil>
19 changes: 11 additions & 8 deletions WCS/os.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ class OS {
static $DEBUG=FALSE;
static $solver="\\WebIS\\bin\OSSolverService.exe";
static $tmp="\\WebIS\\tmp\\"; // trailing slash required.

private $linear=FALSE;

private $osil=NULL;
private $osrl=NULL;
private $var=array(); // Reverse IDX mapping ($idx->$name).
private $value=NULL; // Solution value.
private $solution=NULL;

private $linear=FALSE;

function __construct($maxOrMin='min') {
function __construct($maxOrMin='max') {
$osil=new \SimpleXMLElement('<osil/>');
$osil->addChild('instanceHeader')->addChild('name',php_uname('n').' '.date('c'));
$data=$osil->addChild('instanceData');
Expand Down Expand Up @@ -42,7 +43,7 @@ function getName(){
return (string)$this->osrl->general->instanceName;
}

function addVariable($name,$type=null){
function addVariable($name,$type=NULL){
$variables=$this->osil->instanceData->variables; // shortcut
$this->var[$name]=$variables->var->count(); // $name to $idx (zero based -- preinsert)
$var=$variables->addChild('var');
Expand Down Expand Up @@ -72,7 +73,7 @@ function addObjCoef($name,$value){
$obj['numberOfObjCoef']=$obj->coef->count();
}

function addConstraint($ub=null,$lb=null){
function addConstraint($lb,$ub){
$constraints=$this->osil->instanceData->constraints;
$con=$constraints->addChild('con');
if(!is_null($lb)){
Expand Down Expand Up @@ -135,7 +136,8 @@ function solve(){
$this->osrl=new \SimpleXMLElement($xml);
$result=(string)$this->osrl->optimization->solution->status->attributes()->type;
if($result!=='optimal'){
return FALSE;
$this->solution=NULL;
return $result;
}

// save values to array
Expand All @@ -148,11 +150,12 @@ function solve(){
if(self::$DEBUG) print (float)$var."\n";
}

return (double)$this->osrl->optimization->solution->objectives->values->obj;
$this->solution=(double)$this->osrl->optimization->solution->objectives->values->obj;
return TRUE;
}

function getSolution(){
return (double)$solution->objectives->values->obj;
return $this->solution;
}

}
Expand Down
18 changes: 11 additions & 7 deletions WCS/osTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,33 @@ function testOS() {

function testSolver(){
$os=new WebIS\OS();
$this->assertEquals(0.0,$os->solve(),"Solve empty problem");
$this->assertTrue($os->solve(),"Solve empty problem");
$this->assertEquals(0.0,$os->getSolution(),"Solution to empty problem is 0.0");
$this->assertContains(date('Y-m-d'),$os->getName(),"Solved today");
$os->addVariable('x1');
$this->assertEquals(0.0,$os->solve(),"Solve problem with only one variable");
$this->assertTrue($os->solve(),"Solve problem with only one variable");
$this->assertEquals(0.0,$os->getSolution(),"Solution to one variable is 0.0");
$this->assertEquals(0.0,$os->getVariable('x1'),"x1 is zero");
$os->addObjCoef('x1', '-1');
$this->assertEquals(0.0,$os->solve());
$this->assertEquals("unbounded",$os->solve(),"Problem is unbounded");
$this->assertEquals(0.0,$os->getSolution());
$os->addVariable('x2');
$os->addObjCoef('x2', '-2');
$os->addConstraint(40);
$os->addConstraint(NULL,40);
$os->addConstraintCoef('x1',1);
$os->addConstraintCoef('x2',1);
$os->addConstraint(60);
$os->addConstraint(NULL,60);
$os->addConstraintCoef('x1',2);
$os->addConstraintCoef('x2',1);
$this->assertEquals(-80.0,$os->solve());
$this->assertTrue($os->solve());
$this->assertEquals(-80.0,$os->getSolution());
$this->assertEquals(0,$os->getVariable('x1'));
$this->assertEquals(40,$os->getVariable('x2'));

## test LB
$os=new WebIS\OS();
$os->addVariable('x1');
$os->addConstraint(NULL,40);
$os->addConstraint(40,NULL);
$this->assertContains('lb=',$os->getOsil());

}
Expand Down
28 changes: 28 additions & 0 deletions Web/ExamRestart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

$capacity= array("s1" => 600, "s2" => 300, "s3" => 200);
//print_r($capacity);

$demand=array("d1"=>600, "d2"=>200, "d3"=>300);
//print_r($demand);

$profit=array("d1"=>20, "d2"=>30, "d3"=>40);
//print_r($profit['d1']);

$distance=array("s1,d1"=>2, "s1,d2"=>3, "s1,d3"=>3,"s2,d1"=>5, "s2,d2"=>2, "s2,d3"=>4, "s3,d1"=>3, "s3,d2"=>2, "s3,d3"=>8);
//print_r($distance['s1,d1']);

$a=$demand['d1']*$profit['d1'];
//print_r($a);
$b=$demand['d2']*$profit['d2'];
$c=$demand['d3']*$profit['d3'];

$revenue=$a+$b+$c;
//print_r($revenue);

//300 from s1 to d1

//300 from s1 to d3


?>
Loading