Thread: Well having issues with this other program here...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434

    Well having issues with this other program here...

    Well i wanted to attempt to model a basic perceptron in C++.

    I know this isn't right or nothing, ha, but i gave it a go anyways.

    I get all sorts of segfaults throughout, but here's the program:

    Code:
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    struct ending
    {
    	int input;
    };
    
    struct perceptron
    {
    	ending *ends[8];
    	int value;
    };
    
    int ops[7];
    
    //Functions
    void returnEnds(ending *e[8], int a[8]);
    bool learn(perceptron *p, int ops[7], int result);
    int createResult(int a[8]);
    int operations(int input1, int input2, int op);
    void displayEq(perceptron *p, int ops[]);
    
    int main()
    {
    	//Perceptron (single) AI Self-Learning Program
    	perceptron *p;
    	p = new perceptron;
    	ending *ends[8];
    	int a[8];
    	int dResult;
    	dResult = createResult(a);
    	//initialize
    	ops[0] = -1; //being used as a flag
    	for(int i=0; i<8; i++)
    	{
    		ends[i] = new ending;
    		a[i] = (i*2);
    		ends[i]->input = a[i];
    	}
    	//Display
    	cout<<"Target Number: "<<dResult<<endl;
    	cout<<"The program will now try and figure out how to achieve this number and constantly"
    		<<"compare it's own feedback, comparing it to the result, and hopefully learn the process."<<endl<<endl;
    	//Start AI
    	while(learn(p, ops, dResult) == false)
    	{
    		returnEnds(ends, a);
    		for(int i=0; i<8; i++)
    		{
    			p->ends[i] = ends[i];
    		}
    	}
    }
    
    void returnEnds(ending *e[8], int a[8])
    {
    	for(int i=0; i<8; i++)
    	{
    		e[i]->input = a[i];
    	}
    }
    
    int createResult(int a[8])
    {
    	return(a[0] + a[1] + a[2] - a[3] + a[4] - a[5] + a[6] - a[7]);
    }
    
    int operations(int input1, int input2, int op)
    {
    	int ret=0;
    	if(op == 0)
    	{
    		//addition
    		ret = input1 + input2;
    	}else if(op == 1)
    	{
    		//subtraction
    		ret = input1 - input2;
    	}
    	else{
    		ret = 0;
    	}
    	return ret;
    }
    
    bool learn(perceptron *p, int ops[7], int result)
    {
    	if(ops[0] == -1)
    	{
    		if(result <= 0)
    		{
    			for(int i=0; i<7; i++)
    			{
    				ops[i] = 1;
    			}
    		}else if(result >0)
    		{
    			for(int i=0; i<7; i++)
    			{
    				ops[i] = 0;
    			}
    		}
    	}
    	int value=0;
    	for(int i=0; i<7; i++)
    	{
    		value += (operations(p->ends[i]->input, p->ends[i+1]->input, ops[i]));
    	}
    	cout<<"Target: "<<result<<endl;
    	cout<<"Value:  "<<value<<endl;
    	cout<<"Approach: ";
    	displayEq(p, ops);
    	cout<<endl;
    	Sleep(150);
    	if(value > result)
    	{
    		//Decrease
    	}else if(value < result)
    	{
    		//Increase
    	}else if(value == result)
    	{
    		//Did what it is supposed to, return true
    		return true;
    	}
    	return false;
    }
    
    void displayEq(perceptron *p, int ops[])
    {
    	int j;
    	for(int i=0; i<8; i++)
    	{
    		if(i >= 7)
    		{
    			j = 6;
    		}else{
    			j = i;
    		}
    		cout<<p->ends[i]->input<<" ";
    		if(ops[j] == 0)
    		{
    			//addition
    			cout<<"+";
    		}else if(ops[j] == 1)
    		{
    			//subtraction
    			cout<<"-";
    		}else{
    			cout<<" ";
    		}
    		cout<<" ";
    	}
    }

    Danke!
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  2. #2
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    First, you haven't allocated memory for the ending pointers when you pass the perceptron object to the "learn" function.

    Second, I'm having a hard time understanding how this is a perceptron. Normally, you'd use a perceptron for data classification tasks, and I have heard that except for XOR, you can implement boolean relations with a perceptron, but whatever it's for, there are three things you always need no matter what and I don't see that here.

    You need
    (1) a weight vector
    (2) a feature vector
    (3) a threshold.

    You get the vector product of the weight vector and feature vector, sum over it, compare it to the threshold (which in your case would be dResult), and if the sum is greater, you decrement the weight vectors corresponding to the features and if the sum is less, you increment the weight vectors. You iterate over this until you "converge" or you the marginal increase in accuracy is minimal.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Ah... i see

    Well this was just a (sad) attempt on my part
    "Anyone can aspire to greatness if they try hard enough."
    - Me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. Continued issues with Winows Errors when running program
    By hpteenagewizkid in forum C Programming
    Replies: 6
    Last Post: 11-14-2006, 03:51 PM
  3. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  4. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  5. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 10:49 PM