3 Sub-models

Models can be organized into sub-models each with its own name space, and they can be distributed into several file. Let’s give a small simple example that shows also how the data can be separated from a model structure by using the two linear models above (open subModels to run it, modify it by assigning 2 to the parameter selectData) :

model subModels; 
  parameter selectData:=1; 
  if selectData=1 then data1; else data2; end; 
  set i; j; 
  parameter a{i,j}; c{j}; b{i}; 
  variable x{j}; 
  constraint C{i}: sum{j} a[i,j]*x[j] <= b[i]; 
  maximize Obj: sum{j} c[j]*x[j]; 
  Write('Objective Value = %7.2f ' n', Obj); 
  Write{j|x}(' x%-4s = %6.2f' n' , j,x); 
 
  model data1; 
    i := [1 2]; 
    j := [a b]; 
    b{i} := [350 300]; 
    c{j} := [300 200]; 
    a{i,j} := [5 5 , 6 2]; 
  end; 
 
  model data2; 
    parameter m := 1000;  n := 2000;; 
    i := 1..m;  j := 1..n; 
    a{i,j} := if(Rnd(0,1)<0.02 , Rnd(0,60)); 
    c{j}   := if(Rnd(0,1)<0.87 , Rnd(0,9)); 
    b{i}   := if(Rnd(0,1)<0.87 , Rnd(10,70000)); 
  end; 
end

The code contains the main model subModels and two sub-models data1 and data2. The main model does not contain data, only the declarations. One of the sub-model is called and ran from the main model, depending on the value of the parameter selectData. Note the double semicolon in the sub-model data2. m and n are two local parameters and after their declaration follows a double semicolon. This is important in this context because it terminates the declaration with an empty statement (a semicolon, because the next i := ... is not local, it is an assignment to the global set i. The data model could also be in another file. To simplify the presentation, the data – in fact two data-sets – are included within the main model.