Title: Peter Gorm Larsen
1Modelling unordered collections
2Agenda
- Set Characteristics and Primitives
- The Robot Controller
3Set Characteristics
- Sets are unordered collections of elements
- There is only one copy of each element
- The elements themselves can be arbitrary complex,
e.g. they can be sets as well - Sets in VDM are finite
- Set types in VDM are written as
- set of Type
4Set Membership
- If an object x is a member (an element) of a set
A, then we write x ? A if it is not a member
then we write x ? A. - x ? A can be written as x in set A
- x ? A can be written as x not in set A
5Set Enumeration
- A set enumeration consists of a comma-separated
list enclosed between curly braces, - For example
- 1,5,8,1,3
- true, false
- , 4,3,2,4
- g,o,d
- 3.567, 0.33455,7,7,7,7
- Are all sets
- The empty set can be written as or ?
6The Subset Relation
- The set A is said to be a subset of the set B if
every element of A is also an element of B. - The subset relation is written as A ? B or as
A subset B - Quick examples
- 1,2,3 ? 1,2,3,4,5
- ? 1,2,3
- 3,2,3,2 ? 2,3
7Set Equality
- Two sets are equal if both are subsets of each
other i.e. - A ? B and B ? A implies that A B
- Quick examples
- 2,4,1,2 4,1,2
- true, true, false false, true
- 1,1,1,1,1,1,1,1,1,1,1,1 1
- 3,4,5 3,5,5
8Proper Subsets
- The set A is said to be a proper subset of the
set B if every element of A is also an element of
B and B has at least member that is not a member
of A. - The subset relation is written as A ? B or as
A psubset B - Quick examples
- 1,2,3 ? 1,2,3,4,5
- ? 1,2,3
- 3,2,3,2 ? 2,3
9Set Cardinality
- The cardinality of a set is the number of
distinct elements i.e. its size - The cardinality of a set S is written as card S
- Quick examples
- card 1,2,3
- card
- card 3,2,3,2
10Powersets
- If S is a set then the power set of S is the set
of all subsets of S. - The powerset of a set S is written as P S or
power S - Quick examples
- power 1,2,2
- power
- power 3,2,3,1
- power power 6,7
11Set Union
- The union of two sets combines all their elements
into one set - The union of two sets A and B is written as A ?
B or A union B - Quick examples
- 1,2,2 union 1,6,5
- union true
- 3,2,3,1 union 4
12Set Intersection
- The intersection of two sets is the set of all
elements that are in both of the original sets - The intersection of two sets A and B is written
as A ? B or A inter B - Quick examples
- 1,2,2 inter 1,6,5
- inter true
- 3,2,3,1 inter 4
13Distributed Set Operators
- Union and intersection can be distributed over a
set of sets - Distributed set union
- To be written as ? (or dunion in ASCII)
- dunion 2,4,3,1,2,2,3,4,3
- dunion 2,4,3,1,1,
- dunion true,false,
- Distributed set intersection
- To be written as ? (or dinter in ASCII)
- dinter 2,4,3,1,2,2,3,4,3
- dinter 2,4,3,1,1,
- dinter true,false,
14Set Difference
- The set difference of two sets A and B is the set
of elements from A which is not in B - The set difference of two sets A and B is written
as A \ B - Quick examples
- 1,2,2 \ 1,6,5
- \ true
- 3,2,3,1 \ 4
15Overview of Set Operators
e in set s1 Membership (?) A set of A -gt
bool e not in set s1 Not membership (?) A set
of A -gt bool s1 union s2 Union (?) set of A
set of A -gt set of A s1 inter s2 Intersection
(?) set of A set of A -gt set of A s1 \
s2 Difference (\) set of A set of A -gt set of
A s1 subset s2 Subset (?) set of A set of A
-gt bool s1 psubset s2 Proper subset (?) set of
A set of A -gt bool s1 s2 Equality () set
of A set of A -gt bool s1 ltgt s2 Inequality (?)
set of A set of A -gt bool card s1 Cardinality
set of A -gt nat dunion s1 Distr. Union (?) set
of set of A -gt set of A dinter s1 Distr.
Intersection (?) set of set of A -gt set of
A power s1 Finite power set (P) set of A -gt
set of set of A
16Set Comprehensions
- Using predicates to define sets implicitly
- In VDM formulated like
- element list of bindings predicate
- The predicate part is optional
- Quick examples
- 3 x x nat x lt 3 or 3 x x in set
0,,2 - x x nat x lt 5 or x x in set 0,,4
17Questions
- What are the set enumerations for
- xx nat x lt 3
- xx nat x gt 3 and x lt 6
- y y in set 3,1,7,3
- xy x in set 1,2, y in set 7,8
- mk_(x,y) x in set 1,2,7, y in set 2,7,8 x
gt y - yy in set 0,1,2 exists x in set 0,,3 x
2 y - x 7 x in set 1,,10 x lt 6
18Set Range Expressions
- The set range expression is a special case of a
set comprehension. It has the form - e1, ..., e2
- where e1 and e2 are numeric expressions. The set
range expression denotes the set of integers from
e1 to e2 inclusive. - If e2 is smaller than e1 the set range expression
denotes the empty set. - Examples
- 2.718,...,3.141
- 3.141,...,2.718
- 1,...,5
- 8,...,6
19Agenda
- Set Characteristics and Primitives
- The Robot Controller
20The Robot Controller
- A system for navigating a robot from a start
point, via a collection of waypoints to a final
destination, where it performs some task, e.g.,
delivering a payload.
21Existing Subsystems
- Position Sensor This is used to find the robot's
current location and the direction in which it is
moving. - Steering Controller This controls the direction
in which the robot travels. - Steering Monitor A system used to ensure that
the steering controller is operating within known
safe boundaries.
22Controller Requirements
- The robot's current position is always available
to the controller from a position sensor. - The robot has a predetermined journey plan based
on a collection of waypoints. - The robot must navigate from waypoint to waypoint
without missing any. - The robot moves only horizontally or vertically
in the Cartesian plane. It is not physically
capable of changing direction with an angle
greater than 90o. Attempts to do so should be
logged. - If the robot is off-course, i.e., it cannot find
a route to the next waypoint, it should stop in
its current position. - The robot is able to detect obstacles in its
path.
23Class Diagram for Robot Controller
24A Collection of Points
class Point instance variables x nat y
nat index nat end Point
- What instance variables should the Point class
have? - How should the journeyPlan association between
the Controller and Point be made?
class Controller instance variables journeyPlan
set of Point end Controller
25Example Journey Plan
- new Point(1, 4, 1),
- new Point(4, 5, 2),
- new Point(6, 8, 3),
- new Point(10, 8, 4),
- new Point(9, 11, 5),
- new Point(8, 13, 6),
- new Point(11, 13, 7)
26Getting a Point at a Particular Index
- public static GetPointAtIndex set of Point nat
-gt - Point
- GetPointAtIndex(pts, index)
- find that value p in the set pts where
p.GetIndex() equals index - VDM Construct
- let x in set s be st predicate on x
- in
- expression using x
27The GetPointAtIndex Operation
- public static GetPointAtIndex set of Point nat
-gt - Point
- GetPointAtIndex(pts, index)
- let p in set pts be st p.GetIndex() index
- in
- p
- pre exists p in set pts p.GetIndex() index
28Checking Coordinates
- What is the value of
- new Point(1,1,1) in set new Point(1,1,1)
- Assume we have an operation inside Point
- GetCoord () gt nat nat
- How can we then test whether a waypoint has been
reached? - wp.GetCoord() in set o.GetCoord()o in set obs
29Arriving at a Waypoint
- journeyPlan desirable index properties
- Next waypoint has index 1
- Final waypoint has index equal to number of
waypoints - Indices are numbered consecutively
- Modeled as invariant inside Controller
- inv p.GetIndex() p in set journeyPlan
- 1,..., card journeyPlan
30Taking a Step on a Journey
- Inside the Point class
- public TakeStep () gt Point
- TakeStep()
- ( index index - 1
- return self
- )
- pre index gt 1
- Inside Route
- static public TakeStep set of Point -gt set of
Point - TakeStep(pts)
- let laterPoints pt pt in set pts
- pt.GetIndex() ltgt 1
- in
- p.TakeStep() p in set laterPoints
31Controlling the Robot
- Find out the robot's current position.
- Find out the next waypoint that the robot must
visit. - If this waypoint has the same location as the
current position then there are two
possibilities - Either this is the last waypoint, i.e., the robot
has reached its final destination and can
therefore complete its journey - or there are further waypoints to visit, in which
case the journey plan must be updated. - Otherwise do nothing.
- Calculate the commands needed by the steering
controller to get the robot to this next
waypoint. - Give these commands to the steering controller.
32The Update Operation
- Update () gt ()
- Update()
- let currentPosition ins.GetPosition()
- in
- ( if RouteGetPointAtIndex(journeyPlan,1).GetCoord
() - currentPosition.GetCoord()
- then
- if card journeyPlan 1
- then CompleteJourney()
- else
- ( journeyPlan RouteTakeStep(journeyPlan)
- let obstacles obs.GetData(),
- route PlotCourse(obstacles)
- in
- if route nil
- then emergencyBrake.Enable()
- else
- def dfps ComputeDesiredSteerPosition(
- ins.GetDirection(),
33Neighbours of a Journey Point
- class Point
-
- public Neighbour () gt set of Point
- Neighbour ()
- return new Point(x, y1, index 1)
- y1 in set y-1,y1
- y1 gt 0 union
- new Point(x1, y, index 1)
- x1 in set x-1,x1
- x1 gt 0
- end Point
34Plotting a Course
- class Controller
-
- PlotCourse set of (nat nat) gt Route
- PlotCourse(obstacles)
- let nextWaypoint RouteGetPointAtIndex(journeyP
lan, 1), - posRoutes RouteAvoidanceRoutes(obstacles,
-
ins.GetPosition(), -
nextWaypoint) - in
- if posRoutes
- then return nil
- else ShortestFeasibleRoute(posRoutes)
- end Controller
35Avoiding Obstacles
- class Route
-
- static
- public AvoidanceRoutes(
- obstacles set of (nat nat),
- currentPosition Point,
- nextWaypoint Point) routesset
of Route - post forall r in set routes
- r.GetFirst().GetCoord()
- currentPosition.GetCoord() and
- r.GetLast().GetCoord()
- nextWaypoint.GetCoord() and
- r.GetCoords() inter obstacles
- end Route
Does this work?
36An Invariant for the Route Class
- class Route
-
- instance variables
- points set of Point
- inv forall p1, p2 in set points
- p1.GetCoord() p2.GetCoord() gt p1 p2
and - forall p in set points
- p.GetIndex() ltgt card points
- gt GetNext(p).GetCoord() in set
- n.GetCoord() n in set p.Neighbour()
- end Route
37Summary
- What have I presented today?
- The notion of sets as unordered collections
- The basic operations in VDM for manipulating
sets - The robot controller example
- What do you need to do now?
- Continue with your project
- Present your status to all of us
- Read chapter 7 before next lecture
38Quote of the day
Do not worry about your difficulties in
Mathematics. I can assure you mine are still
greater.
By Albert Einstein (1879 - 1955)