Producing formatted output
In some of the previous examples the procedures write and writeln have been used for displaying data, solution values and some accompanying text. To produce better formatted output, these procedures can be combined with the formatting procedure strfmt. In its simplest form, strfmt's second argument indicates the (minimum) space reserved for writing the first argument and its placement within this space (negative values mean left justified printing, positive right justified). When writing a real, a third argument may be used to specify the maximum number of digits after the decimal point.
For example, if file fo.mos contains
model FO
parameters
r = 1.0 ! A real
i = 0 ! An integer
end-parameters
writeln("i is ", i)
writeln("i is ", strfmt(i,6) )
writeln("i is ", strfmt(i,-6) )
writeln("r is ", r)
writeln("r is ", strfmt(r,6) )
writeln("r is ",strfmt(r,10,4) )
end-model and we run Mosel thus:
mosel exec fo 'i=123, r=1.234567'
we get output
i is 123 i is 123 i is 123 r is 1.23457 r is 1.23457 r is 1.2346
The following example (model transport2.mos) prints out the solution of model `Transport' (Section A transport example) in table format. The reader may be reminded that the objective of this problem is to compute the product flows from a set of plants (PLANT) to a set of sales regions (REGION) so as to minimize the total cost. The solution needs to comply with the capacity limits of the plants (PLANTCAP) and satisfy the demand DEMAND of all regions.
procedure print_table
declarations
rsum: array(REGION) of integer ! Auxiliary data table for printing
psum,ct,iflow: integer ! Counters
end-declarations
! Print heading and the first line of the table
writeln("\nProduct Distribution\n", "="*20)
writeln(strfmt("Sales Region",48))
write(strfmt("",15), "| ")
forall(r in REGION) write(strfmt(r,9))
writeln(" |", strfmt("TOTAL",6), " Capacity")
writeln("-"*80)
! Print the solution values of the flow variables and
! calculate totals per region and per plant
ct:=0
forall(p in PLANT, ct as counter) do
if ct=2 then
write(" Plant ", strfmt(p,-8), "|")
else
write(" ", strfmt(p,-8), "|")
end-if
psum:=0
forall(r in REGION) do
iflow:=integer(getsol(flow(p,r)))
psum += iflow
rsum(r) += iflow
if iflow<>0 then
write(strfmt(iflow,9))
else
write(" -- ")
end-if
end-do
writeln(" |", strfmt(psum,6), strfmt(integer(PLANTCAP(p)),8))
end-do
! Print the column totals
writeln("-"*80)
write(strfmt(" TOTAL",-15), "|")
prsum:=0
forall(r in REGION) write(strfmt(rsum(r),9))
writeln(" |", strfmt(sum(r in REGION) rsum(r),6))
! Print demand of every region
write(strfmt(" Demand",-15), "|")
forall(r in REGION) write(strfmt(integer(DEMAND(r)),9))
! Print objective function value
writeln("\n\nTotal cost of distribution = ", strfmt(getobjval/1e6,0,3),
" million.")
end-procedure Notice the shorthand "-"*80 meaning that the string '-' is repeated 80 times. This functionality is provided by the module mmsystem.
With the data from Chapter More advanced modeling features the procedure print_table produces the following output:
Product Distribution
====================
Sales Region
| Scotland North SWest SEast Midlands | TOTAL Capacity
--------------------------------------------------------------------------------
Corby | -- 80 -- 920 2000 | 3000 3000
Plant Deeside | -- 1450 1000 -- 250 | 2700 2700
Glasgow | 2840 1270 -- -- -- | 4110 4500
Oxford | -- -- 1600 1900 500 | 4000 4000
--------------------------------------------------------------------------------
TOTAL | 2840 2800 2600 2820 2750 | 13810
Demand | 2840 2800 2600 2820 2750
Total cost of distribution = 81.018 million.
© 2001-2019 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.
