$val){
$allChars .= $key;
}
$rand1 = $allChars[rand(0,5)];
$rand2 = $allChars[rand(0,5)];
while($rand1 == $rand2){
$rand2 = $allChars[rand(0,5)];
}
$packet = $rand1.$rand2;
return $packet;
}
function findNextJunction($second, $NET, $OCCUPIED){ //takes letter of current location
$destination = $NET[$second][rand(0,count($NET[$second])-1)];
$nextJ = $second.$destination;
if(in_array($nextJ, $OCCUPIED)){
return false;
}
else{
return $nextJ;
}
}
//static set up of neural net
$net = array(
"A"=>array("B","C"),
"B"=>array("A","C","D","E"),
"C"=>array("A","B","D","E"),
"D"=>array("B","C","E","F"),
"E"=>array("C","D","B","F"),
"F"=>array("D","E")
);
$packets = array(0); //an array of arrays of packet labels and current locations
$occupied = array(0); //an unorganized array of occupied spaces
//create a random starting packet
//$initial = getRandomPacket($net);
//$iniJunction = findNextJunction($initial[0], $net, $occupied);
$initial = "AF";
$iniJunction = "AB";
$occupied[0] = $iniJunction;
$packets[0] = array($initial,$iniJunction);
//begin loop
for($i=0; $i<=500; $i++){
//create new packet this could be looped a random number of times to simulate changes in stimulation
$newPacket = getRandomPacket($net);
$newJunction = findNextJunction($newPacket[0], $net, $occupied);
if($newJunction != false){
$occupied[] = $newJunction;
$packets[] = array($newPacket,$newJunction);
}
//print_r($packets);
//visit all packets
foreach($packets as $key=>$val){
//check to see if packet has arrived
if($val[0][1]==$val[1][1]){
$arrived++;
unset($packets[$key]);
foreach($occupied as $keyO=>$valO){ //unset occupied
if($valO == $val[1]){
unset($occupied[$keyO]);
}
}
echo $key. "was unset by arrival. ";
}
else{ //if not move packets
if(($nextJunction = findNextJunction($val[1][1], $net, $occupied)) != false){
//packet can move
//unset occupied previous spot
foreach($occupied as $keyO=>$valO){ //unset occupied
if($valO == $val[1]){
unset($occupied[$keyO]);
}
}
//fill occupied with new spot
$occupied[] = $nextJunction;
$packets[$key][1] = $nextJunction;
}
else{ //kill stalled
$lost++;
unset($packets[$key]);
foreach($occupied as $keyO=>$valO){ //unset occupied
if($valO == $val[1]){
unset($occupied[$keyO]);
}
}
echo $key. "was unset by stall. ";
}
} //end of move packets if
} //end of packets foreach
echo "
";
} //end of timing loop
echo "Arrived: ".$arrived;
echo "
Lost: ".$lost;
?>