|
| 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 | +} |
0 commit comments