Lindo Systems

! Do ABC Analysis of a set of items.
Essentially, sort a set of items to identify a small fraction
of the items that constitute a major fraction of profit;
! Keywords: ABC Analysis, Sorting, Inventory, Pareto analysis;
SETS:
  ITEM: Q, V, VOQ, SORTORD;
ENDSETS
DATA:
 ! Target fraction for Class A and C.
  TARGAC controls where the boundaries between A, B, and C
  are chosen;
   TARGAC = 0.3;
! The items, sales quantity and profit;
 ITEM,  Q,     V = 
 101	 8.93   41.95  
 102	 5.38    2.80  
 103   5.02   26.20  
 104	 7.55    2.43  
 105	11.02   20.34  
 106	 4.27    0.28  
 107 	18.29    1.43  
 108	29.27    0.38  
 109   8.46    3.61  
 110   1.76    0.55 ;
ENDDATA
CALC:
  @SET( 'TERSEO',2);  ! Output level (0:verb, 1:terse, 2:only errors, 3:none);
 ! Compute value over quantity ratio;
@FOR( ITEM( i):
    VOQ(i) = V(i)/ Q(i);
    );
! There are various ways of computing an importance measure,
 however, the key idea is to sort from most important to least;
! Sort decreasing in VOQ;
   SORTORD = @SORT( -VOQ);

! Get total Q and V;
   TOTQ = @SUM( ITEM(I): Q(i));
   TOTV = @SUM( ITEM(i): V(i));
   CUMQ = 0; ! Cumulative Q;
   CUMV = 0;
! Display an ordered list, important items first, inserting
a boundary between Class A and B when the Cumulative volume
fraction is first > TARGAC, and 
a boundary between Class B and C when the Cumulative volume
fraction is first > 1 - TARGAC;
@WRITE(' ABC Analysis of a set of items.', @NEWLINE(1));
@WRITE('      Item       Q       Cum Q%       V      Cum V%', @NEWLINE(1));
@WRITE('  Class A:', @NEWLINE(1));
CLASS = 1;
 @FOR( ITEM(i):
   ii = SORTORD( i);
   @IFC( CLASS #EQ# 1 #AND# CUMV #GT# TARGAC:
     @WRITE('  Class B:', @NEWLINE(1));
     CLASS = 2;
    @ELSE
      @IFC( CLASS #EQ# 2 #AND# CUMV #GT# 1-TARGAC:
        @WRITE('  Class C:', @NEWLINE(1));
        CLASS = 3;
           );
       );
   CUMQ = CUMQ + Q(ii)/ TOTQ;
   CUMV = CUMV + V(ii)/ TOTV;
   @WRITE( @FORMAT( ITEM(ii), '10S'), @FORMAT( Q(ii), '10.2f'), @FORMAT( CUMQ, '10.2f'),
                                      @FORMAT( V(ii), '10.2f'), @FORMAT( CUMV, '10.2f'),@NEWLINE(1)); 
     );
ENDCALC