From c50452d81b08d54b734f1da73b736bfbc2997215 Mon Sep 17 00:00:00 2001 From: Aleksander Sadikov Date: Tue, 22 Sep 2015 17:57:04 +0200 Subject: New problems with hints for famrel section added. This practically completes famrel section without some details. --- prolog/problems/family_relations/aunt_2/common.py | 100 ++++++++++++++++++++++ prolog/problems/family_relations/aunt_2/sl.py | 60 +++++++++++++ 2 files changed, 160 insertions(+) create mode 100644 prolog/problems/family_relations/aunt_2/sl.py (limited to 'prolog/problems/family_relations/aunt_2') diff --git a/prolog/problems/family_relations/aunt_2/common.py b/prolog/problems/family_relations/aunt_2/common.py index f0af2dc..ed291fc 100644 --- a/prolog/problems/family_relations/aunt_2/common.py +++ b/prolog/problems/family_relations/aunt_2/common.py @@ -17,3 +17,103 @@ aunt(X, Y) :- sister98(X, Z), parent(Z, Y). ''' + +hint_type = { + 'x_and_y_mixed_up': Hint('x_and_y_mixed_up'), + 'precedence_fail': Hint('precedence_fail'), + 'x_must_be_female': Hint('x_must_be_female'), + 'aunt_vs_mother': Hint('aunt_vs_mother'), + 'x_need_not_be_parent': Hint('x_need_not_be_parent'), + 'y_need_not_be_parent': Hint('y_need_not_be_parent'), +} + +test_cases = [ + # TODO +] + +def test(program, solved_problems): + code = (program + '\n' + + server.problems.solutions_for_problems('prolog', solved_problems) + '\n' + + server.problems.load_facts('prolog', facts).facts) + + engine_id = None + try: + engine_id, output = prolog.engine.create(code=code, timeout=1.0) + if not engine_id or 'error' in map(itemgetter(0), output): + # Engine creation failed, or syntax error in code. + return 0, len(test_cases) + + n_correct = 0 + for query, answers in test_cases: + # Limit inferences for each solution to curb unbounded recursion. + limited = 'call_with_inference_limit(({}), 100000, _)'.format(query) + if prolog.engine.check_answers(engine_id, query=limited, answers=answers, timeout=1.0): + n_correct += 1 + finally: + if engine_id: + prolog.engine.destroy(engine_id) + + passed = n_correct == len(test_cases) + hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_cases)}}] + return passed, hints + +def hint(program, solved_problems): + tokens = prolog.util.tokenize(program) + + code = (program + '\n' + + server.problems.solutions_for_problems('prolog', solved_problems) + '\n' + + server.problems.load_facts('prolog', facts).facts) + + engine_id = None + try: + engine_id, output = prolog.engine.create(code=code, timeout=1.0) + if not engine_id: + raise Exception('Prolog engine failed to create.') + + # X and Y mixed up + # warning: knowledge base dependent + # independent: match on findall(X/Y, (sister(Y, P), parent(P, X)), L) + if prolog.engine.ask_truth(engine_id, + 'findall(X/Y, aunt(X, Y), \ + [vanessa/sally, patricia/sally, joanne/melanie, john/vanessa, susan/patricia])'): + return [{'id', 'x_and_y_mixed_up'}] + + # precedence fail (AND block vs OR block) + # case in point: female(X), parent(P, Y), brother(P, X) ; sister(P, X) + # warning: knowledge base dependent + if prolog.util.Token('SEMI', ';') in tokens and prolog.engine.ask_truth(engine_id, + 'findall(_, aunt(X, Y), L), length(L, 15)'): + return [{'id', 'precedence_fail'}] + + # X must be female + if prolog.engine.ask_truth(engine_id, + 'aunt(X, _), male(X)'): + return [{'id', 'x_must_be_female'}] + + # X and P can be the same person + # this can occur if the problem is not solved using sister/2 + if prolog.engine.ask_truth(engine_id, + 'aunt(X, Y), mother(X, Y)'): + return [{'id', 'aunt_vs_mother'}] + + # X does not necessarily need to be a parent + # perhaps risky as only one aunt that is not a parent exists in DB (melanie) + if prolog.engine.ask_one(engine_id, + 'aunt(X, _), \+ parent(X, _)') == 'false': + return [{'id': 'x_need_not_be_parent'}] + + # Y does not necessarily need to be a parent + # perhaps risky as only one such nephew exists in DB (susan) + if prolog.engine.ask_one(engine_id, + 'aunt(_, Y), \+ parent(Y, _)') == 'false': + return [{'id': 'y_need_not_be_parent'}] + + except socket.timeout as ex: + pass + + finally: + if engine_id: + prolog.engine.destroy(engine_id) + + return None + diff --git a/prolog/problems/family_relations/aunt_2/sl.py b/prolog/problems/family_relations/aunt_2/sl.py new file mode 100644 index 0000000..95fc6b2 --- /dev/null +++ b/prolog/problems/family_relations/aunt_2/sl.py @@ -0,0 +1,60 @@ +# coding=utf-8 + +id = 98 +name = 'aunt/2' +slug = 'X je teta od Y' + +description = '''\ +

aunt(X, Y): X je teta od Y.

+
+  ?- aunt(sally, X).
+    X = vanessa ;
+    X = patricia.
+
''' + +plan = ['''\ +

Morda lahko uporabiš rešitev kakšne prejšnje naloge?

+

+

Loni naj zbriše imena relacij (parent, sister) s te slike!

+''', '''\ +

+''', '''\ +

Če je X sestra od starša od Y, +potem je X teta od Y.

+'''] + +hint = { + 'x_and_y_mixed_up': '''\ +

X mora biti teta od Y, ne obratno.

+''', + + 'precedence_fail': '''\ +

Si morda narobe upošteval prioriteto operatorjev IN ter ALI?

+

Operator IN veže močneje od ALI, če želiš spremeniti prioriteto, +lahko uporabiš oklepaje.

+''', + + 'x_must_be_female': '''\ +

Teta je navadno ženskega spola.

+''', # TODO: Loni should mark female gender on some aunt hints! + + 'aunt_vs_mother': '''\ +

Kako je teta lahko hkrati še mama od Y? +Si morda pozabil, da teta in starš od Y ne smeta biti ista oseba?

+

+''', + + 'x_need_not_be_parent': '''\ +

X pravzaprav ne rabi imeti otrok, da je lahko teta od nekoga...

+''', + + 'y_need_not_be_parent': '''\ +

Nečak ali nečakinja Y pravzaprav ne rabi imeti otrok, da ima teto...

+''', + + 'interesting_tidbit': '''\ +

Zanimivost: nalogo bi lahko rešil tudi z uporabo rešitve za relacijo "sister". +Teta je namreč sestra od starša od Y.

+''', # TODO: only trigger if sister is not part of correct solution +} + -- cgit v1.2.1