📄 ids_bpn.cpp
字号:
#include "conio.h"
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#define HN 3 //隐含层神经元个数, 请多次修改其值来进行比较
#define IN 3 //输入的每个样本含的项目数
#define ON 2
#define N 50 //样本数
#define g(x) 1/(1+exp(-x)) //sigmod函数
double x_out[IN]; //输入层的输出值
double hx_out[N]; //我用来归一化的中间数组
double hn_out[HN]; //隐含层的输出值
double y_out[ON]; //输出层的输出值
double y[ON]; //输出层的期望输出值
double w[IN][HN]; //输入层到隐含层的权值
double v[HN][ON]; //隐含层到输出层的权值
double deltaw[IN][HN];
double deltav[HN][ON];
double hn_delta[HN]; //隐含层的delta
double y_delta[ON]; //输出层的delta
double error; //全局误差
double errlimit=0.001; //预设的全局误差极限
double alpha=0.7, beta=0.7; //预设的学习速率
using namespace std;
int flage1=0,flage2=0;
int Start_Show()
{
//clrscr();
printf(" **************************************************\n");
printf(" * Intrusion Detection System Engerneering *\n");
printf(" * Based on Back Propagation Neural Network *\n");
printf(" * Copyright by: 伍友良 *\n");
printf(" * Welcome! *\n");
printf(" **************************************************\n");
printf("Before using this simplification of IDS, please read the following guidances:\n");
printf("The purpose of this IDS is to detect the system's activities so to certify whether it's in safe state.\n");
printf("The three items of sample , separately, are:\n");
printf("1. System's activity time(AT)\n");
printf("2. Login times during the AT\n");
printf("3. The biggest login interval\n");
getch();
//clrscr();
return 0;
}
int End_Show()
{
//clrscr();
printf(" **************************************************\n");
printf(" * Intrusion Detection System Engerneering *\n");
printf(" * Based on Back Propagation Neural Network *\n");
printf(" * Copyright by: 伍友良 *\n");
printf(" * Thanks for testing! *\n");
printf(" **************************************************\n");
printf("I have to tell you something when this programe comes to its end:\n");
printf("I use another unitary arithmetic which is quite different from the referenced programe's .\n");
printf("I Unit the data only when they are the values of same attribution,\n ");
printf("but I am not sure whether my quomodo is right in the Name of maths. \n");
printf("Press any key ti exit\n");
getch();
//clrscr();
return 0;
}
int main()
{
Start_Show();
int loop=0; //训练次数
int times=40000; //预设的训练次数上限
int i, j, m;
double max, min;
double sumtemp;
double errtemp;
struct Data //训练样本对
{
double input[IN];
double teach[ON];
}data[N] = \
/*{{86.4479, 17.875, 24.925, 1,0}, //1
{ 92.5748, 17.99, 27.637, 1,0}, //2
{ 85.3533, 19.964, 24.787, 1,0},//3
{ 106.3806, 19.07, 24.463, 1,0},
{ 74.7335, 13.653, 29.721, 1,0},
{ 139.4121, 18.065, 21.306, 1,0},
{ 107.4832, 16.825, 27.325, 1,0},
{ 86.0032, 18.99, 25.637, 1,0},
{ 97.3436, 17.974, 24.787, 1,0},
{ 110.386, 14.07, 27.463, 1,0}, //10
{ 106.7335, 27.653, 36.721, 1,0},//11
{ 88.4121, 14.065, 27.306, 1,0},//12
{ 87.4479, 17.875, 23.925, 1,0},//13
{ 96.5748, 17.99, 27.637, 1,0}, //14
{ 116.3533, 19.964, 25.787, 1,0},//15
{ 118.3806, 19.07, 29.463, 1,0},//16
{ 93.7335, 18.653, 26.721, 1,0},//17
{ 84.4121, 23.065, 26.306, 1,0},//18
{ 108.8977, 18.422, 24.925, 1,0},//19
{ 117.5748, 17.99, 25.637, 1,0}, //20
{ 96.0000, 19.375, 28.925, 1,0},
{ 89.3215, 17.099, 30.637, 1,0},
{ 112.3463, 18.964, 29.787, 1,0},
{ 103.3806, 19.07, 29.463, 1,0},
{ 94.7335, 17.653, 26.721, 1,0},
{ 106.4621, 18.065, 24.306, 1,0},
{ 95.4445, 19.895, 38.925, 1,0},
{ 86.5576, 17.889, 35.637, 1,0},
{ 115.8933, 18.974, 25.787, 1,0},
{ 93.3966, 19.078, 24.463, 1,0}, //30
{ 112.7335,17.653, 27.721, 1,0},
{ 116.4121, 12.065, 36.306, 1,0},
{ 126.4479, 17.875, 28.925, 1,0},
{ 107.5748, 17.99, 28.637, 1,0},
{ 96.3533, 16.964, 24.787, 1,0},
{ 103.3806, 14.07, 28.777, 1,0},
{ 96.7366, 17.653, 26.721, 1,0},
{ 76.4651, 16.065, 19.306, 1,0},
{ 116.4479, 17.875, 24.925, 1,0},
{ 125.5743, 19.569, 28.637, 1,0},//40
{ 134.5748, 17.99, 45.637, 1,0},
{ 126.0066, 18.785, 31.925, 1,0}, //42
{ 125.3785, 18.099, 39.534, 1,0},
{ 128.3463, 19.964, 39.787, 1,0},//44
{ 123.1135, 25.07, 29.463, 1,0},
{ 79.7335, 27.653, 26.721, 1,0},//46
{96.3821, 17.364, 26.927, 1,0},
{ 98.4777, 17.447, 21.535, 1,0},//48
{ 109.5576, 17.889, 20.637, 1,0}, //49
{ 100, 19, 24, 1,0}};//50 //注意这里的值需要改变*/
{{86.4479, 17.875, 24.925, 1,0}, //1
{ 92.5748, 17.99, 27.637, 1,0}, //2
{ 85.3533, 19.964, 24.787, 1,0},//3
{ 206.3806, 14.07, 24.463, 0,1},
{ 74.7335, 13.653, 59.721, 0,1},
{ 139.4121, 14.065, 21.306, 0,1},
{ 107.4832, 16.825, 27.325, 1,0},
{ 86.0032, 18.99, 25.637, 1,0},
{ 97.3436, 17.974, 24.787, 1,0},
{ 110.386, 14.07, 67.463, 0,1}, //10
{ 306.7335, 27.653, 36.721, 0,1},//11
{ 58.4121, 14.065, 17.306, 0,1},//12
{ 87.4479, 17.875, 23.925, 1,0},//13
{ 96.5748, 17.99, 27.637, 1,0}, //14
{ 116.3533, 19.964, 25.787, 1,0},//15
{ 218.3806, 14.07, 79.463, 0,1},//16
{ 93.7335, 8.653, 46.721, 0,1},//17
{ 44.4121, 23.065, 26.306, 0,1},//18
{ 108.8977, 18.422, 24.925, 1,0},//19
{ 117.5748, 17.99, 25.637, 1,0}, //20
{ 96.0000, 19.375, 28.925, 1,0},
{ 89.3215, 17.099, 30.637, 1,0},
{ 112.3463, 18.964, 29.787, 1,0},
{ 103.3806, 14.07, 79.463, 0,1},
{ 94.7335, 17.653, 56.721, 0,1},
{ 106.4621, 8.065, 76.306, 0,1},
{ 95.4445, 19.895, 38.925, 1,0},
{ 86.5576, 17.889, 35.637, 1,0},
{ 115.8933, 18.974, 44.787, 1,0},
{ 93.3966, 12.078, 14.463, 0,1}, //30
{ 112.7335,17.653, 77.721, 0,1},
{ 76.4121, 12.065, 56.306, 0,1},
{ 126.4479, 17.875, 28.925, 1,0},
{ 107.5748, 17.99, 28.637, 1,0},
{ 96.3533, 16.964, 24.787, 1,0},
{ 246.3806, 14.07, 78.777, 0,1},
{ 336.7366, 17.653, 26.721, 0,1},
{ 256.4651, 16.065, 26.306, 0,1},
{ 116.4479, 17.875, 24.925, 1,0},
{ 125.5743, 19.569, 28.637, 1,0},//40
{ 134.5748, 17.99, 45.637, 1,0},
{ 126.0066, 18.785, 31.925, 1,0}, //42
{ 125.3785, 18.099, 39.534, 1,0},
{ 128.3463, 19.964, 39.787, 1,0},//44
{ 333.1135, 5.07, 69.463, 0,1},
{ 421.7335, 27.653, 76.721, 0,1},//46
{36.3821, 11.364, 16.927, 0,1},
{ 98.4777, 17.447, 21.535, 1,0},//48
{ 109.5576, 17.889, 20.637, 1,0}, //49
{ 1000, 998, 1000, 0,1}};//50 //注意这里的值需要改变
Data data_copy[N];
//-----------------------------------------------------------
//初始化. 将连接权值、偏置系数置为(-1,1)之间的随机数
cout<<"initializing............"<<endl;
srand((unsigned)time(NULL));
for(i=0; i<IN; i++) //w的随机输入
for(j=0; j<HN; j++)
{
w[i][j] = ((double)rand()/32767.0)*2-1;
deltaw[i][j] = 0;
// cout<<"w["<<i+1<<"]["<<j+1<<"]="<<w[i][j]<<endl;
}
for(i=0; i<HN; i++) //v的随机输入
for(j=0;j<ON;j++)
{
v[i][j] = ((double)rand()/32767.0)*2-1;
deltav[i][j] = 0;
// cout<<"v["<<i+1<<"]["<<j+1<<"]="<<v[i][j]<<endl;
}
//训练.
cout<<endl<<"training beginning............"<<endl;
for(i=0;i<IN;i++)
{ max=0; min=1000;
for(m=0;m<N;m++)
{
hx_out[m] = data[m].input[i];
if (max < hx_out[m]) //比较得出输入的最大最小值
max = hx_out[m];
if (min > hx_out[m])
min = hx_out[m];
// cout<<"x_out["<<i+1<<"]="<<x_out[i]<<endl;
//cout<<"max="<<max<<endl<<"min="<<min<<endl;
}
for(m=0; m<N; m++) //归一化后的输入值
{
data_copy[m].input[i] = (hx_out[m]) /max;
//cout<<"data_copy["<<m+1<<"].input["<<i+1<<"]="<<data_copy[m].input[i]<<endl;
}
}
while(loop < times)
{
loop++;
error = 0.0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -