كد:
static void Main(string[] args)
{
double[,] x = new double[,] { { 1, 1 }, { 1, -1 }, { -1, 1 }, { -1, -1 } };
double[,] w = new double[,] { { 0.05, 0.1 }, { 0.2, 0.2 }, { 0.5, 0.5 } };
int[] t = new int[] { -1, 1, 1, -1 };
double[] b = new double[] { 0.3, 0.15, 0.5 };
double alpha = 0.5;
Console.WriteLine("X inputs are:\n");
for (int i = 0; i < 4; i++)
{
Console.WriteLine("\t{0}\t{1}", x[i, 0], x[i, 1]);
}
//int z1 = f(z_in_one, 0); ;
//int z2 = f(z_in_two, 0); ;
//double y_in = b[2] + (z1 * w[2, 0]) + (z2 * w[2, 1]);
//int y = f(y_in, 0); ;
int ep = 0;
int r = 0;
double z_in_one = 0.55;
double z_in_two = 0.45;
while (ep < 1)
{
r = 0;
for (int p = 0; p < 1; p++)
{
switch (t[p])
{
case -1:
if (z_in_two > 0 && z_in_one > 0)
{
b[0] = b[0] + alpha * (-1 - z_in_one);
w[0, 0] = w[0, 0] + alpha * (-1 - z_in_one) * x[r, 0];
w[1, 0] = w[1, 0] + alpha * (-1 - z_in_one) * x[r, 1];
//z_in_one = b[0] + w[0, 0] * x[r, 0] + w[1, 0] * x[r, 1];
b[1] = b[1] + alpha * (-1 - z_in_two);
w[0, 1] = w[0, 1] + alpha * (-1 - z_in_two) * x[r, 0];
w[1, 1] = w[1, 1] + alpha * (-1 - z_in_two) * x[r, 1];
//z_in_two = b[1] + w[0, 1] * x[r, 0] + w[1, 1] * x[r, 1];
}
else if (z_in_one > 0 && z_in_two <= 0)
{
b[0] = b[0] + alpha * (-1 - z_in_one);
w[0, 0] = w[0, 0] + alpha * (-1 - z_in_one) * x[r, 0];
w[1, 0] = w[1, 0] + alpha * (-1 - z_in_one) * x[r, 1];
//z_in_one = b[0] + w[0, 0] * x[r, 0] + w[1, 0] * x[r, 1];
}
else if (z_in_two > 0 && z_in_one <= 0)
{
b[1] = b[1] + alpha * (-1 - z_in_two);
w[0, 1] = w[0, 1] + alpha * (-1 - z_in_two) * x[r, 0];
w[1, 1] = w[1, 1] + alpha * (-1 - z_in_two) * x[r, 1];
//z_in_two = b[1] + w[0, 1] * x[r, 0] + w[1, 1] * x[r, 1];
}
break;
case 1:
double dist1 = Math.Abs(0 - z_in_one);
double dist2 = Math.Abs(0 - z_in_two);
if (dist1 < dist2)
{
b[0] = b[0] + alpha * (1 - z_in_one);
w[0, 0] = w[0, 0] + alpha * (1 - z_in_one) * x[r, 0];
w[1, 0] = w[1, 0] + alpha * (1 - z_in_one) * x[r, 1];
//z_in_one = b[0] + w[0, 0] * x[r, 0] + w[1, 0] * x[r, 1];
}
else if (dist2 < dist1)
{
b[1] = b[1] + alpha * (1 - z_in_two);
w[0, 1] = w[0, 1] + alpha * (1 - z_in_two) * x[r, 0];
w[1, 1] = w[1, 1] + alpha * (1 - z_in_two) * x[r, 1];
//z_in_two = b[1] + w[0, 1] * x[r, 0] + w[1, 1] * x[r, 1];
}
break;
}
z_in_one = b[0] + w[0, 0] * x[r, 0] + w[1, 0] * x[r, 1];
z_in_two = b[1] + w[0, 1] * x[r, 0] + w[1, 1] * x[r, 1];
r = r + 1;
}
ep++;
}