2016-05-24 14:10:39 +02:00
|
|
|
<?php namespace Moserware\Skills\FactorGraphs;
|
2010-09-18 11:11:44 -04:00
|
|
|
|
2016-05-24 14:10:39 +02:00
|
|
|
use Exception;
|
2010-09-18 11:11:44 -04:00
|
|
|
use Moserware\Skills\Guard;
|
|
|
|
use Moserware\Skills\HashMap;
|
|
|
|
|
2010-09-23 22:14:56 -04:00
|
|
|
abstract class Factor
|
2010-09-18 11:11:44 -04:00
|
|
|
{
|
|
|
|
private $_messages = array();
|
|
|
|
private $_messageToVariableBinding;
|
|
|
|
|
|
|
|
private $_name;
|
|
|
|
private $_variables = array();
|
|
|
|
|
|
|
|
protected function __construct($name)
|
|
|
|
{
|
|
|
|
$this->_name = "Factor[" . $name . "]";
|
2010-09-25 18:25:56 -04:00
|
|
|
$this->_messageToVariableBinding = new HashMap();
|
2010-09-18 11:11:44 -04:00
|
|
|
}
|
|
|
|
|
2010-10-08 21:44:36 -04:00
|
|
|
/**
|
2016-05-31 10:01:06 +02:00
|
|
|
* @return mixed The log-normalization constant of that factor
|
2010-10-08 21:44:36 -04:00
|
|
|
*/
|
2010-09-18 11:11:44 -04:00
|
|
|
public function getLogNormalization()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-10-08 21:44:36 -04:00
|
|
|
/**
|
2016-05-31 10:01:06 +02:00
|
|
|
* @return int The number of messages that the factor has
|
2010-10-08 21:44:36 -04:00
|
|
|
*/
|
2010-09-18 11:11:44 -04:00
|
|
|
public function getNumberOfMessages()
|
|
|
|
{
|
|
|
|
return count($this->_messages);
|
|
|
|
}
|
2016-05-24 14:10:39 +02:00
|
|
|
|
2016-05-24 16:31:21 +02:00
|
|
|
protected function getVariables()
|
2010-09-18 11:11:44 -04:00
|
|
|
{
|
|
|
|
return $this->_variables;
|
|
|
|
}
|
2016-05-24 14:10:39 +02:00
|
|
|
|
2016-05-24 16:31:21 +02:00
|
|
|
protected function getMessages()
|
2010-09-18 11:11:44 -04:00
|
|
|
{
|
|
|
|
return $this->_messages;
|
|
|
|
}
|
|
|
|
|
2010-10-08 21:44:36 -04:00
|
|
|
/**
|
|
|
|
* Update the message and marginal of the i-th variable that the factor is connected to
|
2016-05-24 16:31:21 +02:00
|
|
|
* @param $messageIndex
|
|
|
|
* @throws Exception
|
2010-10-08 21:44:36 -04:00
|
|
|
*/
|
2010-09-18 11:11:44 -04:00
|
|
|
public function updateMessageIndex($messageIndex)
|
|
|
|
{
|
|
|
|
Guard::argumentIsValidIndex($messageIndex, count($this->_messages), "messageIndex");
|
2016-05-24 16:31:21 +02:00
|
|
|
$message = $this->_messages[$messageIndex];
|
|
|
|
$variable = $this->_messageToVariableBinding->getValue($message);
|
2010-09-28 08:12:06 -04:00
|
|
|
return $this->updateMessageVariable($message, $variable);
|
2010-09-18 11:11:44 -04:00
|
|
|
}
|
|
|
|
|
2010-09-25 10:15:51 -04:00
|
|
|
protected function updateMessageVariable(Message $message, Variable $variable)
|
2010-09-18 11:11:44 -04:00
|
|
|
{
|
|
|
|
throw new Exception();
|
|
|
|
}
|
|
|
|
|
2010-10-08 21:44:36 -04:00
|
|
|
/**
|
|
|
|
* Resets the marginal of the variables a factor is connected to
|
|
|
|
*/
|
2010-09-18 11:11:44 -04:00
|
|
|
public function resetMarginals()
|
|
|
|
{
|
2016-05-24 16:31:21 +02:00
|
|
|
$allValues = $this->_messageToVariableBinding->getAllValues();
|
|
|
|
foreach ($allValues as $currentVariable) {
|
2010-09-18 11:11:44 -04:00
|
|
|
$currentVariable->resetToPrior();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-08 21:44:36 -04:00
|
|
|
/**
|
|
|
|
* Sends the ith message to the marginal and returns the log-normalization constant
|
2016-05-24 16:31:21 +02:00
|
|
|
* @param $messageIndex
|
|
|
|
* @return
|
|
|
|
* @throws Exception
|
2010-10-08 21:44:36 -04:00
|
|
|
*/
|
2010-09-18 11:11:44 -04:00
|
|
|
public function sendMessageIndex($messageIndex)
|
|
|
|
{
|
2010-09-28 08:12:06 -04:00
|
|
|
Guard::argumentIsValidIndex($messageIndex, count($this->_messages), "messageIndex");
|
2010-09-18 11:11:44 -04:00
|
|
|
|
2016-05-24 16:31:21 +02:00
|
|
|
$message = $this->_messages[$messageIndex];
|
|
|
|
$variable = $this->_messageToVariableBinding->getValue($message);
|
2010-09-18 11:11:44 -04:00
|
|
|
return $this->sendMessageVariable($message, $variable);
|
|
|
|
}
|
|
|
|
|
2016-05-24 16:31:21 +02:00
|
|
|
protected abstract function sendMessageVariable(Message $message, Variable $variable);
|
2010-09-18 11:11:44 -04:00
|
|
|
|
2016-05-24 16:31:21 +02:00
|
|
|
public abstract function createVariableToMessageBinding(Variable $variable);
|
2010-09-18 11:11:44 -04:00
|
|
|
|
2016-05-24 16:31:21 +02:00
|
|
|
protected function createVariableToMessageBindingWithMessage(Variable $variable, Message $message)
|
2010-09-18 11:11:44 -04:00
|
|
|
{
|
2010-09-23 22:14:56 -04:00
|
|
|
$this->_messageToVariableBinding->setValue($message, $variable);
|
2016-05-24 16:31:21 +02:00
|
|
|
$this->_messages[] = $message;
|
|
|
|
$this->_variables[] = $variable;
|
2010-09-18 11:11:44 -04:00
|
|
|
return $message;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __toString()
|
|
|
|
{
|
2016-05-31 10:01:06 +02:00
|
|
|
return $this->_name;
|
2010-09-18 11:11:44 -04:00
|
|
|
}
|
2016-05-24 14:10:39 +02:00
|
|
|
}
|