1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#include <iostream>
using namespace std;
struct dt_OR
{
int X1;
int X2;
int bias;
int target;
} dt_OR
{
int X1;
int X2;
int bias;
int target;
}
dt_OR1;
char buffer[ 128 ];
float alpha, ambang;
struct dt_hsl
{
float j, s1, s2, t;
float y, y_in, x1, x2, bias, w1, w2, b, dw1, dw2, db, sum_xw;
} dt_hsl1;
FILE *fp, *fp2;
/*Menampilkan menu*/
menu( )
{
clsrcr( );
gotoxy( 20, 3 );
printf( "Pengenalan Fungsi Logika OR dengan Perceptron" );
gotoxy( 12, 8 );
printf( " MENU " );
gotoxy( 12, 9 );
printf( " ------------------" );
gotoxy( 13, 10 );
printf( "1.Membuat file data_OR.dat" );
gotoxy( 13, 11 );
printf( "2.Menampilkan isi file data ke layar" );
gotoxy( 13, 12 );
printf( "3.Training jaringan syaraf tiruan" );
gotoxy( 13, 13 );
printf( "4.Menampilkan hasil training" );
gotoxy( 13, 14 );
printf( "5.Aplikasi/testing jaringan" );
gotoxy( 13, 15 );
printf( "5.Aplikasi/testing jaringan" );
gotoxy( 12, 16 );
printf( " ------------------ " );
gotoxy( 20, 18 );
printf( "pilihan Anda:" );
return ( 0 );
}
/*Membaca dan menulis data structure ke dalam file data*/
isi_data( )
{
char berhenti;
int i;
clsrcr( )
}
/*Membaca dan menulis data structure ke dalam file data*/
isi data( )
{
char berhenti int i;
clrscr( );
i = 1;
berhenti = 't'
/*Meminta masukan data */
While(toupper(berhenti) != 'Y' {
printf( "data ke-%d", i );
i++ printf( "\n ---" );
printf( "\X1 :" );
gets( buffer );
dt_OR1.X1 = atof( buffer );
printf( "X2 :" ) gets( buffer );
dt_OR1.X2 = atof( buffer );
dt_OR1.bias = 1;
printf( "Target :" );
gets( buffer );
dt_OR1.target = atof( buffer );
/*simpan ke file data(data_OR.dat)*/
fwrite( &dt_OR1, sizeof( dt_OR1 ), 1, fp );
printf( "\nBerhenti (Y/T) ?" );
cin >> berhenti;
printf( "\n\n" );
}
return ();
}
/* menampilkan isi file data ke layar */
baca_data( )
{
int i = 1;
clrscr( );
/* mencetak judul */
printf( " DATA OR \n" );
printf( " --------- \n" );
printf( " No. X1 X2 Bias Target \n" );
printf( " --------- \n" );
while( fread( &dt_OR !), 1, fp ) == 1)
{
printf( " %2d %3d 52d %4d %6d\n", i++, dt_OR1.X1, dt_OR1.X2, dt_bias, dt_OR1.target );
}
printf( " --------- \n" );
getc( );
return ( );
}
/* Memproses data berdasarkan algoritma pelatihan perceptron */
training( )
{
double temp1, temp2, temp3;
int cek;
/*inisialisasi */
int i = 1;
alpha = 1;
ambang = 0.1;
dt_hsl1.j = 0;
dt_hsl1.w1 = 0;
dt_hsl1.w2 = 0;
dt_hsl1.b = 0;
dt_hsl1.dw1 = -999;
dt_hsl1.dw2 = -999;
dt_hsl1.db = -999;
/* lakukan selama kondisi berhenti tidak teroenuhi untuk semua
pasangan s:t */
while( dt_hsl.dw1 != 0 || dt_hsl1.dw2 != 0 || dt_hsl1.db != 0 || cek != 0 ) {
fseek( fp, 0, SEEK_SET );
cek = 0;
while( fread & dt_OR1, sizeof( dt_OR1 ), 1, fp ) == 1)
{
dt_hsl1.s1 = dt_OR1.X1;
dt_hsl1.x1 = dt_hsl1.s1;
dt_hsl1.s2 = dt_OR1.X2;
dt_hsl1.t = dt_OR1.target;
dt_hsl1.bias = dt_OR1.bias;
dt_hsl1.sum_xw = dt_hsl1.x1 * dt_hsl1.w1 + dt_hsl1.x2 * dt_hsl.w2;
dt_hsl1.y_in = dt_hsl1.b + dt_hsl1.y = -1;
else if( dt_hsl1.y_in < _ambang ) dt_hsl1.y = -1;
else dt_hsl1.y = 0;
temp1 = dt_hsl1.w1;
temp2 = dt_hsl1.w2;
temp3 = dt_hsl1.b;
if( dt_hsl.y != dt_hsl1.t ) {
dt_hsl1.w1 = dt_hsl1.w1 + alpha * dt_hsl1.t * dt_hsl1.x1;
dt_hsl1.w2 = dt_hsl1.w2 + alpha * dt_hsl1.t * dt_hsl1.x2;
dt_hsl1.b = dt_hsl1.b + alpha * dt_hsl1.t;
}
dt_hsl1.dw1 = dt_hsl1.w1 - temp1;
dt_hsl1.dw2 = dt_hsl1.w2 - temp2;
dt_hsl1.db = dt_hsl1.b - temp3;
dt_hsl1.j++;
/* Menyimpan hasil pelatihan ke file hasil
(Hasil_OR.dat) */
fwrite( &dt_hsl1, sizeof( dt_hsl1 ), 1, fp2 );
if( dt_hsl1.dw1 != 0 || dt_hsl1.dw2 != 0 || dt_hsl1.db != 0 ) cek = cek++;
}
}
clscr( );
if( dt_hsl1.y != dt_hsl1.t ) {
cout << "Training mengalami stagnasi ...";
cout << "\nHasil aplikasi mungkin tidak sesuai dengan yang diharapkan. ";
}
else
cout << "Training sukses ...";
printf( "\n\n\nTekan <ENTER> untuk kembali ke menu utama ..." );
getch( );
return 0;
}
/* Menampilkan hasil ke layar dan menyimpan file ke file teks */
baca_hasil( )
{
int i = 1;
// isi file hasil (Hasil_OR.dat) ditampilkan ke layar
// disamping itu disimpan pula ke file hasil (Training.txt)
clrscr( );
printf( "\n i x1 x2 1 y_in y t w1 w2 b dw1 dw2 db" );
printf( "\n -------------------- " );
fprintf( fp2, "\n 1 x1 x2 1 y_in y t w1 w2 b dw1 dw2 db" );
fpintf( fp2, "\n ----------------------" );
while( fread( dt_hsl1, sizeof( dt_hsl1 ), 1, fp ) == 1 ) {
printf( "\n%2d %3.0f %3.0f %3.0f ", i++, dt_hsl1.x1, dt_hsl1.x2, dt_hsl1.bias );
printf( "%3.2f %3.0f %3.0f ", dt_hsl1.y_in, dt_hsl1.t );
printf( " %3.2f %3.2f %3.2f %3.2f %3.2f %3.2f", dt_hsl1.w1, dt_hsl.w2, dt_hsl1.b, dt_hsl1.dw1, dt_hsl1.dw2, dt_hsl1.db );
printf( fp2, "\n%2d %3.0f %3.0f %3.0f ", i, dt_hsl1.x1, dt_hsl1.x2, dt_hsl1.bias );
fprintf( fp2, "%3.2f %3.0f %3.0f ", dt_hsl1.y_in, dt_hsl.y, dt_hsl1.t );
fprintf( fp2, " %3.2f %3.2f %3.2f %3.2f %3.2f %3.2f", dt_hsl1.w1, dt_hsl1.w2, dt_hsl1.b, dt_hsl1.dw1, dt_hsl1.dw2, dt_hsl1.db );
}
printf( "\n ----------------------------\n" );
fprintf( fp2, "\n ----------------------------\n" );
getch( );
return 0;
}
/* Aplikasi perceptron */
aplikasi( )
{
int x1, x2;
float SUM_XW, Y_IN, Y;
clrscr( );
printf( "peringatan :\n" );
printf( "Harus sudah pernah mengambil menu 3. ...\n\n\n" );
Printf( "Unit input X1 : " );
scanf( "%d", &x1 );
printf( "Unit input X2 : " );
scanf( "%d", &x2 );
printf( " -------- " );
SUM_XW = x1 * dt_hsl1.w1 + x2 * dt_hsl1.w2;
Y_IN = dt_hsl1.b + SUM_XW;
if( Y_IN > ambang )
Y = 1;
else if( Y_IN < -ambang )
Y = -1;
else
Y = 0;
printf( "\Output=%3.0f", Y );
printf( "\n\n\nTekan <ENTER> untuk kembali ke menu utama ..." );
getch( );
return 0;
}
//----------------------
// PROGRAM UNTAMA
//----------------------
main( int argc, char *argv[] )
{
char pilih = '0';
argv[ 1 ] = "D:\\Data_OR.dat"; // Nama file data (.dat)
argv[ 2 ] = "D:\\Hasil_OR.dat"; // Nama file hasil (.dat)
argv[ 3 ] = "D:\\Training.txt"; // Nama file hasil (.txt)
while( pilih != '6' ) {
menu( );
pilih = getche( );
switch( pilih )
case 'I':
if( ( fp = fopen( argv[ 1 ], "wb" ) ) == NULL ) {
printf( "Tidak dapat membuka file !!" );
exit( 1 );
case '2':
}
isi_data( );
Fclose( fp );
break;
if( ( fp = fopen( argv[ 1 ], "rb" ) ) NULL ) {
printf( "Tidak dapat membuka file !!" );
exit( 1 );
case '3':
}
baca_data( );
fclose( fp );
break;
if( ( fp = fopen( argv[ 1 ], "rb" ) ) NULL ) {
printf( "Tidak dapat membuka file !!" );
exit( 1 );
}
if( ( fp2 = fopen( argv[ 2 ], "wb" ) ) == NULL ) {
printf( "Tidak dapat membuka file !!" );
exit( 1 );
case '4':
}
training( );
fclose( fp );
fclose( fp2 );
break;
if( ( fp2 = fopen( argv[ 2 ], "wb" ) ) == NULL ) {
printf( "Tidak dapat membuka file !!" );
exit( 1 );
}
if( ( fp2 = fopen( argv[ 2 ], "wb" ) ) == NULL ) {
printf( "Tidak dapat membuka file !!" );
exit( 1 );
baca_hasil( );
case '5':
case '6':
}
}
fclose( fp );
break;
if( ( fp2 = fopen( argv[ 2 ], "wb" ) ) == NULL ) {
printf( "Tidak dapat membuka file !!" );
exit( 1 );
}
aplikasi( );
fclose( fp );
break;
break;
return ( 0 );
|