I am taking my first look at the AMPL IDE and solving a specific linear optimization problem:
--- Start of craft.mod ---
var x >= 0;
var y >= 0;
minimize cost: 4 * x + 5 * y;
subject to m1: 9 * x + 15 * y >= 174;
subject to m2 : 1 * x + 6 * y >= 28;
subject to m3 : 7 * x + 3 * y >= 40;
--- End of craft.mod ---
--- Start of craft.run ---
reset;
model 'craft.mod';
option solver gurobi;
solve;
display x, y, cost;
--- End of craft.run ---
Model and the script combined together do the job. In this example there are 2 buyable products that have 3 different components, the problem is to find the optimal combination of these two products so that the amount of every component is sufficient.
However I realize that once I have more products and more components it will be ridiculous to write out all the constraints. Therefore I am trying to create a generalized version which takes data from a separate file and that can be easily expanded to work with as many coefficient as (realistically) needed.
I'm starting with a data file which, as I imagine, should look something like this:
--- Start of craft2.dat ---
param: PROD: cost :=
Z1 4
Z2 5;
param: COMP: val1 val2 req :=
A 9 15 174
B 1 6 28
C 7 3 40;
--- End of craft2.dat ---
But how should the model look like now?
In the model part you have to work in addition with sets and parameters.
set COMPONENTS = {"A","B","C"};
param val1 {COMPONENTS };
param val2 {COMPONENTS };
param req {COMPONENTS };
The parameters get then filled in your datapart and can be index with the set in your equations.
subject to m {i in COMPONENTS}:
val1[i] * x + val2[i] * y >= req[i];
Same goes for your objective. But since you just have one objective there is probably no need for a dedicated set. It's also possible to define the set contents in the datapart and just write set COMPONENTS;
in the Modelpart.