Cleanup in src/, adding namespaces, removing php closing tag and general code cleanup

This commit is contained in:
Alexander Liljengård
2016-05-24 14:10:39 +02:00
parent 9f97eb1653
commit 5694a2fb30
64 changed files with 891 additions and 1328 deletions

View File

@ -0,0 +1,21 @@
<?php namespace Moserware\Skills\FactorGraphs;
use Exception;
class DefaultVariable extends Variable
{
public function __construct()
{
parent::__construct("Default", null);
}
public function &getValue()
{
return null;
}
public function setValue(&$value)
{
throw new Exception();
}
}

View File

@ -1,11 +1,6 @@
<?php
namespace Moserware\Skills\FactorGraphs;
require_once(dirname(__FILE__) . "/../Guard.php");
require_once(dirname(__FILE__) . "/../HashMap.php");
require_once(dirname(__FILE__) . "/Message.php");
require_once(dirname(__FILE__) . "/Variable.php");
<?php namespace Moserware\Skills\FactorGraphs;
use Exception;
use Moserware\Skills\Guard;
use Moserware\Skills\HashMap;
@ -38,12 +33,12 @@ abstract class Factor
{
return count($this->_messages);
}
protected function &getVariables()
{
return $this->_variables;
}
protected function &getMessages()
{
return $this->_messages;
@ -56,7 +51,7 @@ abstract class Factor
{
Guard::argumentIsValidIndex($messageIndex, count($this->_messages), "messageIndex");
$message = &$this->_messages[$messageIndex];
$variable = &$this->_messageToVariableBinding->getValue($message);
$variable = &$this->_messageToVariableBinding->getValue($message);
return $this->updateMessageVariable($message, $variable);
}
@ -71,8 +66,7 @@ abstract class Factor
public function resetMarginals()
{
$allValues = &$this->_messageToVariableBinding->getAllValues();
foreach ($allValues as &$currentVariable)
{
foreach ($allValues as &$currentVariable) {
$currentVariable->resetToPrior();
}
}
@ -108,6 +102,4 @@ abstract class Factor
{
return ($this->_name != null) ? $this->_name : base::__toString();
}
}
?>
}

View File

@ -1,7 +1,4 @@
<?php
namespace Moserware\Skills\FactorGraphs;
require_once(dirname(__FILE__) . "/VariableFactory.php");
<?php namespace Moserware\Skills\FactorGraphs;
class FactorGraph
{
@ -17,5 +14,4 @@ class FactorGraph
{
$this->_variableFactory = &$factory;
}
}
?>
}

View File

@ -1,9 +1,4 @@
<?php
namespace Moserware\Skills\FactorGraphs;
require_once(dirname(__FILE__) . "/Factor.php");
require_once(dirname(__FILE__) . "/FactorGraph.php");
require_once(dirname(__FILE__) . "/Schedule.php");
<?php namespace Moserware\Skills\FactorGraphs;
abstract class FactorGraphLayer
{
@ -69,6 +64,4 @@ abstract class FactorGraphLayer
{
return null;
}
}
?>
}

View File

@ -1,8 +1,4 @@
<?php
namespace Moserware\Skills\FactorGraphs;
require_once(dirname(__FILE__) . "/Factor.php");
<?php namespace Moserware\Skills\FactorGraphs;
/**
* Helper class for computing the factor graph's normalization constant.
@ -14,8 +10,7 @@ class FactorList
public function getLogNormalization()
{
$list = &$this->_list;
foreach($list as &$currentFactor)
{
foreach ($list as &$currentFactor) {
$currentFactor->resetMarginals();
}
@ -23,22 +18,19 @@ class FactorList
$listCount = count($this->_list);
for ($i = 0; $i < $listCount; $i++)
{
for ($i = 0; $i < $listCount; $i++) {
$f = $this->_list[$i];
$numberOfMessages = $f->getNumberOfMessages();
for ($j = 0; $j < $numberOfMessages; $j++)
{
for ($j = 0; $j < $numberOfMessages; $j++) {
$sumLogZ += $f->sendMessageIndex($j);
}
}
$sumLogS = 0;
foreach($list as &$currentFactor)
{
foreach ($list as &$currentFactor) {
$sumLogS = $sumLogS + $currentFactor->getLogNormalization();
}
@ -55,6 +47,4 @@ class FactorList
$this->_list[] = $factor;
return $factor;
}
}
?>
}

View File

@ -0,0 +1,18 @@
<?php namespace Moserware\Skills\FactorGraphs;
class KeyedVariable extends Variable
{
private $_key;
public function __construct(&$key, $name, &$prior)
{
parent::__construct($name, $prior);
$this->_key = &$key;
}
public function &getKey()
{
$key = &$this->_key;
return $key;
}
}

View File

@ -1,14 +1,13 @@
<?php
namespace Moserware\Skills\FactorGraphs;
<?php namespace Moserware\Skills\FactorGraphs;
class Message
{
private $_name;
private $_name;
private $_value;
public function __construct(&$value = null, $name = null)
{
$this->_name = $name;
$this->_name = $name;
$this->_value = $value;
}
@ -19,14 +18,12 @@ class Message
}
public function setValue(&$value)
{
{
$this->_value = &$value;
}
public function __toString()
{
return $this->_name;
return (string)$this->_name;
}
}
?>
}

View File

@ -1,7 +1,4 @@
<?php
namespace Moserware\Skills\FactorGraphs;
require_once(dirname(__FILE__) . "/Factor.php");
<?php namespace Moserware\Skills\FactorGraphs;
abstract class Schedule
{
@ -18,77 +15,4 @@ abstract class Schedule
{
return $this->_name;
}
}
class ScheduleStep extends Schedule
{
private $_factor;
private $_index;
public function __construct($name, Factor &$factor, $index)
{
parent::__construct($name);
$this->_factor = $factor;
$this->_index = $index;
}
public function visit($depth = -1, $maxDepth = 0)
{
$currentFactor = &$this->_factor;
$delta = $currentFactor->updateMessageIndex($this->_index);
return $delta;
}
}
class ScheduleSequence extends Schedule
{
private $_schedules;
public function __construct($name, array $schedules)
{
parent::__construct($name);
$this->_schedules = $schedules;
}
public function visit($depth = -1, $maxDepth = 0)
{
$maxDelta = 0;
$schedules = &$this->_schedules;
foreach ($schedules as &$currentSchedule)
{
$currentVisit = $currentSchedule->visit($depth + 1, $maxDepth);
$maxDelta = max($currentVisit, $maxDelta);
}
return $maxDelta;
}
}
class ScheduleLoop extends Schedule
{
private $_maxDelta;
private $_scheduleToLoop;
public function __construct($name, Schedule &$scheduleToLoop, $maxDelta)
{
parent::__construct($name);
$this->_scheduleToLoop = $scheduleToLoop;
$this->_maxDelta = $maxDelta;
}
public function visit($depth = -1, $maxDepth = 0)
{
$totalIterations = 1;
$delta = $this->_scheduleToLoop->visit($depth + 1, $maxDepth);
while ($delta > $this->_maxDelta)
{
$delta = $this->_scheduleToLoop->visit($depth + 1, $maxDepth);
$totalIterations++;
}
return $delta;
}
}
?>
}

View File

@ -0,0 +1,26 @@
<?php namespace Moserware\Skills\FactorGraphs;
class ScheduleLoop extends Schedule
{
private $_maxDelta;
private $_scheduleToLoop;
public function __construct($name, Schedule &$scheduleToLoop, $maxDelta)
{
parent::__construct($name);
$this->_scheduleToLoop = $scheduleToLoop;
$this->_maxDelta = $maxDelta;
}
public function visit($depth = -1, $maxDepth = 0)
{
$totalIterations = 1;
$delta = $this->_scheduleToLoop->visit($depth + 1, $maxDepth);
while ($delta > $this->_maxDelta) {
$delta = $this->_scheduleToLoop->visit($depth + 1, $maxDepth);
$totalIterations++;
}
return $delta;
}
}

View File

@ -0,0 +1,25 @@
<?php namespace Moserware\Skills\FactorGraphs;
class ScheduleSequence extends Schedule
{
private $_schedules;
public function __construct($name, array $schedules)
{
parent::__construct($name);
$this->_schedules = $schedules;
}
public function visit($depth = -1, $maxDepth = 0)
{
$maxDelta = 0;
$schedules = &$this->_schedules;
foreach ($schedules as &$currentSchedule) {
$currentVisit = $currentSchedule->visit($depth + 1, $maxDepth);
$maxDelta = max($currentVisit, $maxDelta);
}
return $maxDelta;
}
}

View File

@ -0,0 +1,21 @@
<?php namespace Moserware\Skills\FactorGraphs;
class ScheduleStep extends Schedule
{
private $_factor;
private $_index;
public function __construct($name, Factor &$factor, $index)
{
parent::__construct($name);
$this->_factor = $factor;
$this->_index = $index;
}
public function visit($depth = -1, $maxDepth = 0)
{
$currentFactor = &$this->_factor;
$delta = $currentFactor->updateMessageIndex($this->_index);
return $delta;
}
}

View File

@ -16,12 +16,12 @@ class Variable
public function &getValue()
{
$value = &$this->_value;
$value = &$this->_value;
return $value;
}
public function setValue(&$value)
{
{
$this->_value = &$value;
}
@ -34,40 +34,4 @@ class Variable
{
return $this->_name;
}
}
class DefaultVariable extends Variable
{
public function __construct()
{
parent::__construct("Default", null);
}
public function &getValue()
{
return null;
}
public function setValue(&$value)
{
throw new Exception();
}
}
class KeyedVariable extends Variable
{
private $_key;
public function __construct(&$key, $name, &$prior)
{
parent::__construct($name, $prior);
$this->_key = &$key;
}
public function &getKey()
{
$key = &$this->_key;
return $key;
}
}
?>
}

View File

@ -1,8 +1,4 @@
<?php
namespace Moserware\Skills\FactorGraphs;
require_once(dirname(__FILE__) . "/Variable.php");
<?php namespace Moserware\Skills\FactorGraphs;
class VariableFactory
{
@ -28,5 +24,3 @@ class VariableFactory
return $newVar;
}
}
?>