Read 322 times | Created 2014-05-16 06:10:07 | Updated 2014-05-16 06:11:00 | | |

 

<?php
/*********************************
FILENAME  : fcm.php
CREATE BY  : cahya dsn
PURPOSE   : Fuzzy c-Means Engine
CREATE DATE : 2014-05-16
***********************************/
$data=array(
array('x'=>0,'y'=>0),
array('x'=>0,'y'=>2),
array('x'=>0,'y'=>4),
array('x'=>1,'y'=>1),
array('x'=>1,'y'=>2),
array('x'=>1,'y'=>3),
array('x'=>2,'y'=>2),
array('x'=>3,'y'=>2),
array('x'=>4,'y'=>2),
array('x'=>5,'y'=>1),
array('x'=>5,'y'=>2),
array('x'=>5,'y'=>3),
array('x'=>6,'y'=>0),
array('x'=>6,'y'=>2),
array('x'=>6,'y'=>3)
);
$fuzz=2;
$num_cluster=3;
$max_iter=100;

// Random values 0 - 1
function random_float ($min,$max) {
  return ($min+lcg_value()*(abs($max-$min)));
}
//  Fuzzy C Means Algorithm
function fcm($data, $C,$m)
{
  global $MatrixCentroids,$max_iter;
  $num_of_data = sizeof($data);
  $MatrixPointX = $data;
  // Fill the  $MatrixU table with random values from 0 to 1
  for($j = 0; $j < $num_of_data; $j++)
  {
    $sum = 0;
    for($i = 0; $i < $C; $i++)
    {
      $MatrixU[$j][$i] = random_float(0,1);
      $sum += $MatrixU[$j][$i];
    }
    // Normalize Data
    for($i = 0; $i < $C; $i++)
    {
      $MatrixU[$j][$i] = $MatrixU[$j][$i]/$sum;
    }
  }
  // repeat max_iter
  for($a = 0; $a<$max_iter; $a++)
  {
    for($i = 0; $i < $C; $i++)
    {
      $tempAx = 0;
      $tempBx = 0;
      $tempAy = 0;
      $tempBy = 0;
      for($j = 0 ; $j < $num_of_data; $j++)
      {
        $tempAx = $tempAx + pow($MatrixU[$j][$i],$m);
        $tempBx += pow($MatrixU[$j][$i],$m) * $MatrixPointX[$j]['x'];
        $tempAy = $tempAy + pow($MatrixU[$j][$i],$m);
        $tempBy += pow($MatrixU[$j][$i],$m) * $MatrixPointX[$j]['y'];
      }
      $MatrixCentroids[$i]['x'] = $tempBx / $tempAx;
      $MatrixCentroids[$i]['y'] = $tempBy / $tempAy;
    }
    // Cluster Centers
    for($j = 0 ; $j < $num_of_data; $j++)
    {
      $tempSum = 0;
      for($i = 0; $i < $C; $i++)
      {
        // Distance between 2 points
        $distance1 = pow(($MatrixPointX[$j]['x'] - $MatrixCentroids[$i]['x']),2);
        $distance2 = pow(($MatrixPointX[$j]['y'] - $MatrixCentroids[$i]['y']),2);
        $distance = $distance1 + $distance2;
        $distance = sqrt($distance);
        $MatrixU[$j][$i] = pow(1/$distance , 2/($m-1));
        $tempSum += pow(1/$distance, 2/($m-1));
      }
      for($i = 0; $i < $C; $i++)
      {
        $MatrixU[$j][$i] = ($MatrixU[$j][$i]/$tempSum);
      }
      $sum = 0;
      for($i = 0; $i < $C; $i++)
      {
        $sum += $MatrixU[$j][$i];
      }
      // Norm Data
      for($i = 0; $i < $C; $i++)
      {
        $MatrixU[$j][$i] = $MatrixU[$j][$i]/$sum;
      }
    }
  }
  //print_r($MatrixCentroids);
  return $MatrixU ;
}
$data_in_clusters = fcm($data,$num_cluster,$fuzz);
$size = sizeof($data);
for($i = 0; $i < $size ; $i++)
{
  $max = 0;
  $pos = 0;
  for($j = 0; $j < $num_cluster; $j++)
  {  
    $tmp_point = $data_in_clusters[$i][$j];
    if($tmp_point > $max)
    {
      $max = $tmp_point;
      $pos = $j;
    }  
  }
}
$show_data=array();
foreach($data as $d) $show_data[]='('.$d['x'].','.$d['y'].')';
?>
<!DOCTYPE html>
<html>
  <head>
    <title>protoFCM</title>
    <style type='text/css'>
      * {font-family:verdana,arial,sans-serif;font-size:10pt;}
      h1{font-size:18pt;}
      h2{font-size:14pt;line-height:16pt;}
      fieldset{margin:5px;padding:5px;background-color:#eee;}
      legend {font-weight:bold;padding:5px;background-color:#ee9;}
      .inptxt{text-align:right;}
      .member{background-color:#fcc;font-weight:bold;color:#009;}
    </style>
  </head>
  <body>
    <div id='container'>
      <h2>Sistem Fuzzy Clustering</h2>
      <h1>Metode Fuzzy C-Means</h1>
      <fieldset>
        <legend>Parameter</legend>
         Data  =(<?php echo implode(",",$show_data);?>)<br>
         Jumlah Cluster =<?=$num_cluster?><br>
         Jumlah iterasi =<?=$max_iter?><br>
      </fieldset>
      <fieldset>
        <legend>Hasil</legend>
        <table border='1'>
          <tr><th>points</th>
<?php
for($i = 0; $i < $num_cluster; $i++){
  $xxx = $MatrixCentroids[$i]['x'];
  $yyy = $MatrixCentroids[$i]['y'];
  echo "<th>Cluster $i <br>Centroid:<br>X:".number_format($xxx,3)." Y:".number_format($yyy,3)."</Th>";
}
for($i = 0; $i < $size ; $i++)
{
  $x = $data[$i]['x'];
  $y = $data[$i]['y'];
  echo "<tr align='center'>";
  echo "<td><b>x:$x</b>;<b>y:$y</b></Td>";
  for($j = 0; $j < $num_cluster; $j++)
  {
    $tmp_point = $data_in_clusters[$i][$j];
    echo "<td".($tmp_point==max($data_in_clusters[$i])?" class='member'":"")."> ".number_format($tmp_point,3)."</td>";
  }
}
?>
      </table>
    </fieldset>
  </body>
</html>