Skip to content

Commit

Permalink
Merge pull request opencog#24 from kasimebrahim/support-dontspecializ…
Browse files Browse the repository at this point in the history
…e-var

Support dontspecialize var
  • Loading branch information
ngeiswei authored Dec 15, 2020
2 parents bd8fe69 + 5ba0b80 commit aa8d4ea
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 44 deletions.
2 changes: 1 addition & 1 deletion opencog/miner/Miner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ HandleTree Miner::specialize_alt(const Handle& pattern,

// Calculate all shallow abstractions of pattern
// No type support for cpp-miner.
HandleSetSeq shabs = MinerUtils::shallow_abstract(valuations, param.minsup, false, false);
HandleSetSeq shabs = MinerUtils::shallow_abstract(valuations, param.minsup, false, false, {});

// Generate all associated specializations
for (unsigned i = 0; i < shabs.size(); i++) {
Expand Down
11 changes: 7 additions & 4 deletions opencog/miner/MinerSCM.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ class MinerSCM : public ModuleWrap
*/
Handle do_shallow_specialize(Handle pattern, Handle db,
Handle ms, Handle mv,
Handle type_check, Handle glob_support);
Handle type_check, Handle glob_support,
Handle ignore);

/**
* Given a pattern, a db concept and a minimum support, return
Expand Down Expand Up @@ -207,7 +208,7 @@ Handle MinerSCM::do_shallow_abstract(Handle pattern,

// Generate all shallow abstractions
HandleSetSeq shabs_per_var = // TODO add type and glob params.
MinerUtils::shallow_abstract(pattern, db_seq, ms, false, false);
MinerUtils::shallow_abstract(pattern, db_seq, ms, false, false, {});

// Turn that sequence of handle sets into a set of ready to be
// applied shallow abstractions
Expand All @@ -234,7 +235,8 @@ Handle MinerSCM::do_shallow_specialize(Handle pattern,
Handle ms_h,
Handle mv_h,
Handle type_check,
Handle glob_support)
Handle glob_support,
Handle ignore)
{
AtomSpace *as = SchemeSmob::ss_get_env_as("cog-shallow-specialize");

Expand All @@ -249,7 +251,8 @@ Handle MinerSCM::do_shallow_specialize(Handle pattern,
HandleSet shaspes =
MinerUtils::shallow_specialize(pattern, db_seq, ms, mv,
type_check->getTruthValue()->get_mean() > 0,
glob_support->getTruthValue()->get_mean() > 0);
glob_support->getTruthValue()->get_mean() > 0,
ignore->getOutgoingSet());

return as->add_link(SET_LINK, HandleSeq(shaspes.begin(), shaspes.end()));
}
Expand Down
23 changes: 17 additions & 6 deletions opencog/miner/MinerUtils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,26 @@ namespace opencog

HandleSetSeq MinerUtils::shallow_abstract(const Valuations& valuations,
unsigned ms, bool type_check,
bool glob_support)
bool glob_support,
const HandleSeq& ignore)
{
// Base case
if (valuations.no_focus())
return HandleSetSeq();

// Dont Specialize Variable if it is in ignore.
if (std::count(ignore.begin(), ignore.end(), valuations.focus_variable())) {
valuations.inc_focus_variable();
HandleSetSeq shabs_per_var = shallow_abstract(valuations, ms, type_check, glob_support, ignore);
valuations.dec_focus_variable();
return shabs_per_var;
}

// Recursive case
HandleSetSeq shabs_per_var{focus_shallow_abstract(valuations, ms,
type_check, glob_support)};
valuations.inc_focus_variable();
HandleSetSeq remaining = shallow_abstract(valuations, ms, type_check, glob_support);
HandleSetSeq remaining = shallow_abstract(valuations, ms, type_check, glob_support, ignore);
valuations.dec_focus_variable();
append(shabs_per_var, remaining);
return shabs_per_var;
Expand Down Expand Up @@ -472,22 +481,24 @@ HandleSetSeq MinerUtils::shallow_abstract(const Handle& pattern,
const HandleSeq& db,
unsigned ms,
bool type_check,
bool glob_support)
bool glob_support,
const HandleSeq& ignore)
{
Valuations valuations(pattern, db);
return shallow_abstract(valuations, ms, type_check, glob_support);
return shallow_abstract(valuations, ms, type_check, glob_support, ignore);
}

HandleSet MinerUtils::shallow_specialize(const Handle& pattern,
const HandleSeq& db,
unsigned ms,
unsigned mv,
bool type_check,
bool glob_support)
bool glob_support,
const HandleSeq& ignore)
{
// Calculate all shallow abstractions of pattern
HandleSetSeq shabs_per_var =
shallow_abstract(pattern, db, ms, type_check, glob_support);
shallow_abstract(pattern, db, ms, type_check, glob_support, ignore);

// For each variable of pattern, generate the corresponding shallow
// specializations
Expand Down
9 changes: 6 additions & 3 deletions opencog/miner/MinerUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ class MinerUtils
*/
static HandleSetSeq shallow_abstract(const Valuations &valuations,
unsigned ms, bool type_check,
bool glob_support);
bool glob_support,
const HandleSeq& ignore);

/**
* Given valuations produce all shallow abstractions reaching
Expand Down Expand Up @@ -246,7 +247,8 @@ class MinerUtils
const HandleSeq& db,
unsigned ms,
bool type_check,
bool glob_support);
bool glob_support,
const HandleSeq& ignore);

/**
* Return all shallow specializations of pattern with support ms
Expand All @@ -260,7 +262,8 @@ class MinerUtils
unsigned ms,
unsigned mv=UINT_MAX,
bool type_check=false,
bool glob_support=false);
bool glob_support=false,
const HandleSeq& ignore={});

/**
* Create a pattern body from clauses, introducing an AndLink if
Expand Down
30 changes: 20 additions & 10 deletions opencog/miner/miner-utils.scm
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
(define default-db-ratio 1)
(define default-type-check #f)
(define default-glob-support #f)
(define default-ignore '())

;; For some crazy reason I need to repaste absolutely-true here while
;; it is already defined in ure.
Expand Down Expand Up @@ -175,7 +176,8 @@
maximum-variables
maximum-spcial-conjuncts
type-check
glob-support)
glob-support
ignore)
(define mv (min 9 maximum-variables))

;; Only define such rules if maximum-spcial-conjuncts if above 0
Expand All @@ -192,7 +194,7 @@
(definify (lambda (i)
(DefineLink
(aliasify i)
(gen-shallow-specialization-rule i mv type-check glob-support))))
(gen-shallow-specialization-rule i mv type-check glob-support ignore))))
(rule-tv (stv 0.9 1))
(rulify (lambda (i) (definify i) (list (aliasify i) rule-tv)))
(rules (map rulify (iota-plus-one maximum-spcial-conjuncts))))
Expand Down Expand Up @@ -245,13 +247,15 @@
(maximum-spcial-conjuncts default-maximum-spcial-conjuncts)
(maximum-cnjexp-variables default-maximum-cnjexp-variables)
(type-check default-type-check)
(glob-support default-glob-support))
(glob-support default-glob-support)
(ignore default-ignore))
;; Load shallow specialization and associate to pm-rbs
(configure-shallow-specialization-rules pm-rbs
maximum-variables
maximum-spcial-conjuncts
type-check
glob-support)
glob-support
ignore)

;; Load conjunction-expansion and associate to pm-rbs
(configure-conjunction-expansion-rules pm-rbs
Expand All @@ -269,7 +273,8 @@
(maximum-spcial-conjuncts default-maximum-spcial-conjuncts)
(maximum-cnjexp-variables default-maximum-cnjexp-variables)
(type-check default-type-check)
(glob-support default-glob-support))
(glob-support default-glob-support)
(ignore default-ignore))
(configure-mandatory-rules pm-rbs)
(configure-optional-rules pm-rbs
#:conjunction-expansion conjunction-expansion
Expand All @@ -279,7 +284,8 @@
#:maximum-spcial-conjuncts maximum-spcial-conjuncts
#:maximum-cnjexp-variables maximum-cnjexp-variables
#:type-check type-check
#:glob-support glob-support))
#:glob-support glob-support
#:ignore ignore))

(define* (configure-surprisingness surp-rbs mode maximum-conjuncts db-ratio)
;; Add surprisingness rules
Expand Down Expand Up @@ -331,7 +337,8 @@
(maximum-spcial-conjuncts default-maximum-spcial-conjuncts)
(maximum-cnjexp-variables default-maximum-cnjexp-variables)
(type-check default-type-check)
(glob-support default-glob-support))
(glob-support default-glob-support)
(ignore default-ignore))
"
Given a Concept node representing a rule based system for the
pattern miner. Automatically configure it with the appropriate
Expand Down Expand Up @@ -396,7 +403,8 @@
#:maximum-spcial-conjuncts maximum-spcial-conjuncts
#:maximum-cnjexp-variables maximum-cnjexp-variables
#:type-check type-check
#:glob-support glob-support)
#:glob-support glob-support
#:ignore ignore)

;; Set parameters
(ure-set-jobs pm-rbs jobs)
Expand Down Expand Up @@ -611,7 +619,8 @@

;; enable type-check
(type-check default-type-check)
(glob-support default-glob-support))
(glob-support default-glob-support)
(ignore default-ignore))
"
Mine patterns in db (data trees, a.k.a. grounded hypergraphs) with minimum
support ms, optionally using mi iterations and starting from the initial
Expand Down Expand Up @@ -984,7 +993,8 @@
#:maximum-spcial-conjuncts mspc
#:maximum-cnjexp-variables mcev
#:type-check type-check
#:glob-support glob-support))
#:glob-support glob-support
#:ignore ignore))

(dummy (miner-logger-debug "Initial pattern:\n~a" (get-initial-pattern)))
(dummy (miner-logger-debug "Has enough support (min support = ~a)" ms))
Expand Down
12 changes: 7 additions & 5 deletions opencog/miner/rules/shallow-specialization.scm
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
;;
;; mv is the maximum number of variable allowed in the resulting
;; patterns.
(define (gen-shallow-specialization-rule nary mv type-check glob-support)
(define (gen-shallow-specialization-rule nary mv type-check glob-support ignore)
(let* (;; Variables
(pattern-vardecl (Variable "$vardecl"))
(cnjs (gen-variables "$cnj" nary))
Expand Down Expand Up @@ -88,22 +88,24 @@
; such. Or perhaps use Glob.
minsup-pattern
(cog-new-node 'PredicateNode "type-check" (cog-new-stv (btoi type-check) 1))
(cog-new-node 'PredicateNode "glob-support" (cog-new-stv (btoi glob-support) 1)))))))
(cog-new-node 'PredicateNode "glob-support" (cog-new-stv (btoi glob-support) 1))
ignore)))))

;; Shallow specialization formula
;;
;; mv is the maximimum number of variables
(define (gen-shallow-specialization-formula mv)
(lambda (conclusion . premises)
;; (cog-logger-debug "gen-shallow-specialization-formula mv=~a, conclusion=~a, premises=~a" mv conclusion premises)
(if (= (length premises) 3)
(cog-logger-debug "gen-shallow-specialization-formula mv=~a, conclusion=~a, premises=~a" mv conclusion premises)
(if (= (length premises) 4)
(let* ((minsup-pattern (car premises))
(type-check (cadr premises))
(glob-support (caddr premises))
(ignore (cadddr premises))
(pattern (get-pattern minsup-pattern))
(db (get-db minsup-pattern))
(ms (get-ms minsup-pattern))
(shaspes (cog-shallow-specialize pattern db ms (Number mv) type-check glob-support))
(shaspes (cog-shallow-specialize pattern db ms (Number mv) type-check glob-support ignore))
(minsup-shaspe (lambda (x) (cog-set-tv!
(minsup-eval x db ms)
(stv 1 1))))
Expand Down
56 changes: 49 additions & 7 deletions tests/miner/MinerUTest.cxxtest
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ private:
bool enforce_specialization=true,
double complexity_penalty=0.0,
bool type_check=false,
bool glob_support=false);
bool glob_support=false,
std::vector<std::string> ignore_set={});
Handle ure_pm(const HandleSeq& db, int minsup,
int max_iter=-1, Handle initpat=Handle::UNDEFINED,
bool conjunction_expansion=false,
Expand All @@ -132,7 +133,8 @@ private:
bool enforce_specialization=true,
double complexity_penalty=0.0,
bool type_check=false,
bool glob_support=false);
bool glob_support=false,
std::vector<std::string> ignore_set={});

/**
* Configure the C++ Miner and run it.
Expand Down Expand Up @@ -200,6 +202,7 @@ public:
void test_type_support_1();
void test_type_support_2();
void test_typed_glob();
void test_ignore_var();
void test_2conjuncts_1();
void test_2conjuncts_2();
void test_2conjuncts_3();
Expand Down Expand Up @@ -250,7 +253,8 @@ Handle MinerUTest::ure_pm(const AtomSpace& db_as, int minsup,
bool enforce_specialization,
double complexity_penalty,
bool type_check,
bool glob_support)
bool glob_support,
vector<string> ignore_set)
{
return MinerUTestUtils::ure_pm(_as, _scm, pm_rb, db_as, minsup,
maximum_iterations, initpat,
Expand All @@ -261,7 +265,8 @@ Handle MinerUTest::ure_pm(const AtomSpace& db_as, int minsup,
enforce_specialization,
complexity_penalty,
type_check,
glob_support);
glob_support,
ignore_set);
}

Handle MinerUTest::ure_pm(const HandleSeq& db, int minsup,
Expand All @@ -274,7 +279,8 @@ Handle MinerUTest::ure_pm(const HandleSeq& db, int minsup,
bool enforce_specialization,
double complexity_penalty,
bool type_check,
bool glob_support)
bool glob_support,
vector<string> ignore_set)
{
return MinerUTestUtils::ure_pm(_as, _scm, pm_rb, db, minsup,
maximum_iterations, initpat,
Expand All @@ -285,7 +291,8 @@ Handle MinerUTest::ure_pm(const HandleSeq& db, int minsup,
enforce_specialization,
complexity_penalty,
type_check,
glob_support);
glob_support,
ignore_set);
}

HandleTree MinerUTest::cpp_pm(const AtomSpace& db_as,
Expand Down Expand Up @@ -894,7 +901,7 @@ void MinerUTest::test_shallow_abstract()
int ms = 5;

// Calculate the shallow abstractions
HandleSetSeq result = MinerUtils::shallow_abstract(pattern, db, ms, false, false);
HandleSetSeq result = MinerUtils::shallow_abstract(pattern, db, ms, false, false, {});

// Construct expected shallow abstractions
Handle
Expand Down Expand Up @@ -1641,6 +1648,41 @@ void MinerUTest::test_typed_glob()
TS_ASSERT(are_in(ure_expected->getOutgoingSet(), ure_results->getOutgoingSet()));
}

void MinerUTest::test_ignore_var()
{
logger().info("BEGIN TEST: %s", __FUNCTION__);

// Define db
Handle InhAB = al(INHERITANCE_LINK, A, B),
InhAC = al(INHERITANCE_LINK, A, C),
InhBC = al(INHERITANCE_LINK, B, C);
HandleSeq db{A, B, C, InhAB, InhAC, InhBC};

// Define pattern parts
Handle VarXY = al(VARIABLE_SET, X, Y),
InhXY = al(INHERITANCE_LINK, X, Y),
InhAY = al(INHERITANCE_LINK, A, Y);

// Define initpat
Handle initpat = MinerUtils::mk_pattern_no_vardecl({InhXY});

// Ignore vars
// Handle ignore_set = createLink(SET_LINK, Y);
vector<string> ignore_set = {"$Y"};

// Run URE pattern miner
Handle ure_results = ure_pm(db, 2, 10, initpat,
false, UINT_MAX, UINT_MAX, 1, UINT_MAX, true, 0.0, false, false, ignore_set),
ure_expected = mk_minsup_evals(2,
{MinerUtils::mk_pattern(VarXY, {InhXY}),
MinerUtils::mk_pattern(Y, {InhAY}) });

logger().debug() << "ure_results = " << oc_to_string(ure_results);
logger().debug() << "ure_expected = " << oc_to_string(ure_expected);

TS_ASSERT(content_eq(ure_results, ure_expected));
}

void MinerUTest::test_2conjuncts_1()
{
logger().info("BEGIN TEST: %s", __FUNCTION__);
Expand Down
Loading

0 comments on commit aa8d4ea

Please sign in to comment.