diff --git a/src/agents/link_creation_agent/LinkCreationProcessor.cc b/src/agents/link_creation_agent/LinkCreationProcessor.cc index 93d77d58c..e18b09b43 100644 --- a/src/agents/link_creation_agent/LinkCreationProcessor.cc +++ b/src/agents/link_creation_agent/LinkCreationProcessor.cc @@ -68,7 +68,7 @@ void LinkCreationProcessor::thread_process_one_query(shared_ptr proxy->untokenize(proxy->args); string command = proxy->get_command(); if (command == ServiceBus::LINK_CREATION) { - LOG_INFO("Proxy: " << proxy->to_string()); + LOG_INFO("(Thread: " + std::to_string(gettid()) + ") Proxy: " << proxy->to_string()); this->link_creation(monitor, proxy); } else { RAISE_ERROR("Invalid command " + command + " in LinkCreationProcessor"); @@ -182,7 +182,8 @@ void LinkCreationProcessor::link_creation(shared_ptr monitor, count_created += stats.created; count_used_query_answers++; count_query_answers_in_cycle++; - LOG_DEBUG("Created links: " + std::to_string(count_query_answers_in_cycle)); + LOG_DEBUG("Answers used to create links: " + + std::to_string(count_query_answers_in_cycle)); unproductive_visit = 0; visit_attempts = 0; proxy->push(query_answer); @@ -210,6 +211,7 @@ void LinkCreationProcessor::link_creation(shared_ptr monitor, } } proxy->flush_determiners(); + proxy->flush_stimuli(); proxy->cycle_ended(); if (!pm_proxy->finished()) { // stopping pattern matching query diff --git a/src/agents/link_creation_agent/LinkCreationProxy.cc b/src/agents/link_creation_agent/LinkCreationProxy.cc index 93c670765..2c9448613 100644 --- a/src/agents/link_creation_agent/LinkCreationProxy.cc +++ b/src/agents/link_creation_agent/LinkCreationProxy.cc @@ -18,6 +18,7 @@ string LinkCreationProxy::MAX_VISIT_ATTEMPTS_PER_ROUND = "max_visit_attempts_per string LinkCreationProxy::MAX_ROUNDS = "max_rounds"; string LinkCreationProxy::LINK_CREATION_STRENGTH_THRESHOLD = "link_creation_strength_threshold"; string LinkCreationProxy::LINK_CREATION_LOG_FILE_NAME = "link_creation_log_file_name"; +string LinkCreationProxy::LINK_CREATION_SPREAD_ACTIVATION = "link_creation_spread_activation"; string LinkCreationProxy::LOG_NEW_LINKS = "log_new_links"; string LinkCreationProxy::LINK_CREATOR_EXTRA_PARAMETERS = "link_creator_extra_parameters"; @@ -124,7 +125,7 @@ bool LinkCreationProxy::stop_criteria_met() { void LinkCreationProxy::set_link_creator_function_tag(const string& tag) { STACK_TRACE(); - LOG_INFO("LinkCreator: " + tag); + LOG_DEBUG("LinkCreator: " + tag); lock_guard semaphore(this->api_mutex); if (tag == LinkCreatorRegistry::REMOTE_FUNCTION) { RAISE_ERROR("Remote evaluation of link creators is not implemented yet."); @@ -147,6 +148,9 @@ void LinkCreationProxy::set_link_creator_function_tag(const string& tag) { this->parameters.get(LOG_NEW_LINKS)); this->link_creation_function_object->extra_parameters( this->parameters.get_or(LINK_CREATOR_EXTRA_PARAMETERS, "")); + this->link_creation_function_object->set_stimuli( + this->parameters.get_or(LINK_CREATION_SPREAD_ACTIVATION, false)); + this->link_creation_function_object->set_context(get_context()); } } } @@ -173,6 +177,23 @@ void LinkCreationProxy::flush_determiners() { } } +void LinkCreationProxy::flush_stimuli() { + STACK_TRACE(); + if ((this->link_creation_function_object != nullptr) && + (this->link_creation_function_object->buffer_stimuli().size() > 0)) { + AttentionBrokerClient::stimulate(this->link_creation_function_object->buffer_stimuli(), + get_context()); + this->link_creation_function_object->clear_stimuli(); + } +} + +void LinkCreationProxy::cycle_ended() { + if (this->link_creation_function_object != nullptr) { + this->link_creation_function_object->reset_visited(); + } + BaseQueryProxy::cycle_ended(); +} + // --------------------------------------------------------------------------------------------- // Virtual superclass API and the piggyback methods called by it diff --git a/src/agents/link_creation_agent/LinkCreationProxy.h b/src/agents/link_creation_agent/LinkCreationProxy.h index 4385a9d61..f79f6dec9 100644 --- a/src/agents/link_creation_agent/LinkCreationProxy.h +++ b/src/agents/link_creation_agent/LinkCreationProxy.h @@ -33,13 +33,18 @@ class LinkCreationProxy : public BaseQueryProxy { // Optional parameter which are not part of the configuration file static string LOG_NEW_LINKS; - static string LINK_CREATOR_EXTRA_PARAMETERS; // LINK_CREATOR_EXTRA_PARAMETERS is tipically - // parsed as a list of tokens separated by a - // character defined in - // CustomizableLinkCreator::EXTRA_PARAMETERS_SPLIT_CHAR - // If the separator character is used inside - // LINK_CREATOR_EXTRA_PARAMETERS, the parsing - // will fail, even if it's quoted or escaped. + + static string LINK_CREATION_SPREAD_ACTIVATION; // Flag to indicate if activation spreading is + // supposed to be performed at the end of each + // link creation cycle. + + static string LINK_CREATOR_EXTRA_PARAMETERS; // LINK_CREATOR_EXTRA_PARAMETERS is tipically + // parsed as a list of tokens separated by a + // character defined in + // EXTRA_PARAMETERS_SPLIT_CHAR + // If the separator character is used inside + // LINK_CREATOR_EXTRA_PARAMETERS, the parsing + // will fail, even if it's quoted or escaped. LinkCreationProxy(); @@ -102,6 +107,12 @@ class LinkCreationProxy : public BaseQueryProxy { */ void flush_determiners(); + /** + * Call Attention Broker to spread activation according to a state buffer with accumulated + * stimuli from one entire link creation round. This buffer is emptied as a side effect. + */ + void flush_stimuli(); + // --------------------------------------------------------------------------------------------- // Virtual superclass API and the piggyback methods called by it @@ -125,6 +136,11 @@ class LinkCreationProxy : public BaseQueryProxy { */ void tokenize(vector& output) override; + /** + * Notifies remote proxy that a cycle just ended. + */ + virtual void cycle_ended() override; + private: void set_link_creator_function_tag(const string& tag); void init(); diff --git a/src/agents/link_creation_agent/link_creators/AndTwoPredicates.cc b/src/agents/link_creation_agent/link_creators/AndTwoPredicates.cc index c92642521..454f8f15f 100644 --- a/src/agents/link_creation_agent/link_creators/AndTwoPredicates.cc +++ b/src/agents/link_creation_agent/link_creators/AndTwoPredicates.cc @@ -1,5 +1,6 @@ #include "AndTwoPredicates.h" +#include "AtomDBUtils.h" #include "tags.h" using namespace link_creators; @@ -16,6 +17,16 @@ AndTwoPredicates::~AndTwoPredicates() {} LinkCreationStats AndTwoPredicates::create(shared_ptr query_answer) { STACK_TRACE(); + LinkCreationStats stats; + double strength = 1; + for (string& h : query_answer->get_handles_vector()) { + strength *= AtomDBUtils::get_strength(h); + } + if (!Utils::epsilon_equals(strength, 1.0)) { + LOG_DEBUG("AndTwoPredicates discarding low strength (" + std::to_string(strength) + + ") link. QueryAnswer: " + query_answer->to_string(true)); + return stats; + } string concept_ = query_answer->get(CONCEPT); string predicates[2]; predicates[0] = query_answer->get(PREDICATE1); @@ -25,24 +36,23 @@ LinkCreationStats AndTwoPredicates::create(shared_ptr query_answer) predicates[0] = predicates[1]; predicates[1] = aux; } - string key = predicates[0] + " " + predicates[1] + concept_; + string key = predicates[0] + " " + predicates[1] + " " + concept_; - LinkCreationStats stats; if (predicates[0] != predicates[1]) { if (!visited(key)) { visit(key); stats.visited = true; + AddLinkStatus add_status; set mentioned_predicates0, mentioned_predicates1; - extract_mentioned_predicates(mentioned_predicates0, predicates[0]); - extract_mentioned_predicates(mentioned_predicates1, predicates[1]); + AtomDBUtils::reachable_terminal_set(mentioned_predicates0, predicates[0], true); + AtomDBUtils::reachable_terminal_set(mentioned_predicates1, predicates[1], true); if (!Utils::intersects(mentioned_predicates0, mentioned_predicates1)) { vector targets = {LOGICAL_AND_HANDLE, predicates[0], predicates[1]}; - if (add_or_update_link(targets, 1.0) == CREATED) { + add_status = add_or_update_link(targets, 1.0); + if (add_status == CREATED) { stats.created++; - } - double strength = 1; - for (string& h : query_answer->get_handles_vector()) { - strength *= get_strength(h); + } else if (add_status == UPDATED) { + stats.updated++; } string new_predicate_handle = Hasher::link_handle(EXPRESSION, targets); AddLinkStatus add_status = @@ -73,20 +83,3 @@ LinkCreationStats AndTwoPredicates::create(shared_ptr query_answer) } return stats; } - -void AndTwoPredicates::extract_mentioned_predicates(set& mentioned, const string& handle) { - STACK_TRACE(); - shared_ptr node; - shared_ptr link = atomdb()->get_link(handle); - if (link != nullptr) { - for (string& target_handle : link->targets) { - if ((node = atomdb()->get_node(target_handle)) != nullptr) { - if ((node->name != PREDICATE_TAG) && (node->name != LOGICAL_AND_TAG)) { - mentioned.insert(node->name); - } - } else { - extract_mentioned_predicates(mentioned, target_handle); - } - } - } -} diff --git a/src/agents/link_creation_agent/link_creators/AndTwoPredicates.h b/src/agents/link_creation_agent/link_creators/AndTwoPredicates.h index 5f916cd01..ddf389ba6 100644 --- a/src/agents/link_creation_agent/link_creators/AndTwoPredicates.h +++ b/src/agents/link_creation_agent/link_creators/AndTwoPredicates.h @@ -36,8 +36,6 @@ class AndTwoPredicates : public LinkCreator { private: static string LOGICAL_AND_HANDLE; static string EVALUATION_HANDLE; - - void extract_mentioned_predicates(set& mentioned, const string& handle); }; } // namespace link_creators diff --git a/src/agents/link_creation_agent/link_creators/CustomizableLinkCreator.cc b/src/agents/link_creation_agent/link_creators/CustomizableLinkCreator.cc index 40b264383..aa2468f36 100644 --- a/src/agents/link_creation_agent/link_creators/CustomizableLinkCreator.cc +++ b/src/agents/link_creation_agent/link_creators/CustomizableLinkCreator.cc @@ -41,10 +41,24 @@ void CustomizableLinkCreator::LinkSpecification::check() { } } -CustomizableLinkCreator::CustomizableLinkCreator() {} +CustomizableLinkCreator::CustomizableLinkCreator(bool check_nested_target_reference) + : check_nested_target_reference(check_nested_target_reference) {} CustomizableLinkCreator::~CustomizableLinkCreator() {} +bool CustomizableLinkCreator::check_targets(vector& targets) { + if (this->check_nested_target_reference) { + if (targets.size() != 2) { + RAISE_ERROR("Nested target references check is only available for links of arity 2"); + } + set mentioned0, mentioned1; + AtomDBUtils::reachable_terminal_set(mentioned0, targets[0], true); + AtomDBUtils::reachable_terminal_set(mentioned1, targets[1], true); + return !Utils::intersects(mentioned0, mentioned1); + } + return true; +} + LinkCreationStats CustomizableLinkCreator::create(shared_ptr query_answer) { STACK_TRACE(); LinkCreationStats stats; @@ -53,20 +67,33 @@ LinkCreationStats CustomizableLinkCreator::create(shared_ptr query_ RAISE_ERROR("Invalid empty target elements or link_type"); break; } - vector handles; + vector targets, handles; handles.push_back(Hasher::node_handle(SYMBOL, spec.link_type)); for (QueryAnswerElement& element : spec.target_elements) { - handles.push_back(query_answer->get(element)); + string handle = query_answer->get(element); + targets.push_back(handle); + handles.push_back(handle); } - string key = Utils::join(handles, ' '); + string key = spec.link_type + " " + Utils::join(targets, ' '); if (!visited(key)) { visit(key); stats.visited = true; - AddLinkStatus add_status = add_or_update_link(handles, compute_strength(query_answer, spec)); - if (add_status == CREATED) { - stats.created++; - } else if (add_status == UPDATED) { - stats.updated++; + if (check_targets(targets)) { + vector strengths = compute_strength(query_answer, spec); + AddLinkStatus add_status = add_or_update_link(handles, strengths[0]); + if (add_status == CREATED) { + stats.created++; + } else if (add_status == UPDATED) { + stats.updated++; + } + if (spec.create_reverse() && (strengths.size() == 2)) { + add_status = add_or_update_link({handles[0], handles[2], handles[1]}, strengths[1]); + if (add_status == CREATED) { + stats.created++; + } else if (add_status == UPDATED) { + stats.updated++; + } + } } } } @@ -81,6 +108,12 @@ void CustomizableLinkCreator::extra_parameters(const string& extra_parameters) { } } +string CustomizableLinkCreator::extra_parameters() { + vector tokens; + tokenize(tokens); + return Utils::join(tokens, EXTRA_PARAMETERS_SPLIT_CHAR); +} + void CustomizableLinkCreator::add_link_specification(const vector& target_elements, const vector& strength_elements, const string& link_type, @@ -98,6 +131,7 @@ void CustomizableLinkCreator::add_link_specification(const vector& tokens) { STACK_TRACE(); + tokens.push_back(this->check_nested_target_reference ? "1" : "0"); tokens.push_back(std::to_string(this->link_specification.size())); for (LinkSpecification& spec : this->link_specification) { tokens.push_back(std::to_string(spec.target_elements.size())); @@ -128,6 +162,8 @@ static inline string& safe_get_next_token(vector& tokens, unsigned int& void CustomizableLinkCreator::untokenize(vector& tokens) { STACK_TRACE(); unsigned int cursor = 0; + unsigned int flag = Utils::string_to_uint(safe_get_next_token(tokens, cursor)); + this->check_nested_target_reference = (flag > 0); unsigned int num_specs = Utils::string_to_uint(safe_get_next_token(tokens, cursor)); for (unsigned int i = 0; i < num_specs; i++) { vector _target_elements; @@ -234,7 +270,7 @@ void CustomizableLinkCreator::compute_counts(shared_ptr base_query_ } else { d = 1; for (string& h : query_answer->get_handles_vector()) { - d *= get_strength(h); + d *= AtomDBUtils::get_strength(h); } handle = query_answer->get(spec.strength_elements[i]); insert_or_update(count_map[i], handle, d); @@ -268,42 +304,48 @@ void CustomizableLinkCreator::compute_counts(shared_ptr base_query_ to_string(count_intersection) + " " + to_string(count_union)); } -double CustomizableLinkCreator::compute_strength(shared_ptr query_answer, - LinkSpecification& spec) { +vector CustomizableLinkCreator::compute_strength(shared_ptr query_answer, + LinkSpecification& spec) { STACK_TRACE(); - double answer = 0.0; + vector answer; + double strength; double count_A = 0.0; double count_B = 0.0; double count_intersection = 0.0; double count_union = 0.0; if ((spec.strength_composition == INTERSECTION_OVER_UNION) || - (spec.strength_composition == INTERSECTION_OVER_A) || - (spec.strength_composition == INTERSECTION_OVER_B)) { + (spec.strength_composition == INTERSECTION_OVER_A)) { compute_counts(query_answer, spec, count_A, count_B, count_intersection, count_union); } switch (spec.strength_composition) { case PRODUCT: - answer = 1.0; + strength = 1.0; for (QueryAnswerElement& element : spec.strength_elements) { - answer *= get_strength(query_answer->get(element)); + strength *= AtomDBUtils::get_strength(query_answer->get(element)); } + answer.push_back(strength); break; case INTERSECTION_OVER_UNION: + strength = 0.0; if (!Utils::is_zero(count_union)) { - answer = count_intersection / count_union; + strength = count_intersection / count_union; } + answer.push_back(strength); + answer.push_back(strength); break; case INTERSECTION_OVER_A: + strength = 0.0; if (!Utils::is_zero(count_A)) { - answer = count_intersection / count_A; + strength = count_intersection / count_A; } - break; - case INTERSECTION_OVER_B: + answer.push_back(strength); + strength = 0.0; if (!Utils::is_zero(count_B)) { - answer = count_intersection / count_B; + strength = count_intersection / count_B; } + answer.push_back(strength); break; default: RAISE_ERROR("Invalid strength composition: " + std::to_string(spec.strength_composition)); diff --git a/src/agents/link_creation_agent/link_creators/CustomizableLinkCreator.h b/src/agents/link_creation_agent/link_creators/CustomizableLinkCreator.h index 95956d8c9..e24c51b26 100644 --- a/src/agents/link_creation_agent/link_creators/CustomizableLinkCreator.h +++ b/src/agents/link_creation_agent/link_creators/CustomizableLinkCreator.h @@ -19,15 +19,15 @@ class CustomizableLinkCreator : public LinkCreator { PRODUCT, INTERSECTION_OVER_UNION, INTERSECTION_OVER_A, - INTERSECTION_OVER_B }; static char EXTRA_PARAMETERS_SPLIT_CHAR; - CustomizableLinkCreator(); + CustomizableLinkCreator(bool check_nested_target_reference = false); ~CustomizableLinkCreator(); LinkCreationStats create(shared_ptr query_answer); virtual void extra_parameters(const string& extra_parameters); + virtual string extra_parameters(); private: class LinkSpecification { @@ -42,12 +42,17 @@ class CustomizableLinkCreator : public LinkCreator { string link_type; StrengthComposition strength_composition; vector queries; + bool create_reverse() { + return (this->strength_composition == INTERSECTION_OVER_UNION) || + (this->strength_composition == INTERSECTION_OVER_A); + } private: void check(); }; vector link_specification; + bool check_nested_target_reference; void insert_or_update(map& count_map, const string& key, double value); shared_ptr issue_link_count_query(const string& query_str); @@ -57,7 +62,10 @@ class CustomizableLinkCreator : public LinkCreator { double& count_B, double& count_intersection, double& count_union); - double compute_strength(shared_ptr query_answer, LinkSpecification& spec); + vector compute_strength(shared_ptr query_answer, LinkSpecification& spec); + + protected: + virtual bool check_targets(vector& targets); public: void tokenize(vector& tokens); diff --git a/src/agents/link_creation_agent/link_creators/LinkCreator.cc b/src/agents/link_creation_agent/link_creators/LinkCreator.cc index deedda9dc..dd5dec10f 100644 --- a/src/agents/link_creation_agent/link_creators/LinkCreator.cc +++ b/src/agents/link_creation_agent/link_creators/LinkCreator.cc @@ -13,31 +13,37 @@ LinkCreator::LinkCreator() { this->_strength_threshold = 0; this->_link_creation_log_file_name = ""; this->_log_new_links = false; + this->_activation_spreading_flag = false; } LinkCreator::AddLinkStatus LinkCreator::add_or_update_link(const vector& targets, double strength) { STACK_TRACE(); - if (strength < this->_strength_threshold) { - LOG_DEBUG("Rejecting low stregnth link " << strength << " < " << this->_strength_threshold); - return REJECTED; - } auto db = atomdb(); - bool new_link_created_flag = false; shared_ptr new_link = make_shared(Link(EXPRESSION, targets, true, {{STRENGTH_TAG, strength}})); LOG_DEBUG("Add or update: " + new_link->to_string()); string handle = new_link->handle(); if (db->link_exists(handle)) { auto old_link = db->get_atom(handle); - LOG_DEBUG("Link already exists: " + old_link->to_string() + ". Updating"); - if (strength != old_link->custom_attributes.get_or(STRENGTH_TAG, 1)) { + LOG_DEBUG("Link already exists: " + old_link->to_string() + ". Updating it."); + if (Utils::epsilon_equals(strength, + old_link->custom_attributes.get_or(STRENGTH_TAG, 1))) { + // No change in strength. No need to update. + return REJECTED; + } else { // Default merger (NULL) upserts/replaces the existing atom. + for (auto& target_handle : new_link->targets) { + add_stimulus(target_handle, strength); + } db->add_link(new_link.get()); + return UPDATED; } - } else { - new_link_created_flag = true; + } else if (strength >= this->_strength_threshold) { LOG_DEBUG("Adding new Link to AtomDB"); + for (auto& target_handle : new_link->targets) { + add_stimulus(target_handle, strength); + } db->add_link(new_link.get()); this->newly_created_links.push_back(handle); if (log_new_links()) { @@ -51,30 +57,27 @@ LinkCreator::AddLinkStatus LinkCreator::add_or_update_link(const vector& if (get_log_file() != "") { save_link_metta(new_link); } + return CREATED; + } else { + LOG_DEBUG("Rejecting low stregnth link " << strength << " < " << this->_strength_threshold); + return REJECTED; } - return (new_link_created_flag ? CREATED : UPDATED); } -string LinkCreator::get_node_name(const string& handle) { - STACK_TRACE(); - auto node = atomdb()->get_node(handle); - if (node == nullptr) { - return ""; +void LinkCreator::strength_threshold(double value) { + if ((value < 0.0) || (value > 1.0)) { + RAISE_ERROR("strength_threashold is expected to be in [0..1]"); } else { - return node->name; + this->_strength_threshold = value; } } -double LinkCreator::get_strength(const string& handle) { - STACK_TRACE(); - double answer = 1.0; - auto atom = atomdb()->get_atom(handle); - if (atom == nullptr) { - RAISE_ERROR("Atom does not exist: " + handle); - } else { - answer = atom->custom_attributes.get_or(STRENGTH_TAG, 1.0); +void LinkCreator::add_stimulus(const string& handle, double strength) { + if (this->_activation_spreading_flag && (strength > _strength_threshold)) { + double rescaled = (strength - _strength_threshold) / (1.0 - _strength_threshold); + this->_buffer_stimuli[handle] = + max((unsigned int) std::lround(rescaled * 1000), this->_buffer_stimuli[handle]); } - return answer; } void LinkCreator::save_link_metta(shared_ptr link) { diff --git a/src/agents/link_creation_agent/link_creators/LinkCreator.h b/src/agents/link_creation_agent/link_creators/LinkCreator.h index 8aebdeeaa..53ce77a31 100644 --- a/src/agents/link_creation_agent/link_creators/LinkCreator.h +++ b/src/agents/link_creation_agent/link_creators/LinkCreator.h @@ -86,7 +86,7 @@ class LinkCreator { * * @param value New value for sytrength threshold */ - inline void strength_threshold(double value) { this->_strength_threshold = value; } + void strength_threshold(double value); /** * Getter for the buffer for Attention Broker's importance determiners. @@ -95,11 +95,23 @@ class LinkCreator { */ inline const vector>& buffer_determiners() { return this->_buffer_determiners; } + /** + * Getter for the buffer for Attention Broker's stimuli. + * + * @return The buffer for Attention Broker's stimuli. + */ + inline const map& buffer_stimuli() { return this->_buffer_stimuli; } + /** * Clear the buffer for Attention Broker's importance determiners */ inline void clear_determiners() { this->_buffer_determiners.clear(); } + /** + * Clear the buffer for Attention Broker's stimuli + */ + inline void clear_stimuli() { this->_buffer_stimuli.clear(); } + /** * Sets the name of the link creation log file. Un empty name means that no logging of * new link creation is supposed to be made. @@ -124,13 +136,21 @@ class LinkCreator { inline bool log_new_links() { return this->_log_new_links; } /** - * Sets the flag which indicates is newly created links are supposed to be logged in the + * Sets the flag which indicates if newly created links are supposed to be logged in the * - * @param flag which indicates is newly created links are supposed to be logged in the default + * @param flag which indicates if newly created links are supposed to be logged in the default * logger. */ inline void set_log_new_links(bool value) { this->_log_new_links = value; } + /** + * Sets the flag which indicates if stimuli is supposed to be spread at the end of each cycle. + * + * @param flag which indicates if stimuli is supposed to be spread at the end of each cycle. + * logger. + */ + inline void set_stimuli(bool value) { this->_activation_spreading_flag = value; } + // ---------------------------------------------------------------------------------- // Concrete sub-classes API @@ -153,6 +173,16 @@ class LinkCreator { */ virtual void extra_parameters(const string& extra_parameters) {} + /** + * Concrete subclasses may implement this in order to create the extra parameters string + * which can be passed to extra_parameters(string). The idea is to build the LinkCreator + * using API methods and then calling this method in order to create a string which is + * passed through LINK_CREATOR_EXTRA_PARAMETERS from client proxy to server proxy. + * + * @return a string representation of the extra parameters required to build this LinkCreator + */ + virtual string extra_parameters() { return ""; } + /** * Return the AttentionBroker context to be used. * @@ -172,9 +202,8 @@ class LinkCreator { inline void add_determiners(vector& entry) { this->_buffer_determiners.push_back(entry); } AddLinkStatus add_or_update_link(const vector& targets, double strength); - double get_strength(const string& handle); - string get_node_name(const string& handle); void save_link_metta(shared_ptr link); + void add_stimulus(const string& handle, double strength); // ---------------------------------------------------------------------------------- // Private stuff @@ -184,6 +213,8 @@ class LinkCreator { string _context; double _strength_threshold; vector> _buffer_determiners; + map _buffer_stimuli; + bool _activation_spreading_flag; string _link_creation_log_file_name; bool _log_new_links; }; diff --git a/src/tests/cpp/link_creation_agent_test.cc b/src/tests/cpp/link_creation_agent_test.cc index 3b127bdab..76890b19b 100644 --- a/src/tests/cpp/link_creation_agent_test.cc +++ b/src/tests/cpp/link_creation_agent_test.cc @@ -114,7 +114,7 @@ TEST(LinkCreation, customizable_tokenization) { copy2[i].extra_parameters(tokens_string); copy2[i].tokenize(tokens3); if (i == 0) { - ASSERT_EQ(tokens_string, "1,2,_1,_2,2,$v1,$v2,type0,2,2,blah,bleh blih"); + ASSERT_EQ(tokens_string, "0,1,2,_1,_2,2,$v1,$v2,type0,2,2,blah,bleh blih"); } ASSERT_EQ(tokens1, tokens2); ASSERT_EQ(tokens1, tokens3); diff --git a/src/tests/integration/cpp/lca_integration_test.cc b/src/tests/integration/cpp/lca_integration_test.cc index 84e947b18..77ba618dd 100644 --- a/src/tests/integration/cpp/lca_integration_test.cc +++ b/src/tests/integration/cpp/lca_integration_test.cc @@ -155,12 +155,14 @@ static double get_strength(const string& handle) { return answer; } -static double compute_expected_strength(const string& handle1, - const string& handle2, - CustomizableLinkCreator::StrengthComposition composition) { +static vector compute_expected_strength( + const string& handle1, + const string& handle2, + CustomizableLinkCreator::StrengthComposition composition) { auto db = AtomDBSingleton::get_instance(); auto concept_link1 = db->get_link(handle1); auto concept_link2 = db->get_link(handle2); + vector answer; if ((concept_link1 != nullptr) && (concept_link2 != nullptr)) { auto terminal1 = db->get_node(concept_link1->targets[1]); auto terminal2 = db->get_node(concept_link2->targets[1]); @@ -194,20 +196,26 @@ static double compute_expected_strength(const string& handle1, LOG_DEBUG("Counts: " << set1.size() << " " << set2.size() << " " << _intersection.size() << " " << _union.size()); + double s; switch (composition) { case CustomizableLinkCreator::INTERSECTION_OVER_UNION: - return (_intersection.size() == 0) ? 0 - : ((double) _intersection.size() / _union.size()); + s = (_intersection.size() == 0) ? 0 + : ((double) _intersection.size() / _union.size()); + answer.push_back(s); + answer.push_back(s); + break; case CustomizableLinkCreator::INTERSECTION_OVER_A: - return (set1.size() == 0) ? 0 : ((double) _intersection.size() / set1.size()); - case CustomizableLinkCreator::INTERSECTION_OVER_B: - return (set2.size() == 0) ? 0 : ((double) _intersection.size() / set2.size()); + s = (set1.size() == 0) ? 0 : ((double) _intersection.size() / set1.size()); + answer.push_back(s); + s = (set2.size() == 0) ? 0 : ((double) _intersection.size() / set2.size()); + answer.push_back(s); + break; default: RAISE_ERROR("Invalid composition: " + std::to_string((unsigned int) composition)); } } } - return 0; + return answer; } static bool test_customizable() { @@ -360,7 +368,7 @@ static bool test_customizable_counts() { // clang-format on vector queries = {Utils::join(count_query_tokens_A), Utils::join(count_query_tokens_B)}; - CustomizableLinkCreator link_creator[3]; + CustomizableLinkCreator link_creator[2]; link_creator[0].add_link_specification({QueryAnswerElement("v1"), QueryAnswerElement("v2")}, {QueryAnswerElement("Node"), QueryAnswerElement("Node")}, "FunctionalTest0", @@ -371,20 +379,13 @@ static bool test_customizable_counts() { "FunctionalTest1", CustomizableLinkCreator::INTERSECTION_OVER_A, queries); - link_creator[2].add_link_specification({QueryAnswerElement("v1"), QueryAnswerElement("v2")}, - {QueryAnswerElement("Node"), QueryAnswerElement("Node")}, - "FunctionalTest2", - CustomizableLinkCreator::INTERSECTION_OVER_B, - queries); - CustomizableLinkCreator::StrengthComposition composition[3] = { - CustomizableLinkCreator::INTERSECTION_OVER_UNION, - CustomizableLinkCreator::INTERSECTION_OVER_A, - CustomizableLinkCreator::INTERSECTION_OVER_B}; + CustomizableLinkCreator::StrengthComposition composition[2] = { + CustomizableLinkCreator::INTERSECTION_OVER_UNION, CustomizableLinkCreator::INTERSECTION_OVER_A}; vector tokens; - shared_ptr proxy[3]; - for (unsigned int i = 0; i < 3; i++) { + shared_ptr proxy[2]; + for (unsigned int i = 0; i < 2; i++) { tokens.clear(); link_creator[i].tokenize(tokens); proxy[i] = make_proxy(query_tokens, LinkCreatorRegistry::CUSTOMIZABLE); @@ -395,7 +396,7 @@ static bool test_customizable_counts() { ServiceBusSingleton::get_instance()->issue_bus_command(proxy[i]); } - for (unsigned int i = 0; i < 3; i++) { + for (unsigned int i = 0; i < 2; i++) { unsigned int count_answers = 0; while (true) { if (proxy[i]->finished()) { @@ -403,14 +404,20 @@ static bool test_customizable_counts() { } shared_ptr answer = proxy[i]->pop(); if (answer != nullptr) { + vector expected_strength = + compute_expected_strength(answer->get("v1"), answer->get("v2"), composition[i]); string handle = proxy[i]->get_supplementary_info(BaseQueryProxy::BUILT_ATOMS)[count_answers++]; double strength = get_strength(handle); - double expected_strength = - compute_expected_strength(answer->get("v1"), answer->get("v2"), composition[i]); - LOG_DEBUG("[" << strength << ", " << expected_strength << "] " + LOG_DEBUG("[" << strength << ", " << expected_strength[0] << "] " + << AtomDBUtils::handle_to_metta(handle)); + success &= assert_true(Utils::is_zero(strength - expected_strength[0]), + "link strength composition: " + std::to_string(i)); + handle = proxy[i]->get_supplementary_info(BaseQueryProxy::BUILT_ATOMS)[count_answers++]; + strength = get_strength(handle); + LOG_DEBUG("[" << strength << ", " << expected_strength[1] << "] " << AtomDBUtils::handle_to_metta(handle)); - success &= assert_true(Utils::is_zero(strength - expected_strength), + success &= assert_true(Utils::is_zero(strength - expected_strength[1]), "link strength composition: " + std::to_string(i)); } else { Utils::sleep(); @@ -525,7 +532,7 @@ int main(int argc, char* argv[]) { insert_type_symbols(); bool success = true; - timeout_after_minutes(15); + timeout_after_minutes(20); success &= test_customizable(); success &= test_customizable_counts(); success &= test_and_two_predicates(); diff --git a/src/tests/main/BUILD b/src/tests/main/BUILD index 99f8903bd..40cdb0325 100644 --- a/src/tests/main/BUILD +++ b/src/tests/main/BUILD @@ -50,6 +50,8 @@ cc_library( deps = [ "//agents/context_broker:context_broker_lib", "//agents/evolution:evolution_lib", + "//agents/link_creation_agent:link_creation_proxy", + "//agents/link_creation_agent/link_creators:link_creators_lib", "//agents/query_engine:query_engine_lib", "//atomdb:atomdb_api_types", "//atomdb:atomdb_initializer", diff --git a/src/tests/main/evaluation_evolution.cc b/src/tests/main/evaluation_evolution.cc index 631f6e404..5ff231ef6 100644 --- a/src/tests/main/evaluation_evolution.cc +++ b/src/tests/main/evaluation_evolution.cc @@ -1,75 +1,36 @@ -#include - -#include #include -#include -#include -#include +#include "AndTwoPredicates.h" #include "AtomDBAPITypes.h" #include "AtomDBInitializer.h" #include "AtomDBSingleton.h" #include "AtomDBUtils.h" #include "AttentionBrokerClient.h" -#include "ContextBrokerProxy.h" -#include "CountLetterFunction.h" +#include "CustomizableLinkCreator.h" #include "FitnessFunctionRegistry.h" #include "Hasher.h" #include "JsonConfigParser.h" +#include "LinkCreationProxy.h" +#include "LinkCreatorRegistry.h" #include "Logger.h" #include "MettaParser.h" -#include "PatternMatchingQueryProxy.h" #include "QueryAnswer.h" #include "QueryEvolutionProxy.h" #include "RemoteAtomDB.h" #include "ServiceBusSingleton.h" #include "SystemParametersSingleton.h" -#include "TestAtomDBJsonConfig.h" #include "Utils.h" #include "commons/atoms/MettaParserActions.h" -#include "httplib.h" -#include "nlohmann/json.hpp" - -// Symbols -#define AND_OPERATOR "AND" -#define ANDNOT_OPERATOR "ANDNOT" -#define OR_OPERATOR "OR" -#define CHAIN_OPERATOR "CHAIN" -#define LINK_TEMPLATE "LINK_TEMPLATE" -#define LINK "LINK" -#define NODE "NODE" -#define VARIABLE "VARIABLE" -#define ATOM "ATOM" -#define EXPRESSION "Expression" -#define SYMBOL "Symbol" -#define EVALUATION "Evaluation" -#define CONCEPT "Concept" -#define PREDICATE "Predicate" -#define EQUIVALENCE "Equivalence" -#define IMPLICATION "Implication" -#define LOGICAL_AND "LogicalAnd" +#include "tags.h" // Variables #define V1 "V1" #define V2 "V2" #define V3 "V3" -#define PREDICATE1 "Predicate1" -#define PREDICATE2 "Predicate2" -#define PREDICATE3 "Predicate3" -#define CONCEPT1 "Concept1" -#define CONCEPT2 "Concept2" -#define CONCEPT3 "Concept3" // Misc -#define STRENGTH_TAG "strength" -#define IS_LITERAL "is_literal" #define FITNESS_FUNCTION "inference_toy" -static string IMPLICATION_HANDLE = Hasher::node_handle(SYMBOL, IMPLICATION); -static string EQUIVALENCE_HANDLE = Hasher::node_handle(SYMBOL, EQUIVALENCE); -static string PREDICATE_HANDLE = Hasher::node_handle(SYMBOL, PREDICATE); -static string EVALUATION_HANDLE = Hasher::node_handle(SYMBOL, EVALUATION); -static string LOGICAL_AND_HANDLE = Hasher::node_handle(SYMBOL, LOGICAL_AND); static float RENT_RATE = 0.25; static float SPREADING_RATE_LOWERBOUND = 0.90; static float SPREADING_RATE_UPPERBOUND = 0.90; @@ -84,24 +45,12 @@ static string TARGET_PREDICATE = "undefined"; static string TARGET_CONCEPT_HANDLE = "undefined"; static string TARGET_PREDICATE_HANDLE = "undefined"; -static bool USE_MORK = false; -static bool SETUP_ONLY = false; -static bool USE_HTTP = false; -static bool USE_REMOTE_FITNESS = false; -static string HTTP_ENDPOINT = ""; -static double LINK_CREATION_STRENGTH_THRESHOLD = (SETUP_ONLY ? 0.0 : 0.1); -static unsigned int LINK_CREATION_COUNT = 10; -static unsigned int LINK_CREATION_MAX_VISIT_ATTEMPTS = LINK_CREATION_COUNT; -static unsigned int LINK_CREATION_MAX_ATTEMPTS = 500; -static double ATTENTION_FOCUS_STRICTNESS = 0.30; +static double ATTENTION_FOCUS_STRICTNESS = 0.10; static unsigned int RANDOM_SEED = 1236; static string PRESET_LINKS_FILE_PREFIX = "/opt/das/_PRESET_LINKS_"; static string PRESET_LINKS_FILE = PRESET_LINKS_FILE_PREFIX; static string CONTEXT_FILE_NAME_PREFIX = "/opt/das/_CONTEXT_DUMP_"; -static bool WRITE_CREATED_LINKS_TO_DB = true; -static bool WRITE_CREATED_LINKS_TO_FILE = SETUP_ONLY; -static bool PRINT_CREATED_LINKS_METTA = true; using namespace std; using namespace atomdb; @@ -110,69 +59,15 @@ using namespace query_engine; using namespace evolution; using namespace service_bus; using namespace attention_broker; -using namespace context_broker; -using namespace fitness_functions; -using json = nlohmann::json; +using namespace link_creation_agent; enum ContextTaskType { UNDEFINED = 0, DETERMINER, CORRELATION, ACTIVATION }; static shared_ptr db; static AtomDBUtils DECODER; static shared_ptr bus; -static vector> buffer_determiners; -static map> weight_calculation_cache; static vector, unsigned int>> recorded_answers; -static string metta_expr3(const string& expr1, const string& expr2, const string& expr3) { - return "(" + expr1 + " " + expr2 + " " + expr3 + ")"; -} - -static string metta_or(const string& expr1, const string& expr2) { - return metta_expr3("or", expr1, expr2); -} - -static string metta_and(const string& expr1, const string& expr2) { - return metta_expr3("and", expr1, expr2); -} - -static string metta_chain(const string& source, const string& target, const string& query) { - return "(chain 0 1 2 " + source + " " + target + " " + query + ")"; -} - -static string metta_var(const string& name) { return "$" + name; } - -static string hard_wired_metta_expression(const string& handle) { - STACK_TRACE(); - string answer = "UNDEFINED"; - if (handle == TARGET_CONCEPT_HANDLE) { - answer = TARGET_CONCEPT; - } else if (handle == TARGET_PREDICATE_HANDLE) { - answer = TARGET_PREDICATE; - } else { - RAISE_ERROR("Unknown handle: " + handle); - } - return answer; -} - -static double get_strength(const string& handle) { - STACK_TRACE(); - auto atom = db->get_atom(handle); - return atom->custom_attributes.get_or(STRENGTH_TAG, 1.0); -} - -static void save_link_metta(shared_ptr link) { - STACK_TRACE(); - ofstream file; - file.open(PRESET_LINKS_FILE, ios::app); - if (file.is_open()) { - file << link->custom_attributes.get_or("strength", 1.0) << "," - << link->metta_representation(DECODER) << endl; - file.close(); - } else { - RAISE_ERROR("Couldn't open file for writing: " + PRESET_LINKS_FILE); - } -} - static string answer_to_string_2(shared_ptr answer) { STACK_TRACE(); vector paths; @@ -245,57 +140,47 @@ static string answer_to_string(shared_ptr answer) { } } -static shared_ptr issue_link_building_query( - const vector& query_tokens, const string& context) { - STACK_TRACE(); - auto proxy = make_shared(query_tokens, context); - proxy->parameters[BaseQueryProxy::ATTENTION_CORRELATION] = (unsigned int) BaseQueryProxy::NONE; - proxy->parameters[BaseQueryProxy::ATTENTION_UPDATE] = (unsigned int) BaseQueryProxy::NONE; - proxy->parameters[BaseQueryProxy::UNIQUE_ASSIGNMENT_FLAG] = true; - proxy->parameters[BaseQueryProxy::ALLOW_INCOMPLETE_CHAIN_PATH] = false; - proxy->parameters[BaseQueryProxy::ATTENTION_FOCUS_STRICTNESS] = - (double) (SETUP_ONLY ? 0.0 : ATTENTION_FOCUS_STRICTNESS); - proxy->parameters[PatternMatchingQueryProxy::DISREGARD_IMPORTANCE_FLAG] = false; - proxy->parameters[PatternMatchingQueryProxy::POSITIVE_IMPORTANCE_FLAG] = true; +static shared_ptr create_lca_proxy( + const vector& query_tokens, + const string& context, + const string& link_creator_tag, + LinkCreator& link_creator, + BaseProxy::ORCHESTRATION_SCHEMA_TYPE orchestration) { + auto proxy = make_shared(query_tokens, context, link_creator_tag, orchestration); + proxy->parameters[LinkCreationProxy::LINK_CREATOR_EXTRA_PARAMETERS] = + (string) link_creator.extra_parameters(); + proxy->parameters[LinkCreationProxy::MAX_SUCCESSFUL_CREATION_PER_ROUND] = (unsigned int) 10; + proxy->parameters[LinkCreationProxy::MAX_UNPRODUCTIVE_VISITS_PER_ROUND] = (unsigned int) 500; + proxy->parameters[LinkCreationProxy::MAX_VISIT_ATTEMPTS_PER_ROUND] = (unsigned int) 20; + proxy->parameters[LinkCreationProxy::MAX_ROUNDS] = (unsigned int) 0; + proxy->parameters[LinkCreationProxy::LINK_CREATION_STRENGTH_THRESHOLD] = (double) 0.1; + proxy->parameters[LinkCreationProxy::LINK_CREATION_LOG_FILE_NAME] = (string) "_new_links.txt"; + proxy->parameters[LinkCreationProxy::LOG_NEW_LINKS] = (bool) true; + proxy->parameters[LinkCreationProxy::LINK_CREATION_SPREAD_ACTIVATION] = (bool) true; proxy->parameters[PatternMatchingQueryProxy::MAX_ANSWERS] = (unsigned int) 0; - proxy->parameters[BaseQueryProxy::USE_METTA_AS_QUERY_TOKENS] = (query_tokens.size() == 1); - proxy->parameters[BaseQueryProxy::POPULATE_METTA_MAPPING] = USE_MORK; - proxy->parameters[PatternMatchingQueryProxy::UNIQUE_VALUE_FLAG] = true; - - ServiceBusSingleton::get_instance()->issue_bus_command(proxy); - return proxy; -} - -static shared_ptr issue_weight_count_query(const vector& query_tokens, - const string& context) { - STACK_TRACE(); - auto proxy = make_shared(query_tokens, context); - proxy->parameters[BaseQueryProxy::UNIQUE_ASSIGNMENT_FLAG] = true; + proxy->parameters[PatternMatchingQueryProxy::DISREGARD_IMPORTANCE_FLAG] = (bool) false; + proxy->parameters[PatternMatchingQueryProxy::POSITIVE_IMPORTANCE_FLAG] = (bool) true; + proxy->parameters[PatternMatchingQueryProxy::UNIQUE_VALUE_FLAG] = (bool) true; + proxy->parameters[BaseQueryProxy::POPULATE_METTA_MAPPING] = true; + proxy->parameters[BaseQueryProxy::USE_METTA_AS_QUERY_TOKENS] = false; + proxy->parameters[BaseQueryProxy::UNIQUE_ASSIGNMENT_FLAG] = (bool) true; + proxy->parameters[BaseQueryProxy::ALLOW_INCOMPLETE_CHAIN_PATH] = (bool) false; proxy->parameters[BaseQueryProxy::ATTENTION_CORRELATION] = (unsigned int) BaseQueryProxy::NONE; proxy->parameters[BaseQueryProxy::ATTENTION_UPDATE] = (unsigned int) BaseQueryProxy::NONE; - proxy->parameters[PatternMatchingQueryProxy::DISREGARD_IMPORTANCE_FLAG] = true; - proxy->parameters[PatternMatchingQueryProxy::POSITIVE_IMPORTANCE_FLAG] = false; - proxy->parameters[BaseQueryProxy::USE_METTA_AS_QUERY_TOKENS] = USE_MORK; - proxy->parameters[BaseQueryProxy::POPULATE_METTA_MAPPING] = false; - - ServiceBusSingleton::get_instance()->issue_bus_command(proxy); + proxy->parameters[BaseQueryProxy::ATTENTION_FOCUS_STRICTNESS] = (double) ATTENTION_FOCUS_STRICTNESS; return proxy; } -static shared_ptr issue_context_creation_query( - const vector& query_tokens, const string& context) { - STACK_TRACE(); +static shared_ptr issue_sti_snapshot_query(const vector& query_tokens, + const string& context) { auto proxy = make_shared(query_tokens, context); - proxy->parameters[BaseQueryProxy::ATTENTION_CORRELATION] = (unsigned int) BaseQueryProxy::NONE; - proxy->parameters[BaseQueryProxy::ATTENTION_UPDATE] = (unsigned int) BaseQueryProxy::NONE; - proxy->parameters[BaseQueryProxy::UNIQUE_ASSIGNMENT_FLAG] = false; - proxy->parameters[PatternMatchingQueryProxy::DISREGARD_IMPORTANCE_FLAG] = true; - proxy->parameters[PatternMatchingQueryProxy::POSITIVE_IMPORTANCE_FLAG] = false; proxy->parameters[PatternMatchingQueryProxy::MAX_ANSWERS] = (unsigned int) 0; - proxy->parameters[BaseQueryProxy::USE_METTA_AS_QUERY_TOKENS] = (query_tokens.size() == 1); - proxy->parameters[BaseQueryProxy::POPULATE_METTA_MAPPING] = false; - proxy->parameters[PatternMatchingQueryProxy::UNIQUE_VALUE_FLAG] = false; - + proxy->parameters[PatternMatchingQueryProxy::DISREGARD_IMPORTANCE_FLAG] = (bool) false; + proxy->parameters[PatternMatchingQueryProxy::POSITIVE_IMPORTANCE_FLAG] = (bool) true; + proxy->parameters[PatternMatchingQueryProxy::UNIQUE_VALUE_FLAG] = (bool) true; + proxy->parameters[BaseQueryProxy::POPULATE_METTA_MAPPING] = true; + proxy->parameters[BaseQueryProxy::USE_METTA_AS_QUERY_TOKENS] = false; + proxy->parameters[BaseQueryProxy::UNIQUE_ASSIGNMENT_FLAG] = (bool) true; ServiceBusSingleton::get_instance()->issue_bus_command(proxy); return proxy; } @@ -308,722 +193,14 @@ static void flush_remote_link_template_cache(bool force = false) { } } -static void insert_or_update(map& count_map, const string& key, double value) { - STACK_TRACE(); - auto iterator = count_map.find(key); - if (iterator == count_map.end()) { - count_map[key] = value; - } else { - if (value > iterator->second) { - count_map[key] = value; - } - } -} - -static void compute_counts(const vector>& query_tokens, - const string& context, - const QueryAnswerElement& target_element, - const string& handle0, - const string& handle1, - double& count_0, - double& count_1, - double& count_intersection, - double& count_union) { - STACK_TRACE(); - LOG_LOCAL_DEBUG("Computing counts for " + handle0 + " and " + handle1); - shared_ptr proxy[2]; - - count_0 = 0.0; - count_1 = 0.0; - count_intersection = 0.0; - count_union = 0.0; - - map count_map[2]; - map count_map_union; - map count_map_intersection; - - double d; - string handle; - for (unsigned int i = 0; i < 2; i++) { - proxy[i] = issue_weight_count_query(query_tokens[i], context); - } - LOG_LOCAL_DEBUG("Queries issued"); - shared_ptr query_answer; - for (unsigned int i = 0; i < 2; i++) { - LOG_LOCAL_DEBUG("i: " + to_string(i)); - while (!proxy[i]->finished()) { - if ((query_answer = proxy[i]->pop()) == NULL) { - Utils::sleep(); - } else { - d = 1; - for (string& h : query_answer->get_handles_vector()) { - d *= get_strength(h); - } - handle = query_answer->get(target_element); - insert_or_update(count_map[i], handle, d); - } - } - } - LOG_LOCAL_DEBUG("Query answers processed"); - count_map_union = count_map[0]; - for (auto pair : count_map[1]) { - auto iterator = count_map[0].find(pair.first); - if (iterator == count_map[0].end()) { - insert_or_update(count_map_union, pair.first, pair.second); - } else { - insert_or_update(count_map_union, pair.first, pair.second); - insert_or_update(count_map_intersection, pair.first, min(pair.second, iterator->second)); - } - } - - for (auto pair : count_map_intersection) { - count_intersection += pair.second; - } - for (auto pair : count_map_union) { - count_union += pair.second; - } - for (auto pair : count_map[0]) { - count_0 += pair.second; - } - for (auto pair : count_map[1]) { - count_1 += pair.second; - } - LOG_DEBUG("Counts: " + to_string(count_0) + " " + to_string(count_1) + " " + - to_string(count_intersection) + " " + to_string(count_union)); -} - -static shared_ptr add_or_update_link(const string& type_handle, - const string& target1, - const string& target2, - double strength, - const string& context, - bool& link_created_flag) { - STACK_TRACE(); - link_created_flag = false; - if (strength < LINK_CREATION_STRENGTH_THRESHOLD) { - return nullptr; - } - LOG_DEBUG("add_or_update_link(" + type_handle + ", " + target1 + ", " + target2 + ", " + - to_string(strength) + ")"); - shared_ptr new_link( - new Link(EXPRESSION, {type_handle, target1, target2}, true, {{STRENGTH_TAG, strength}})); - LOG_DEBUG("Add or update: " + new_link->to_string()); - string handle = new_link->handle(); - if (db->link_exists(handle)) { - auto old_link = db->get_atom(handle); - LOG_DEBUG("Link already exists: " + old_link->to_string()); - if (strength != old_link->custom_attributes.get_or(STRENGTH_TAG, 1)) { - if (WRITE_CREATED_LINKS_TO_DB) { - LOG_DEBUG("Updating Link in AtomDB"); - // Upsert/replace the existing atom (same content-addressed handle, new strength). - // Default merger (NULL) upserts/replaces — do not delete_link first. - db->add_link(new_link.get()); - } - if (WRITE_CREATED_LINKS_TO_FILE) { - LOG_DEBUG("Writing Link to file: " + PRESET_LINKS_FILE); - save_link_metta(new_link); - } - } - } else { - link_created_flag = true; - if (WRITE_CREATED_LINKS_TO_DB) { - LOG_DEBUG("Creating Link in AtomDB"); - if (PRINT_CREATED_LINKS_METTA) { - LOG_INFO("ADD LINK: [" + std::to_string(strength) + "] " + - new_link->metta_representation(DECODER)); - } - db->add_link(new_link.get()); - buffer_determiners.push_back({handle, target1, target2}); - AttentionBrokerClient::correlate(set({target1, target2}), context); - } - if (WRITE_CREATED_LINKS_TO_FILE) { - LOG_DEBUG("Writing Link to file: " + PRESET_LINKS_FILE); - save_link_metta(new_link); - } - } - LOG_LOCAL_DEBUG("Returning from add_or_update_link()"); - return new_link; -} - -static void extract_mentioned_predicates(set& mentioned, const string& handle) { - STACK_TRACE(); - shared_ptr node; - shared_ptr link = db->get_link(handle); - if (link != nullptr) { - for (string& target_handle : link->targets) { - if ((node = db->get_node(target_handle)) != nullptr) { - if ((node->name != PREDICATE) && (node->name != LOGICAL_AND)) { - mentioned.insert(node->name); - } - } else { - extract_mentioned_predicates(mentioned, target_handle); - } - } - } -} - -static shared_ptr add_and_predicate(const string& handle1, - const string& handle2, - const string& context, - bool& link_created_flag) { - STACK_TRACE(); - if (handle1 == handle2) { - return nullptr; - } - set mentioned_predicates1, mentioned_predicates2; - extract_mentioned_predicates(mentioned_predicates1, handle1); - extract_mentioned_predicates(mentioned_predicates2, handle2); - if (Utils::intersects(mentioned_predicates1, mentioned_predicates2)) { - LOG_DEBUG("Disregarded AND predicate: " + db->get_atom(handle1)->metta_representation(DECODER) + - " AND " + db->get_atom(handle2)->metta_representation(DECODER)); - return nullptr; - } - - string h1, h2; - if (handle1 < handle2) { - h1 = handle1; - h2 = handle2; - } else { - h1 = handle2; - h2 = handle1; - } - - return add_or_update_link(LOGICAL_AND_HANDLE, h1, h2, 1.0, context, link_created_flag); -} - -static bool build_and_predicate_link(shared_ptr query_answer, - const string& context, - const string& custom_handle) { - STACK_TRACE(); - string predicate1 = query_answer->get(PREDICATE1); - string predicate2 = (custom_handle == "" ? query_answer->get(PREDICATE2) : custom_handle); - string concept1 = query_answer->get(CONCEPT); - - if (predicate1 == predicate2) { - LOG_DEBUG("Skipping link building because targets are the same: " + predicate1); - return false; - } - - bool link_created_flag; - shared_ptr new_predicate = - add_and_predicate(predicate1, predicate2, context, link_created_flag); - if ((link_created_flag) && (new_predicate != nullptr)) { - double strength = 1; - for (string& h : query_answer->get_handles_vector()) { - strength *= get_strength(h); - } - add_or_update_link( - EVALUATION_HANDLE, new_predicate->handle(), concept1, strength, context, link_created_flag); - return link_created_flag; - } else { - return false; - } -} - -static bool build_implication_link(shared_ptr query_answer, - const string& context, - const string& custom_handle, - set>& visited, - bool& visited_at_least_one) { - STACK_TRACE(); - visited_at_least_one = false; - string predicates[2]; - string metta_predicates[2]; - if (custom_handle == "") { - predicates[0] = query_answer->get(PREDICATE1); - predicates[1] = query_answer->get(PREDICATE2); - metta_predicates[0] = query_answer->metta_expression[predicates[0]]; - metta_predicates[1] = query_answer->metta_expression[predicates[1]]; - } else { - predicates[0] = query_answer->get(PREDICATE1); - predicates[1] = custom_handle; - metta_predicates[0] = query_answer->metta_expression[predicates[0]]; - metta_predicates[1] = hard_wired_metta_expression(custom_handle); - } - - if (predicates[0] == predicates[1]) { - LOG_DEBUG("Skipping link building because targets are the same: " + predicates[0]); - return false; - } - - if (visited.find({predicates[0], predicates[1]}) != visited.end()) { - LOG_DEBUG("Skipping link building because targets have already been visited this cycle: " + - predicates[0] + " " + predicates[1]); - return false; - } - - set mentioned_predicates1, mentioned_predicates2; - extract_mentioned_predicates(mentioned_predicates1, predicates[0]); - extract_mentioned_predicates(mentioned_predicates2, predicates[1]); - if (Utils::intersects(mentioned_predicates1, mentioned_predicates2)) { - LOG_DEBUG("Disregarded IMPLICATION predicates: " + - db->get_atom(predicates[0])->metta_representation(DECODER) + " <=> " + - db->get_atom(predicates[1])->metta_representation(DECODER)); - return false; - } - - LOG_DEBUG("Visiting: " + predicates[0] + " " + predicates[1]); - visited_at_least_one = true; - visited.insert({predicates[0], predicates[1]}); - - // build Evaluation of the AND of both predicates - build_and_predicate_link(query_answer, context, custom_handle); - - vector> query; - for (unsigned int i = 0; i < 2; i++) { - // clang-format off - if (USE_MORK) { - /* - vector metta_query = {metta_expr3(EVALUATION, metta_predicates[i], metta_var(CONCEPT1))}; - */ - vector metta_query = { - metta_or( - metta_expr3(EVALUATION, metta_predicates[i], metta_var(CONCEPT1)), - metta_and( - metta_expr3(EVALUATION, metta_predicates[i], metta_var(CONCEPT2)), - metta_expr3(EQUIVALENCE, metta_var(CONCEPT2), metta_var(CONCEPT1))))}; - LOG_DEBUG("Counting query (implication): " + metta_query[0]); - query.push_back(metta_query); - } else { - query.push_back({ - OR_OPERATOR, "2", - LINK_TEMPLATE, EXPRESSION, "3", - NODE, SYMBOL, EVALUATION, - ATOM, predicates[i], - VARIABLE, CONCEPT1, - AND_OPERATOR, "2", - LINK_TEMPLATE, EXPRESSION, "3", - NODE, SYMBOL, EVALUATION, - ATOM, predicates[i], - VARIABLE, CONCEPT2, - LINK_TEMPLATE, EXPRESSION, "3", - NODE, SYMBOL, EQUIVALENCE, - VARIABLE, CONCEPT2, - VARIABLE, CONCEPT1, - }); - } - // clang-format on - } - double count_0, count_1, count_intersection, count_union; - QueryAnswerElement target_element(CONCEPT1); - compute_counts(query, - context, - target_element, - predicates[0], - predicates[1], - count_0, - count_1, - count_intersection, - count_union); - bool link_created_flag = false; - if (count_intersection > 0) { - if (count_0 > 0) { - if (count_1 > 0) { - if (count_0 < count_1) { - add_or_update_link(IMPLICATION_HANDLE, - predicates[0], - predicates[1], - count_intersection / count_0, - context, - link_created_flag); - } else { - add_or_update_link(IMPLICATION_HANDLE, - predicates[1], - predicates[0], - count_intersection / count_1, - context, - link_created_flag); - } - } else { - add_or_update_link(IMPLICATION_HANDLE, - predicates[0], - predicates[1], - count_intersection / count_0, - context, - link_created_flag); - } - } else { - if (count_1 > 0) { - add_or_update_link(IMPLICATION_HANDLE, - predicates[1], - predicates[0], - count_intersection / count_1, - context, - link_created_flag); - } - } - } - return link_created_flag; -} - -static bool build_equivalence_link(shared_ptr query_answer, - const string& context, - const string& custom_handle, - set>& visited, - bool& visited_at_least_one) { - STACK_TRACE(); - visited_at_least_one = false; - string concepts[2]; - string metta_concepts[2]; - if (custom_handle == "") { - concepts[0] = query_answer->get(CONCEPT1); - concepts[1] = query_answer->get(CONCEPT2); - metta_concepts[0] = query_answer->metta_expression[concepts[0]]; - metta_concepts[1] = query_answer->metta_expression[concepts[1]]; - } else { - concepts[0] = query_answer->get(CONCEPT1); - concepts[1] = custom_handle; - metta_concepts[0] = query_answer->metta_expression[concepts[0]]; - metta_concepts[1] = hard_wired_metta_expression(custom_handle); - } - - if (concepts[0] == concepts[1]) { - LOG_DEBUG("Skipping link building because targets are the same: " + concepts[0]); - return false; - } - pair p; - if (concepts[0] < concepts[1]) { - p = {concepts[0], concepts[1]}; - } else { - p = {concepts[1], concepts[0]}; - } - if (visited.find(p) != visited.end()) { - LOG_DEBUG("Skipping link building because targets have already been visited this cycle: " + - concepts[0] + " " + concepts[1]); - return false; - } - - LOG_DEBUG("Visiting: " + concepts[0] + " " + concepts[1]); - visited_at_least_one = true; - visited.insert(p); - - vector> query; - for (unsigned int i = 0; i < 2; i++) { - // clang-format off - if (USE_MORK) { - /* - vector metta_query = {metta_expr3(EVALUATION, metta_var(PREDICATE1), metta_concepts[i])}; - */ - vector metta_query = { - metta_or( - metta_expr3(EVALUATION, metta_var(PREDICATE1), metta_concepts[i]), - metta_and( - metta_expr3(EVALUATION, metta_var(PREDICATE2), metta_concepts[i]), - metta_expr3(IMPLICATION, metta_var(PREDICATE2), metta_var(PREDICATE1))))}; - LOG_DEBUG("Counting query (equivalence): " + metta_query[0]); - query.push_back(metta_query); - } else { - query.push_back({ - OR_OPERATOR, "2", - LINK_TEMPLATE, EXPRESSION, "3", - NODE, SYMBOL, EVALUATION, - VARIABLE, PREDICATE1, - ATOM, concepts[i], - AND_OPERATOR, "2", - LINK_TEMPLATE, EXPRESSION, "3", - NODE, SYMBOL, EVALUATION, - VARIABLE, PREDICATE2, - ATOM, concepts[i], - LINK_TEMPLATE, EXPRESSION, "3", - NODE, SYMBOL, IMPLICATION, - VARIABLE, PREDICATE2, - VARIABLE, PREDICATE1, - }); - } - // clang-format on - } - double count_0, count_1, count_intersection, count_union; - QueryAnswerElement target_element(PREDICATE1); - compute_counts(query, - context, - target_element, - concepts[0], - concepts[1], - count_0, - count_1, - count_intersection, - count_union); - bool link_created_flag1 = false; - bool link_created_flag2 = false; - if ((count_intersection > 0) && (count_union > 0)) { - double strength = count_intersection / count_union; - add_or_update_link( - EQUIVALENCE_HANDLE, concepts[0], concepts[1], strength, context, link_created_flag1); - add_or_update_link( - EQUIVALENCE_HANDLE, concepts[1], concepts[0], strength, context, link_created_flag2); - } - return (link_created_flag1 || link_created_flag2); -} - -static bool build_evaluation_link(shared_ptr query_answer, - const string& context, - const string& custom_handle, - set>& visited, - bool& visited_at_least_one) { - STACK_TRACE(); - visited_at_least_one = false; - string predicate = query_answer->get(PREDICATE); - string concept_ = query_answer->get(CONCEPT); - if (visited.find({predicate, concept_}) != visited.end()) { - LOG_DEBUG("Skipping link building because targets have already been visited this cycle: " + - predicate + " " + concept_); - return false; - } - LOG_DEBUG("Visiting: " + predicate + " " + concept_); - visited_at_least_one = true; - visited.insert({predicate, concept_}); - double strength = 1; - for (string& h : query_answer->get_handles_vector()) { - strength *= get_strength(h); - } - bool link_created_flag; - add_or_update_link(EVALUATION_HANDLE, predicate, concept_, strength, context, link_created_flag); - return link_created_flag; -} - -static void build_links(const vector& query, - const string& context, - const string& custom_handle, - bool (*build_link)(shared_ptr query_answer, - const string& context, - const string& custom_handle, - set>& visited, - bool& visited_at_least_one)) { - STACK_TRACE(); - auto proxy = issue_link_building_query(query, context); - unsigned int count_created = 0; - unsigned int count_visit_attemps = 0; - unsigned int count_attempts = 0; - shared_ptr query_answer; - set> visited; - while (true) { - if ((query_answer = proxy->pop()) == nullptr) { - if (proxy->finished()) { - break; - } - Utils::sleep(); - } else { - LOG_DEBUG("Processing query answer " + to_string(count_created) + ": " + - query_answer->to_string(USE_MORK)); - bool visited_at_least_one = false; - if (build_link(query_answer, context, custom_handle, visited, visited_at_least_one)) { - count_created++; - count_visit_attemps = 0; - count_attempts = 0; - if (count_created >= LINK_CREATION_COUNT) { - break; - } - } else if (visited_at_least_one) { - count_visit_attemps++; - count_attempts = 0; - if (count_visit_attemps >= LINK_CREATION_MAX_VISIT_ATTEMPTS) { - break; - } - } else { - count_attempts++; - if (count_attempts >= LINK_CREATION_MAX_ATTEMPTS) { - break; - } - } - } - } - if (!proxy->finished()) { - proxy->abort(); - } - // Persist staged writes once per build cycle instead of per created link. - flush_remote_link_template_cache(); - LOG_DEBUG("Built " + to_string(count_created) + " links"); -} - // clang-format off -static void record_evolution_answer(shared_ptr query_answer, - unsigned int iteration, - double& best_fitness) { - if (query_answer->strength > best_fitness) { - best_fitness = query_answer->strength; - LOG_INFO("ANSWER: " + answer_to_string(query_answer)); - recorded_answers.push_back({query_answer, iteration}); - } -} - -static pair split_host_port(const string& endpoint, int default_port) { - auto colon = endpoint.rfind(':'); - if (colon == string::npos) { - return {endpoint, default_port}; - } - string host = endpoint.substr(0, colon); - int port = Utils::string_to_int(endpoint.substr(colon + 1)); - return {host, port}; -} - -static json metta_tokens_object(const vector& tokens) { - json token_array = json::array(); - for (const string& token : tokens) { - token_array.push_back(token); - } - return {{"syntax", USE_MORK ? "metta" : "link_template"}, {"tokens", token_array}}; -} - -static void query_evolution_http( +static shared_ptr query_evolution( const vector& query_to_evolve, const vector>& correlation_query_template, unsigned int iteration, const string& context) { STACK_TRACE(); - QueryAnswerElement qa_predicate(PREDICATE); - QueryAnswerElement qa_concept(CONCEPT); - QueryAnswerElement qa_path1(0, 1, 2, false, false, true); - QueryAnswerElement qa_path2(1, 1, 2, false, false, true); - QueryAnswerElement qa_nothing; - - json correlation_queries = json::array(); - for (const auto& query : correlation_query_template) { - correlation_queries.push_back(metta_tokens_object(query)); - } - - json correlation_replacements = json::array({ - json::array({json::array({V1, PREDICATE})}), - json::array({json::array({V2, CONCEPT})}), - }); - - json correlation_mappings = json::array(); - auto mapping_pair = [](const QueryAnswerElement& left, const QueryAnswerElement& right) { - auto as_token = [](const QueryAnswerElement& element) { - string encoded = element.to_string(); - if (!encoded.empty() && encoded[0] == '$') { - return encoded.substr(1); - } - return encoded; - }; - return json::array({as_token(left), as_token(right)}); - }; - correlation_mappings.push_back(json::array({ - mapping_pair(qa_concept, qa_concept), - mapping_pair(qa_path1, qa_nothing), - mapping_pair(qa_path2, qa_nothing), - })); - correlation_mappings.push_back(json::array({ - mapping_pair(qa_predicate, qa_predicate), - mapping_pair(qa_path1, qa_nothing), - mapping_pair(qa_path2, qa_nothing), - })); - - const string fitness_tag = - USE_REMOTE_FITNESS ? FitnessFunctionRegistry::REMOTE_FUNCTION : string(FITNESS_FUNCTION); - - json body = { - {"command", "evolution"}, - {"params", - {{"evolution", - {{"query", metta_tokens_object(query_to_evolve)}, - {"fitness_function_tag", fitness_tag}, - {"correlation_queries", correlation_queries}, - {"correlation_replacements", correlation_replacements}, - {"correlation_mappings", correlation_mappings}}}, - {"context", context}, - {"unique_assignment_flag", false}, - {"populate_metta_mapping", true}, - {"use_metta_as_query_tokens", USE_MORK}, - {"allow_incomplete_chain_path", true}, - {"max_bundle_size", 1000}, - {"attention_focus_strictness", ATTENTION_FOCUS_STRICTNESS}, - {"disregard_importance_flag", false}, - {"positive_importance_flag", true}, - {"unique_value_flag", false}, - {"count_flag", false}, - {"population_size", POPULATION_SIZE}, - {"max_generations", MAX_GENERATIONS}, - {"elitism_rate", ELITISM_RATE}, - {"selection_rate", SELECTION_RATE}}}}; - - auto [host, port] = split_host_port(HTTP_ENDPOINT, 40009); - httplib::Client http(host, port); - http.set_connection_timeout(5); - http.set_read_timeout(600); - - auto create = http.Post("/command-router/executions", body.dump(), "application/json"); - if (!create || create->status != 202) { - string detail = create ? create->body : "no response"; - RAISE_ERROR("HTTP evolution create failed: " + detail); - } - const string execution_id = json::parse(create->body)["execution_id"].get(); - LOG_INFO("HTTP evolution execution_id=" + execution_id); - - httplib::ws::WebSocketClient ws("ws://" + host + ":" + std::to_string(port) + - "/command-router/ws/" + execution_id); - if (!ws.is_valid() || !ws.connect()) { - RAISE_ERROR("HTTP evolution WebSocket connect failed for " + execution_id); - } - ws.set_read_timeout(600, 0); - LOG_INFO("HTTP evolution WebSocket connected (fitness_function_tag=" + fitness_tag + ")"); - - auto fitness_fn = FitnessFunctionRegistry::function(FITNESS_FUNCTION); - unsigned int count_answers = 0; - static double best_fitness = 0.0; - static unsigned int count_iterations = 1; - string terminal_status; - string msg; - - while (ws.read(msg)) { - auto event = json::parse(msg); - const string command = event.value("command", ""); - if (command == "eval_fitness") { - const int seq = event["params"]["seq"].get(); - const json& answers = event["params"]["answers"]; - json fitness_values = json::array(); - for (const auto& answer_json : answers) { - auto answer = make_shared(); - answer->from_json(answer_json); - fitness_values.push_back(fitness_fn->eval(answer)); - } - json response = {{"command", "eval_fitness_response"}, - {"params", - {{"execution_id", execution_id}, - {"seq", seq}, - {"fitness", fitness_values}}}}; - if (!ws.send(response.dump())) { - RAISE_ERROR("Failed to send eval_fitness_response"); - } - } else if (command == "query_answers") { - for (const auto& answer_json : event["params"]["answers"]) { - auto answer = make_shared(); - answer->from_json(answer_json); - count_answers++; - record_evolution_answer(answer, iteration, best_fitness); - } - } else if (command == "execution_status") { - terminal_status = event["params"].value("status", ""); - if (terminal_status == "completed" || terminal_status == "error" || - terminal_status == "aborted") { - if (terminal_status == "error") { - string message = event["params"].value("message", "unknown error"); - ws.close(); - RAISE_ERROR("HTTP evolution failed: " + message); - } - break; - } - } - } - ws.close(); - if (terminal_status != "completed") { - RAISE_ERROR("HTTP evolution ended with status: " + terminal_status); - } - LOG_INFO("Total answers in iteration " << count_iterations++ << ": " << count_answers); -} - -static void query_evolution( - const vector& query_to_evolve, - const vector>& correlation_query_template, - unsigned int iteration, - const string& context) { - - STACK_TRACE(); - if (USE_HTTP) { - query_evolution_http(query_to_evolve, correlation_query_template, iteration, context); - return; - } - QueryAnswerElement qa_predicate(PREDICATE); QueryAnswerElement qa_concept(CONCEPT); QueryAnswerElement qa_path1(0, 1, 2, false, false, true); @@ -1052,129 +229,27 @@ static void query_evolution( proxy->parameters[BaseQueryProxy::UNIQUE_ASSIGNMENT_FLAG] = false; proxy->parameters[BaseQueryProxy::POPULATE_METTA_MAPPING] = true; - proxy->parameters[BaseQueryProxy::USE_METTA_AS_QUERY_TOKENS] = USE_MORK; + proxy->parameters[BaseQueryProxy::USE_METTA_AS_QUERY_TOKENS] = false; proxy->parameters[BaseQueryProxy::ALLOW_INCOMPLETE_CHAIN_PATH] = true; proxy->parameters[BaseQueryProxy::MAX_BUNDLE_SIZE] = (unsigned int) 1000; - proxy->parameters[BaseQueryProxy::ATTENTION_FOCUS_STRICTNESS] = (double) ATTENTION_FOCUS_STRICTNESS; - proxy->parameters[BaseQueryProxy::PUBLIC_KEY_TOKENS] = ""; + proxy->parameters[BaseQueryProxy::ATTENTION_FOCUS_STRICTNESS] = (double) 0.0; proxy->parameters[PatternMatchingQueryProxy::DISREGARD_IMPORTANCE_FLAG] = false; proxy->parameters[PatternMatchingQueryProxy::POSITIVE_IMPORTANCE_FLAG] = true; proxy->parameters[PatternMatchingQueryProxy::UNIQUE_VALUE_FLAG] = false; proxy->parameters[PatternMatchingQueryProxy::COUNT_FLAG] = false; proxy->parameters[QueryEvolutionProxy::POPULATION_SIZE] = (unsigned int) POPULATION_SIZE; - proxy->parameters[QueryEvolutionProxy::MAX_GENERATIONS] = (unsigned int) MAX_GENERATIONS; - proxy->parameters[QueryEvolutionProxy::ELITISM_RATE] = (double) ELITISM_RATE; + proxy->parameters[QueryEvolutionProxy::MAX_GENERATIONS] = (unsigned int) MAX_GENERATIONS; // XXXXX + proxy->parameters[QueryEvolutionProxy::ELITISM_RATE] = (double) ELITISM_RATE; // XXXXX proxy->parameters[QueryEvolutionProxy::SELECTION_RATE] = (double) SELECTION_RATE; bus->issue_bus_command(proxy); - - shared_ptr query_answer; - unsigned int count_answers = 0; - static double best_fitness = 0.0; - static unsigned int count_iterations = 1; - - while (!proxy->finished()) { - if ((query_answer = proxy->pop()) == NULL) { - Utils::sleep(); - } else { - count_answers++; - record_evolution_answer(query_answer, iteration, best_fitness); - } - } - LOG_INFO("Total answers in iteration " << count_iterations++ << ": " << count_answers); -} -// clang-format on - -static void add_to_context_file(const filesystem::path& context_file_name, - const string& context, - ContextTaskType task, - const vector& query, - const vector>& selector) { - STACK_TRACE(); - ofstream file; - file.open(context_file_name, ios::app); - string mnemonic = ""; - if (file.is_open()) { - auto proxy = issue_context_creation_query(query, context); - unsigned int count = 0; - shared_ptr query_answer; - while (!proxy->finished()) { - if ((query_answer = proxy->pop()) == nullptr) { - Utils::sleep(); - } else { - count++; - switch (task) { - case DETERMINER: - mnemonic = "DET"; - case CORRELATION: - if (mnemonic == "") mnemonic = "COR"; - for (auto pair : selector) { - if (pair.size() != 2) { - RAISE_ERROR("Invalid context task selector for " + mnemonic); - return; - } - file << mnemonic << " " << query_answer->get(pair[0]) << " " - << query_answer->get(pair[1]) << endl; - } - break; - case ACTIVATION: - mnemonic = "ACT"; - if (selector.size() != 1) { - RAISE_ERROR("Invalid context task selector for " + mnemonic); - } - file << mnemonic; - for (auto element : selector[0]) { - file << " " << query_answer->get(element); - } - file << endl; - break; - default: - RAISE_ERROR("Invalid context creation task: " + - std::to_string((unsigned int) task)); - } - } - } - file.close(); - } else { - RAISE_ERROR("Couldn't open file for writing: " + string(context_file_name)); - } -} - -static void add_to_context_file(const filesystem::path& context_file_name, - const string& context, - ContextTaskType task, - const vector& handles) { - STACK_TRACE(); - ofstream file; - file.open(context_file_name, ios::app); - if (file.is_open()) { - for (string handle : handles) { - switch (task) { - case DETERMINER: - RAISE_ERROR("Not implemented"); - break; - case CORRELATION: - RAISE_ERROR("Not implemented"); - break; - case ACTIVATION: - file << "ACT " << handle << endl; - break; - default: - RAISE_ERROR("Invalid context creation task: " + std::to_string((unsigned int) task)); - } - } - file.close(); - } else { - RAISE_ERROR("Couldn't open file for writing: " + string(context_file_name)); - } + return proxy; } -static void add_preset_links(const vector& implication_to_target_predicate_metta_query, - const vector& implication_to_target_predicate_query, - const vector& equivalence_to_target_concept_metta_query, - const vector& equivalence_to_target_concept_query, - const string& context) { +static void add_preset_links(const string& context) { STACK_TRACE(); + map stimuli; + vector> buffer_determiners; ifstream file(PRESET_LINKS_FILE); if (file.is_open()) { LOG_INFO("Reading preset links from file: " + PRESET_LINKS_FILE); @@ -1186,8 +261,16 @@ static void add_preset_links(const vector& implication_to_target_predica MettaParser parser(line[1], parser_handler); parser.parse(); auto link = std::dynamic_pointer_cast(parser_handler->element_stack.top()); - link->custom_attributes["strength"] = (double) Utils::string_to_float(line[0]); - LOG_DEBUG("Adding Link: [" + line[0] + "] " + line[1]); + string link_handle = link->handle(); + double strength = (double) Utils::string_to_float(line[0]); + unsigned int stimulus = (unsigned int) std::lround(strength * 1000); + for (string& target_handle : link->targets) { + if ((stimuli.find(target_handle) == stimuli.end()) || (stimuli[target_handle] < stimulus)) { + stimuli[target_handle] = stimulus; + } + } + link->custom_attributes["strength"] = strength; + LOG_INFO("Adding initial Link: [" + line[0] + "] " + line[1]); vector atoms_to_add; atoms_to_add.reserve(parser_handler->handle_to_atom.size()); for (const auto& [_, atom] : parser_handler->handle_to_atom) { @@ -1196,388 +279,597 @@ static void add_preset_links(const vector& implication_to_target_predica db->add_atoms(atoms_to_add, true); count++; line.clear(); - buffer_determiners.push_back({link->handle(), link->targets[1], link->targets[2]}); + buffer_determiners.push_back({link_handle, link->targets[1], link->targets[2]}); AttentionBrokerClient::correlate({link->targets[1], link->targets[2]}, context); } LOG_INFO(std::to_string(count) + " preset links read."); } else { - LOG_INFO("Couldn't open file: " + PRESET_LINKS_FILE); - LOG_INFO("Building Implication links to TARGET_PREDICATE"); - build_links((USE_MORK ? implication_to_target_predicate_metta_query - : implication_to_target_predicate_query), - context, - TARGET_PREDICATE_HANDLE, - build_implication_link); - LOG_INFO("Building Equivalence links to TARGET_CONCEPT"); - build_links( - (USE_MORK ? equivalence_to_target_concept_metta_query : equivalence_to_target_concept_query), - context, - TARGET_CONCEPT_HANDLE, - build_equivalence_link); + RAISE_ERROR("Couldn't open file: " + PRESET_LINKS_FILE); } file.close(); LOG_INFO("Updating determiners in AttentionBroker"); AttentionBrokerClient::set_determiners(buffer_determiners, context); + AttentionBrokerClient::stimulate(stimuli, context); buffer_determiners.clear(); flush_remote_link_template_cache(); } -static void run(const string& context_tag) { - STACK_TRACE(); - // clang-format off - vector implication_query = { - ANDNOT_OPERATOR, "3", - LINK_TEMPLATE, EXPRESSION, "3", - NODE, SYMBOL, EVALUATION, - VARIABLE, PREDICATE1, - VARIABLE, CONCEPT, - LINK_TEMPLATE, EXPRESSION, "3", - NODE, SYMBOL, EVALUATION, - VARIABLE, PREDICATE2, - VARIABLE, CONCEPT, - LINK_TEMPLATE, EXPRESSION, "3", - NODE, SYMBOL, IMPLICATION, - VARIABLE, PREDICATE1, - VARIABLE, PREDICATE2, - }; - - vector implication_to_target_predicate_query = { +static vector make_implication_query(bool target_flag=false) { + return { AND_OPERATOR, "2", LINK_TEMPLATE, EXPRESSION, "3", - NODE, SYMBOL, EVALUATION, + NODE, SYMBOL, EVALUATION_TAG, VARIABLE, PREDICATE1, VARIABLE, CONCEPT, LINK_TEMPLATE, EXPRESSION, "3", - NODE, SYMBOL, EVALUATION, - ATOM, TARGET_PREDICATE_HANDLE, - VARIABLE, CONCEPT, - }; - vector implication_to_target_predicate_metta_query = { - metta_and( - metta_expr3(EVALUATION, metta_var(PREDICATE1), metta_var(CONCEPT)), - metta_expr3(EVALUATION, TARGET_PREDICATE, metta_var(CONCEPT))) + NODE, SYMBOL, EVALUATION_TAG, + (target_flag ? ATOM : VARIABLE), (target_flag ? TARGET_PREDICATE_HANDLE : PREDICATE2), + VARIABLE, CONCEPT }; +} - vector equivalence_query = { - ANDNOT_OPERATOR, "3", - LINK_TEMPLATE, EXPRESSION, "3", - NODE, SYMBOL, EVALUATION, - VARIABLE, PREDICATE, - VARIABLE, CONCEPT1, - LINK_TEMPLATE, EXPRESSION, "3", - NODE, SYMBOL, EVALUATION, - VARIABLE, PREDICATE, - VARIABLE, CONCEPT2, +/* +static string make_implication_count_query(const string& _predicate) { + return Utils::join({ + OR_OPERATOR, "2", LINK_TEMPLATE, EXPRESSION, "3", - NODE, SYMBOL, EQUIVALENCE, + NODE, SYMBOL, EVALUATION_TAG, + ATOM, _predicate, VARIABLE, CONCEPT1, - VARIABLE, CONCEPT2, - }; + AND_OPERATOR, "2", + LINK_TEMPLATE, EXPRESSION, "3", + NODE, SYMBOL, EVALUATION_TAG, + ATOM, _predicate, + VARIABLE, CONCEPT2, + LINK_TEMPLATE, EXPRESSION, "3", + NODE, SYMBOL, EQUIVALENCE_TAG, + VARIABLE, CONCEPT2, + VARIABLE, CONCEPT1 + }); +} +*/ - vector equivalence_to_target_concept_query = { +static string make_implication_count_query(const string& _predicate) { + return Utils::join({ + LINK_TEMPLATE, EXPRESSION, "3", + NODE, SYMBOL, EVALUATION_TAG, + ATOM, _predicate, + VARIABLE, CONCEPT1, + }); +} + +static vector make_equivalence_query(bool target_flag=false) { + return { AND_OPERATOR, "2", LINK_TEMPLATE, EXPRESSION, "3", - NODE, SYMBOL, EVALUATION, + NODE, SYMBOL, EVALUATION_TAG, VARIABLE, PREDICATE, VARIABLE, CONCEPT1, LINK_TEMPLATE, EXPRESSION, "3", - NODE, SYMBOL, EVALUATION, + NODE, SYMBOL, EVALUATION_TAG, VARIABLE, PREDICATE, - ATOM, TARGET_CONCEPT_HANDLE, - }; - vector equivalence_to_target_concept_metta_query = { - metta_and( - metta_expr3(EVALUATION, metta_var(PREDICATE), metta_var(CONCEPT1)), - metta_expr3(EVALUATION, metta_var(PREDICATE), TARGET_CONCEPT)) + (target_flag ? ATOM : VARIABLE), (target_flag ? TARGET_CONCEPT_HANDLE : CONCEPT2) }; +} + +/* +static string make_equivalence_count_query(const string& _concept) { + return Utils::join({ + OR_OPERATOR, "2", + LINK_TEMPLATE, EXPRESSION, "3", + NODE, SYMBOL, EVALUATION_TAG, + VARIABLE, PREDICATE1, + ATOM, _concept, + AND_OPERATOR, "2", + LINK_TEMPLATE, EXPRESSION, "3", + NODE, SYMBOL, EVALUATION_TAG, + VARIABLE, PREDICATE2, + ATOM, _concept, + LINK_TEMPLATE, EXPRESSION, "3", + NODE, SYMBOL, IMPLICATION_TAG, + VARIABLE, PREDICATE2, + VARIABLE, PREDICATE1 + }); +} +*/ + +static string make_equivalence_count_query(const string& _concept) { + return Utils::join({ + LINK_TEMPLATE, EXPRESSION, "3", + NODE, SYMBOL, EVALUATION_TAG, + VARIABLE, PREDICATE1, + ATOM, _concept, + }); +} - vector evaluation_fixed_predicate_query = { - ANDNOT_OPERATOR, "3", +static vector make_evaluation_predicate_query() { + return { + AND_OPERATOR, "2", LINK_TEMPLATE, EXPRESSION, "3", - NODE, SYMBOL, EVALUATION, + NODE, SYMBOL, EVALUATION_TAG, VARIABLE, PREDICATE, VARIABLE, CONCEPT1, LINK_TEMPLATE, EXPRESSION, "3", - NODE, SYMBOL, EQUIVALENCE, + NODE, SYMBOL, EQUIVALENCE_TAG, VARIABLE, CONCEPT1, - VARIABLE, CONCEPT, - LINK_TEMPLATE, EXPRESSION, "3", - NODE, SYMBOL, EVALUATION, - VARIABLE, PREDICATE, - VARIABLE, CONCEPT, + VARIABLE, CONCEPT }; +} - vector evaluation_fixed_concept_query = { - ANDNOT_OPERATOR, "3", +static vector make_evaluation_concept_query() { + return { + AND_OPERATOR, "2", LINK_TEMPLATE, EXPRESSION, "3", - NODE, SYMBOL, EVALUATION, + NODE, SYMBOL, EVALUATION_TAG, VARIABLE, PREDICATE1, VARIABLE, CONCEPT, LINK_TEMPLATE, EXPRESSION, "3", - NODE, SYMBOL, IMPLICATION, + NODE, SYMBOL, IMPLICATION_TAG, VARIABLE, PREDICATE1, VARIABLE, PREDICATE, - LINK_TEMPLATE, EXPRESSION, "3", - NODE, SYMBOL, EVALUATION, - VARIABLE, PREDICATE, - VARIABLE, CONCEPT, }; +} - vector implication_to_path_query = { - ANDNOT_OPERATOR, "4", - LINK_TEMPLATE, EXPRESSION, "3", - NODE, SYMBOL, EVALUATION, - VARIABLE, PREDICATE1, - VARIABLE, CONCEPT, - LINK_TEMPLATE, EXPRESSION, "3", - NODE, SYMBOL, EVALUATION, - VARIABLE, PREDICATE2, - VARIABLE, CONCEPT, - CHAIN_OPERATOR, "0", "1", "2", - VARIABLE, PREDICATE2, - ATOM, TARGET_PREDICATE_HANDLE, - LINK_TEMPLATE, EXPRESSION, "3", - NODE, SYMBOL, IMPLICATION, - VARIABLE, V1, - VARIABLE, V2, - LINK_TEMPLATE, EXPRESSION, "3", - NODE, SYMBOL, IMPLICATION, - VARIABLE, PREDICATE1, - VARIABLE, PREDICATE2, +static vector make_plain_evaluation_query() { + return { + LINK_TEMPLATE, EXPRESSION, "3", + NODE, SYMBOL, EVALUATION_TAG, + VARIABLE, PREDICATE, + VARIABLE, CONCEPT, }; +} - vector equivalence_to_path_query = { - ANDNOT_OPERATOR, "4", - LINK_TEMPLATE, EXPRESSION, "3", - NODE, SYMBOL, EVALUATION, - VARIABLE, PREDICATE, - VARIABLE, CONCEPT1, - LINK_TEMPLATE, EXPRESSION, "3", - NODE, SYMBOL, EVALUATION, - VARIABLE, PREDICATE, - VARIABLE, CONCEPT2, - CHAIN_OPERATOR, "0", "1", "2", - VARIABLE, CONCEPT2, - ATOM, TARGET_CONCEPT_HANDLE, - LINK_TEMPLATE, EXPRESSION, "3", - NODE, SYMBOL, EQUIVALENCE, - VARIABLE, V1, - VARIABLE, V2, - LINK_TEMPLATE, EXPRESSION, "3", - NODE, SYMBOL, EQUIVALENCE, - VARIABLE, CONCEPT1, - VARIABLE, CONCEPT2, +static vector make_plain_implication_query() { + return { + LINK_TEMPLATE, EXPRESSION, "3", + NODE, SYMBOL, IMPLICATION_TAG, + VARIABLE, PREDICATE1, + VARIABLE, PREDICATE2, }; +} + +static vector make_plain_equivalence_query() { + return { + LINK_TEMPLATE, EXPRESSION, "3", + NODE, SYMBOL, EQUIVALENCE_TAG, + VARIABLE, CONCEPT1, + VARIABLE, CONCEPT2, + }; +} +// clang-format on + +static void sti_snapshot(const string& context, bool report_evaluation = false) { + float sti_threshold = 0.001; + // float sti_threshold = 0.0; + unsigned int index_count = 0; + unsigned int predicate_index = index_count++; + unsigned int concept_index = index_count++; + unsigned int implication_index = index_count++; + unsigned int equivalence_index = index_count++; + unsigned int evaluation_index = index_count++; + map tag; + tag[predicate_index] = "PREDICATES"; + tag[concept_index] = "CONCEPTS"; + tag[implication_index] = "IMPLICATION LINKS"; + tag[equivalence_index] = "EQUIVALENCE LINKS"; + tag[evaluation_index] = "EVALUATION LINKS"; + vector> handle_set = {{}, {}, {}, {}, {}}; + vector>> data = {{}, {}, {}, {}, {}}; + vector> handles = {{}, {}, {}, {}, {}}; + vector> sti = {{}, {}, {}, {}, {}}; + shared_ptr query_answer; + shared_ptr proxy; + + proxy = issue_sti_snapshot_query(make_plain_evaluation_query(), context); + while (true) { + if ((query_answer = proxy->pop()) == nullptr) { + if (proxy->finished()) { + break; + } + Utils::sleep(); + } else { + handle_set[predicate_index].insert(query_answer->get(QueryAnswerElement(PREDICATE))); + handle_set[concept_index].insert(query_answer->get(QueryAnswerElement(CONCEPT))); + handle_set[evaluation_index].insert(query_answer->get(QueryAnswerElement(0))); + } + } + + proxy = issue_sti_snapshot_query(make_plain_implication_query(), context); + while (true) { + if ((query_answer = proxy->pop()) == nullptr) { + if (proxy->finished()) { + break; + } + Utils::sleep(); + } else { + handle_set[implication_index].insert(query_answer->get(QueryAnswerElement(0))); + } + } + + proxy = issue_sti_snapshot_query(make_plain_equivalence_query(), context); + while (true) { + if ((query_answer = proxy->pop()) == nullptr) { + if (proxy->finished()) { + break; + } + Utils::sleep(); + } else { + handle_set[equivalence_index].insert(query_answer->get(QueryAnswerElement(0))); + } + } + + LOG_INFO("................................................................................"); + LOG_INFO("STI Snapshot"); + LOG_INFO(""); + for (unsigned int i = 0; i < index_count; i++) { + LOG_INFO(to_string(handle_set[i].size()) + " " + tag[i]); + } + LOG_INFO(""); + LOG_INFO("Listing only links with STI >= " + to_string(sti_threshold)); + for (unsigned int i = 0; i < index_count; i++) { + if (!report_evaluation && (i == evaluation_index)) { + break; + } + LOG_INFO(""); + LOG_INFO(tag[i]); + LOG_INFO(""); + handles[i] = vector(handle_set[i].begin(), handle_set[i].end()); + AttentionBrokerClient::get_importance(handles[i], context, sti[i]); + if (handles[i].size() != sti[i].size()) { + RAISE_ERROR("Invalid attention broker answer. " + to_string(handle_set[i].size()) + + " handles has been queried and " + to_string(sti[i].size()) + + " STI values has returned."); + } + for (unsigned int j = 0; j < handles[i].size(); j++) { + data[i].push_back(make_pair(handles[i][j], sti[i][j])); + } + std::sort(data[i].begin(), + data[i].end(), + [](const std::pair& left, const std::pair& right) { + return left.second > right.second; + }); + for (unsigned int j = 0; j < data[i].size(); j++) { + if (data[i][j].second >= sti_threshold) { + LOG_INFO("[" + to_string(data[i][j].second) + "] " + + AtomDBUtils::handle_to_metta(data[i][j].first)); + } + } + } + LOG_INFO("................................................................................"); +} + +static vector> one_shot_lca_proxy; +// TODO XXX FIXME +// start_one_shot_lca ignores handle in its query and always builds implication links. +// +// make_implication_query(true) always binds the second evaluation to +// TARGET_PREDICATE_HANDLE. The function does not use handle in the query. +// handle appears only in the link specification and in the second count query. +// This causes two problems: +// +// start_one_shot_lca(TARGET_CONCEPT_HANDLE, context, true) at Line 760 creates +// IMPLICATION links of the form Predicate1 -> . The candidates +// come from co-occurrence with the target predicate. A concept target needs an +// EQUIVALENCE link and a concept-based query. +// +// For hop handles at Line 844, candidate predicates are still selected by +// co-occurrence with the target predicate, not with handle. +// +// equivalence_to_target_link_creator and implication_to_target_link_creator are +// configured at Lines 692-705 but never used. This suggests the concept case +// was meant to use the equivalence creator. Pass the handle into the query +// builder, and pick the creator by target kind. +void start_one_shot_lca(const string& handle, const string& context, bool initial_links = false) { + CustomizableLinkCreator implication_to_handle_link_creator(true); + implication_to_handle_link_creator.add_link_specification( + {QueryAnswerElement(PREDICATE1), QueryAnswerElement(handle, true)}, + {QueryAnswerElement(CONCEPT1), QueryAnswerElement(CONCEPT1)}, + IMPLICATION_TAG, + CustomizableLinkCreator::INTERSECTION_OVER_A, + {make_implication_count_query("QueryAnswerElement($Predicate1)"), + make_implication_count_query("QueryAnswerElement(#" + handle + ")")}); + BaseProxy::ORCHESTRATION_SCHEMA_TYPE orchestration = + (initial_links ? BaseProxy::SYNC_ON_CYCLE_START : BaseProxy::NONE); + shared_ptr implication_to_handle = + create_lca_proxy(make_implication_query(true), + context, + LinkCreatorRegistry::CUSTOMIZABLE, + implication_to_handle_link_creator, + orchestration); + implication_to_handle->parameters[LinkCreationProxy::MAX_ROUNDS] = (unsigned int) 1; + if (initial_links) { + implication_to_handle->parameters[LinkCreationProxy::MAX_SUCCESSFUL_CREATION_PER_ROUND] = + (unsigned int) 100; + implication_to_handle->parameters[LinkCreationProxy::MAX_UNPRODUCTIVE_VISITS_PER_ROUND] = + (unsigned int) 100000; + implication_to_handle->parameters[LinkCreationProxy::MAX_VISIT_ATTEMPTS_PER_ROUND] = + (unsigned int) 100000; + implication_to_handle->parameters[PatternMatchingQueryProxy::DISREGARD_IMPORTANCE_FLAG] = + (bool) true; + implication_to_handle->parameters[PatternMatchingQueryProxy::POSITIVE_IMPORTANCE_FLAG] = + (bool) false; + implication_to_handle->parameters[BaseQueryProxy::ATTENTION_FOCUS_STRICTNESS] = (double) 0.0; + implication_to_handle->parameters[LinkCreationProxy::LINK_CREATION_LOG_FILE_NAME] = + (string) "_initial_links.txt"; + } + one_shot_lca_proxy.push_back(implication_to_handle); + ServiceBusSingleton::get_instance()->issue_bus_command(implication_to_handle); + Utils::sleep(2000); // XXXXX + if (initial_links) { + implication_to_handle->allow_cycle_start(); + Utils::sleep(2000); // XXXXX + while (!implication_to_handle->finished_cycle(true)) { + Utils::sleep(); + } + } +} + +static void run(const string& context_tag) { + STACK_TRACE(); + + string context = Hasher::context_handle(context_tag); + LOG_INFO("Context: " + context_tag + " -> " + context); + LOG_INFO("Setting up STI in attention broker by stimulating only: " + TARGET_PREDICATE + + " Handle: " + TARGET_PREDICATE_HANDLE + " and " + TARGET_CONCEPT + + " Handle: " + TARGET_CONCEPT_HANDLE); + AttentionBrokerClient::stimulate({{TARGET_PREDICATE_HANDLE, 1}, {TARGET_CONCEPT_HANDLE, 1}}, + context); + PRESET_LINKS_FILE = PRESET_LINKS_FILE_PREFIX + context + ".txt"; + string context_file_name = CONTEXT_FILE_NAME_PREFIX + context + ".txt"; + filesystem::path context_file_path = context_file_name; + LOG_INFO("Setting up AttentionBroker context: " + context_file_name); + if (!filesystem::exists(context_file_path)) { + RAISE_ERROR("Context file doesn't exist"); + } + AttentionBrokerClient::drop_and_load_context(context, string(context_file_path)); + LOG_INFO("Context " + context + " is ready"); + + // clang-format off vector query_to_evolve = { OR_OPERATOR, "3", AND_OPERATOR, "2", LINK_TEMPLATE, EXPRESSION, "3", - NODE, SYMBOL, EVALUATION, + NODE, SYMBOL, EVALUATION_TAG, VARIABLE, PREDICATE, ATOM, TARGET_CONCEPT_HANDLE, CHAIN_OPERATOR, "0", "1", "2", VARIABLE, PREDICATE, ATOM, TARGET_PREDICATE_HANDLE, LINK_TEMPLATE, EXPRESSION, "3", - NODE, SYMBOL, IMPLICATION, + NODE, SYMBOL, IMPLICATION_TAG, VARIABLE, PREDICATE1, VARIABLE, PREDICATE2, AND_OPERATOR, "2", LINK_TEMPLATE, EXPRESSION, "3", - NODE, SYMBOL, EVALUATION, + NODE, SYMBOL, EVALUATION_TAG, ATOM, TARGET_PREDICATE_HANDLE, VARIABLE, CONCEPT, CHAIN_OPERATOR, "0", "1", "2", VARIABLE, CONCEPT, ATOM, TARGET_CONCEPT_HANDLE, LINK_TEMPLATE, EXPRESSION, "3", - NODE, SYMBOL, EQUIVALENCE, + NODE, SYMBOL, EQUIVALENCE_TAG, VARIABLE, CONCEPT1, VARIABLE, CONCEPT2, AND_OPERATOR, "3", LINK_TEMPLATE, EXPRESSION, "3", - NODE, SYMBOL, EVALUATION, + NODE, SYMBOL, EVALUATION_TAG, VARIABLE, PREDICATE, VARIABLE, CONCEPT, CHAIN_OPERATOR, "0", "1", "2", VARIABLE, PREDICATE, ATOM, TARGET_PREDICATE_HANDLE, LINK_TEMPLATE, EXPRESSION, "3", - NODE, SYMBOL, IMPLICATION, + NODE, SYMBOL, IMPLICATION_TAG, VARIABLE, PREDICATE1, VARIABLE, PREDICATE2, CHAIN_OPERATOR, "0", "1", "2", VARIABLE, CONCEPT, ATOM, TARGET_CONCEPT_HANDLE, LINK_TEMPLATE, EXPRESSION, "3", - NODE, SYMBOL, EQUIVALENCE, + NODE, SYMBOL, EQUIVALENCE_TAG, VARIABLE, CONCEPT1, VARIABLE, CONCEPT2, }; - vector metta_query_to_evolve = { - metta_or( - metta_and( - metta_expr3(EVALUATION, metta_var(PREDICATE), TARGET_CONCEPT), - metta_chain(metta_var(PREDICATE), TARGET_PREDICATE, metta_expr3(IMPLICATION, metta_var(PREDICATE1) , metta_var(PREDICATE2))) - ), - metta_or( - metta_and( - metta_expr3(EVALUATION, TARGET_PREDICATE, metta_var(CONCEPT)), - metta_chain(metta_var(CONCEPT), TARGET_CONCEPT, metta_expr3(EQUIVALENCE, metta_var(CONCEPT1) , metta_var(CONCEPT2))) - ), - metta_and( - metta_and( - metta_expr3(EVALUATION, metta_var(PREDICATE), metta_var(CONCEPT)), - metta_chain(metta_var(PREDICATE), TARGET_PREDICATE, metta_expr3(IMPLICATION, metta_var(V1) , metta_var(V2))) - ), - metta_and( - metta_expr3(EVALUATION, metta_var(PREDICATE), metta_var(CONCEPT)), - metta_chain(metta_var(CONCEPT), TARGET_CONCEPT, metta_expr3(EQUIVALENCE, metta_var(V1) , metta_var(V2))) - ) - ) - ) - ) + /* + vector query_to_evolve = { + AND_OPERATOR, "2", + LINK_TEMPLATE, EXPRESSION, "3", + NODE, SYMBOL, EVALUATION_TAG, + VARIABLE, PREDICATE, + ATOM, TARGET_CONCEPT_HANDLE, + CHAIN_OPERATOR, "0", "1", "2", + VARIABLE, PREDICATE, + ATOM, TARGET_PREDICATE_HANDLE, + LINK_TEMPLATE, EXPRESSION, "3", + NODE, SYMBOL, IMPLICATION_TAG, + VARIABLE, PREDICATE1, + VARIABLE, PREDICATE2, }; + */ vector> correlation_query_template = { {LINK_TEMPLATE, EXPRESSION, "3", - NODE, SYMBOL, EVALUATION, + NODE, SYMBOL, EVALUATION_TAG, VARIABLE, V1, VARIABLE, CONCEPT}, {LINK_TEMPLATE, EXPRESSION, "3", - NODE, SYMBOL, EVALUATION, + NODE, SYMBOL, EVALUATION_TAG, VARIABLE, PREDICATE, VARIABLE, V2} }; - vector> correlation_metta_query_template = { - {metta_expr3(EVALUATION, metta_var(V1), metta_var(CONCEPT))}, - {metta_expr3(EVALUATION, metta_var(PREDICATE), metta_var(V2))} - }; - - vector context_determiner_query = { - LINK_TEMPLATE, EXPRESSION, "3", - NODE, SYMBOL, EVALUATION, - VARIABLE, PREDICATE, - VARIABLE, CONCEPT, - }; - - vector context_correlation_query = { - LINK_TEMPLATE, EXPRESSION, "3", - NODE, SYMBOL, EVALUATION, - VARIABLE, PREDICATE, - VARIABLE, CONCEPT, - }; - - vector context_activation_query1 = { - LINK_TEMPLATE, EXPRESSION, "3", - NODE, SYMBOL, EVALUATION, - ATOM, TARGET_PREDICATE_HANDLE, - VARIABLE, CONCEPT, - }; - vector context_activation_metta_query1 = { - metta_expr3(EVALUATION, TARGET_PREDICATE, metta_var(CONCEPT)) - }; - - vector context_activation_query2 = { - LINK_TEMPLATE, EXPRESSION, "3", - NODE, SYMBOL, EVALUATION, - VARIABLE, PREDICATE, - ATOM, TARGET_CONCEPT_HANDLE, - }; - vector context_activation_metta_query2 = { - metta_expr3(EVALUATION, metta_var(PREDICATE), TARGET_CONCEPT) - }; // clang-format on - if (SETUP_ONLY) { - LOG_INFO("****************************** Running for SETUP only ******************************"); - } - LOG_INFO("Setting up context for tag: " + context_tag); - string context = Hasher::context_handle(context_tag); - PRESET_LINKS_FILE = PRESET_LINKS_FILE_PREFIX + context + ".txt"; - filesystem::path context_file_name = CONTEXT_FILE_NAME_PREFIX + context + ".txt"; - if (!filesystem::exists(context_file_name)) { - LOG_INFO("Context file doesn't exist. Creating it..."); - QueryAnswerElement qe_predicate(PREDICATE); - QueryAnswerElement qe_concept(CONCEPT); - QueryAnswerElement qe_toplevel(0); - LOG_INFO("Creating determiners"); - add_to_context_file(context_file_name, - context, - DETERMINER, - context_determiner_query, - {{qe_toplevel, qe_predicate}, {qe_toplevel, qe_concept}}); - LOG_INFO("Making correlations"); - add_to_context_file(context_file_name, - context, - CORRELATION, - context_correlation_query, - {{qe_concept, qe_predicate}, {qe_predicate, qe_concept}}); - LOG_INFO("Spreading activation"); - add_to_context_file(context_file_name, - context, - ACTIVATION, - (USE_MORK ? context_activation_metta_query1 : context_activation_query1), - {{qe_concept}}); - add_to_context_file(context_file_name, - context, - ACTIVATION, - (USE_MORK ? context_activation_metta_query2 : context_activation_query2), - {{qe_predicate}}); - add_to_context_file( - context_file_name, context, ACTIVATION, {TARGET_PREDICATE_HANDLE, TARGET_CONCEPT_HANDLE}); - - } else { - LOG_INFO("Context file already exists. Reusing it..."); - } - LOG_INFO("Updating AttentionBroker"); - AttentionBrokerClient::drop_and_load_context(context, string(context_file_name)); - AttentionBrokerClient::stimulate({{TARGET_PREDICATE_HANDLE, 1}, {TARGET_CONCEPT_HANDLE, 1}}, - context); - LOG_INFO("Context " + context + " is ready"); - - add_preset_links(implication_to_target_predicate_metta_query, - implication_to_target_predicate_query, - equivalence_to_target_concept_metta_query, - equivalence_to_target_concept_query, - context); - - if (SETUP_ONLY) { - LOG_INFO("****************************** SETUP finished ******************************"); - return; - } - + AndTwoPredicates and_two_predicates; + CustomizableLinkCreator implication_to_target_link_creator(true); + CustomizableLinkCreator equivalence_to_target_link_creator; + CustomizableLinkCreator implication_link_creator(true); + CustomizableLinkCreator equivalence_link_creator; + CustomizableLinkCreator evaluation_link_creator; + + // initial target atoms + implication_to_target_link_creator.add_link_specification( + {QueryAnswerElement(PREDICATE1), QueryAnswerElement(TARGET_PREDICATE_HANDLE, true)}, + {QueryAnswerElement(CONCEPT1), QueryAnswerElement(CONCEPT1)}, + IMPLICATION_TAG, + CustomizableLinkCreator::INTERSECTION_OVER_A, + {make_implication_count_query("QueryAnswerElement($Predicate1)"), + make_implication_count_query("QueryAnswerElement(#" + TARGET_PREDICATE_HANDLE + ")")}); + equivalence_to_target_link_creator.add_link_specification( + {QueryAnswerElement(CONCEPT1), QueryAnswerElement(CONCEPT2)}, + {QueryAnswerElement(PREDICATE1), QueryAnswerElement(PREDICATE1)}, + EQUIVALENCE_TAG, + CustomizableLinkCreator::INTERSECTION_OVER_UNION, + {make_equivalence_count_query("QueryAnswerElement($Concept1)"), + make_equivalence_count_query("QueryAnswerElement(#" + TARGET_CONCEPT_HANDLE + ")")}); + + // Implication and equivalence + implication_link_creator.add_link_specification( + {QueryAnswerElement(PREDICATE1), QueryAnswerElement(PREDICATE2)}, + {QueryAnswerElement(CONCEPT1), QueryAnswerElement(CONCEPT1)}, + IMPLICATION_TAG, + CustomizableLinkCreator::INTERSECTION_OVER_A, + {make_implication_count_query("QueryAnswerElement($Predicate1)"), + make_implication_count_query("QueryAnswerElement($Predicate2)")}); + equivalence_link_creator.add_link_specification( + {QueryAnswerElement(CONCEPT1), QueryAnswerElement(CONCEPT2)}, + {QueryAnswerElement(PREDICATE1), QueryAnswerElement(PREDICATE1)}, + EQUIVALENCE_TAG, + CustomizableLinkCreator::INTERSECTION_OVER_UNION, + {make_equivalence_count_query("QueryAnswerElement($Concept1)"), + make_equivalence_count_query("QueryAnswerElement($Concept2)")}); + + // Weak evaluations + evaluation_link_creator.add_link_specification( + {QueryAnswerElement(PREDICATE), QueryAnswerElement(CONCEPT)}, + {QueryAnswerElement(0), QueryAnswerElement(1)}, + EVALUATION_TAG, + CustomizableLinkCreator::PRODUCT, + {}); + + // LOG_INFO("----- Adding initial links"); + // add_preset_links(context); + // sti_snapshot(context, false); // XXXXX + // exit(0); // XXXXX + /* + shared_ptr implication_to_target = create_lca_proxy(make_implication_query(true), + context, LinkCreatorRegistry::CUSTOMIZABLE, implication_to_target_link_creator, + BaseProxy::SYNC_ON_CYCLE_START); + implication_to_target->parameters[LinkCreationProxy::MAX_SUCCESSFUL_CREATION_PER_ROUND] = (unsigned + int) 100; implication_to_target->parameters[LinkCreationProxy::MAX_UNPRODUCTIVE_VISITS_PER_ROUND] = + (unsigned int) 100000; + implication_to_target->parameters[LinkCreationProxy::MAX_VISIT_ATTEMPTS_PER_ROUND] = (unsigned int) + 100000; implication_to_target->parameters[PatternMatchingQueryProxy::DISREGARD_IMPORTANCE_FLAG] = + (bool) true; implication_to_target->parameters[PatternMatchingQueryProxy::POSITIVE_IMPORTANCE_FLAG] = + (bool) false; implication_to_target->parameters[BaseQueryProxy::ATTENTION_FOCUS_STRICTNESS] = + (double) 0.0; implication_to_target->parameters[LinkCreationProxy::LINK_CREATION_LOG_FILE_NAME] = + (string) "_initial_links.txt"; + ServiceBusSingleton::get_instance()->issue_bus_command(implication_to_target); + Utils::sleep(2000); // XXXXX + implication_to_target->allow_cycle_start(); + while (!implication_to_target->finished_cycle(true)) { + Utils::sleep(); + } + */ + // LOG_INFO("--------------------------------------------------------------------------------"); + // query_evolution(query_to_evolve, correlation_query_template, 1, context); // XXXXX + // exit(0); // XXXXX + + start_one_shot_lca(TARGET_PREDICATE_HANDLE, context, true); + start_one_shot_lca(TARGET_CONCEPT_HANDLE, context, true); + // sti_snapshot(context, false); // XXXXX + // exit(0); // XXXXX + + shared_ptr and2predicates = + create_lca_proxy(make_implication_query(), + context, + LinkCreatorRegistry::AND_TWO_PREDICATES, + and_two_predicates, + BaseProxy::NONE); + shared_ptr implication = create_lca_proxy(make_implication_query(), + context, + LinkCreatorRegistry::CUSTOMIZABLE, + implication_link_creator, + BaseProxy::NONE); + shared_ptr equivalence = create_lca_proxy(make_equivalence_query(), + context, + LinkCreatorRegistry::CUSTOMIZABLE, + equivalence_link_creator, + BaseProxy::NONE); + shared_ptr evaluation_predicate = + create_lca_proxy(make_evaluation_predicate_query(), + context, + LinkCreatorRegistry::CUSTOMIZABLE, + evaluation_link_creator, + BaseProxy::NONE); + shared_ptr evaluation_concept = + create_lca_proxy(make_evaluation_concept_query(), + context, + LinkCreatorRegistry::CUSTOMIZABLE, + evaluation_link_creator, + BaseProxy::NONE); + + evaluation_predicate->parameters[LinkCreationProxy::LINK_CREATION_STRENGTH_THRESHOLD] = + (double) 0.05; + evaluation_predicate->parameters[LinkCreationProxy::LINK_CREATION_LOG_FILE_NAME] = + (string) "_new_weak_evaluation_links.txt"; + evaluation_predicate->parameters[LinkCreationProxy::LINK_CREATION_SPREAD_ACTIVATION] = (bool) false; + + evaluation_concept->parameters[LinkCreationProxy::LINK_CREATION_STRENGTH_THRESHOLD] = (double) 0.05; + evaluation_concept->parameters[LinkCreationProxy::LINK_CREATION_LOG_FILE_NAME] = + (string) "_new_weak_evaluation_links.txt"; + evaluation_concept->parameters[LinkCreationProxy::LINK_CREATION_SPREAD_ACTIVATION] = (bool) false; + + ServiceBusSingleton::get_instance()->issue_bus_command(and2predicates); + Utils::sleep(2000); // XXXXX + ServiceBusSingleton::get_instance()->issue_bus_command(implication); + Utils::sleep(2000); // XXXXX + ServiceBusSingleton::get_instance()->issue_bus_command(equivalence); + Utils::sleep(2000); // XXXXX + ServiceBusSingleton::get_instance()->issue_bus_command(evaluation_predicate); + Utils::sleep(2000); // XXXXX + ServiceBusSingleton::get_instance()->issue_bus_command(evaluation_concept); + Utils::sleep(2000); // XXXXX + + Utils::sleep(180000); // XXXXX + // Utils::sleep(300000); // XXXXX + + double best_fitness = 0.0; for (unsigned int iteration = 1; iteration <= NUM_ITERATIONS; iteration++) { LOG_INFO("--------------------------------------------------------------------------------"); LOG_INFO("Iteration " + to_string(iteration)); LOG_INFO("--------------------------------------------------------------------------------"); - LOG_INFO("----- Building links"); - AttentionBrokerClient::stimulate({{TARGET_PREDICATE_HANDLE, 1}, {TARGET_CONCEPT_HANDLE, 1}}, - context); - LOG_INFO("Building Implication links"); - build_links(implication_query, context, "", build_implication_link); - // LOG_INFO("Building Implication links to paths"); - // build_links(implication_to_path_query, context, "", build_implication_link); - LOG_INFO("Building Equivalence links"); - build_links(equivalence_query, context, "", build_equivalence_link); - // LOG_INFO("Building Equivalence links to paths"); - // build_links(equivalence_to_path_query, context, "", build_equivalence_link); - LOG_INFO("Building Evaluation links"); - build_links(evaluation_fixed_predicate_query, context, "", build_evaluation_link); - build_links(evaluation_fixed_concept_query, context, "", build_evaluation_link); - LOG_INFO("----- Updating AttentionBroker"); - AttentionBrokerClient::set_determiners(buffer_determiners, context); - buffer_determiners.clear(); - AttentionBrokerClient::stimulate({{TARGET_PREDICATE_HANDLE, 1}, {TARGET_CONCEPT_HANDLE, 1}}, - context); - LOG_INFO("----- Evolving query"); - const bool use_metta_query = USE_MORK; - query_evolution( - (use_metta_query ? metta_query_to_evolve : query_to_evolve), - (use_metta_query ? correlation_metta_query_template : correlation_query_template), - iteration, - context); + shared_ptr qe_proxy = + query_evolution(query_to_evolve, correlation_query_template, iteration, context); + + shared_ptr query_answer; + unsigned int count_answers = 0; + set hop_handles_to_process; + + while (!qe_proxy->finished()) { + if ((query_answer = qe_proxy->pop()) == NULL) { + Utils::sleep(); + } else { + count_answers++; + if (query_answer->strength > best_fitness) { + best_fitness = query_answer->strength; + LOG_INFO("ANSWER: " + answer_to_string(query_answer)); + recorded_answers.push_back({query_answer, iteration}); + } + for (unsigned int path_index = 0; path_index < query_answer->get_paths_size(); + path_index++) { + QueryAnswerElement hops(path_index, 1, 2, false, false, false); + vector hop_handles = query_answer->get_all(hops, db); + hop_handles_to_process.insert(hop_handles.begin(), hop_handles.end()); + } + } + } + for (const string& handle : hop_handles_to_process) { + start_one_shot_lca(handle, context); + } + LOG_INFO("Total answers in iteration " << iteration << ": " << count_answers); + flush_remote_link_template_cache(); } LOG_INFO("--------------------------------------------------------------------------------"); @@ -1590,7 +882,7 @@ static void run(const string& context_tag) { static void insert_type_symbols() { STACK_TRACE(); - vector to_insert = {EQUIVALENCE, IMPLICATION, LOGICAL_AND}; + vector to_insert = {EQUIVALENCE_TAG, IMPLICATION_TAG, LOGICAL_AND_TAG}; Node* node; for (string node_name : to_insert) { node = new Node(SYMBOL, node_name); @@ -1602,35 +894,13 @@ static void insert_type_symbols() { int main(int argc, char* argv[]) { STACK_TRACE(); - vector positional; - for (int i = 1; i < argc; ++i) { - string arg(argv[i]); - if (arg.rfind("--use-http=", 0) == 0) { - USE_HTTP = (arg.substr(string("--use-http=").size()) == "true"); - } else if (arg == "--use-http") { - USE_HTTP = true; - } else if (arg.rfind("--remote-fitness=", 0) == 0) { - USE_REMOTE_FITNESS = (arg.substr(string("--remote-fitness=").size()) == "true"); - } else if (arg == "--remote-fitness") { - USE_REMOTE_FITNESS = true; - } else if (arg.rfind("--http-endpoint=", 0) == 0) { - HTTP_ENDPOINT = arg.substr(string("--http-endpoint=").size()); - } else if (arg.rfind("--", 0) == 0) { - cerr << "Unknown flag: " << arg << endl; - exit(1); - } else { - positional.push_back(arg); - } - } - // clang-format off - if (positional.size() != 15) { + if (argc != 16) { cerr << "Usage: " << argv[0] << " " " " " " - " " - " [--use-http=true] [--remote-fitness=true] [--http-endpoint=host:port]" << endl; + " " << endl; cerr << endl; cerr << " are MeTTa expressions" << endl; cerr << endl; @@ -1651,23 +921,23 @@ int main(int argc, char* argv[]) { int cursor = 0; - string client_endpoint = positional[cursor++]; - string server_endpoint = positional[cursor++]; - auto ports_range = Utils::parse_ports_range(positional[cursor++]); - string config_file = positional[cursor++]; - string context_tag = positional[cursor++]; - TARGET_PREDICATE = positional[cursor++]; - TARGET_CONCEPT = positional[cursor++]; + string client_endpoint = argv[++cursor]; + string server_endpoint = argv[++cursor]; + auto ports_range = Utils::parse_ports_range(argv[++cursor]); + string config_file = argv[++cursor]; + string context_tag = argv[++cursor]; + TARGET_PREDICATE = argv[++cursor]; + TARGET_CONCEPT = argv[++cursor]; - RENT_RATE = Utils::string_to_float(positional[cursor++]); - SPREADING_RATE_LOWERBOUND = Utils::string_to_float(positional[cursor++]); - SPREADING_RATE_UPPERBOUND = Utils::string_to_float(positional[cursor++]); + RENT_RATE = Utils::string_to_float(string(argv[++cursor])); + SPREADING_RATE_LOWERBOUND = Utils::string_to_float(string(argv[++cursor])); + SPREADING_RATE_UPPERBOUND = Utils::string_to_float(string(argv[++cursor])); - ELITISM_RATE = (double) Utils::string_to_float(positional[cursor++]); - SELECTION_RATE = (double) Utils::string_to_float(positional[cursor++]); - POPULATION_SIZE = (unsigned int) Utils::string_to_int(positional[cursor++]); - MAX_GENERATIONS = (unsigned int) Utils::string_to_int(positional[cursor++]); - NUM_ITERATIONS = (unsigned int) Utils::string_to_int(positional[cursor++]); + ELITISM_RATE = (double) Utils::string_to_float(string(argv[++cursor])); + SELECTION_RATE = (double) Utils::string_to_float(string(argv[++cursor])); + POPULATION_SIZE = (unsigned int) Utils::string_to_int(string(argv[++cursor])); + MAX_GENERATIONS = (unsigned int) Utils::string_to_int(string(argv[++cursor])); + NUM_ITERATIONS = (unsigned int) Utils::string_to_int(string(argv[++cursor])); if (cursor != 15) { RAISE_ERROR("Error setting up parameters"); @@ -1678,29 +948,17 @@ int main(int argc, char* argv[]) { SystemParametersSingleton::init(json_config); AtomDBInitializer::init(atomdb_config); - if (HTTP_ENDPOINT.empty()) { - HTTP_ENDPOINT = json_config.at_path("agents.command_router.http_api.endpoint") - .get_or("localhost:40009"); - } - Utils::init_random(RANDOM_SEED); db = AtomDBSingleton::get_instance(); ServiceBusSingleton::init(client_endpoint, server_endpoint, ports_range.first, ports_range.second); FitnessFunctionRegistry::initialize_statics(); + LinkCreatorRegistry::initialize_statics(); bus = ServiceBusSingleton::get_instance(); AttentionBrokerClient::set_parameters( RENT_RATE, SPREADING_RATE_LOWERBOUND, SPREADING_RATE_UPPERBOUND); insert_type_symbols(); - LOG_INFO("USE_HTTP: " + string(USE_HTTP ? "true" : "false")); - LOG_INFO("USE_REMOTE_FITNESS: " + string(USE_REMOTE_FITNESS ? "true" : "false")); - if (USE_HTTP) { - LOG_INFO("HTTP_ENDPOINT: " + HTTP_ENDPOINT); - LOG_INFO("HTTP fitness_function_tag: " + string(USE_REMOTE_FITNESS - ? FitnessFunctionRegistry::REMOTE_FUNCTION - : FITNESS_FUNCTION)); - } LOG_INFO("ELITISM_RATE: " + to_string(ELITISM_RATE)); LOG_INFO("RENT_RATE: " + to_string(RENT_RATE)); LOG_INFO("SPREADING_RATE_LOWERBOUND: " + to_string(SPREADING_RATE_LOWERBOUND)); diff --git a/src/tests/main/inference_toy_kb_generator.cc b/src/tests/main/inference_toy_kb_generator.cc index 42de3e46f..43b238810 100644 --- a/src/tests/main/inference_toy_kb_generator.cc +++ b/src/tests/main/inference_toy_kb_generator.cc @@ -4,6 +4,7 @@ #include #include "Logger.h" +#include "Utils.h" using namespace commons; @@ -15,10 +16,12 @@ static unsigned int CONCEPT_PART_SIZE = 0; static string CHARSET = ""; static unsigned int BCD_SORT_COUNT = 0; static unsigned int SORT_COUNT = 0; +static bool CREATE_AND_PREDICATES = false; static string EVALUATION = "Evaluation"; static string PREDICATE = "Predicate"; static string CONCEPT = "Concept"; +static string LOGICAL_AND = "LogicalAnd"; // -------------------------------------------------------------------------------- // Utils @@ -58,6 +61,7 @@ void print_setup() { LOG_INFO("CONCEPT_SIZE: " + std::to_string(CONCEPT_SIZE)); LOG_INFO("CONCEPT_PART_SIZE: " + std::to_string(CONCEPT_PART_SIZE)); LOG_INFO("CHARSET: " + CHARSET); + LOG_INFO("CREATE_AND_PREDICATES: " + (CREATE_AND_PREDICATES ? string("yes") : string("no"))); } void create_concepts(vector>& concepts) { @@ -174,18 +178,60 @@ void export_sort(ofstream& metta_file, vector& _concept) { } } +void export_and_predicates(ofstream& metta_file, vector& _concept) { + for (string& part1 : _concept) { + for (string& part2 : _concept) { + if (part1 < part2) { + string predicate1_exp = "(" + PREDICATE + " " + predicate_name("contains", part1) + ")"; + string predicate2_exp = "(" + PREDICATE + " " + predicate_name("contains", part2) + ")"; + string predicate_exp = + "(" + LOGICAL_AND + " " + predicate1_exp + " " + predicate2_exp + ")"; + string concept_exp = "(" + CONCEPT + " " + concept_name(_concept) + ")"; + string evaluation_exp = "(" + EVALUATION + " " + predicate_exp + " " + concept_exp + ")"; + metta_file << evaluation_exp << endl; + } + } + } + for (string& part1 : _concept) { + for (string& part2 : _concept) { + for (string& part3 : _concept) { + if ((part1 < part2) && (part2 < part3)) { + string predicate1_exp = + "(" + PREDICATE + " " + predicate_name("contains", part1) + ")"; + string predicate2_exp = + "(" + PREDICATE + " " + predicate_name("contains", part2) + ")"; + string predicate3_exp = + "(" + PREDICATE + " " + predicate_name("contains", part3) + ")"; + string predicate_nested_exp = + "(" + LOGICAL_AND + " " + predicate2_exp + " " + predicate3_exp + ")"; + string predicate_exp = + "(" + LOGICAL_AND + " " + predicate1_exp + " " + predicate_nested_exp + ")"; + string concept_exp = "(" + CONCEPT + " " + concept_name(_concept) + ")"; + string evaluation_exp = + "(" + EVALUATION + " " + predicate_exp + " " + concept_exp + ")"; + metta_file << evaluation_exp << endl; + } + } + } + } +} + void export_concept(ofstream& metta_file, vector& _concept) { export_contains(metta_file, _concept); export_start_end(metta_file, _concept, true); export_start_end(metta_file, _concept, false); export_sort(metta_file, _concept); + if (CREATE_AND_PREDICATES) { + export_and_predicates(metta_file, _concept); + } } void export_metta(vector>& concepts) { string file_name = METTA_FILE_PREFIX + "_" + std::to_string(KB_SIZE) + "_" + std::to_string(CONCEPT_SIZE) + "_" + std::to_string(CONCEPT_PART_SIZE) + "_" + CHARSET + "_" + std::to_string(BCD_SORT_COUNT) + "_" + - std::to_string(SORT_COUNT) + ".metta"; + std::to_string(SORT_COUNT) + + (CREATE_AND_PREDICATES ? string("_AND_PREDICATES") : string("")) + ".metta"; ofstream metta_file(file_name); if (!metta_file.is_open()) { RAISE_ERROR("Unable to open file: " + file_name); @@ -194,18 +240,21 @@ void export_metta(vector>& concepts) { export_concept(metta_file, _concept); } metta_file.close(); + LOG_INFO("Done. Output written to: " + file_name); } // -------------------------------------------------------------------------------- // Main int main(int argc, char* argv[]) { - if (argc != 7) { + if (argc != 8) { RAISE_ERROR( "Usage: inference_toy_kb_generator " - " "); + " <0|1>"); + exit(1); } + Utils::init_random(1503); unsigned int count = 1; KB_SIZE = Utils::string_to_uint(string(argv[count++])); CONCEPT_SIZE = Utils::string_to_uint(string(argv[count++])); @@ -213,6 +262,7 @@ int main(int argc, char* argv[]) { CHARSET = string(argv[count++]); BCD_SORT_COUNT = Utils::string_to_uint(string(argv[count++])); SORT_COUNT = Utils::string_to_uint(string(argv[count++])); + CREATE_AND_PREDICATES = (Utils::string_to_uint(string(argv[count++])) == 1); print_setup(); if ((SORT_COUNT > BCD_SORT_COUNT) || (BCD_SORT_COUNT > KB_SIZE) || (CONCEPT_SIZE < 3)) { @@ -229,5 +279,4 @@ int main(int argc, char* argv[]) { // Exports predicates (contains, start/end, etc). BCD predicates are not explicitly exported but // sort predicates are. export_metta(concepts); - LOG_INFO("Finished"); } diff --git a/src/tests/scripts/run_toy.sh b/src/tests/scripts/run_toy.sh index 466fb98db..ef4a3180c 100755 --- a/src/tests/scripts/run_toy.sh +++ b/src/tests/scripts/run_toy.sh @@ -33,16 +33,18 @@ make run-db-loader OPTIONS="--config=/opt/das/config/das.json --file=$KB --threa echo echo "--------------------------------------------------" -echo "Stating agents" +echo "Starting agents" echo make run-attention-broker &>> /tmp/ab.log & sleep 2 make run-busnode OPTIONS="--service=query-engine --config=/opt/das/config/das.json --seed=$SEED" &>> /tmp/qa.log & sleep 2 +make run-busnode OPTIONS="--service=link-creation-agent --bus-endpoint=localhost:40002 --config=/opt/das/config/das.json --seed=$SEED" &>> /tmp/lca.log & +sleep 2 make run-busnode OPTIONS="--service=evolution-agent --bus-endpoint=localhost:40002 --config=/opt/das/config/das.json --seed=$SEED" &>> /tmp/ev.log & sleep 2 docker update -m $AVAIABLE_RAM --memory-swap $AVAIABLE_RAM $(docker ps -q) -echo "Done. Logs are in /tmp/ab.log /tmp/qa.log /tmp/ev.log" +echo "Done. Logs are in /tmp/ab.log /tmp/qa.log /tmp/ev.log /tmp/lca.log" command_line=(src/scripts/run.sh evaluation_evolution localhost:35000 localhost:40002 35001:35999 /opt/das/config/das.json $CONTEXT_TAG "$TARGET_PREDICATE" "$TARGET_CONCEPT" $RENT $SPREAD_LOWER $SPREAD_UPPER $ELITISM $SELECTION $POPULATION_SIZE $NUM_GENERATIONS $NUM_ITERATIONS) echo