⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 graph.c

📁 bp神经网络算法
💻 C
字号:
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <bios.h>
#include <dos.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
#include <graphics.h>

#define N 1000000
#define I 2
#define K 2
#define J 3

double ran(void)
{
 int a;
 double r;

 a=rand()%10000;
 r=(a+0.00)/10000.00;

 return r;

}

double g(double h, double beta)
{
 return 0.318309886*atan(beta*h)+0.50;

}

double dg(double h, double beta)
{
 return 0.318309886*beta/(1+beta*beta*h*h);

}

double nowtime(void)
{
 long bios_time;
 double cur;

 bios_time = biostime(0, 0L);
 cur = bios_time / CLK_TCK;
 return cur;

}

void main()
{
 double In[K];
 double Out[I],hi[J],hj[J],H[J];
 double di[I],dj[J];
 double wij[I][J],wjk[J][K],detawij[I][J],detawjk[J][K];
 double beta=1.0,sum;
 double start,end;
 const int basex=60, basey=420;
 const int basebkcolor=LIGHTBLUE, basecolor=RED;
 int gdriver=VGA, gmode=VGAHI, errorcode;
 int i,j,k,x,y;
 long n;

 initgraph(&gdriver,&gmode,".\\");
 errorcode = graphresult();
 if (errorcode != grOk)
 {
	printf("\nGraphics error: %s\n", grapherrormsg(errorcode));
	printf("Press any key to halt!");
	getch();
	exit(1);
 }

 start=nowtime();

 setbkcolor(basebkcolor);
 setcolor(basecolor);

 line(0,basey,getmaxx(),basey);
 line(basex,0,basex,getmaxy());

 setfillstyle(SOLID_FILL,basecolor);
 fillellipse(basex,basey,3,3);

 wij[0][0]=-17.305192;
 wij[0][1]=-36.116124;
 wij[0][2]=20.689234;
 wij[1][0]=31.058342;
 wij[1][1]=-100.782596;
 wij[1][2]=-106.840632;

 wjk[0][0]=128.721548;
 wjk[0][1]=-89.184149;
 wjk[1][0]=-95.867742;
 wjk[1][1]=-92.288898;
 wjk[2][0]=-7.434370;
 wjk[2][1]=4.477464;

 randomize();

 for(n=0;n<N;n++)
 {
  In[0]=ran()/2.0+1.10;
  In[1]=ran()/2.0+1.60;

  for(j=0;j<J;j++)
  {
	hj[j]=0;
	for(k=0;k<K;k++)
	  hj[j]=hj[j]+wjk[j][k]*In[k];

	H[j]=g(hj[j],beta);

  }
  sum=0;
  for(i=0;i<I;i++)
  {
	hi[i]=0;
	for(j=0;j<J;j++)
	  hi[i]=hi[i]+wij[i][j]*H[j];

	Out[i]=g(hi[i],beta);
	sum=sum+Out[i];

  }
  for(i=0;i<I;i++)
	Out[i]=Out[i]/sum;
  x=(int)((In[0]-1.10)/0.50*(getmaxx()-basex)+basex);
  y=(int)(basey-(In[1]-1.60)/0.50*(basey-10));
  if(Out[0]<=0.05) putpixel(x,y,RED);
  else if(Out[0]>=0.95) putpixel(x,y,YELLOW);
  else putpixel(x,y,BLUE);

 }

 getch();
 end=nowtime();
 
 closegraph();
 
 printf("Run time:%lf\n",end-start);

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -