Arsip Blog

Pemrograman C++ Trafic Light (Lampu Lalu Lintas) dengan Logika Fuzzy


#include<iostream.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<conio.h>
#include<iomanip.h>

void delay(int i) {
int n,m,o;
for(n=0;n<i;n++){
for(m=0;m<i;m++){
for(o=0;o<i;o++) { } } }
}
void main()
{
float acak[5],miu[5],rule[5][5],perbandingan[5][5],lampu[5],alfa[5][5];
int x,y,z,m,hijau[5],merah[5],hijau1[5],kuning[5];
char keterangan[50][50];
srand(time(NULL));
//input default//
for (x=1;x<=4;x++)
{
acak[x]=0;
merah[x]=0;
hijau[x]=0;
}
m=1;
//output//
do{
clrscr();
for(x=1;x<=4;x++){
hijau1[x]=hijau[x]; kuning[x]=5; }

do
{ clrscr();
for(x=1;x<=4;x++){
cout<<" jarak kendaraan sisi "<<x<<" : "<<acak[x]<<" ("<<keterangan[x]<<" )"<<endl;
cout<<" miu : "<<miu[x]<<endl; }
for(x=1;x<=4;x++){
cout<<"\nlampu hijau  "<<x<<" : "<<hijau[x]<<endl;
}
if (m==1)
{merah[4]=hijau1[1]+hijau1[2]+hijau1[3]+13;
}
else if (m==2)
{merah[1]=hijau1[4]+hijau1[2]+hijau1[3]+13;
}
else if (m==3)
{merah[2]=hijau1[1]+hijau1[4]+hijau1[3]+13;
}
else
{merah[3]=hijau1[1]+hijau1[2]+hijau1[4]+13;
}

cout<<setiosflags(ios::right)<<"\nhijau";
cout<<setiosflags(ios::right)<<setw(8)<<hijau1[1];
cout<<setiosflags(ios::right)<<setw(8)<<hijau1[2];
cout<<setiosflags(ios::right)<<setw(8)<<hijau1[3];
cout<<setiosflags(ios::right)<<setw(8)<<hijau1[4]<<endl;
cout<<setiosflags(ios::right)<<"\nkuning";
cout<<setiosflags(ios::right)<<setw(8)<<kuning[1];
cout<<setiosflags(ios::right)<<setw(8)<<kuning[2];
cout<<setiosflags(ios::right)<<setw(8)<<kuning[3];
cout<<setiosflags(ios::right)<<setw(8)<<kuning[4]<<endl;
cout<<setiosflags(ios::right)<<"\nmerah";
cout<<setiosflags(ios::right)<<setw(8)<<merah[1];
cout<<setiosflags(ios::right)<<setw(8)<<merah[2];
cout<<setiosflags(ios::right)<<setw(8)<<merah[3];
cout<<setiosflags(ios::right)<<setw(8)<<merah[4]<<endl;
for(x=1;x<=4;x++)
{merah[x]--;}
merah[m]++;
if ( hijau1[m]<1)
{kuning[m]--;}
else{hijau1[m]--;}

delay(800);
}while(kuning[m]>=0);
cout<<"Nilai Input dan Nilai Miu\n"<<endl;
//input acak//
 acak[m]=rand()%30;

for (x=1;x<=4;x++)

{

//komposisi fuzzy//
if (acak[x]<=10)
{miu[x]=(15-acak[x])/15;
strcpy (keterangan[x],"rendah");
}
else if ((acak[x]>10)&&(acak[x]<20))
{ if (acak[x]<15)
{miu[x]=(acak[x]-10)/5;}
else
{miu[x]=(20-acak[x])/5;}
strcpy (keterangan[x],"sedang");
}
else if (acak[x]>=20)
{miu[x]=(acak[x]-15)/15;
strcpy (keterangan[x],"tinggi");

}
}

//rule//
z=m+1;
for(y=1;y<=3;y++)
{if (z>4)
{z=1;}

if(strcmp(keterangan[m],"rendah")==0&& strcmp(keterangan[z],"rendah")==0)
{
if (miu[m]<miu[z]){
perbandingan[m][y]=miu[m];}
else{
perbandingan[m][y]=miu[z];}
rule[m][z]=15-(15*perbandingan[m][y]);}
else if(strcmp(keterangan[m],"rendah")==0&& strcmp(keterangan[z],"sedang")==0)
{
if (miu[m]<miu[z]){
perbandingan[m][y]=miu[m];}
else{
perbandingan[m][y]=miu[z];}
rule[m][z]=15-(15*perbandingan[m][y]);}
else if(strcmp(keterangan[m],"rendah")==0&& strcmp(keterangan[z],"tinggi")==0)
{
if (miu[m]<miu[z]){
perbandingan[m][y]=miu[m];}
else{
perbandingan[m][y]=miu[z];}
rule[m][z]=15-(15*perbandingan[m][y]);}
else if(strcmp(keterangan[m],"sedang")==0&& strcmp(keterangan[z],"rendah")==0)
{
if (miu[m]<miu[z]){
perbandingan[m][y]=miu[m];}
else{
perbandingan[m][y]=miu[z];}
rule[m][z]=(15*perbandingan[m][y])+15;}
else if(strcmp(keterangan[m],"sedang")==0&& strcmp(keterangan[z],"sedang")==0)
{
if (miu[m]<miu[z]){
perbandingan[m][y]=miu[m];}
else{
perbandingan[m][y]=miu[z];}
rule[m][z]=15-(15*perbandingan[m][y]);}
else if(strcmp(keterangan[m],"sedang")==0&& strcmp(keterangan[z],"tinggi")==0)
{
if (miu[m]<miu[z]){
perbandingan[m][y]=miu[m];}
else{
perbandingan[m][y]=miu[z];}
rule[m][z]=15-(15*perbandingan[m][y]);}
else if(strcmp(keterangan[m],"tinggi")==0&& strcmp(keterangan[z],"rendah")==0)
{
if (miu[m]<miu[z]){
perbandingan[m][y]=miu[m];}
else{
perbandingan[m][y]=miu[z];}
rule[m][z]=(15*perbandingan[m][y])+15;}
else if(strcmp(keterangan[m],"tinggi")==0&& strcmp(keterangan[z],"sedang")==0)
{
if (miu[m]<miu[z]){
perbandingan[m][y]=miu[m];}
else{
perbandingan[m][y]=miu[z];}
rule[m][z]=(15*perbandingan[m][y])+15;}
else if(strcmp(keterangan[m],"tinggi")==0&& strcmp(keterangan[z],"tinggi")==0)
{
if (miu[m]<miu[z]){
perbandingan[m][y]=miu[m];}
else{
perbandingan[m][y]=miu[z];}
rule[m][z]=(15*perbandingan[m][y])+15;}

alfa[m][y]=perbandingan[m][y]*rule[m][z];
z=z+1;
}
//hasil//
lampu[m]=(alfa[m][1]+alfa[m][2]+alfa[m][3])/(perbandingan[m][1]+perbandingan[m][2]+perbandingan[m][3]);

cout<<"\nhasil fuzzy :\n";
cout<<setiosflags(ios::fixed);

hijau[1]=(int)lampu[1];
hijau[2]=(int)lampu[2];
hijau[3]=(int)lampu[3];
hijau[4]=(int)lampu[4];

m++;
if (m>4 )
{m=1;}

}while(!kbhit());


}

Artikel Terkait

0 komentar :

Posting Komentar

 

Catatannya Didit Copyright © 2011-2015 | Powered by Blogger