0:09

So here's our army model that we made a few lectures ago,

Â but we've made one change.

Â So before we had that the budget was 10,000, but

Â we've removed that, so we've left the budget unspecified.

Â So this is a parameter where we haven't given it the value.

Â This is a parameter without a value.

Â 0:30

So, we need to run that.

Â So when we run that model, we're going to have to give that parameter a value.

Â So a parameter is not a decision, so it has to be given a value before

Â the solver can actually know what problem it's solving.

Â So we're going to have to get that value some way.

Â So one way is to do it this way, so we're running with a model file and

Â a data file, army.dzn.

Â And we'll assume this data file here has this content, budget equals 20000.

Â So this is an assignment giving the missing value to budget.

Â So now the parameter, budget, has a value, and now it can be solved.

Â And we'll get a new description saying, okay, if we have that much army,

Â 1:23

Alternatively, we can do it this way.

Â Since this data file is so small,

Â we can actually put the data file on the command line.

Â And we do this with this -D flag, and

Â then this string here, budget = 20000, is in fact the data file, itself.

Â So we can run this alternatively this way by putting the missing

Â values to the parameters on the command line, itself.

Â 1:50

Okay, of course,

Â we can use the integrated development environment to do this instead.

Â If we run this model, MiniZinc will realize, hang on,

Â you haven't given me a value for a parameter.

Â It will actually pop up a window and ask you to type in the missing parameter.

Â So we can type in the missing parameter for

Â budget here, and hit Continue and we'll get a solution.

Â 2:09

Another way to do it is if we have army.dzn loaded into the IDE and

Â we free run, we can select a data file from this drop-down menu when we run.

Â And that will allow us to select army.dzn which has the parameter value for

Â budget, and that will also allow us to give the parameter a value.

Â The brothers observed that there was a shortage of soldiers in their army, so

Â they planned to recruit some more, but

Â they needed to loan money from some business man in order to afford this.

Â 2:40

Zhang Shiping suggested that they ask his friend, Su Shuang, for the loan.

Â Zhang Shiping spoke highly of the brothers and encouraged Su Shuang to help them.

Â The businessman, Su Shuang, agreed to lend money to the brothers, provided they would

Â pay back the loan, plus interest, over four installments in the following year.

Â 3:27

So let's look at the way the loan is going to work.

Â The money lender will lend them an amount, which is the principle.

Â That's how much money they get to lend.

Â They'll make a regular repayment, R, and there's a quarterly interest rate, I.

Â And at the end of Ith quarter, the balance that they'll owe, the I, is given by

Â the previous balance, plus the interest on the previous balance minus the repayment.

Â 4:24

So we've basically got these decisions, the repayment, the principal,

Â the balances after each of the quarters, and then the constraints that relate them.

Â So the balance after the first quarter, we take the principal,

Â we multiply by 1.0 + the interest rate to get the new principal plus the interest,

Â and we subtract the repayment.

Â Similarly, the balance after the second quarter is we take the balance after

Â the first quarter and do the same calculation.

Â And we can see the same thing for each of the other quarters.

Â 5:02

Now, let's ask some questions of this model.

Â So if Liu Bei wants to borrow $10000 at 4%,

Â repaying $2600 every quarter, how much does he owe at the end?

Â So we can have a data file that defines this instance by saying, well,

Â the interest rate is 4%, the principal is 10000 and the repayment is 2600.

Â And we can run the model by saying minizinc loan.mzn loan1.dzn.

Â So this is our data file here for this particular set of data.

Â And if we run this, we'll get the result saying that at the end we're going to

Â owe $657.78.

Â So how would we use the IDE?

Â Well, it's a bit tricky here because MiniZinc's

Â expecting when you use the IDE to fill in parameter values,

Â and here we're actually filling in decision variable values.

Â So it won't, by default, ask you for a data file.

Â But we can go to the configuration page, and select a data file in this top,

Â select a field, that's one way of doing it, and then type run.

Â Or, if we've got a project file open, we can go down, find the data file we're

Â interested with, have our model open, here, and say, run with this data file.

Â And that will also allow us to run our model with the data file.

Â So we can ask different questions with this model,

Â that's one of the reasons of our having different data files.

Â And, here, because we're changing the variables,

Â the decision variables that we're giving, we're asking different kinds of questions.

Â So if Liu Bei wants to borrow 10000 at 4% and

Â owe nothing at the end, how much does he have to repay each quarter?

Â So here's the data file for this where we're given the interest rate,

Â the principal, and the balance at the end rather than the repayment amount.

Â And this time, if we run this using the same thing, we'll find out that he has

Â to repay, basically, 2,700 every quarter in order to make this loan work.

Â 7:04

So this is an example where we're using the same model

Â with different data to actually answer a different question because

Â the different data is specifying different decision variable entry values.

Â So here's a more interesting question.

Â If Liu Bei wants to borrow $10000, owe nothing at the end, and

Â he can repay $3000 quarterly, what is the interest rate he could afford to pay?

Â So here we're given a principal of 10000, a balance of 0.0 and

Â a repayment amount of 3000.

Â Again, we can run it just the same as we did before, and

Â we'll find out that he can repay up to an interest rate of 7.71%.

Â 7:43

So, interestingly, this last question is quite a hard question to answer,

Â it's actually solving non-linear constraints.

Â And if we ask this to be solved via the G12 MIP solver, for example, which is

Â a mixed interest programming solver, we're going to get this kind of error,

Â that built-in operation float times is not supported by the MIP solver backend.

Â So to explain what's happening here is this problem is, essentially,

Â non-linear for a loan3.dzn.

Â Basically, we have to work out the interest rate,

Â we're going to solve a polynomial equation.

Â And the MIP solver just cannot express that multiplication of

Â continuous variables.

Â So it's just cannot solve this problem.

Â The default solver that we use, Gecode, uses interval arithmetic, and it can

Â solve this problem, so that's why it could solve loan3.dzn without any problem.

Â So not all solvers are equal, and you may see this that some models can run for

Â some solver and others won't.

Â Because of features of the solver, you'll certainly see differences in how quickly

Â various solvers can solve which model,

Â and which solvers can solve which models in practice.

Â 8:50

So usually, though, data files define values for parameters,

Â that's the main use of parameters.

Â We also note they could be enumerated types as well.

Â For example, for

Â our color file we could have changed the enumerator type to just say color, so

Â just declaring that there is a color in the enumerator type in our MiniZinc model.

Â And then we could have had the color in the enumerator type declared in

Â a data file.

Â So, we'd get a five color data in the data file, and

Â we could have five-colored our map, instead.

Â 9:23

So MiniZinc data files must end in .dzn.

Â And usually the data files only contain assignment items and usually only for

Â parameters.

Â But it's also possible for decision variables, as we saw in the lone examples.

Â And any parameters not assigned in the model must be assigned in the data file.

Â So there must be some way of getting that value into the model,

Â otherwise we won't be able to run it.

Â You can add multiple data files where you assign different values and

Â different parameters.

Â So on the command line it's straightforward by using

Â multiple data files, one after the other.

Â Much more difficult in the IDE, and

Â we won't go into how to do that because usually we won't have to bother.

Â 10:06

So, in summary, a model, remember,

Â is a formal description of actually a class of optimization problems.

Â And an instance is one particular optimization problem.

Â And the way you get an instance from a model is you add the data.

Â So, typically, we're going to build now models which answer many,

Â many different optimization problems, and then we'll give them different data in

Â order to solve different instances of the problem.

Â So in MiniZinc, we do this by using data files, which is .dzn files.

Â