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!