In this video, I'll introduce how to separate the model file and the data file in AMPL for
LP problems.
For small problems, it is convenient to combine the model and the data into just one file,
and the coding in AMPL is straightforward.
However, for large-scale optimization problems the model and the data are usually separated
into two files.
The model file contains the generic problem formulation, and the data file contains the
known values of the parameters in the model.
If later the data is changed, you only need to make modifications on the data file and
keep the model file untouched.
This is the standard form of LP problems.
There a total of n variables and m constraints.
It can be a maximization or minimization problem.
The objective function z is equal to the sum of each coefficient cj times the variable
xj.
For each constraint, the LHS can be greater than or equal to, equal to, or less than or
equal to the RHS.
We can re-write the standard form in a more compact way like this.
The parameters are the objective function coefficients cj, the constraints coefficients
aij, and the RHS bi.
We will try to separate these coefficients and put their values in the data file.
Now let's consider this example.
This is a minimization problem.
We have 4 decision variables, x1 through x4, they are all greater than or equal to 0.
I have created the needed files for this problem.
You can download them by following the links below this video.
Make sure you download all three files (example2.mod, example2.dat, and example2.run) and put them
all in the same folder.
Now, let's start the AMPL IDE.
You need to change the current directory to the folder that contains the downloaded files.
Now, open the model file example2.mod by a double click.
"param" is a keyword.
We define two variables that store the values of constant parameters n and m, which are
the number of variables and the number of constraints, respectively.
"set" is another keyword.
It defines a finite collection of elements.
These elements can be numerical or non-numerical, and they are frequently used as indexes.
Here, we define capital letter J, which is a set that contains the indexes of all the
decision variables x1 through xn.
We put them in a pair of braces.
The two dots denote "from 1 to n".
Then, we define capital letter I, which is a set that contains the indexes of all the
constraints 1 through m.
Now we define a constant vector C {J} to represent the objective function coefficients, a constant
matrix A {I,J} to repesent the contraint coefficient matrix, a constant vector B {I} to represent
the RHS of the constraints.
After that, we define X {J} as the decision variables.
Variables by default are assumed to be real numbers.
This is a minimization problem.
z is equal to the sum of C[j] * X[j] for every j.
We have a total of m constraints.
Lower case i in upper case I means the constraints are indexed from 1 to m.
For each constraint i, we are trying to sum up the contributions of every decision variable
X[j] by multiplying it with a coefficient A[i,j].
Each constraint is greater than or equal to the RHS B[i].
These two lines of code works like a loop.
It will take care of all the constraints.
Now, open the data file example2.dat by a double click.
In this file, we assign specific values to the parameters we defined in the model file.
We have 4 decision variables, so n = 4.
We have 4 constraints, so m = 4.
Note that we use colon equal to represent value assignment.
We have four coefficients in the objective function, so c1=50, c2=20, and so on.
The first column represents the index, and the second column represents the actual value.
The constraints coefficient A is a 4 by 4 matrix, so the first row also contains the
indexes, and we separate A from the indexes with a colon.
The RHS of the constrains are assigned in B.
Now, open the command file example2.run by a double click.
The reset command is needed because when you debug the programs and make small changes,
you may need to run them multiple times.
If you don't use reset to clear the old model variables, you cannot load new ones with the
same names.
You need to reset to allow AMPL to forget the old variables.
Then we load the model, load the data, and display the problem so you can make sure it
is formulated correctly before you solve it.
After that, we change the solver (if necessary), solve the problem, and display the optimal
solution X and z.
Finally, we type "include example2.run;" in the command window and hit enter.
From the output here, we can see that this is the correct problem formulation.
The optimal solution is x1=0, x2=3.5, x3=0.75, x4=0, and z=92.5.
Okay, that is how to separate the model file and the data file in AMPL for LP problems.
Thanks for watching.