Skip to content

Commit

Permalink
Optimize complexity_factor calculation
Browse files Browse the repository at this point in the history
Calculate once at insertion rather than at each iteration.

Address the exp part of opencog#54
  • Loading branch information
ngeiswei committed Feb 21, 2020
1 parent 05f3f3b commit ec3da1a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
14 changes: 10 additions & 4 deletions opencog/ure/forwardchainer/SourceSet.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@

namespace opencog {

Source::Source(const Handle& bdy, const Handle& vdcl, double cpx)
: body(bdy), vardecl(vdcl), complexity(cpx), exhausted(false)
Source::Source(const Handle& bdy, const Handle& vdcl, double cpx, double cpx_fctr)
: body(bdy),
vardecl(vdcl),
complexity(cpx),
complexity_factor(cpx_fctr),
exhausted(false)
{
}

Expand Down Expand Up @@ -137,11 +141,13 @@ void SourceSet::insert(const HandleSet& products, const Source& src, double prob

// Calculate the complexity of the new sources
double new_cpx = src.expand_complexity(prob);
double new_cpx_fctr = exp(-_config.get_complexity_penalty() * new_cpx);

// Insert all new sources
int new_sources = 0;
for (const Handle& product : products) {
Source* new_src = new Source(product, empty_variable_list, new_cpx);
Source* new_src = new Source(product, empty_variable_list,
new_cpx, new_cpx_fctr);

// Make sure it isn't already in the sources
if (boost::binary_search(sources, *new_src)) {
Expand Down Expand Up @@ -188,7 +194,7 @@ double SourceSet::get_weight(const Source& src) const

double SourceSet::complexity_factor(const Source& src) const
{
return exp(-_config.get_complexity_penalty() * src.complexity);
return src.complexity_factor;
}

std::string oc_to_string(const Source& src, const std::string& indent)
Expand Down
13 changes: 9 additions & 4 deletions opencog/ure/forwardchainer/SourceSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ class Source : public boost::totally_ordered<Source>
public:
Source(const Handle& body,
const Handle& vardecl=Handle::UNDEFINED,
double complexity=0.0);
double complexity=0.0,
double complexity_factor=1.0);

/**
* Comparison operators. For operator< compare by complexity, or by
Expand Down Expand Up @@ -86,13 +87,17 @@ class Source : public boost::totally_ordered<Source>
std::string to_string(const std::string& indent=empty_string) const;

// Body of the source
Handle body;
const Handle body;

// Variable declaration, if any, associated to body
Handle vardecl;
const Handle vardecl;

// Sum of the complexities of the steps involved in producing it.
double complexity;
const double complexity;

// Proxy for the prior probability of the source, taking into
// account the complexity penalty.
const double complexity_factor;

// True iff all rules that could expand the source have been tried
bool exhausted;
Expand Down

0 comments on commit ec3da1a

Please sign in to comment.