| 
									
										
										
										
											2022-07-05 15:55:47 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-02 14:53:38 +00:00
										 |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-05 15:55:47 +02:00
										 |  |  | namespace DNW\Skills\FactorGraphs; | 
					
						
							| 
									
										
										
										
											2010-09-18 21:19:51 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-08 21:44:36 -04:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Helper class for computing the factor graph's normalization constant. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2010-09-18 11:11:44 -04:00
										 |  |  | class FactorList | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-08-02 09:36:44 +00:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @var Factor[] $list | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-08-01 14:02:12 +00:00
										 |  |  |     private array $list = []; | 
					
						
							| 
									
										
										
										
											2010-09-18 11:11:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-02 09:36:44 +00:00
										 |  |  |     public function getLogNormalization(): float | 
					
						
							| 
									
										
										
										
											2010-09-18 11:11:44 -04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-08-01 14:02:12 +00:00
										 |  |  |         $list = $this->list; | 
					
						
							| 
									
										
										
										
											2016-05-24 14:10:39 +02:00
										 |  |  |         foreach ($list as &$currentFactor) { | 
					
						
							| 
									
										
										
										
											2010-09-18 11:11:44 -04:00
										 |  |  |             $currentFactor->resetMarginals(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $sumLogZ = 0.0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-01 14:02:12 +00:00
										 |  |  |         $listCount = count($this->list); | 
					
						
							| 
									
										
										
										
											2010-09-18 11:11:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-21 13:48:37 +00:00
										 |  |  |         for ($i = 0; $i < $listCount; ++$i) { | 
					
						
							| 
									
										
										
										
											2023-08-01 14:02:12 +00:00
										 |  |  |             $f = $this->list[$i]; | 
					
						
							| 
									
										
										
										
											2010-09-18 11:11:44 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |             $numberOfMessages = $f->getNumberOfMessages(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-21 13:48:37 +00:00
										 |  |  |             for ($j = 0; $j < $numberOfMessages; ++$j) { | 
					
						
							| 
									
										
										
										
											2010-09-18 11:11:44 -04:00
										 |  |  |                 $sumLogZ += $f->sendMessageIndex($j); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $sumLogS = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-24 14:10:39 +02:00
										 |  |  |         foreach ($list as &$currentFactor) { | 
					
						
							| 
									
										
										
										
											2022-07-05 16:21:06 +02:00
										 |  |  |             $sumLogS += $currentFactor->getLogNormalization(); | 
					
						
							| 
									
										
										
										
											2010-09-18 11:11:44 -04:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $sumLogZ + $sumLogS; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-02 09:36:44 +00:00
										 |  |  |     public function count(): int | 
					
						
							| 
									
										
										
										
											2010-09-18 11:11:44 -04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-08-01 14:02:12 +00:00
										 |  |  |         return count($this->list); | 
					
						
							| 
									
										
										
										
											2010-09-18 11:11:44 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-02 09:36:44 +00:00
										 |  |  |     public function addFactor(Factor $factor): Factor | 
					
						
							| 
									
										
										
										
											2010-09-18 11:11:44 -04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-08-01 14:02:12 +00:00
										 |  |  |         $this->list[] = $factor; | 
					
						
							| 
									
										
										
										
											2022-07-05 15:55:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-18 11:11:44 -04:00
										 |  |  |         return $factor; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-07-05 15:55:47 +02:00
										 |  |  | } |