All right, here we are at workshop four, and which you should guess at the end of this week, is about multiple model. Which Jimmy is a big expert on, by the way. So if we have a look at our What we meant to do, basically Lui Bei has to compose, make a musical composition where every note is different. So all the notes from 1 to n. And the difference between each note is also different. >> So it is again a permutation problem. >> Exactly. We're permuting the notes from 1 to n, as you can see in the example there. So let's go back to music, and start thinking about this thing. So we've got the size, N. Let's go, set of int, let's just give it a name. So these are notes. >> Yeah. >> And we'll actually have two sets. Even though the same. Just so we can keep track of what we're actually doing. Okay? So the first obvious model for this is we're going to build an array in the order. >> Yeah. >> So 1 to N. Of note. And that's the order. Right? That's the order of the notes. So we're saying which note is in position 1, which note is in position 2, which note is in position 3, up to which node is in position N. >> Right >> And the obvious thing we need to do, obviously, is make them all different. So, because it's a permutation. Yeah. >> Yeah. >> And then we also need to do something else. >> The difference is between adjacent notes. >> Right. >> They are all different as well. >> So, it is one less of those, right? >> Yeah. >> They're not really notes, they're differences. >> Right. >> But it's the absolute difference. >> Yeah. >> So if we introduce, so the absolute differences are going to differ from 1 to n minus 1, right? Even though they're the same. We're just going to use two sets so that we're very clear which representation, this number represents a position in the order, or this number represents a note. So this is the differences. We should call it diff n, just to separate it from the built in- >> How about this? Diffs. >> Diffs. >> N has the same name as a global constraint that we saw before. >> That's true. Yeah, that's a good idea. So we need those to be all different as well. And, of course, we have to relate the two. Write that for all IN, order one, right? >> Yeah. >> Diffs of i is equal to the absolute difference between order of i minus order of i minus 1, plus 1. So we gotta think about indexes a bit. Right, so order one. >> Yeah. >> Only goes up to so we're looking at the pairs this way. And let's save that in here. All right, I should have started from the overwrite what we already had. That should be enough. If we only. So, we're missing it close brackets there. >> And also, soft item. >> Yes. Solved, satisfied. We're just looking for any solution, right? >> Yeah. >> If we run, there we just give it some, maybe turn that off. Give it a value of 5, let's say. All right. 321415 and we can see the differences. >> Yeah. >> They're all different. >> It works. >> Yeah. All right. First thing to do was to do the output a bit more complicatedly. >> Yeah. >> So we're going to output all the notes in the order, and then the differences in the order. But we need to sort of make use of the right size. So basically, if our numbers, so how many digits do we need to do a number? And that is the, I gotta think about. If we take the logarithm of n divided by the log of 2, that's log of 10, sorry. I think that's the number digits, except that we need to, I think, take ceiling. >> [CROSSTALK] yeah. >> I hope that that's the right thing for. How many digits we need? I guess we should try it. If we take log 10, minus log 10 will be 1. But, no. So, we need- >> Plus 1. >> Plus 1. But 11 would also be one, right? Seal, so I think if we put the plus 1 in there. So, that 9 would give us 1, which is correct. All right. So that's how many digits we need to output a number. And then, basically, we're going to output the number, the order number. So we're going to use show int to output it in the right size. D digits, order of i. And then we're going to have to put D digits inside. And so how do we do that? The simplest way is, that's a good question. Or do I just need one? No I need d digits, so. >> Right. >> Let's have a try. I have a feeling that's not going to be accepted. Log of int. All right. Maybe it does LN. So we gotta take this and make it float. And I can't remember how you do that. Something like that. [LAUGH] All right, we're going to cheat, and have a look at our solution. That didn't do the complicated output. None of these are doing the complicated output. All right, we might just- >> Let's use the Web again. >> Yeah. >> No. >> [LAUGH] >> It's not the Web. >> This one? >> Yes, for some reason, I can't find the Web. All right, so we need to remember how to convert a float to an int. Or maybe its natural logarithm, that's probably what's going on, now that I think about it. Let's get rid of that, and that's the one we're working on. I think it's LM. Because we shouldn't have to do any float coercions, that should be automatic. Let's hope that works Okay, so now the problem is this is not correct. So, but we should be able to just build the string of that size. That's array. Hope that works. All right, 0.5. All right, so [LAUGH] That's output 3, 2. That was only on half of the output function. >> Right. >> And now we're going to do the same thing for the- >> Differences. >> Yeah, yeah. Order one, but we need to put- >> Space in front of it. >> Spaces in front of it. Let see what that looks like. [LAUGH] We need a new line. We can put a new line in there. Okay. Well, the real test is if we go to a bigger number. So, did we get out, I think nine works. >> Okay. >> Right? And 10. These will be the tricky ones when we go past the limit. That looks like it worked to me. >> Yeah. Peter? >> Yeah? >> Does this show_int, this is something new. Can you explain? >> Okay, so show_int is, this is an integer that were going to convert to a string. And this is the number of spaces or characters we're going to use. So it's going to pad with blanks. >> So it belongs to the same family as the show function? >> Yes, so it's just a show_int with more formatting, basically. All right. Let's get on to the next bit. That was a bit more difficult than I remembered. [LAUGH] So now, we want to think about adding an inverse view. >> Right. >> Right. So we're going to also have these other things, which are the inverse view. So this is an array of, for each note, what position is it in? Let's call that position. >> Yeah. >> And for each difference. So that's on NOTE1. What's the position of that difference? >> Exactly. >> Yeah. We still need to relate the two. Now, we're going to relate these using the inverse function. And in fact, if you have an inverse relationship, then all difference is forced to occur, right? So we don't need to keep the all different. >> Right. >> So there's an inverse relationship between order and position, and there's also an inverse relationship between diffs and the diff position. >> So, I suppose we could remove the all different constraint just because this is a permutation problem? >> Exactly, an inverse will force the permutation. >> Right. >> And the point of this was that we had this new constraint that we could add. Which says that the absolute value of position 1 minus position n Equals 1. And then we could think about this a while. This is, we know that the 1 and the N have to be next to each other. >> Yeah. >> Because that's the only way you can get an absolute value of n minus one. And we know there has to be a difference of n minus one somewhere. So that's what this is saying. The position where 1 is, and the position where n minus 1 is have to be next to each other. >> So this is an example of a redundant constraint? >> Yeah, so this is not going to change the solutions, but it is going to add more into our solver in order to work out what's going on. >> Right. >> Okay? And we can run the same thing, and I encourage you to experiment on how big a problem you can run with these two different versions of the model. >> Yeah, and you'll be amazed at how the combined model would be able to solve larger size instances of the problem. >> Although, it's not doing too well on n equals 18. But we'll leave it there.