// Name : perceptron.cpp
// Author : konishi
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include
#include
using namespace std;
double CalcNet(double *pX,double *pW,int nSize)
{
double net = 0.0;
int i = 0;
for(i = 0; i < nSize;i++)
{
net = net+pX[i]*pW[i];
}
return net;
}
void ChangeWait(double *pW,double *pX,double out,double Y,int nSize)
{
int i = 0;
for(i = 0; i < nSize;i++)
{
pW[i] = pW[i] + (Y-out)*pX[i];
}
for(i = 0; i < nSize;i++)
{
printf("%lf,",pW[i]);
}
printf("\n");
}
int main() {
double net = 0.0;
double W[3] = {0.0,0.0,0.0}; //重み
double X[][3] = {{1.0,0.0,0.0},
{1.0,0.0,1.0},
{1.0,1.0,0.0},
{1.0,1.0,1.0}}; //入力信号
double Y[4] = {0.0,1.0,1.0,1.0}; //教師信号
double out = 0.0;
int nLearnFlg = 0;
do
{
nLearnFlg = 0;
for(int i = 0; i < 4;i++)
{
net = CalcNet(X[i],W,4);
out = net >= 0 ? 1.0:0.0;
if((out-Y[i]) != 0.0)
{
nLearnFlg = 1;
ChangeWait(W,X[i],out,Y[i],3);
}
}
}while(nLearnFlg==1);
printf("end\n");
return 0;
}