M
Matthew
Recently, I began to write a program that demostrates memory
representation as a network of concepts. AI is a hobby of mine and I
just began to throw the program together. I didn't use the best style,
but it still should have worked. (You can guess what's coming next!)
It didn't! At the debug() call the program starts printing random
data. I am running Mac OS X 10.3 using XCode. Here is the file, I hope
it isn't too long. Thank you for your help.
#include <iostream>
#include <string>
#include <vector>
#include <stdlib.h>
using namespace std;
class Node
{
public:
Node(string repr) { rep = repr;}
void addConnection(Node * newc, bool typer) {
connections.push_back(newc); types.push_back(typer); }
string getRep() { return rep; }
void setRep(string newr) { rep = newr; }
void activate(int strength);
void debug();
private:
string rep;
vector<bool> types;
vector<Node*> connections;
};
vector<Node> memory;
bool temp;
void Node::activate(int strength)
{
cout << strength << "\t" << rep << endl;
srand(time(NULL));
int ns = strength-(1 + rand() % (memory.size()-1));
if(ns <= 0)
return;
for(int i = 0; i < connections.size(); ++i)
{
if(types)
cout << "Isa: ";
else
cout << "P: ";
(*connections).activate(ns);
}
}
void Node::debug()
{
cout << "!DEBUGing " << rep << endl;
for(int i = 0; i < connections.size(); ++i)
{
cout << i << " " << (*connections).getRep() << endl;
}
cout << "DEBUG!" << endl;
}
void addNewMem();
void actMem();
void addMem();
void gatherConnections(Node * newMemory);
void dumpMem();
int main()
{
int choice = 0;;
for(;
{
cout << "\nWhat to do?" << endl;
cout << " (1) Add new memory" << endl;
cout << " (2) Activate memory" << endl;
cout << " (3) Exit" << endl;
cin >> choice;
getchar();
switch(choice)
{
case 1:
addNewMem();
break;
case 2:
actMem();
break;
case 3:
cout << "Positronic Brains Coming Soon To A Store Near You!" <<
endl;
return 0;
default:
cout << "Butthead" << endl;
return 0;
}
}
return 0;
}
void addNewMem()
{
char input;
while(true)
{
addMem();
cout << "Do you want to create another memory? [yn]: ";
cin >> input;
getchar();
switch(input)
{
case 'y':
break;
case 'n':
return;
default:
return;
}
}
}
void addMem()
{
dumpMem();
string repr;
cout << "Enter stimulus: ";
getline(cin, repr);
Node newMemory(repr);
memory.push_back(newMemory);
gatherConnections(&(memory[memory.size()-1]));
dumpMem();
}
void gatherConnections(Node * newMemory)
{
char input;
char input2;
string minput;
string typs;
bool typer;
cout << "Do you wish to make connections? [yn]: ";
cin >> input2;
getchar();
switch(input2)
{
case 'y':
break;
case 'n':
return;
default:
return;
}
while(true)
{
cout << "Enter name of memory to connect: ";
getline(cin, minput);
cout << "Enter type of connection [isa/p]: ";
getline(cin, typs);
if(typs == "isa")
typer = true;
else
typer = false;
for(int i = 0; i < memory.size(); ++i)
{
if(memory.getRep() == minput)
{
(*newMemory).addConnection(&memory, typer);
memory.addConnection(newMemory, typer);
(*newMemory).debug();
memory.debug();
break;
}
}
cout << "Add another connection? [yn]: ";
cin >> input;
getchar();
switch(input)
{
case 'y':
break;
case 'n':
return;
default:
return;
}
}
}
void actMem()
{
string toAct;
cout << "Enter stimulus: ";
getline(cin, toAct);
for(int i = 0; i < memory.size(); ++i)
{
if(memory.getRep() == toAct)
{
cout << "Begin: ";
memory.activate(memory.size());
break;
}
}
}
void dumpMem()
{
for(int i = 0; i < memory.size(); ++i)
{
cout << i << " " << &memory << " " << memory.getRep() << endl;
}
}
representation as a network of concepts. AI is a hobby of mine and I
just began to throw the program together. I didn't use the best style,
but it still should have worked. (You can guess what's coming next!)
It didn't! At the debug() call the program starts printing random
data. I am running Mac OS X 10.3 using XCode. Here is the file, I hope
it isn't too long. Thank you for your help.
#include <iostream>
#include <string>
#include <vector>
#include <stdlib.h>
using namespace std;
class Node
{
public:
Node(string repr) { rep = repr;}
void addConnection(Node * newc, bool typer) {
connections.push_back(newc); types.push_back(typer); }
string getRep() { return rep; }
void setRep(string newr) { rep = newr; }
void activate(int strength);
void debug();
private:
string rep;
vector<bool> types;
vector<Node*> connections;
};
vector<Node> memory;
bool temp;
void Node::activate(int strength)
{
cout << strength << "\t" << rep << endl;
srand(time(NULL));
int ns = strength-(1 + rand() % (memory.size()-1));
if(ns <= 0)
return;
for(int i = 0; i < connections.size(); ++i)
{
if(types)
cout << "Isa: ";
else
cout << "P: ";
(*connections).activate(ns);
}
}
void Node::debug()
{
cout << "!DEBUGing " << rep << endl;
for(int i = 0; i < connections.size(); ++i)
{
cout << i << " " << (*connections).getRep() << endl;
}
cout << "DEBUG!" << endl;
}
void addNewMem();
void actMem();
void addMem();
void gatherConnections(Node * newMemory);
void dumpMem();
int main()
{
int choice = 0;;
for(;
{
cout << "\nWhat to do?" << endl;
cout << " (1) Add new memory" << endl;
cout << " (2) Activate memory" << endl;
cout << " (3) Exit" << endl;
cin >> choice;
getchar();
switch(choice)
{
case 1:
addNewMem();
break;
case 2:
actMem();
break;
case 3:
cout << "Positronic Brains Coming Soon To A Store Near You!" <<
endl;
return 0;
default:
cout << "Butthead" << endl;
return 0;
}
}
return 0;
}
void addNewMem()
{
char input;
while(true)
{
addMem();
cout << "Do you want to create another memory? [yn]: ";
cin >> input;
getchar();
switch(input)
{
case 'y':
break;
case 'n':
return;
default:
return;
}
}
}
void addMem()
{
dumpMem();
string repr;
cout << "Enter stimulus: ";
getline(cin, repr);
Node newMemory(repr);
memory.push_back(newMemory);
gatherConnections(&(memory[memory.size()-1]));
dumpMem();
}
void gatherConnections(Node * newMemory)
{
char input;
char input2;
string minput;
string typs;
bool typer;
cout << "Do you wish to make connections? [yn]: ";
cin >> input2;
getchar();
switch(input2)
{
case 'y':
break;
case 'n':
return;
default:
return;
}
while(true)
{
cout << "Enter name of memory to connect: ";
getline(cin, minput);
cout << "Enter type of connection [isa/p]: ";
getline(cin, typs);
if(typs == "isa")
typer = true;
else
typer = false;
for(int i = 0; i < memory.size(); ++i)
{
if(memory.getRep() == minput)
{
(*newMemory).addConnection(&memory, typer);
memory.addConnection(newMemory, typer);
(*newMemory).debug();
memory.debug();
break;
}
}
cout << "Add another connection? [yn]: ";
cin >> input;
getchar();
switch(input)
{
case 'y':
break;
case 'n':
return;
default:
return;
}
}
}
void actMem()
{
string toAct;
cout << "Enter stimulus: ";
getline(cin, toAct);
for(int i = 0; i < memory.size(); ++i)
{
if(memory.getRep() == toAct)
{
cout << "Begin: ";
memory.activate(memory.size());
break;
}
}
}
void dumpMem()
{
for(int i = 0; i < memory.size(); ++i)
{
cout << i << " " << &memory << " " << memory.getRep() << endl;
}
}