Hi there!
John Bawler again, we're going to be looking at
the problem number three in the intermediate section of the information
theory part of this course.
Number three asks us to modify the "CoinFlipInformationContent.nlogo"
model and change it into "DiceRollInformationContent.nlogo".
So instead of flipping a coin, we're going to roll a die.
Alright, let's look how we would do that.
Here is our model.
And I'm going to make that a little bit bigger so we can see things well.
Now first thing we'll do is save this as DieRoll.nlogo.
Okay, so we'll save that.
Alright, so the first thing I would do is instead of flipping a coin,
I'm going to leave a lot of the interface elements and a lot of the
backend code in place, just because we'll use that as analogy to make our code.
First thing instead of flipping a fair coin, clearly we're going to need a new
button that says "roll" and we'll name that to roll a die.
So we're going to have a command that's called "Roll".
So instead of flipping the fair coin, let's see what that does.
That says Flip Fair Coin, there's a procedure in the backend called
"Flip Fair Coin".
Our procedure is going to be "Roll". This box here is an important box.
This we'll edit this and instead of "coin result" we will call it, if I hit the
right one, call this "Roll Result".
So, let's again look at the interface. A lot of things broke when I changed that
so we'll see if we can fix those.
We know that we're going to have a box that collects the results of the rolls.
Let's just step back a second and see how the original program worked because when
we flipped the coin, a heads and tails showed up here and essentially it
calculated the amount of information using Shannon's information equation.
And let me see if I can make this smaller again so we can see.
Yeah, and that information content showed up down here if you look at this box here
you will see that there's a reporter that was just looking at the value of "h".
So, in the backend we can see that all of the interesting work was done, not
"interesting" but, all of the heavy lifting was done by these procedures here
that actually calculated Shannon's information from a probability table,
and that the probability table was generated here from our frequency table
and that frequency table was built right here and that relied on a word or a string
of words that was created by this procedure.
Fortunately, we don't have to mess with any of those procedures because all we
have to do is change a different set of words that will represent the values of
the rolled die and we'll put those in this Roll Result.
And we'll use those same procedures just to calculate the thing.
So our task here is to create an animation I guess, an image of a die
and make that image report, instead of heads and tails, 1, 2, 3, 4, 5 or 6.
So it's actually a fairly easy process because the calculations will be done
for us, we just need to be able to generate text in this
window that is different.
So let's again look here.
I'm going to revisit how this does Flip Fair Coin.
So then we're going to go to the backend and see how flipfaircoin works.
It's a very simple procedure that just sets the result,
which is a global variable, to one of heads or tails.
So we will make our own procedure that's analogous to this to Roll, and this will
set the results to, instead of heads or tails we're going to be using 1,2,3,4,5,6.
So we're going to be using those variables no, not variables, those are strings.
And they're going to refer to this shape that we'll create in a second.
Whenever I see a literal like this then I suspect we might use it multiple times.
I will actually copy it and put it in the set up procedure, actually first I'll make
that a global variable. Let's call that "the list."
Because we're going to use this more than once so I want to call that, and we'll
just make a literal that allows us to use that over and over again.
Now instead of coming down here saying all of this, I just refer to it as the list.
That's a little shorthand because I know that I'm going to be using that list of
those values again. So, its nice to have it in one place.
So, we set the result to one of the list. That's the same as they've done there.
Now, we'll have another procedure that shows what the roll does.
We'll want to animate that roll after it happens. Now let's see.
We're going to need to have a bunch of shapes so I'm going to jump into the shape
editor and I'll show you the first one and I'll let you run on your own to
generate the rest of it.
Quickly, what we'll do is to go into the shape editor.
Look here's something that already looks like a die pretty much.
I'm just going to get this, change the color to white, turn off snap to grid.
Draw a little circle. Duplicate that circle, put it there. Duplicate, there.
Duplicate, there. Duplicate, there. Duplicate, there.
If I was a picky sort of person I would make lines like so to see if things were
lined up. But I'm not a picky person like that.
We need to make sure that we label these correctly. This one would be labeled one.
And then once we have 1 labeled we can duplicate it and label the next one 2.
And just move things around until it looks correct to you.
Let's see if that's a 2 then let's make this side 4.
This is a little bit tedious but why don't you go ahead and put me on pause and go
ahead and finish all six of these. We're going to need 6 die.
1,2,3,4,5,6. This one's obviously number 2.
So just finish those. Label them correctly - when you get them done turn me back on.
Now you're back and I'm just saving my final one number 6.
We've got them all done - 1,2,3,4,5,6. A bunch of shapes in our shape editor.
Now we're ready to jump back to our model and let's look at our set up procedure.
In the original model they set up and set the shape to "heads".
We have this thing called the list, so we could set the shape to, instead of heads
to one-of the-list.
So let's see what happens now when we click to our interface and view setup.
Nothing.
Set shape to one of the list and the list is that.
I wonder if we have a problem with our capitalization?
The shapes are actually only allowed to be lower case.
Let's see if we have a problem.
Might be that we have these other problems in the code that we need to resolve before
we can run setup. I think that's probably the case.
Nothing in coin results so let's change our "coinresults" to "rollresults."
So every place we could use the search and replace here, but I think there are
only a couple of them.
And there's no show roll to show roll. No coin results. Set roll-results.
Okay, lets see if that works.
Now, we click setup. Okay, now it works.
So, we get these different random die. Now what we don't have is in their
original script, we set result there and then we show the flip.
We set the result and we show the roll. We don't do that.
Their show the roll actually did more then just showing the roll.
Their show, or flip I should say, actually kept track of how many heads
and tails there were.
Ultimately and importantly, it set the value in that window "rollresults" to a
string like this.
So I'm going to copy this, this is an important line.
This is the last thing I have so we need to make sure that whatever else show roll
does that it sets the results to this. Now let's see what happens.
Now when we setup, we roll the die. It looks like it sets the results there.
Looks like we can use a space in there. I'm going to go into this "set roll
results" and it's currently using this word operator which is a concatenation
operator. It basically makes a string out of everything that's already in roll
results and tags on this result.
So, what I'm going to do is I'm going to use the other version of this command,
which is the parentheses version. I'm going to insert a space right in there
a little string space. Let's see if that fixes that problem.
Now when we roll die, sure enough, we get values inserted there.
My first attempt at fixing this was to change these things,
that doesn't really matter. That was a red herring.
So now let's see. We don't really have any kind of animation.
Let's build a little animation to make this pretty. They had the flip it.
They flopped back and forth between some other shapes.
We can do that as well. We will show roll. We will say if "results=1"
then what are we going to do?
We are going to ask the turtles to repeat four times.
What are we going to repeat? We are going to set the shape to one of the list.
So four times we're going to ask them to change their shape.
And then we'll ask them to wait for half a sec. And then we'll ask them to set the
shape to one. So, this will give us a nice little animation. It's going to randomly
flip the shape to a few things wait a little bit, finally set the shape to one.
So, that's what it'll do if it's one. Let's see is that correct?
Looks like I'm missing a bracket. So once we get one of them correct,
we can copy and paste that.
Call this one two, this one three, four, (fast forward if you like)
five, six. Four, three and two.
Okay now we've set the animation. Let's see if it works.
So we go back here, nothing in results has been defined.
Let's look at that set result. Okay, let's see if that works now.
Looks like it's working. So, what it's doing, it's generating a list and if we
calculate the information on that list. That looks about right to me.
So we can roll the die again, calculate the information.
It's calculating the information off of the frequency of this list right here.
So let's start over again. Roll it one time.
We should get an information content of zero when there's only one.
Now if we roll it twice, we should still have an information content of zero.
Because it's the same value. Oh my gosh.
There we go. So looks like it's working. There you go.