Skip to content

Commit a43ee4e

Browse files
committed
Make a copy of Misgana's old experiments
1 parent 2240276 commit a43ee4e

8 files changed

+764
-0
lines changed

Diff for: experiments/CMakeLists.txt

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
INCLUDE_DIRECTORIES(${CMAKE_BINARY_DIR})
2+
3+
ADD_LIBRARY(attentionexperiment SHARED
4+
ExperimentSetupModule
5+
SentenceGenStimulateAgent
6+
)
7+
8+
TARGET_LINK_LIBRARIES(attentionexperiment
9+
${ATTENTION_LIBRARIES}
10+
${COGSERVER_LIBRARIES}
11+
${ATOMSPACE_LIBRARIES}
12+
)

Diff for: experiments/ExperimentSetupModule.cc

+210
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
/*
2+
* ExperimentSetupModule.cc
3+
*
4+
* Created on: Apr 19, 2015
5+
* Author: misgana
6+
*/
7+
8+
#include <sstream>
9+
#include <string>
10+
#include <fstream>
11+
12+
#include <stdio.h>
13+
#include <chrono>
14+
#include <thread>
15+
16+
#include <boost/algorithm/string/predicate.hpp>
17+
18+
#include <opencog/atomspace/AtomSpace.h>
19+
#include <opencog/guile/SchemeEval.h>
20+
21+
#include <opencog/attention/atom_types.h>
22+
23+
#include <opencog/cogserver/server/CogServer.h>
24+
#include <opencog/cogserver/server/Module.h>
25+
26+
#include <opencog/util/Config.h>
27+
#include <opencog/util/Logger.h>
28+
29+
#include "ExperimentSetupModule.h"
30+
31+
using namespace opencog;
32+
using namespace opencog::ECANExperiment;
33+
using namespace std::chrono;
34+
35+
DECLARE_MODULE(ExperimentSetupModule);
36+
37+
std::string ExperimentSetupModule::file_name;
38+
39+
std::vector<std::vector<std::string>> opencog::ECANExperiment::swords;
40+
std::vector<std::string> opencog::ECANExperiment::words;
41+
42+
int opencog::ECANExperiment::current_group = 0;
43+
44+
int opencog::ECANExperiment::special_word_occurence_period = 1;
45+
46+
ExperimentSetupModule::ExperimentSetupModule(CogServer& cs) :
47+
Module(cs), _cs(cs)
48+
{
49+
_log = new Logger(); //new Logger("ecanexpe.log");
50+
_log->fine("uuid,cycle,sti_old,sti_new,lti,vlti,wage,rent,agent_name");
51+
_as = &_cs.getAtomSpace();
52+
53+
_AVChangedSignalConnection = _as->AVChangedSignal(
54+
boost::bind(&ExperimentSetupModule::AVChangedCBListener, this, _1,
55+
_2, _3));
56+
_AVChangedSignalConnection = _as->TVChangedSignal(
57+
boost::bind(&ExperimentSetupModule::TVChangedCBListener, this, _1,
58+
_2, _3));
59+
60+
file_name = std::string(PROJECT_SOURCE_DIR)
61+
+ "/experiments/attention/dump";
62+
63+
}
64+
65+
ExperimentSetupModule::~ExperimentSetupModule()
66+
{
67+
unregisterAgentRequests();
68+
}
69+
70+
void ExperimentSetupModule::AVChangedCBListener(const Handle& h,
71+
const AttentionValuePtr& av_old,
72+
const AttentionValuePtr& av_new)
73+
{
74+
NodePtr node;
75+
std::string name;
76+
77+
if (h->isNode()) {
78+
node = NodeCast(h);
79+
name = node->getName();
80+
if (name.find("@") == std::string::npos)
81+
{
82+
std::ofstream outav(file_name + "-av.data", std::ofstream::app);
83+
outav << name << ","
84+
<< av_new->getSTI() << ","
85+
<< av_new->getLTI() << ","
86+
<< av_new->getVLTI() << ","
87+
<< system_clock::now().time_since_epoch().count() << ","
88+
<< current_group << ","
89+
<< _as->get_attentional_focus_boundary() << "\n";
90+
outav.close();
91+
}
92+
}
93+
}
94+
95+
void ExperimentSetupModule::TVChangedCBListener(const Handle& h,
96+
const TruthValuePtr& tv_old,
97+
const TruthValuePtr& tv_new)
98+
{
99+
if (h->getType() == ASYMMETRIC_HEBBIAN_LINK) {
100+
HandleSeq outg = LinkCast(h)->getOutgoingSet();
101+
assert(outg.size() == 2);
102+
103+
if (!outg[0]->isNode() or !outg[1]->isNode())
104+
return;
105+
std::string nn0 = NodeCast(outg[0])->getName();
106+
std::string nn1 = NodeCast(outg[1])->getName();
107+
108+
if (boost::starts_with(nn0, "group") and boost::starts_with(nn1, "group")
109+
and !boost::contains(nn0,"@") and !boost::contains(nn1,"@"))
110+
{
111+
std::ofstream outheb(file_name + "-hebtv.data", std::ofstream::app);
112+
outheb << h.value() << ","
113+
<< nn0 << ","
114+
<< nn1 << ","
115+
<< tv_new->getMean() << ","
116+
<< tv_new->getConfidence() << ","
117+
<< system_clock::now().time_since_epoch().count() << "\n";
118+
outheb.close();
119+
}
120+
}
121+
}
122+
123+
void ExperimentSetupModule::registerAgentRequests()
124+
{
125+
do_start_exp_register();
126+
do_stop_exp_register();
127+
}
128+
129+
void ExperimentSetupModule::unregisterAgentRequests()
130+
{
131+
do_start_exp_unregister();
132+
do_stop_exp_unregister();
133+
}
134+
void ExperimentSetupModule::init(void)
135+
{
136+
registerAgentRequests();
137+
138+
_cs.registerAgent(SentenceGenStimulateAgent::info().id,
139+
&sentenceGenStimulateFactory);
140+
141+
_sentencegenstim_agentptr = _cs.createAgent(
142+
SentenceGenStimulateAgent::info().id, false);
143+
}
144+
145+
std::string ExperimentSetupModule::do_start_exp(Request *req,
146+
std::list<std::string> args)
147+
{
148+
int groups;
149+
int groupsize;
150+
int wordcount;
151+
std::string output;
152+
if (args.size() != 3){
153+
groups = 10;
154+
groupsize = 10;
155+
wordcount = 100;
156+
output = output + "Using Default Configuration \n";
157+
} else {
158+
groups = std::stoi(args.front());
159+
args.pop_front();
160+
groupsize = std::stoi(args.front());
161+
args.pop_front();
162+
wordcount = std::stoi(args.front());
163+
}
164+
genWords(groups,groupsize,wordcount);
165+
166+
std::ofstream outf(file_name + "-av.data", std::ofstream::trunc);
167+
outf << groups << ","
168+
<< groupsize << ","
169+
<< wordcount << "\n";
170+
outf.flush();
171+
outf.close();
172+
173+
remove((file_name + "-hebtv.data").c_str());
174+
175+
_cs.startAgent(_sentencegenstim_agentptr);
176+
177+
output = output + "Started opencog::SentenceGenStimulateAgent\n";
178+
179+
return output;
180+
}
181+
182+
std::string ExperimentSetupModule::do_stop_exp(Request *req,
183+
std::list<std::string> args) {
184+
185+
_cs.stopAgent(_sentencegenstim_agentptr);
186+
187+
return "Stoped Experiment \n";
188+
}
189+
190+
void ExperimentSetupModule::genWords(int groups,int groupsize,int nonspecial)
191+
{
192+
swords.assign(groups,std::vector<std::string>(groupsize));
193+
194+
int i = 0;
195+
int j = 0;
196+
197+
for (auto &sv : swords) {
198+
for (std::string &word : sv) {
199+
word = "group" + std::to_string(i) + "word" + std::to_string(j++);
200+
}
201+
i++;
202+
j=0;
203+
}
204+
205+
words.assign(nonspecial,"");
206+
i = 0;
207+
208+
for (std::string &word : words)
209+
word = "nonspecial" + std::to_string(i++);
210+
}

Diff for: experiments/ExperimentSetupModule.h

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* PLNDynamicsExpSetupModule.h
3+
*
4+
* Created on: Apr 19, 2015
5+
* Author: misgana
6+
*/
7+
8+
#ifndef PLNDYNAMICSEXPSETUPMODULE_H_
9+
#define PLNDYNAMICSEXPSETUPMODULE_H_
10+
11+
#include "SentenceGenStimulateAgent.h"
12+
#include <opencog/cogserver/server/Agent.h>
13+
#include <opencog/cogserver/server/Factory.h>
14+
#include <opencog/attention/AttentionModule.h>
15+
16+
namespace opencog {
17+
18+
class AtomSpace;
19+
class CogServer;
20+
class SchemeEval;
21+
class Module;
22+
class Logger;
23+
24+
namespace ECANExperiment {
25+
26+
extern std::vector<std::vector<std::string>> swords;
27+
extern std::vector<std::string> words;
28+
29+
extern int special_word_occurence_period;
30+
31+
extern int current_group;
32+
33+
class ExperimentSetupModule: public Module {
34+
private:
35+
AgentPtr _sentencegenstim_agentptr;
36+
37+
Factory<SentenceGenStimulateAgent, Agent> sentenceGenStimulateFactory;
38+
39+
AtomSpace * _as;
40+
CogServer& _cs;
41+
Logger * _log;
42+
43+
boost::signals2::connection _AVChangedSignalConnection,_TVChangedSignalConnection,_AtomAddedSignalConnection;
44+
45+
void AVChangedCBListener(const Handle& h, const AttentionValuePtr& av_old,
46+
const AttentionValuePtr& av_new);
47+
48+
void TVChangedCBListener(const Handle& h, const TruthValuePtr& av_old,
49+
const TruthValuePtr& tv_new);
50+
51+
//Load word dict.
52+
DECLARE_CMD_REQUEST(ExperimentSetupModule, "start-exp",do_start_exp,
53+
"Loads word dict for experimentation. The word dict file should have two sections\n"
54+
"SPEICAL_WORDS = [comma separated words]\n"
55+
"NON_SPECIAL_WORDS = [comma separated words] size\n",
56+
"Usage: load-word-dict [FILE_NAME]\n"
57+
"Dump time serise ECAN data\n",
58+
false, true)
59+
60+
DECLARE_CMD_REQUEST(ExperimentSetupModule, "stop-exp",do_stop_exp,
61+
"Loads word dict for experimentation. The word dict file should have two sections\n"
62+
"SPEICAL_WORDS = [comma separated words]\n"
63+
"NON_SPECIAL_WORDS = [comma separated words] size\n",
64+
"Usage: load-word-dict [FILE_NAME]\n"
65+
"Dump time serise ECAN data\n",
66+
false, true)
67+
68+
69+
void registerAgentRequests();
70+
void unregisterAgentRequests();
71+
72+
void genWords(int groups,int groupsize,int nonspecial);
73+
74+
public:
75+
ExperimentSetupModule(CogServer&);
76+
virtual ~ExperimentSetupModule();
77+
static inline const char* id();
78+
virtual void init(void);
79+
80+
std::string dump_ecan_data();
81+
static std::string file_name;
82+
83+
//std::ofstream outav;
84+
//std::ofstream outheb;
85+
86+
};
87+
}
88+
89+
} /* namespace opencog */
90+
91+
#endif /* EXPERIMENTSETUPMODULE_H_ */

Diff for: experiments/README.md

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
2+
ECAN/PLN experimentation
3+
-----------------------
4+
The purposes of this experiment are:
5+
6+
- Test HebbianLink creation between co-occuring atoms
7+
8+
- STI propagation works properly both along HebbianLinks and atoms inside the Attentional Focus
9+
10+
- Implementing Forgetting of atoms based on a threshold of memory for the atomspace
11+
12+
Once we are satisfied with the behaviour of the ECAN system, we might extend the experiment
13+
to see how interaction with other opencog agents works out.
14+
15+
16+
Code organization
17+
-----------------
18+
19+
ExperimentSetupModule- A module that registers commands used for this particular experiments.The commands are *ecan-load,
20+
ecan-start, ecan-pause, start-word-stimulator, pause-word-stimulator, stimulate-atoms, dump-tseries,
21+
and load-word-dict*. Please type help COMMAND to get usage of these commands.
22+
23+
ArtificialStimulatorAgent - Used for fake stimulation of atoms. Since stimulation can only be provided via agents, this Agent
24+
used to give artificial stimulation to atoms when the command stimulate-atoms is isssued.
25+
26+
SentenceGenStimulateAgent - Used for the experiment described [here](http://wiki.opencog.org/wikihome/index.php/Attention_Allocation#Ideas_for_simple_ECAN_tests)
27+
28+
visualization/ - Assorted scripts for plotting and processing dumped data.
29+
30+
Running the experiment
31+
----------------------
32+
33+
**Step 1** - Get the ecan-experiment branch and build
34+
35+
1. cd to opencog directory
36+
37+
2. git remote add misgana https://github.com/misgeatgit/opencog
38+
39+
3. git fetch misgana
40+
41+
4. git checkout --track misgana/ecan-wbench
42+
43+
5. go to you build directory ( mkdir build && cd build)
44+
45+
6. make
46+
47+
48+
**Step 2** - Start cogserver and load and start surprisingness mind agent
49+
50+
1. start cogserver by typing (**./opencog/server/cogserver**) without leaving the build directory
51+
52+
2. open another terminal and type **telnet localhost 17001** OR preferably **rlwrap telnet localhost 17001** if you have rlwrap installed
53+
54+
55+
**Step 3** - Running the Simple experiment
56+
57+
1. type ecan-load followed by ecan-start to start all ECAN agents
58+
59+
2. Load the word-dict.conf file found in data dir by typing the command load-word-dict ABS_PATH_TO_word-dict.conf
60+
61+
3. Run start-word-stimulator command to start WordNode and WordNodeInstances creation and stimulation
62+
63+
4. After letting it run for a while, type dump-tseries ABS_PATH_TO/visualization/dump
64+
will create files named dump-sw.data and dump-nsw.data (sw for special words and nsw for non-special words).
65+
66+
5. Plot the graphs for both speical and non spcial words by using the following commands *./plot.sh [sw|nsw] [sti|lti|vlti]*
67+
68+
69+
70+
71+
72+
73+
TODO
74+
----
75+
76+
77+

0 commit comments

Comments
 (0)