0:00

All right, welcome to the first coding session.

Â Now, these coding sessions are designed for

Â you to try after you've tried all the workshop questions.

Â So, we're going to go through all the workshop questions and build some

Â solutions and make some mistakes in front of you, so you can see what's going on.

Â 0:32

Let's try and look at workshop 0, okay, so we got some very simple things.

Â The first thing you should do for any new language is do Hello World.

Â So we want MiniZinc model to print out hello world, that's what we need to do.

Â So let's go back to MiniZinc, let's start a new model and let's save that.

Â 0:52

Let's call it hello.

Â All right, now, how do you print out something amusing Jimmy?

Â >> Output?

Â >> Output. See, he's well trained.

Â >> [LAUGH] >> So, whoa!

Â Except I can't type anymore.

Â So we'll just output Hello, world.

Â 1:11

>> You seem to have forgotten something.

Â >> I seem to have forgotten something?

Â Why we always save.

Â Type error, what on Earth this rubbish?

Â What have I forgotten, Jimmy?

Â >> A pair of square brackets, perhaps?

Â >> Okay, the output statement takes a list of strings, not a string.

Â And that's what it outputs.

Â It outputs each of those lists in order.

Â So once I add in those square brackets and run.

Â Now I've got an error.

Â >> [INAUDIBLE] >> So

Â the model does not have a solve item.

Â So we're not actually doing anything here, but any model has to have a solve item.

Â So, this ridiculously easy model doesn't make much sense,

Â but it's the rule for MiniZinc.

Â So, we need to say something which is a.

Â Now, when we run we see our beautiful Hello World output.

Â >> Yeah.

Â >> Succeeded.

Â >> Well, Peter.

Â >> Yeah? >> You put the space after output.

Â >> Yeah. >> Does it matter?

Â >> It shouldn't.

Â >> That's right.

Â 2:13

All right, we move on, >> Yeah.

Â >> Into the output.

Â Build a MiniZinc model that takes a data file, defining an integer parameter n, and

Â prints out the value.

Â So hopefully that's not too difficult.

Â Let's have a new model, we're going to call it, we're going to save it,

Â we're going to call it IO, and we have to read in a number.

Â How do we do it?

Â 2:37

>> Well, first of all, you have to define a variable, a parameter.

Â >> A parameter, so we're going to have to declare the parameter N,

Â I mean lower case n and now we have to print it out.

Â 2:56

We right? >> I think you

Â have forgotten something again.

Â >> I've forgotten something again, now it's asking me for

Â input >> Well, it's interesting.

Â Let's see what's happening.

Â >> But it won't let me, yes.

Â Okay. Let's put in some values.

Â Go there.

Â Yes.

Â I can put in the value 4.

Â 3:12

>> There's an error.

Â >> I still don't have so a lot of, thank you.

Â >> [LAUGH] Exactly.

Â >> Interesting, it was asking me for input before it was actually going to run.

Â But here we go.

Â Now we should have a better try, N equals 4.

Â We've printed out 4.

Â Let's see if that actually works.

Â Let's give it another number.

Â Yeah. >> Wow.

Â >> It is actually working.

Â >> Impressive.

Â >> So that's one way of doing it.

Â Another way and we'll see more of this usage is this literate string approach.

Â 3:37

So if we put this backslash parenthiesis and N,

Â it's going to print out the value of that expression inside the string.

Â And that's going to be useful when we have more complicated output.

Â >> Yep. >> Otherwise, everything's the same.

Â >> Yep.

Â >> All right, we're charging through this, doing well.

Â Simple decision, we need to build a model which takes a single decision

Â variable x taken rays from one to ten and outputs its value.

Â And then we want to look at all solutions.

Â >> All right, okay.

Â >> Okay, so we're going to do something slightly more interesting.

Â What we need a new model, and it was called x110.

Â 4:15

So, we need a variable that varies from 1 to 10.

Â So we could say, there's x and add some constraints.

Â Say x is greater than or equal to one and x is less than or equal to ten.

Â >> Yes.

Â >> Then we want to of course add our solve item otherwise we'll be in trouble.

Â >> Yeah, you remember it this time.

Â >> Yes this time around.

Â Thank you.

Â 5:18

Okay, now that's probably not the best way of doing it.

Â You got a better solution?

Â I think you could just specify the type.

Â Give a range to the type of the variable.

Â >> So we could have instead just said, oops.

Â It's not to two.

Â But this is a variable that goes from one to ten.

Â >> Yeah. >> If we do that,

Â 5:49

>> All right, these are all very simple.

Â Let's look at x opt.

Â Build a model that has a decision variable x, techniversion 0 to 10.

Â X has to be divisible by four, and

Â an output of a that gives the minimum value of x minus 7 squared.

Â Mm-hm.

Â >> Okay. >> So were

Â >> More complicated this time.

Â >> Yes, we are getting harder.

Â So we got a new model.

Â 6:12

Actually, why don't we save this one, because it's similar.

Â Let's save this as [INAUDIBLE].

Â Now it was a variable x, from zero to ten and

Â we wanted to minimize I believe is X minus seven, seven squared.

Â >> Yep.

Â 6:38

>> And that's it.

Â We're done. >> I think you have forgotten to specify

Â the condition that this must be divisible by four.

Â >> Okay, X must be divisible by four.

Â How are we going to say that?

Â Well, the obvious thing is say x div 4 equals 0.

Â >> No. >> No.

Â >> I think we should use mod.

Â >> Yes we should use mod, because that's what we mean by divisible by 4.

Â >> Yeah. >> X mod 4 equals 0 okay.

Â >> Semicolon at the end.

Â >> Semicolon at the end.

Â See that's the advantage of pair programming,

Â picks up the errors before the compiler does.

Â >> Yeah. >> Okay so now this is odd,

Â why have we got 0, 4, and 8, for solutions.

Â Well, that is as expected.

Â Well, if we go back to our configuration, and go back to our default behavior,

Â the optimization problem, it prints out all solutions.

Â We're asking it to print out all solutions as it went anyway.

Â But that's what we normally want to do in our optimization problem.

Â It's going to print out all the solutions that we found on the way.

Â It found one at zero.

Â It found one at four.

Â It found one at eight.

Â And eight was the best one.

Â Each one we're getting better.

Â >> I think in this case Peter, we should differentiate between the so

Â called feasible solution and the optimal solution right?

Â >> Yeah. So

Â actually, I think we can make this a bit simpler by, why don't we introduce

Â a variable which actually is going to keep get the objective value in it.

Â 8:03

And now we're trying to minimize obj.

Â >> And then a semicolon >> And

Â then >> Yep, he's the master of semicolons.

Â And now so here's where it starts to get useful to use this literate string format.

Â 8:24

So we're going to print out the two variables.

Â >> Mm-hm.

Â >> And now we'll sort of get a better idea about what's going on in our model.

Â So now if we run, we see we've got an original value of 0 with 49 and

Â 4 with 9 and then 8 with 1.

Â And that is the best solution right there.

Â >> Right.

Â 8:54

>> Wow. >> Not that I used

Â how am I going to write down that x is divisible by 4?

Â >> So we cannot use division?

Â >> Not use division.

Â >> How about multiplication?

Â >> Yeah you can use multiplication.

Â >> Well let's try.

Â >> Okay well tell me Jimmy x div 4 Well that's div, right?

Â >> Would it work?

Â >> X div 4.

Â >> You try equal to 0 it doesn't work right?

Â >> Yeah.

Â >> But let's try something else.

Â >> Okay.

Â >> Would there exist another value y for example.

Â >> Yeah.

Â >> And y multiplied by 4 >> Yeah.

Â 9:33

Let's take a y, so let's say y multiplied by 4.

Â >> 4 would be equal to x.

Â Would it work?

Â >> Right, so that, remember, we're in relations here,

Â so this just says there exists a variable value y, which we multiply by 4,

Â gives x, which I agree, should mean that x is divisible by 4.

Â 9:54

So let's run our model now, nothing change.

Â Same exactly [INAUDIBLE].

Â >> Same solutions.

Â >> All right, now we introduce some arrays.

Â So we're going to get an array of number 6, taking various from zero to nine, and

Â that's of length N.

Â And, what do we want?

Â The sum of the numbers is equal to the product of the numbers.

Â 10:15

>> Okay.

Â So that is an interesting question.

Â Let's start with a new model and it's called array I think.

Â Now, we're going to define an array of 'n' numbers, yes?

Â >> Yes.

Â >> Of values from zero to, I can't remember, ten?

Â 10:46

And we didn't want to optimize anything so we just sort of satisfy and

Â then we want it that the sum of these numbers was equal to the product.

Â We need a constraint there.

Â >> We do, so actually, this is not too difficult to add.

Â We can just write it down.

Â >> Yeah.

Â 11:07

>> Right. >> That's the advantage

Â of declarative programming.

Â >> Exactly, we don't have to make up any complicated sums ourselves.

Â We can just use the built in some in product functions.

Â We need an output statement.

Â 11:37

Okay there's one solution.

Â 0, 0, 0, so yes, they certainly add up and they sum up to the same thing.

Â Maybe we wanted to look at all solutions, so let's get back

Â to our all-solution behavior, and have a look at what we get with all solutions.

Â All right so there's quite a few solutions All right, and if we go carefully.

Â So we got the 0, 0, 0 solution, and then we've got basically all these solutions,

Â which multiply to 6 and add up to 6, so they're correct, but

Â basically they're all really the same solution right,

Â if you're thinking about the set of numbers, they're just 1 and 2 and 3.

Â And so that's why it was suggesting

Â the rest of the questions suggest that we add a constraint.

Â Making sure that the numbers are non-decreasing.

Â >> Right.

Â >> Right.

Â So we want basically for

Â all in 1 to n-1 because we don't kind of treat the last one differently.

Â x (i) <= x (i+1).

Â Yep.

Â >> Yep.

Â >> All right so now if we run >> Just two solutions.

Â Zero, zero, zero and the one, two, three.

Â >> Can you explain the behavior of this for all statement there.

Â >> Okay, so, this saying we're going

Â to try every possible [INAUDIBLE] from one to n minus one.

Â And we're going to add this constraint.

Â X of i is going to be x of i plus 1.

Â So the first one's going to be x sub 1 instead of x sub 2.

Â X of 2 is going to x of 3, etc.

Â Now interestingly, what happens, do you think, if I accidentally had done that?

Â >> You have array out of bound.

Â >> Yeah. So do you think we'll get an error?

Â 13:31

Undefined result becomes false into Boolean context array

Â access out of bounds.

Â So, obviously we access this, and when this is n we access n+1 so

Â that's out of bounds.

Â So basically when I equals three we can see it's x is x of four.

Â And that, basically, saying well that's false, so there's no solution.

Â So, we'd get a fairly good message, but we don't actually get an abort.

Â >> Right.

Â >> We get so that's the model that we wanted.

Â 14:04

Now we're doing a bit more complicated things.

Â So we're building a length and array between zero and three.

Â The first number has to be zero the last number has to be three.

Â The sum of any two adjacent numbers in the array is at most three.

Â 14:27

And so we meant, and we also meant to do the output in a specific way.

Â Here's the sum of all the numbers in the array, and here's the actual array.

Â All right.

Â >> That's a lot of constraints.

Â >> Don't worry, we're going to do much bigger models than that in this course.

Â So actually, you might save, because it's related to what we've done now.

Â So let's save as.

Â Seek.

Â So we had an dimensional array from numbers 0 to 3, I believe.

Â Yep. We didn't need any of this.

Â Now, >> What do we need to do?

Â The first number had to be, Do you remember?

Â You've got it in front of you there Timmy.

Â >> The first number is 0.

Â >> 0.

Â Okay, so that's easy enough to say.

Â 15:22

Yep, Then we needed any two adjacent numbers, right?

Â >> Yeah. The sum-

Â >> The sum,

Â okay so adding up two adjacent numbers.

Â The sum?

Â >> Is less than or equal to three.

Â >> Is less than or equal to three.

Â Is that it?

Â 15:42

That's an additional constraint, saying that the value of x at

Â position is divisible by 3 has to be greater than or equal to 2.

Â >> Okay, so one thing that's important is it's worth running your model early on.

Â 15:58

Before you finished even completing it,

Â just to see that you've got everything so far correct.

Â So it seems to work now, we could try it with a different way, let's try five.

Â Looks to be doing something, we look to never summing up to more than three here.

Â Okay and the last constraint was four positions, which were divisible by three.

Â Was that right?

Â >> Yep.

Â >> So basically these are the positions where i mod3 = 0.

Â So these are the ones which are divisible by three.

Â >> Right.

Â >> Any time we need to add a constraint and the constraint was what?

Â The value in that position.

Â >> Must be greater than or equal to 2.

Â >> Is greater than or equal to 2.

Â 17:01

>> And then the array, right.

Â That's all I had to do, maybe put a new line on the end, and let's have a go.

Â All right, so well we're doing all solutions.

Â >> Yeah.

Â >> Which may make it a bit.

Â Let's go back to one solution.

Â That's normally what we want to do when we're doing satisfaction problems.

Â But we want to try some bigger numbers.

Â 17:49

Why doesn't 10 work?

Â >> 10 saw that, you know, we require the last number to be three.

Â >> Yep.

Â >> And then if n is equal to 10?

Â >> Yep. >> We also require

Â >> All the positions

Â that is divisible by 3- >> Yeah.

Â >> To be greater than or equal to 2.

Â >> Yep.

Â >> And now if we end up the 9th and the 10th number then we violate

Â one of the constraints in the program- >> Right.

Â >> Right? >> So you can see because we're forcing

Â the last number to be 3 then the second last number has to be 0.

Â >> Right.

Â >> Right?

Â >> And that second last number >> In the case where N equals 10 is

Â a position 9, which has to be equal to 2.

Â So. >> Yeah.

Â 18:30

>> There's nothing wrong with that model.

Â Just sometimes problems and no solutions.

Â >> Mm-hm.

Â One more thing Peter.

Â >> Yep.

Â >> I notice that the range, 1 dot, dot N appears twice in your model there.

Â >> Yep. >> You think we could simplify that?

Â >> Yeah, so that's very worthwhile.

Â We should actually give it a name.

Â So let's call it- >> Index.

Â >> Index, yeah, so it's the index set.

Â 18:56

N, and then in many models actually.

Â So in this case we can just use index every time we want to do that.

Â Now, in many models you only ever use this set.

Â But here of course, because we have this last one.

Â >> Right. >> Which is where we're looking at

Â pairs of networks we need to go from 1 to n-1.

Â If we hit that wrong we obviously would get this unsatisfiable problem again.

Â So that hasn't changed anything.

Â Same solution.

Â Yes. Surprise, surprise.

Â >> [LAUGH] >> All right that brings us to the end of

Â the first coding session.

Â