Thread: AND Logic Perceptrons

  1. #1
    The Reel Thing
    Join Date
    Jun 2005

    AND Logic Perceptrons

    Hello, I've decided to look into the subject of AI programming, and am having trouble with changing my weights with the delta rule. I'm trying to make a simple Boolean AND logic perceptron, which will learn to recognize whether both the inputs are the same.

    Here's my code that I have so far:

    // Include header files
    #include <iostream>
    using namespace std;
    int main()
    	// Delcare vars
    	double threshold = 1.0;
    	double weight = -0.2;
    	double activation;
    	int input1, input2;
    	bool output;
    	// Keep looping
    	while (1)
    		// Get the input vars
    		cout << "Please enter the first boolean input: ";
    		cin >> input1;
    		cout << "Please enter the second boolean input: ";
    		cin >> input2;
    		// Get the activation
    		activation = (input1 * weight) + (input2 * weight);
    		// If the activation is greater than the threshold, fire the perceptron
    		if (activation >= threshold)
    			cout << "Perceptron is firing" << endl;
    			output = true;
    		// If the activation is less than the threshold, don't fire
    		else if (activation < threshold)
    			output = false;
    			cout << "Perceptron isn't firing" << endl;	
    			cout << "Changing Weights..." << endl;
    			cout << "Old Weight: " << weight << endl;
    			// Change the weights
    			weight += (input1 + input2) * (1 - output);
    			cout << "New Weight: " << weight << endl;
    	return 0;	
    There is no problem, except once I "train" the perceptron, and the feed it 2 inputs like 1 and 0, it would evaluate to true, but seeing both the inputs aren't true, it shouldn't.

    I don't know, but it may be the way I'm adjusting the weights, I'm using the delta rule off the cprogramming tutorials, and I may have interpreted it wrong.

    I will admit right now that I'm just starting in this subject, so it's probably some simple mistake, or something that I've overlooked. If anyone has any suggestions, or a solution, it would be appreciated.
    Bagpipes putting the fun back in funeral.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    (1 - output);
    output is a bool, which is assigned to false a little earlier on. So that's sort of like saying 1.

    weight += (input1 + input2) * (1 - output);
    is like
    weight += input1 + input1;
    On second thought, you don't even really need output. You only use it once, and where you do, you initialize it just beforehand.

    if (activation >= threshold)
    // ...
    else if (activation < threshold)
    That else if could be an else.

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Digital Logic
    By strokebow in forum Tech Board
    Replies: 3
    Last Post: 12-09-2006, 01:05 PM
  2. Logic
    By LordBronz in forum C++ Programming
    Replies: 6
    Last Post: 05-23-2006, 05:41 PM
  3. Actors, cues, event based logic.
    By Shamino in forum Game Programming
    Replies: 2
    Last Post: 04-27-2006, 10:58 PM
  4. Circular Logic
    By DavidP in forum A Brief History of
    Replies: 1
    Last Post: 10-15-2001, 08:10 PM