Fuzzy Tsukamoto (Part 2)

Posted: November 24, 2014 in Informatika
Tags: , , , ,

Berikut ini contoh source code Fuzzy Tsukamoto dalam  bahasa pemrograman PHP. Untuk studi kasusnya yaitu mencari jumlah kebutuhan obat-obatan dalam satu bulan kedepan.

<?php
error_reporting(0);
include ‘../../config/koneksi.php’;

function prediksi($a, $b)
{

$id_obat = $a; // id_obat yg diprediksi
$tahun = $b; // tahun prediksi
$tahun1 = $tahun-1; // 1 tahun kemarin
$tahun5 = $tahun-4; // 5 tahun kemarin

// stok tahun sekarang
$q0 = mysql_query(“select total_stok as a from trans_dinkes where id_obat=’$id_obat’ and periode=’$tahun'”);
$r0 = mysql_fetch_array($q0);
$x = $r0[a];

// pemakaian tahun sekarang
$q1 = mysql_query(“select total_pakai as a from trans_dinkes where id_obat=’$id_obat’ and periode=’$tahun'”);
$r1 = mysql_fetch_array($q1);
$y = $r1[a];

// kebutuhan tahun kemarin
$q2 = mysql_query(“select total_butuh as a from trans_dinkes where id_obat=’$id_obat’ and periode=’$tahun1′”);
$r2 = mysql_fetch_array($q2);
$z = $r2[a];

// echo “stok: $x – pakai: $y – butuh: $z <br>”;
// echo “<br>”;

$max_stok_temp=mysql_query(“select max(total_stok) as a from trans_dinkes where id_obat=’$id_obat’ and periode between $tahun5 and $tahun”);
$max_stok_temp2=mysql_fetch_array($max_stok_temp);
$max_stok=$max_stok_temp2[a];

// echo “max_stok: $max_stok<br>”;

$min_stok_temp=mysql_query(“select min(total_stok) as a from trans_dinkes where id_obat=’$id_obat’ and periode between $tahun5 and $tahun”);
$min_stok_temp2=mysql_fetch_array($min_stok_temp);
$min_stok=$min_stok_temp2[a];

// echo “min_stok: $min_stok<br>”;

$max_pakai_temp=mysql_query(“select max(total_pakai) as a from trans_dinkes where id_obat=’$id_obat’ and periode between $tahun5 and $tahun”);
$max_pakai_temp2=mysql_fetch_array($max_pakai_temp);
$max_pakai=$max_pakai_temp2[a];

// echo “max_pakai: $max_pakai<br>”;

$min_pakai_temp=mysql_query(“select min(total_pakai) as a from trans_dinkes where id_obat=’$id_obat’ and periode between $tahun5 and $tahun”);
$min_pakai_temp2=mysql_fetch_array($min_pakai_temp);
$min_pakai=$min_pakai_temp2[a];

// echo “min_pakai: $min_pakai<br>”;

$max_butuh_temp=mysql_query(“select max(total_butuh) as a from trans_dinkes where id_obat=’$id_obat’ and periode between $tahun5 and $tahun1”);
$max_butuh_temp2=mysql_fetch_array($max_butuh_temp);
$max_butuh=$max_butuh_temp2[a];

// echo “max_butuh: $max_butuh<br>”;

$min_butuh_temp=mysql_query(“select min(total_butuh) as a from trans_dinkes where id_obat=’$id_obat’ and periode between $tahun5 and $tahun1”);
$min_butuh_temp2=mysql_fetch_array($min_butuh_temp);
$min_butuh=$min_butuh_temp2[a];

// echo “min_butuh: $min_butuh<br>”;

$t_stok=($max_stok+$min_stok)/2;
$t_pakai=($max_pakai+$min_pakai)/2;
$t_butuh=($max_butuh+$min_butuh)/2;

// echo “<br>”;
// echo “t_stok: $t_stok – t_pakai $t_pakai – t_butuh $t_butuh <br>”;

//Fungsi Keanggotaan
//Sisa Stok (x)
if($x<=$min_stok) {
$miu_stok_sedikit=1;
$miu_stok_sedang=0;
$miu_stok_banyak=0;
}
else if (($x>=$min_stok) and ($x<=$max_stok)) {
$miu_stok_sedikit=($max_stok-$x)/($max_stok-$min_stok);
$miu_stok_banyak=($x-$min_stok)/($max_stok-$min_stok);
if ($x==$xt){
$miu_stok_sedang=1;
}
else if (($x>$min_stok) and ($x<$xt)){
$miu_stok_sedang=($x-$min_stok)/($xt-$min_stok);
}
else if (($x>$xt) and ($xt<$max_stok)){
$miu_stok_sedang=($max_stok-$x)/($max_stok-$xt);
}
else if (($x<=$min_stok) or ($x>=$max_stok)){
$miu_stok_sedang=0;
}
}
else if ($x>=$max_stok) {
$miu_stok_sedikit=0;
$miu_stok_sedang=0;
$miu_stok_banyak=1;
}

//Pemakaian Obat (y)
if($y<=$min_pakai){
$miu_pakai_sedikit=1;
$miu_pakai_sedang=0;
$miu_pakai_banyak=0;
}
else if (($y>=$min_pakai) and ($y<=$max_pakai)){
$miu_pakai_sedikit=($max_pakai-$y)/($max_pakai-$min_pakai);
$miu_pakai_banyak=($y-$min_pakai)/($max_pakai-$min_pakai);
if ($y==$yt){
$miu_pakai_sedang=1;
}
else if (($y>$min_pakai) and ($y<$yt)){
$miu_pakai_sedang=($y-$min_pakai)/($yt-$min_pakai);
}
else if (($y>$yt) and ($yt<$max_pakai)){
$miu_pakai_sedang=($max_pakai-$y)/($max_pakai-$yt);
}
else if (($y<=$min_pakai) or ($y>=$max_pakai)){
$miu_pakai_sedang=0;
}
}
else if ($y>=$max_pakai){
$miu_pakai_sedikit=0;
$miu_pakai_sedang=0;
$miu_pakai_banyak=1;
}

//Kebutuhan Obat (z)
if($z<=$min_butuh){
$miu_butuh_sedikit=1;
$miu_butuh_sedang=0;
$miu_butuh_banyak=0;
}
else if (($z>=$min_butuh) and ($z<=$max_butuh)){
$miu_butuh_sedikit=($max_butuh-$z)/($max_butuh-$min_butuh);
$miu_butuh_banyak=($z-$min_butuh)/($max_butuh-$min_butuh);
if ($z==$zt){
$miu_butuh_sedang=1;
}
else if (($z>$min_butuh) and ($z<$zt)){
$miu_butuh_sedang=($z-$min_butuh)/($zt-$min_butuh);
}
else if (($z>$zt) and ($zt<$max_butuh)){
$miu_butuh_sedang=($max_butuh-$z)/($max_butuh-$zt);
}
else if (($z<=$min_butuh) or ($z>=$max_butuh)){
$miu_butuh_sedang=0;
}
}
else if ($z>=$max_butuh){
$miu_butuh_sedikit=0;
$miu_butuh_sedang=0;
$miu_butuh_banyak=1;
}

//Aturan
//[R1] IF sisa stok SEDIKIT AND pemakaian BANYAK THEN kebutuhan BANYAK
$alfa1=min($miu_stok_sedikit, $miu_pakai_banyak);
$z1=$alfa1*($max_butuh-$min_butuh)+$min_butuh;

//[R2] IF sisa stok SEDIKIT AND pemakaian SEDANG THEN kebutuhan BANYAK
$alfa2=min($miu_stok_sedikit, $miu_pakai_sedang);
$z2=$alfa2*($max_butuh-$min_butuh)+$min_butuh;

//[R3] IF sisa stok SEDIKIT AND pemakaian SEDIKIT THEN kebutuhan SEDIKIT
$alfa3=min($miu_stok_sedikit, $miu_pakai_sedikit);
$z3=$max_butuh-$alfa3*($max_butuh-$min_butuh);

//[R4] IF sisa stok SEDANG AND pemakaian BANYAK THEN kebutuhan BANYAK
$alfa4=min($miu_stok_sedang, $miu_pakai_banyak);
$z4=$alfa4*($max_butuh-$min_butuh)+$min_butuh;

//[R5] IF sisa stok SEDANG AND pemakaian SEDANG THEN kebutuhan SEDIKIT
$alfa5=min($miu_stok_sedang, $miu_pakai_sedang);
$z5=$max_butuh-$alfa5*($max_butuh-$min_butuh);

//[R6] IF sisa stok SEDANG AND pemakaian SEDIKIT THEN kebutuhan SEDIKIT
$alfa6=min($miu_stok_sedang, $miu_pakai_sedikit);
$z6=$max_butuh-$alfa6*($max_butuh-$min_butuh);

//[R7] IF sisa stok BANYAK AND pemakaian BANYAK THEN kebutuhan BANYAK
$alfa7=min($miu_stok_banyak, $miu_pakai_banyak);
$z7=$alfa7*($max_butuh-$min_butuh)+$min_butuh;

//[R8] IF sisa stok BANYAK AND pemakaian SEDANG THEN kebutuhan SEDIKIT
$alfa8=min($miu_stok_banyak, $miu_pakai_sedang);
$z8=$max_butuh-$alfa8*($max_butuh-$min_butuh);

//[R9] IF sisa stok BANYAK AND pemakaian SEDIKIT THEN kebutuhan SEDIKIT
$alfa9=min($miu_stok_banyak, $miu_pakai_sedikit);
$z9=$max_butuh-$alfa9*($max_butuh-$min_butuh);

$alfaz1=$alfa1*$z1;
$alfaz2=$alfa2*$z2;
$alfaz3=$alfa3*$z3;
$alfaz4=$alfa4*$z4;
$alfaz5=$alfa5*$z5;
$alfaz6=$alfa6*$z6;
$alfaz7=$alfa7*$z7;
$alfaz8=$alfa8*$z8;
$alfaz9=$alfa9*$z9;

$alfaz_total=$alfaz1+$alfaz2+$alfaz3+$alfaz4+$alfaz5+$alfaz6+$alfaz7+$alfaz8+$alfaz9;

// echo “alfaz_total = $alfaz_total”;
$alfa_total=$alfa1+$alfa2+$alfa3+$alfa4+$alfa5+$alfa6+$alfa7+$alfa8+$alfa9;

// echo “alfa_total = $alfa_total”;
$z=($alfaz_total/$alfa_total);
$hasil = ceil($z);

return $hasil;

$running=set_time_limit(0);
}

$id_obat = $_GET[id];
$tahun = $_GET[tahun];
$hasil_prediksi = prediksi($id_obat, $tahun);

// insert ke database
$simpan_hasil=”UPDATE trans_dinkes SET total_butuh= ‘$_POST[hasil]’ WHERE id_obat=’$_POST[id_obat]’ and periode=’$_POST[tahun]'”;

// echo hasilnya
echo “$hasil_prediksi”;
echo “$simpan_hasil”;?>


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s