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. But they're only valuable to you if you've actually tried this problems already. Okay. >> Okay. >> You ready Jimmy? >> It's going to be fun. >> Yeah, all right so let's look at workshop 0. 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. 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. >> 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. >> No difference. >> Okay, good. >> Okay, so, that's the first part of the problem. 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? >> 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. So, there's a number of ways of doing it, this is the most obvious way. 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. >> 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. 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. 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. Double quotation, okay. >> I'll just do all right. So we can run, we get the answer one. So how are we going to get all solutions? >> Well. >> Okay, so we're going to have to change some behavior here in the output. So we're going to use you to define behavior. Right. >> Yeah. >> And it says print all solutions. And if we go back to our model and we run. We're not seeing a lot of it. >> Yeah. >> You can see we've printed out all the solutions from one to ten. 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, everything's going to be the same. But obviously, the model is much more concise. And let's really attach the information of the range of x to where we're defined. >> 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. 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. >> 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. And then we'll be able to see what's going on. So that's what we're trying to optimize. 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. 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. >> All right. Let's move on. Now we have to do some other things here. Suppose you didn't have the mod function. How could you model that? there you go there's a challenge for you. >> 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. 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. 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. >> 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? >> Nine, I think. >> Zero to nine? >> Yep. >> Okay, zero to nine, lets call that x. 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. >> 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. Well actually, by default we'll just get an output showing us the value of x. So let's run that and see what happens. >> [INAUDIBLE] >> We haven't declared in. So that's a parameter out problem. Now we can run it. Now we're going to need to give it an n value. Let's go with an n value of 3, small value. 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? No. Well, we get something. >> Okay, yeah. >> We get basically this error message here. It's a bit hard to control this mouse. 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. All right, last one. 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. And, wow. The positions which are divisible by three should be greater than or equal to two. 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. And the last number? >> Is 3. >> Is 3. 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? 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. 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. All right. And then we need to something special with the output, right? >> Yeah. >> Output. >> Output n first. >> I put the sum, don't we? >> The sum, yes. >> So the sum of x. So now, I don't actually have to create a variable to hold it. I can just write an arbitrary expression in here. >> 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. Let's try ten. How come? Is there a bug in our problem? In our program? Let's try nine. So nine seems to work. >> Okay, how about 11? >> How about 11. 11 seems to work. So, what is going on? 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. >> 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. 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.