🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

ANN not working

Started by
0 comments, last by ironfroggy 22 years, 10 months ago
I made a test ANN for practice and its not going well.

#include 
#include 
#include 
#include 
using namespace std;

class NNode
{
public:
	NNode* OutputPtr;
	int Weight;
	int Strength;
	int Threshhold;
	int Charge;
	
	NNode()
	{
		Weight = Strength = 10;
		Threshhold = 10;
		Charge = 0;
	}
	
	void Input(int Value)
	{
		Charge = Charge + Value / Weight;
	}
	
	void Output() 
	{
		if (Charge >= Threshhold)
			OutputPtr->Input(Charge);
	}
	
	void Reward()
	{
		Strength++;
	}
	
	void Punish()
	{
		Strength--;
		if (Strength==0)
			if (rand()%1)
				Weight = Weight + (rand()%5);
			else
				Weight = Weight - (rand()%5);
		if (Weight<0)
			Weight -= Weight + 10;
	}
};

void main()
{
	NNode* Input = new NNode[2];
	NNode* Output = new NNode;
	Input[0].OutputPtr = Output;
	Input[1].OutputPtr = Output;
	
	cout << "Begin Training..." << endl;
	
	srand(time(0));
	
	int Successes, Failures;
	Successes = Failures = 0;
	
	for (int i=0;i<100;i++)
	{
		int Val0, Val1;
		srand(time(0)+200+i);
		Val0 = rand()%2;
		srand(time(0)+400+i);
		Val1 = rand()%2;
		
		Input[0].Input(Val0);
		Input[1].Input(Val1);
		Input[0].Output();
		Input[1].Output();
		
		cout << "Inputs: " << Val0 << " and " << Val1 << endl;

		int ans = Val0 && Val1;

		cout << "Charge and Goal is: " << Output->Charge << " and " << ans;
		cout << ", which means this training was a ";

		if (Output->Charge == ans)
		{
			// yay!
			
			Input[0].Reward();
			Input[1].Reward();
			Successes++;
			cout << "Successful.\n";
		}
		
		else
		{
			// boo!
			
			Input[0].Punish();
			Input[1].Punish();
			Failures++;
			cout << "Failure.\n";
		}
	}
	cout << "\n\nTotal Successful trainings: " << Successes << endl;
	cout << "Total Failed trainings: " << Failures << endl;

	for (i=0;i<80;i++)
	{
		Input[0].Input(1);
		Input[1].Input(1);
		Input[0].Output();
		Input[1].Output();
		cout << Output->Charge;
	}
	cout << endl;
}
 
the problem is that it just doesnt learn. I dont know what I am doing wrong. Should teach an AND gate shouldnt it?
(http://www.ironfroggy.com/)(http://www.ironfroggy.com/pinch)
Advertisement
Im not sure on a couple of things u did in your code. but the reason its not learing is because you dont have a learing real learning algorithm. You cant just subtract and add value to the weights. There is a rather complex formula (but easy to code, thankfully) to take the error between the output of the net and the correct answer to change the weights to move the output closer to the correct answer. Although, im not the best AI guy (selftought) i do know that using a backpropagation NN will take to long to be used in a game with lost of varibles. I hope this helps, i know it didnt answer your question. it would take a looooooong time to explain a backprop net over a bbs.

This topic is closed to new replies.

Advertisement