Strictly Judging Objects

by Paul Curzon and Peter W. McOwan, Queen Mary University of London

CraigRevelHorwoodStrictly Come Dancing, has been popular for a long time. It’s not just the celebs, the pros, or the dancing that make it must see TV – it’s having the right mix of personalities in the judges too. Craig Revel Horwood has made a career of being rude, if always fair. By contrast, Darcey Bussell is far more supportive. Bruno Tonioli is supportive but always excited. Len Goodman was similarly supportive but calm. Shirley Ballas the new head judge will presumably refine her character over time, but seems to be aiming for supportive but strict. It’s often the tension between the judges that makes good TV: those looks that Darcy gives Craig, never mind when they start actually arguing. However, if you believe Dr Who, the future of judges will be robots like AnneDroid in the space-age version of The Weakest Link…let’s look at the Bot future. How might you go about designing computer judges, and how might objects help?

Write the code 

We need to write a program. We will use a pseudocode – a mix of code and English – here rather than any particular programming language to make things easier to follow.

The first thing to realise is we don’t want to have to program each judge separately. That would mean describing the rules for every new judge from scratch each time they swap. We want to do as little as possible to describe each new one. Judges have a lot in common so we want to pull out those common patterns and code them up just once.

What makes a judge?

First let’s describe a basic judge. We will create a plan, a bit like an architect’s plan of a building. Programmers call these a ‘class’. The thing to realise about classes is that a class for a Judge is NOT the code of any actual working judge just the code of how to create one: a blueprint. This blueprint can be used to build as many individual judges as you need.

What’s the X-factor that makes a judge a judge? First we need to decide on some basic properties or attributes of judges. We can make a list of them, and what the possibilities for each are. The things common to all judges is they have names, personalities and they make judgements on people. Let’s simply say a judge’s personality can be either supportive or rude, and their judgements are just marks out of 10 for whoever they last watched.

Name : String
CharacterTrait : SUPPORTIVE, RUDE
Judgement : 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

We have just created some new ‘types’ in programming terminology. A type is just a grouping of values. The type CharacterTrait has two values possible (SUPPORTIVE or RUDE), whereas the type Judgement has 10 possible values. We also used one common, existing type: String. Strings are just sequences of letters, numbers or symbols, so we are saying something of type Name is any such sequence.

Let’s start to describe Judges as people with a name, personality and capable of thinking of a mark.

DESCRIPTION OF A Judge:
    Name name
    CharacterTrait personality
    Judgement mark

This says that each judge will be described by three variables, one called name, one called personality and one called mark. This kind of variable is called an instance variable –  because each judge we create from this plan will have their own copy, or instance, of the instance variables that describes that judge.

All we are saying here is whenever we create a Judge it will have a name, a personal character (it will be either RUDE or SUPPORTIVE) and a potential mark.

For any given judge we will always refer to their name using variable name and their character trait using variable personality. Each new judge will also have a current judgement, which we will refer to as mark: a number between 1 and 10. Notice we use the types we created, Name, Character and Judgment to specify the possible values each of these variables can hold.

Best Behaviour

We are now able to say in our judge blueprint, our class, whether a judge is rude or supportive, but we haven’t actually said what that means. We need to set out the actual behaviours associated with being rude and supportive. We will do this in a fairly simple way, just to illustrate. Let’s assume that the personality shows in the things they say when they give their judgement. A rude judge will say “It was a disaster” unless they are awarding a mark above 8/10. For high marks they will grudgingly say “You were ok I suppose”. We translate this into commands of how to give a judgment.

IF (personality IS RUDE) AND (mark <= 8) THEN SAY “It was a disaster” IF (personality IS RUDE) AND (mark > 8)
THEN SAY “You were ok I suppose”

It would be easy for us to give them lots more things to choose to say in a similar way, it’s just more rules. We can do a similar thing for a supportive judge. They will say “You were stunning” if they award more than 5 out of 10 and otherwise say “You tried really hard”.

TO GiveJudgement:
    IF (personality IS RUDE) AND (mark <= 8)     THEN SAY “It was a disaster”     IF (personality IS RUDE) AND (mark > 8)
    THEN SAY “You were ok I suppose”

    IF (personality IS SUPPORTIVE) AND (mark > 5)
    THEN SAY “You were stunning”

    IF (personality IS SUPPORTIVE) AND (mark <= 5)
    THEN SAY “You tried hard”

A Ten from Len

The other thing that judges do is actually come up with their judgement, their mark.  For real judges it would be based on rules about what they saw – a lack of pointed toes pulls it down, lots of wiggle at the right times pushes it up… We will assume, to keep it simple here, that they actually just think of a random number – essentially throw a 10 sided dice under the desk with numbers 1-10 on!

TO MakeJudgement:
    mark = RANDOM (1 TO 10)

Finally, judges can reveal their mark.

TO RevealMark:
    SAY mark

Notice this doesn’t mean they say the word “mark”. mark is a variable so this means say whatever is currently stored in that judges mark.

Putting that all together to make our full judge class we get:

DESCRIPTION OF A Judge:
    Name name
    CharacterTrait personality
    Judgement mark

    TO GiveJudgement:
        IF (personality IS RUDE) AND (mark <= 8)         THEN SAY “It was a disaster”         IF (personality IS RUDE) AND (mark > 8)
        THEN SAY “You were ok I suppose”

        IF (personality IS SUPPORTIVE) AND (mark > 5)
        THEN SAY “You were stunning”

        IF (personality IS SUPPORTIVE) AND (mark <= 5)
        THEN SAY “You tried hard”

    TO MakeJudgement:
        mark = RANDOM (1 TO 10)

    TO RevealMark
        SAY mark

What is a class?

So what is a class? A class says how to build an object. It defines properties or attributes (like name, personality and current mark) but it also defines behaviours: it can speak, it can make a judgement and it can reveal the current mark. These behaviours are defined by methods – mini-programs that specify how any Judge should behave. Our class says that each Judge will have its own set of the methods that use that Judge’s own instance variables to store its properties and decide what to do.

So a class is a blueprint that tells us how to make particular things: objects. We have so far made a class definition for making Judges. We haven’t made any actual objects so far though – defining a class does not in itself give you any actual objects – no actual Judges exist to judge anything yet. We need to write specific commands to do that as we will see.

We can store away our blueprint and just pull it out to make use of it when we need to create some actual judges.

Kind words for our contestants?

Suppose Strictly is starting up so we want to create some judges, starting with a rude judge, called Craig Devil Droidwood. We can use our class as the blueprint to do so. We need to say what its personality is (Judges just think of a mark when they actually see an act so we don’t have to give a mark now.)

CraigDevilDroidwood IS A NEW Judge 
                    WITH name “Craig Devil Droidwood”
                    AND personality RUDE

This creates a new judge called Craig Devil Droidwood and makes it Rude. We have instantiated the class to give a Judge object. We store this object in a variable called CraigDevilDroidwood.

For a supportive judge that we decide to call Len Goodroid we would just say (instantiating the class in a different way):

LenGoodroid IS A NEW Judge 
            WITH name “Len Goodroid”
            AND personality SUPPORTIVE

Another supportive judge DarC3PO BussL would be created with

DarC3POBussL IS A NEW Judge 
             WITH name “DarC3PO BussL”
             AND personality SUPPORTIVE

Whereas in the class we are describing a blueprint to use to create a Judge, here we are actually using that blueprint and making different Judges from it. So this way we can quickly and easily make new judge clones without copying out all the description again. These commands executed at the start of our program (and TV programme) actually create the objects. They create instances of class Judge, which just means they create actual virtual judges with their own name and personality. They also each have their own copy of the rules for the behaviour of judges.

Execute Them

Once actual judges are created, they can execute commands to start the judging. First the program tells them to make judgements using their judgement method. We execute the MakeJudgement method associated with each separate judge object in turn. Each has the same instructions but those instructions work on the particular judges instance variables, so do different things.

EXECUTE MakeJudgement OF CraigDevilDroidwood
EXECUTE MakeJudgement OF DarC3POBussL
EXECUTE MakeJudgement OF LenGoodroid

Then the program has commands telling them to say what they think,

EXECUTE GiveJudgement OF CraigDevilDroidwood
EXECUTE GiveJudgement OF DarC3POBussL
EXECUTE GiveJudgement OF LenGoodroid

and finally give their mark.

EXECUTE RevealMark OF CraigDevilDroidwood
EXECUTE RevealMark OF DarC3POBussL
EXECUTE RevealMark OF LenGoodroid

In our actual program this would sit in a loop so our program might be something like:

CraigDevilDroidwood IS A NEW Judge 
                    WITH name “Craig Devil Droidwood”
                    AND personality RUDE
DarC3POBussL        IS A NEW Judge 
                    WITH name “DarC3PO BussL”
                    AND personality SUPPORTIVE
LenGoodroid         IS A NEW Judge 
                    WITH name “Len Goodroid”
                    AND personality SUPPORTIVE

FOR EACH contestant DO THE FOLLOWING
    EXECUTE MakeJudgement OF CraigDevilDroidwood
    EXECUTE MakeJudgement OF DarC3POBussL
    EXECUTE MakeJudgement OF LenGoodroid

    EXECUTE GiveJudgement OF CraigDevilDroidwood
    EXECUTE GiveJudgement OF DarC3POBussL
    EXECUTE GiveJudgement OF LenGoodroid

    EXECUTE RevealMark OF CraigDevilDroidwood
    EXECUTE RevealMark OF DarC3POBussL
    EXECUTE RevealMark OF LenGoodroid

So we can now create judges to our hearts content, fixing their personalities and putting the words in their mouths based on our single description of what a Judge is. Of course our behaviours so far are simple. We really want to add more kinds of personality like strict judges (Shirley) and excited ones (Bruno). Ideally we want to be able to do different combinations making perhaps excited rude judges as well as excited supportive ones. This really just takes more rules.

A classless society?

Computer Scientists are lazy beings – if they can find a way to do something that involves less work, they do it, allowing them to stay in bed longer. The idea we have been using to save work here is just that of describing classes of things and their properties and behaviour. Scientists have been doing a similar thing for a long time:

Birds have feathers (a property) and lay eggs (a behaviour).

Spiders have eight legs (a property) and make silk (a behaviour)

We can say something is a particular instance of a class of thing and that tells us a lot about it without having to spell it all out each time, even for fictional things: eg Hedwig is a bird (so feathers and eggs). Charlotte is a spider (so legs and silk). The class is capturing the common patterns behind the things we are describing. The difference when Computer Scientists’ write them is because they are programs they can then come alive!

All Change

We have specified what it means to be a robotic judge and we’ve only had to specify the basics of Judgeness once to do it. That means that if we decide to change anything in the basic judge (like giving them a better way to come up with a mark than randomly or having them choose things to say from a big database of supportive or rude comments) changing it in the plan will apply to all the judges of whatever kind. That is one of the most powerful reasons for programming in this way.

We could create robot performers in a similar way (after all don’t all the winners seem to merge into one in the end?). We would then also have to write some instructions about how to work out who won – does the audience have a vote? How many get knocked out each week? … and so on.

Of course we’ve not written a full program, just sketched a program in pseudocode. The next step is to convert this into a program in an object-oriented programming language like Python or Java. Is that hard? Why not give it a try and judge for yourself?

Catch up with Paul’s other blog posts

 

A decomposed face: What is an object?

Paul Curzon, Queen Mary University of London

Object-oriented programming is an important part of modern programming. The concepts involved are complex though, and if learning to program in this way it is vital to understand at a high level what it is about at the outset. The first concept to get to grips with is just what an object actually is and why we might want to use objects to structure our programs. We will use an example of building an unplugged face to demonstrate.

Learn about: Object-oriented programming, objects, decomposition, simulation, computational modelling

Simulation

Object-oriented programming (OOP) arose from a desire to write programs that simulate things in the real world. Simulation has given a different way of doing science that compliments experiments: simulate the phenomena in a virtual way. The real world is made of objects and the natural way to do this is to think about programming the behaviour of those objects, whether people, cars, ants, photons or cannon balls, separately. Once one ant’s behaviour is programmed, we can then have lots of them – as many as we like –  each following the same rules. From one ant program we can get a whole colony.

If the theory embodied in your program is right, the simulation should match the real world behaviour. If we understand ant behaviour and have programmed it correctly, then our virtual ants should behave like a real ant colony. If they don’t we must inspect our rules and work out what needs to change, and so improve our understanding of ant behaviour. This computational modelling of phenomena of interest is one of the main ways Computational Thinking has changed the way the Sciences are done.

Decomposition

OOP also gives a powerful general way to write programs, from games to airline booking systems. As we will see it is really all about decomposition. It gives a new way of organising a program: breaking it in to parts that can be written separately.

Simulating Emotions

Let’s demonstrate by simulating human expressions on a robot face. Here is a video of a robot doing the sort of thing we are interested in. It doesn’t understand the words spoken. It just reacts to the tone of the person’s voice, giving an appropriate expression. If the voice is angry it looks unhappy. If the person talks softly then it cheers up. If they speak suddenly, it is surprised.

Procedural Programming: Rules for Expressions 

How do you write a program to do that? For each expression you need to write rules that move the mouth, eyes and eyebrows in the right way. You could try and write rules about the whole face. It’s always a good idea, though, to break up a program in to separate parts to make it easy to write. Then you can program and test each on its own. Each is a smaller and more manageable problem.

Here you might decompose the program by different expressions. First you might write a procedure, called Happy, that controlled all the parts of a face to give a happy expression. Once that worked, you might write a procedure called Sad to give a sad face, and then one called Surprise that looked surprised, and so on. Each of these procedures would include instructions about the whole face. In procedural programming the focus is on decomposing the program in to the separate operations, separate procedures, like this.

Objects: Rules for Eyes, Eyebrows and Mouth

Instead of thinking about expressions, another, completely different way to think about decomposing, and so programming, the face is in terms of its individual parts: the objects that make up a face. A face is made of eyes, eyebrows and a mouth (and other things we will ignore for now). We can specify their behaviour separately from the behaviour of the rest of the face. We can work out what eyes do without worrying about eyebrows, mouths or even noses. They can be added later. In effect to simulate a face, we can separately write mini-programs that simulate an eye, an eyebrow and a mouth.

An unplugged face

Rather than write a program we will demonstrate this in an unplugged way as it is the concepts of objects and behaviours that we are interested in understanding here. We don’t want to get bogged down in syntax. First we get volunteers to be our robot. Two people hold eyes, two eyebrows and two the mouth (the bits are made out of Blue Peter technology – card, tube and sticky-backed plastic). Each person represents one of the objects that make up the face.

We have a robot, but it does nothing without instructions. We need to write rules that give the behaviour. Each person needs to be told what to do to control the object they control.

Eyes

Let’s focus on eyes first. Notice we are now working on a much simpler, though similar, problem – worrying abut simulating eyes rather than whole faces. What is their behaviour? Well, our robot eye can be wide open or narrowed. We want the former when it hears a nice or a surprising noise. We want the latter when it hears a nasty noise. We could at this point decompose, writing rules for each expression separately, but its simple enough we will do it all at once.

The behaviour of an EYE object is:

If NICE SOUND then WIDE OPEN

If NASTY SOUND then NARROWED

If SUDDEN SOUND then WIDE OPEN

This is essentially a program for an eye (just written in a language a person can follow). We can come up with these rules completely separately from the instructions for any other object. If we make two eye objects that follow these rules we already have the basics of a face.  We could test it and see if it starts to show the right expressions. Of course we only need one set of instructions for an eye. We can then reuse it whenever we need an eye.

We call the rules for behaviours like this methods, and they are just a form of procedure or function but in an object-oriented context. There is also a distinction between an object and a class.

Our rules for an eye above are really a plan or template for how to build an eye object. It is what we call a class definition for eye objects. We need to create an instance of that class – the actual object – each time we want an actual eye. In our unplugged word of volunteers, creating an instance involves taking a volunteer and giving them each a copy of the eye instructions – they each become eye objects. Our simple face is built of two eye objects – so in our instructions of how to build a face we will need commands to create two such instances of eyes, each separately following the same general rules.

Eyebrows

Having done that and tested it to make sure it does the right thing we can move on to programming other objects – writing their class definitions. Let’s do the eyebrows next. An eyebrow can move UP high or be low DOWN over the eyes. For happy and sad expressions the eyebrows will be in a normal, down position, but we raise our eyebrows when surprised.

The behaviour of an EYEBROW object is:

If NICE SOUND then DOWN

If NASTY SOUND then DOWN

If SUDDEN SOUND then UP

A Mouth

Each side of the mouth also then has a similar rule, where the end of the mouth can turn UP (when happy), or it can turn DOWN (when sad), or the mouth can OPEN wide when surprised.

The behaviour of a MOUTH object is:

If NICE SOUND then UP

If NASTY SOUND then DOWN

If SUDDEN SOUND then OPEN

Building the Face

We now build a face object just by putting together the right bits: two eyes, two eyebrows and a mouth. We create instances of them to make a face. Each part follows its programmed behaviours and what emerges is a face acting like a face: looking happy, sad or surprised.

Details, details

Of course to fully complete the program we need to write instructions as to what UP and DOWN, OPEN, NICE, NASTY and SUDDEN sounds actually are. This is another example of decomposition and linked abstraction given we are ignoring the details of how to code them in this explanation. We ultimately need to write methods to do each of those things.

The Power of Objects

Overall the main way we have decomposed the problem is by thinking of the objects that make up a face and separately programming their behaviours. The face itself might just then be part of a bigger endeavour to make a robot body…what about our body language for different emotions? That might be part of a program to simulate an audience at a concert, which might be part of a city simulation,…It is a natural way to build up large programs from smaller parts.

The idea of objects also has power because they are naturally reusable. Each object can be replicated as many times as needed. Want an alien with three eyes. Just add another eye. Its behaviour is already coded. Want a whole crowd of faces, then just replicate the whole face object. This also naturally scales, at least on the right sort of problem. That is ultimately what it is about, trying to find a style of programming, a form of decomposition, that allows us to write massively large and complicated programs in a way that is possible to do and get right.

Decomposing the problem in to objects in the way we’ve described is only the first step though. Once you have done that, you find other opportunities open open for reusing code in natural ways which leads to more complex concepts like encapsulation and inheritance that give even more benefits.

Catch up with Paul’s other blog posts

 

Debugging Spot the Difference

Solve spot the difference puzzles based on programs, practice debugging and develop an attention to detail. 

Spot the difference puzzles are usually done on pictures. Here we do them with code. We have scattered typical mistakes and slips through programs. Do you have the attention to detail to find them all. Check your own code as you write it and when you get compiler errors for similar mistakes. Attention to detail is an important skill for computer scientists to develop.

Learn about:

  • debugging
  • the importance of attention to detail

Resources:

FREE summer school for 14-18yo at UCL on 3D animated short film – #art #computing

Please pass this information on to arts or computing teachers, and pupils / students who might be interested in a free animation summer school.
3dami
When Wednesday 22 July to Thursday 30 July 2015
Where UCL, London
For whom 14 to 18 year old UK pupils / students
Cost FREE (includes free food, accommodation and transport)
What – 3D animation summer school
3Dami is a 7 day summer school where groups of students run their own studio and create their own animated short film from scratch. It operates at the intersection of art and technology (computer science), and is well suited to students with an interest in both. Students get to experience a semi-realistic studio setup, and create their film as a real studio would – it requires teamwork, thinking on their feet and hard work. The skills taught are directly related to the film effects and computer game industries, both of which are booming in the UK. There will be an industry visit and talks given by experts. The event is completely free for UK students aged 14-18 (includes free food, transport and accommodation!) and runs at UCL (London) from July 22nd to 30th. Please visit the website (3dami.org) to watch last years films and for further details, including how to apply.
Find out more and how to apply at http://www.3dami.org
Supported by
3Dami is supported by Creative Skillset’s Film Skills Fund, which is funded by the BFI with National Lottery funds, through the Skills Investment Funds. The event is happening with support from City and Islington and the CAS Hub. There is also another event happening in Cardiff, see the website for more info.