diff options
author | Aleksander Sadikov <sasha@fri.uni-lj.si> | 2015-09-10 20:39:02 +0200 |
---|---|---|
committer | Aleksander Sadikov <sasha@fri.uni-lj.si> | 2015-09-10 20:39:02 +0200 |
commit | ee50df2225a974857e74d5ecb7c5f503e1c6b18a (patch) | |
tree | 79143284009f882fde23f799764fbf063dedc588 /prolog/problems/family_relations | |
parent | 32b86afb8b11c14779c7e7c30c2e3e90d99de980 (diff) |
Hints for mother, father, brother, and sister added.
Diffstat (limited to 'prolog/problems/family_relations')
-rw-r--r-- | prolog/problems/family_relations/brother_2/common.py | 77 | ||||
-rw-r--r-- | prolog/problems/family_relations/brother_2/sl.py | 51 | ||||
-rw-r--r-- | prolog/problems/family_relations/father_2/common.py | 97 | ||||
-rw-r--r-- | prolog/problems/family_relations/father_2/en.py | 17 | ||||
-rw-r--r-- | prolog/problems/family_relations/father_2/sl.py | 53 | ||||
-rw-r--r-- | prolog/problems/family_relations/mother_2/common.py | 84 | ||||
-rw-r--r-- | prolog/problems/family_relations/mother_2/sl.py | 53 | ||||
-rw-r--r-- | prolog/problems/family_relations/sister_2/common.py | 92 | ||||
-rw-r--r-- | prolog/problems/family_relations/sister_2/sl.py | 11 |
9 files changed, 530 insertions, 5 deletions
diff --git a/prolog/problems/family_relations/brother_2/common.py b/prolog/problems/family_relations/brother_2/common.py index 6038a13..41512e6 100644 --- a/prolog/problems/family_relations/brother_2/common.py +++ b/prolog/problems/family_relations/brother_2/common.py @@ -13,3 +13,80 @@ brother(X, Y) :- male(X), X \== Y. ''' + +import prolog.engine +import prolog.util +import server.problems +import socket + +def test(session, program): + # TODO: change queries and answers (these are for sister/2) + # Test queries and expected answers (values of X). + queries = [ + ('sister(melanie, X)', set(['andrew'])), + ('sister(X, andrew)', set(['melanie'])), + ('sister(X, _)', set(['michelle', 'daniela', 'patricia', 'luana', 'sally', 'melanie', 'nevia', 'vanessa', 'anna'])), + ] + + code = program + '\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: + return 0, len(queries) + + n_correct = 0 + for query, solution in queries: + answers, error = prolog.engine.ask_all(engine_id, query=query, timeout=1.0) + if answers and answers[-1] != 'timed out': + values_x = set([ans.get('X') for ans, constraints in answers]) + if values_x == solution: + n_correct += 1 + finally: + if engine_id: + prolog.engine.destroy(engine_id) + + return n_correct, len(queries) + +def general_hint(): + pass + +def hint(session, program): + # tokens = prolog.util.tokenize(program) + + code = program + '\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 must be male + if prolog.engine.ask_truth(engine_id, 'female(X), brother(X, _)'): + return [{'id': 'x_must_be_male'}] + + # X and Y must have a common parent + if prolog.engine.ask_truth(engine_id, + 'brother(X, Y), \+ (parent(P, X), parent(P, Y))'): + return [{'id': 'common_parent_needed'}] + + # Y can be of any gender, incl. female + if prolog.engine.ask_one(engine_id, + 'brother(_, Y), female(Y)') == 'false': + return [{'id': 'Y_can_be_of_any_gender'}] + + # X and Y must be different + ans = prolog.engine.ask_one(engine_id, + 'setof(X, (member(X, [william, alessandro, andrew]), brother(X, X)), L), length(L, N).') + if ans.get('N') == '3': + return [{'id': 'x_y_must_be_different'}] + + except socket.timeout as ex: + pass + + finally: + if engine_id: + prolog.engine.destroy(engine_id) + + return None diff --git a/prolog/problems/family_relations/brother_2/sl.py b/prolog/problems/family_relations/brother_2/sl.py new file mode 100644 index 0000000..f9db389 --- /dev/null +++ b/prolog/problems/family_relations/brother_2/sl.py @@ -0,0 +1,51 @@ +# coding=utf-8 + +id = 97 +name = 'brother/2' +slug = 'X je brat od Y' + +description = '''\ +<p><code>brother(X, Y)</code>: <code>X</code> je brat od <code>Y</code>.</p> +<pre> + ?- brother(jeffrey, X). + X = william ; + X = sally. +</pre>''' + +hint = { + 'general_hint_1': '''\ +<p><img src="Sister-1.png" /></p> +''', + + 'general_hint_2': '''\ +<p><img src="Sister-2.png" /></p> +''', + + 'general_hint_3': '''\ +<p><img src="Sister-2.png" /></p> +<pre> +parent(P, X) +parent(P, Y) +male(X) +</pre> +''', + + 'x_y_must_be_different': '''\ +<p>Si pomislil, da sta <code>X</code> in <code>Y</code> lahko ista oseba? Poskusi naslednjo poizvedbo:</p> +<pre> +?- brother(william, Y). +</pre> +''', + + 'x_must_be_male': '''\ +<p>Brat je navadno moškega spola.</p> +''', + + 'Y_can_be_of_any_gender': '''\ +<p><code>Y</code> je pravzaprav lahko poljubnega spola.</p> +''', + + 'common_parent_needed': '''\ +<p>Dobro bi bilo, da imata <code>X</code> in <code>Y</code> vsaj enega skupnega starša.</p> +''', +} diff --git a/prolog/problems/family_relations/father_2/common.py b/prolog/problems/family_relations/father_2/common.py new file mode 100644 index 0000000..e1b46fd --- /dev/null +++ b/prolog/problems/family_relations/father_2/common.py @@ -0,0 +1,97 @@ +# coding=utf-8 + +id = 999 # TODO: new id has to be defined for this problem +group = 'family_relations' +number = 999 # TODO: this should be problem #2 +visible = True +facts = 'family_relations' + +solution = '''\ +father(X, Y) :- + parent(X, Y), + male(X). +''' + +import prolog.engine +import prolog.util +import server.problems + +def test(session, program): + # TODO: change queries and answers (these are for sister/2) + # Test queries and expected answers (values of X). + queries = [ + ('sister(melanie, X)', set(['andrew'])), + ('sister(X, andrew)', set(['melanie'])), + ('sister(X, _)', set(['michelle', 'daniela', 'patricia', 'luana', 'sally', 'melanie', 'nevia', 'vanessa', 'anna'])), + ] + + code = program + '\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: + return 0, len(queries) + + n_correct = 0 + for query, solution in queries: + answers, error = prolog.engine.ask_all(engine_id, query=query, timeout=1.0) + if answers and answers[-1] != 'timed out': + values_x = set([ans.get('X') for ans, constraints in answers]) + if values_x == solution: + n_correct += 1 + finally: + if engine_id: + prolog.engine.destroy(engine_id) + + return n_correct, len(queries) + +def general_hint(): + pass + +def hint(session, program): + tokens = prolog.util.tokenize(program) + + code = program + '\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.') + + # OR (;) instead of AND (,) + # this hint has to be before the next two + # as otherwise those two would always override it + # and not convey the same (amount of) help/information + if prolog.util.Token('SEMI', ';') in tokens and prolog.engine.ask_truth(engine_id, + 'findall(_, father(X, Y), L1), length(L1, N), \ + findall(_, (parent(X, Y) ; male(X)), L2), length(L2, N)'): + return [{'id': 'or_instead_of_and'}] + + # X must be male + if prolog.engine.ask_truth(engine_id, 'female(X), father(X, _)'): + return [{'id': 'x_must_be_male'}] + + # X must be a parent + if prolog.engine.ask_truth(engine_id, + 'father(X, _), \+ parent(X, _)'): + return [{'id': 'x_must_be_parent'}] + + # Y can be of any gender, incl. female + if prolog.engine.ask_one(engine_id, + 'father(_, Y), female(Y)') == 'false': + return [{'id': 'y_can_be_of_any_gender'}] + + # Y does not necessarily need to be a parent + if prolog.engine.ask_one(engine_id, + 'father(_, 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/father_2/en.py b/prolog/problems/family_relations/father_2/en.py new file mode 100644 index 0000000..108bb9b --- /dev/null +++ b/prolog/problems/family_relations/father_2/en.py @@ -0,0 +1,17 @@ +# coding=utf-8 + +id = 94 +name = 'mother/2' +slug = 'the mother-child relation' + +description = '''\ +<p><code>mother(M, C)</code>: <code>M</code> is the mother of <code>C</code>.</p> +<pre> + ?- mother(tina, william). + true. + ?- mother(nevia, X). + X = luana ; + X = daniela. +</pre>''' + +hint = {} diff --git a/prolog/problems/family_relations/father_2/sl.py b/prolog/problems/family_relations/father_2/sl.py new file mode 100644 index 0000000..c9c38b7 --- /dev/null +++ b/prolog/problems/family_relations/father_2/sl.py @@ -0,0 +1,53 @@ +# coding=utf-8 + +id = 94 +name = 'mother/2' +slug = 'X je mama od Y' + +description = '''\ +<p><code>mother(X, Y)</code>: <code>X</code> je mama od <code>Y</code>.</p> +<pre> + ?- mother(tina, william). + true. + ?- mother(nevia, X). + X = luana ; + X = daniela. +</pre>''' + +hint = { + 'general_hint_1': '''\ +<p><img src="Father-1.png" /></p> +<p>Loni naj zbriše imena relacij (parent, mother) s te slike!</p> +''', + + 'general_hint_2': '''\ +<p><img src="Father-1.png" /></p> +''', + + 'general_hint_3': '''\ +<p>Če je <code>X</code> ženska in je hkrati <code>X</code> starš od <code>Y</code>, +potem je <code>X</code> mama od <code>Y</code>.</p> +''', + + 'or_instead_of_and': '''\ +<p>Si morda uporabil podpičje (ki pomeni ALI) namesto vejice (ki pomeni IN)?</p> +''', + + 'x_must_be_male': '''\ +<p>Oče je navadno moškega spola.</p> +<p><img src="Father-2.png" /></p> +''', + + 'x_must_be_parent': '''\ +<p>Oče naj bi imel vsaj enega otroka... torej je starš od nekoga.</p> +<p><img src="Father-3.png" /></p> +''', + + 'y_can_be_of_any_gender': '''\ +<p><code>Y</code> je pravzaprav lahko poljubnega spola.</p> +''', + + 'y_need_not_be_parent': '''\ +<p><code>Y</code> pravzaprav ne rabi imeti otrok, da ima očeta...</p> +''', +} diff --git a/prolog/problems/family_relations/mother_2/common.py b/prolog/problems/family_relations/mother_2/common.py index 65087b4..613b721 100644 --- a/prolog/problems/family_relations/mother_2/common.py +++ b/prolog/problems/family_relations/mother_2/common.py @@ -11,3 +11,87 @@ mother(X, Y) :- parent(X, Y), female(X). ''' + +import prolog.engine +import prolog.util +import server.problems + +def test(session, program): + # TODO: change queries and answers (these are for sister/2) + # Test queries and expected answers (values of X). + queries = [ + ('sister(melanie, X)', set(['andrew'])), + ('sister(X, andrew)', set(['melanie'])), + ('sister(X, _)', set(['michelle', 'daniela', 'patricia', 'luana', 'sally', 'melanie', 'nevia', 'vanessa', 'anna'])), + ] + + code = program + '\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: + return 0, len(queries) + + n_correct = 0 + for query, solution in queries: + answers, error = prolog.engine.ask_all(engine_id, query=query, timeout=1.0) + if answers and answers[-1] != 'timed out': + values_x = set([ans.get('X') for ans, constraints in answers]) + if values_x == solution: + n_correct += 1 + finally: + if engine_id: + prolog.engine.destroy(engine_id) + + return n_correct, len(queries) + +def general_hint(): + pass + +def hint(session, program): + tokens = prolog.util.tokenize(program) + + code = program + '\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.') + + # OR (;) instead of AND (,) + # this hint has to be before the next two + # as otherwise those two would always override it + # and not convey the same (amount of) help/information + if prolog.util.Token('SEMI', ';') in tokens and prolog.engine.ask_truth(engine_id, + 'findall(_, mother(X, Y), L1), length(L1, N), \ + findall(_, (parent(X, Y) ; female(X)), L2), length(L2, N)'): + return [{'id': 'or_instead_of_and'}] + + # X must be female + if prolog.engine.ask_truth(engine_id, 'male(X), mother(X, _)'): + return [{'id': 'x_must_be_female'}] + + # X must be a parent + if prolog.engine.ask_truth(engine_id, + 'mother(X, _), \+ parent(X, _)'): + return [{'id': 'x_must_be_parent'}] + + # Y can be of any gender, incl. male + if prolog.engine.ask_one(engine_id, + 'mother(_, Y), male(Y)') == 'false': + return [{'id': 'y_can_be_of_any_gender'}] + + # Y does not necessarily need to be a parent + if prolog.engine.ask_one(engine_id, + 'mother(_, 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/mother_2/sl.py b/prolog/problems/family_relations/mother_2/sl.py new file mode 100644 index 0000000..8923eb5 --- /dev/null +++ b/prolog/problems/family_relations/mother_2/sl.py @@ -0,0 +1,53 @@ +# coding=utf-8 + +id = 94 +name = 'mother/2' +slug = 'X je mama od Y' + +description = '''\ +<p><code>mother(X, Y)</code>: <code>X</code> je mama od <code>Y</code>.</p> +<pre> + ?- mother(tina, william). + true. + ?- mother(nevia, X). + X = luana ; + X = daniela. +</pre>''' + +hint = { + 'general_hint_1': '''\ +<p><img src="Mother-1.png" /></p> +<p>Loni naj zbriše imena relacij (parent, mother) s te slike!</p> +''', + + 'general_hint_2': '''\ +<p><img src="Mother-1.png" /></p> +''', + + 'general_hint_3': '''\ +<p>Če je <code>X</code> ženska in je hkrati <code>X</code> starš od <code>Y</code>, +potem je <code>X</code> mama od <code>Y</code>.</p> +''', + + 'or_instead_of_and': '''\ +<p>Si morda uporabil podpičje (ki pomeni ALI) namesto vejice (ki pomeni IN)?</p> +''', + + 'x_must_be_female': '''\ +<p>Mama je navadno ženskega spola.</p> +<p><img src="Mother-2.png" /></p> +''', + + 'x_must_be_parent': '''\ +<p>Mama naj bi imela vsaj enega otroka... torej je starš od nekoga.</p> +<p><img src="Mother-3.png" /></p> +''', + + 'y_can_be_of_any_gender': '''\ +<p><code>Y</code> je pravzaprav lahko poljubnega spola.</p> +''', + + 'y_need_not_be_parent': '''\ +<p><code>Y</code> pravzaprav ne rabi imeti otrok, da ima mamo...</p> +''', +} diff --git a/prolog/problems/family_relations/sister_2/common.py b/prolog/problems/family_relations/sister_2/common.py index 96ff47d..e18a092 100644 --- a/prolog/problems/family_relations/sister_2/common.py +++ b/prolog/problems/family_relations/sister_2/common.py @@ -15,6 +15,7 @@ sister(X, Y) :- ''' import prolog.engine +import prolog.util import server.problems def test(session, program): @@ -46,7 +47,94 @@ def test(session, program): return n_correct, len(queries) +def general_hint(): + pass + def hint(session, program): - if '\=' not in program and '\==' not in program: - return [{'id': 'x_y_must_be_different'}] + # how do I know which general hints were already shown? + # how do I know enough time has elapsed for general hints to be shown? + # how do I know whether the hint button was pressed? + # should we have another hint button which appears in hint window (like more...) + # when a hint is available after testing the current version of the program + # and this button triggers code-specific hints (i.e. non-general ones) + + # one way to do hints is to use a hierarchy of hints... + # similar to how some rule-based systems work + # the trigger-testing could be easier that way as well + # what I have in mind is: + # e.g. when I test if X is sister to itself, I've already done other trigger-tests + # and I don't need to do them again + + # first general hints (triggered by the hint button) + # do we do it like this? this button triggers general hints only? + # not sure this is ok.. discuss with others (include Janez) + + # if hbutton.pressed: + # trigger generalhint(#buttonpress) + + # code-specific hints next + + # tokenize (and similar) only if *needed* for a given exercise + # to reduce server processor load + tokens = prolog.util.tokenize(program) + + # start the engine to unit-test triggers for hints + code = program + '\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 must be female + if prolog.engine.ask_truth(engine_id, 'male(X), sister(X, _)'): + return [{'id': 'x_must_be_female'}] + + # X and Y must have a common parent + if prolog.engine.ask_truth(engine_id, + 'sister(X, Y), \+ (parent(P, X), parent(P, Y))'): + return [{'id': 'common_parent_needed'}] + + # Y can be of any gender, incl. male + # as this is after previous hints, by this stage the code probably + # does return some sensible results already + # this is an example of using the ordering of hints for easier trigger checks + if prolog.engine.ask_one(engine_id, + 'sister(_, Y), male(Y)') == 'false': + return [{'id': 'Y_can_be_of_any_gender'}] + + # X and Y must be different + # this is the last code-specific hint (when everything else doesn't trigger anymore) + ans = prolog.engine.ask_one(engine_id, + 'setof(X, (member(X, [sally, nevia, vanessa]), sister(X, X)), L), length(L, N).') + if ans.get('N') == '3': + return [{'id': 'x_y_must_be_different'}] + + # last hints are connected with programming style + # here, this means detecting such code: + # parent(P1, X), parent(P2, Y), P1 == P2 (or P1 = P2) + # I guess program tokens can be used for this + # do we require that the program is correct for this? + # or is it enough that this is after ALL code-specific hints AND it has to trigger? + + # regular expressions, anyone? + + # QUESTION to discuss: + # where do AI hints come into play? + # only after all code-specific and non stylistic hints are exhausted? + # or randomly before? or? + + # QUESTION to discuss: + # I suggest hint triggers (if code-based or also token-based?) + # are used as CLASSES of unit tests + # although.. this makes random counterexamples harder to implement + # (does it?) + + except socket.timeout as ex: + pass + + finally: + if engine_id: + prolog.engine.destroy(engine_id) + return None diff --git a/prolog/problems/family_relations/sister_2/sl.py b/prolog/problems/family_relations/sister_2/sl.py index c867567..5965bda 100644 --- a/prolog/problems/family_relations/sister_2/sl.py +++ b/prolog/problems/family_relations/sister_2/sl.py @@ -13,15 +13,20 @@ description = '''\ hint = { 'general_hint_1': '''\ -<p>Show Fig. #1 here.</p> +<p><img src="Sister-1.png" /></p> ''', 'general_hint_2': '''\ -<p>Show Fig. #2 here.</p> +<p><img src="Sister-2.png" /></p> ''', 'general_hint_3': '''\ -<p>Show Fig. #3 here.</p> +<p><img src="Sister-2.png" /></p> +<pre> +parent(P, X) +parent(P, Y) +female(X) +</pre> ''', 'x_y_must_be_different': '''\ |