Q1 Using Prolog to Solve a River Crossing Puzzle
Consider the following River Crossing Puzzle:
A family consisting of a mother, a father, three children (Alfie, Bianca and Daisy) and a puppy
want to cross a river. But the only way to cross is by rowing across using a small boat. The
Copyright By PowCoder代写 加微信 powcoder
maximum weight that can be carried in the boat without it sinking is 100 kg and the weights (in
kg) of the family members, Mum, Dad, Alfie, Bianca and Daisy and the puppy are respectively:
60kg, 85kg, 20kg, 25kg, 50kg and 10kg.
Boat crossings must also satisfy the following conditions:
• At least one person must be in the boat.
• Neither Bianca nor Daisy can cross in the boat on their own.
• Alfie can only cross in the boat if either Mum or Dad is also in the boat.
• The puppy can only cross in the boat if either Diasy or Bianca is also in the boat.
Hint: A condition such as “A can only cross with B” can be restated as: “For any crossing, either
A is not in the boat or B is in the boat” (since (A
+B) = (AVB)).
The following restrictions also apply to who can be on either side of the river:
• Neither Alfie nor Daisy nor the puppy can be left on their own on either side of the river.
• There cannot be just Alfie and the puppy (and no other family member) on either side of the
In answering this question you will sove the River Crossing Puzzle using the Prolog
bb planner code that was introduced in the module. As in the module, it is recommended that
you use the online SWISH Prolog interface available at: https://swish.swi-prolog.org
When using SWISH you must ensure that when you save your file you uncheck the “Public”
tick box, so that your file is not readable by anyone else using SWISH. This is INDIVIDUAL work
and it is your repsponsibility to ensure that your code cannot be accessed by any other student.
To help you approach this problem and to break down the marks available, you will first write
some code that addresses simpler problems related to the puzzle.
Q1.1 Total Weight
Assume that a weight predicate has been defined in Prolog as follows:
weight (mum, 60) .
weight (dad, 85).
weight(alfie,20).
weight (bianca,25).
weight (daisy, 50).
weight (puppy, 10) •
Define a predicate total weight (L, W), which is true if and only if: L is a list of entities and
W equals the total weight of the entities in L. You may assume that the predicate is only used
for cases where all entities in the list L have weights defind by the weight, relation.
For example, typical uses would be:
Q1.2 Weight Limited Selection
Since the puzzle involves a weight restriction that limits which combinations of family members
can cross in the boat, it will be useful to define a predicate that can select from a given list of
family members (e.g. those on one side of the river), which combinations of those could go in
the boat. (For this preliminary part of the question we will not worry about the other restrictions
on who can go on the boat.) The followng specification states more formally what you need to
do for this part of the question.
Define a predicate weight_limited_selection(L, Max, S) such that:
• L is either the empty list or a list of family member names (i.e. names for which the weight
relation was defined above). You may assume that L does not contain any duplicates.
S is a list that corresponds to some (possibly empty) subset of the elements in L
• The total weight (as defined by the relation weight) of the elements of S is less than or
equal to Max. (Originally I had missed out “or equal”. but “less than or equal” is a better fit
with the way the question is stated. However, it doesn’t really affect the solution and I will
mark either correct.
• The order of elements in S is the same as the order in which they occur in L
• When weight_limited selection(L, Max, S) is called with L set to a list of family
members and Max set to a particular weight, repeated calling of the predicate will
successively return all possible lists fitting the above criteria. (You can check this behaviour
by calling the predicate at the SWISH prompt — using some example list for L – and then
repeatedly pressing the “Next” button.)
As well as the weight relation, you may also assume that all built-in operations and predicates
provided in the SWISH Prolog interface are available but your code should not include any
external code.
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com