Initializing help system before first use

Output formatting


Type: Programming
Rating: 2 (easy-medium)
Description: The printing format for real numbers may be changed individually with the functions strfmt, textfmt, formattext or by resetting the parameter REALFMT. The function strfmt may also be applied to strings, for instance to change the alignment of the text.
File(s): fo.mos, numformat.mos, transport2.mos, transport2t.mos
Data file(s): transprt.dat


fo.mos
(!******************************************************
   Mosel User Guide Example Problems
   ================================= 

   file fo.mos 
   ``````````` 
   Formatted output printing.
 
   (c) 2008 Fair Isaac Corporation
       author: Bob Daniel, 2002
*******************************************************!)

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

numformat.mos
(!******************************************************
   Mosel User Guide Example Problems
   ================================= 

   file numformat.mos 
   `````````````````` 
   Formatted output printing.
 
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, 2005, rev. Mar 2022
*******************************************************!)

model "Formatting numbers"
 parameters
  a = 123456789000.0
  b = 123456789008.9
  c = 12.0000000045
  d = 12.0
 end-parameters

 setparam("TXTZTOL", false)      ! Disable use of 'zerotol' for display

 writeln("Default:", a, "  ", b, "  ", c, "  ", d)
 
 setparam("REALFMT", "%1.2f")
 writeln("%1.2f:  ", a, "  ", b, "  ", c, "  ", d)

! Exact (conservative) representation of floating point numbers 
 setparam("REALFMT", "%.17g")
 writeln("%.17g:  ", a, "  ", b, "  ", c, "  ", d)

 setparam("REALFMT", "%y")
 writeln("%y:     ", a, "  ", b, "  ", c, "  ", d)

 setparam("REALFMT", "%j")
 writeln("%j:     ", a, "  ", b, "  ", c, "  ", d)
end-model

transport2.mos
(!******************************************************
   Mosel User Guide Example Problems
   ================================= 

   file transport2.mos 
   ``````````````````` 
   Formatted output printing.
 
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, 2001, rev. Dec. 2020
*******************************************************!)

model "Transport 2"
 uses "mmxprs", "mmsystem"

 declarations
  REGION: set of string                 ! Set of customer regions
  PLANT: set of string                  ! Set of plants

  DEMAND: array(REGION) of real         ! Demand at regions
  PLANTCAP: array(PLANT) of real        ! Production capacity at plants
  PLANTCOST: array(PLANT) of real       ! Unit production cost at plants
  TRANSCAP: dynamic array(PLANT,REGION) of real
                                        ! Capacity on each route plant->region
  DISTANCE: dynamic array(PLANT,REGION) of real
                                        ! Distance of each route plant->region
  FUELCOST: real                        ! Fuel cost per unit distance

  flow: dynamic array(PLANT,REGION) of mpvar    ! Flow on each route
 end-declarations

!***********************************************************************
 procedure printtable
  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:=round(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(round(PLANTCAP(p)),8))
  end-do

         ! Print the column totals
  writeln("-"*80)
  write(strfmt(" TOTAL",-15), "|") 
  forall(r in REGION) write(strfmt(rsum(r),9))
  writeln("  |", strfmt(sum(r in REGION) rsum(r),6),
          strfmt(sum(p in PLANT) PLANTCAP(p),8)) 

         ! Print demand of every region
  write(strfmt(" Demand",-15), "|") 
  forall(r in REGION) write(strfmt(round(DEMAND(r)),9))

         ! Print objective function value
  writeln("\n\nTotal cost of distribution = ", strfmt(getobjval/1e6,0,3), 
          " million.")

 end-procedure
!***********************************************************************
 
 initializations from 'transprt.dat'
  DEMAND
  [PLANTCAP,PLANTCOST] as 'PLANTDATA'
  [DISTANCE,TRANSCAP] as 'ROUTES'
  FUELCOST
 end-initializations
 
! Create the flow variables that exist
 forall(p in PLANT, r in REGION | exists(TRANSCAP(p,r)) ) create(flow(p,r))
 
! Objective: minimize total cost
 MinCost:= sum(p in PLANT, r in REGION | exists(flow(p,r))) 
            (FUELCOST * DISTANCE(p,r) + PLANTCOST(p)) * flow(p,r)
 
! Limits on plant capacity
 forall(p in PLANT) sum(r in REGION) flow(p,r) <= PLANTCAP(p)

! Satisfy all demands
 forall(r in REGION) sum(p in PLANT) flow(p,r) = DEMAND(r)
 
! Bounds on flows
 forall(p in PLANT, r in REGION | exists(flow(p,r))) 
  flow(p,r) <= TRANSCAP(p,r)
 
 minimize(MinCost)                       ! Solve the problem

 printtable                              ! Solution printout
 
end-model

transport2t.mos
(!******************************************************
   Mosel User Guide Example Problems
   ================================= 

   file transport2t.mos 
   ```````````````````` 
   Formatted output printing.
   - Using text handling functionality -
 
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, 2001, rev. Mar. 2022
*******************************************************!)

model "Transport 2t"
 uses "mmxprs", "mmsystem"

 declarations
  REGION: set of string                 ! Set of customer regions
  PLANT: set of string                  ! Set of plants

  DEMAND: array(REGION) of real         ! Demand at regions
  PLANTCAP: array(PLANT) of real        ! Production capacity at plants
  PLANTCOST: array(PLANT) of real       ! Unit production cost at plants
  TRANSCAP: dynamic array(PLANT,REGION) of real
                                        ! Capacity on each route plant->region
  DISTANCE: dynamic array(PLANT,REGION) of real
                                        ! Distance of each route plant->region
  FUELCOST: real                        ! Fuel cost per unit distance

  flow: dynamic array(PLANT,REGION) of mpvar    ! Flow on each route
 end-declarations

!***********************************************************************
 procedure printtable
  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(textfmt("Sales Region",48))
  write(textfmt("",15), "| ")
  forall(r in REGION) write(textfmt(r,9))
  writeln(formattext(" |%6s Capacity", "TOTAL")) 
  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(formattext(" Plant %-8s|", p))
    else
      write(formattext("       %-8s|", p))
    end-if
    psum:=0
    forall(r in REGION) do
      iflow:=round(getsol(flow(p,r)))
      psum += iflow
      rsum(r) += iflow
      if iflow<>0 then 
        write(textfmt(iflow,9))
      else
        write("      -- ")
      end-if
    end-do
    writeln(formattext("  |%6g%8g", psum, round(PLANTCAP(p))))
  end-do

         ! Print the column totals
  writeln("-"*80)
  write(formattext("%-15s|"," TOTAL")) 
  forall(r in REGION) write(textfmt(rsum(r),9))
  writeln(formattext("  |%6g%8g", sum(r in REGION) rsum(r), sum(p in PLANT) PLANTCAP(p))) 

         ! Print demand of every region
  write(formattext("%-15s|", " Demand"))
  forall(r in REGION) write(textfmt(round(DEMAND(r)),9))

         ! Print objective function value
  writeln(formattext("\n\nTotal cost of distribution = %.3g million.", getobjval/1e6))

 end-procedure
!***********************************************************************
 
 initializations from 'transprt.dat'
  DEMAND
  [PLANTCAP,PLANTCOST] as 'PLANTDATA'
  [DISTANCE,TRANSCAP] as 'ROUTES'
  FUELCOST
 end-initializations
 
! Create the flow variables that exist
 forall(p in PLANT, r in REGION | exists(TRANSCAP(p,r)) ) create(flow(p,r))
 
! Objective: minimize total cost
 MinCost:= sum(p in PLANT, r in REGION | exists(flow(p,r))) 
            (FUELCOST * DISTANCE(p,r) + PLANTCOST(p)) * flow(p,r)
 
! Limits on plant capacity
 forall(p in PLANT) sum(r in REGION) flow(p,r) <= PLANTCAP(p)

! Satisfy all demands
 forall(r in REGION) sum(p in PLANT) flow(p,r) = DEMAND(r)
 
! Bounds on flows
 forall(p in PLANT, r in REGION | exists(flow(p,r))) 
  flow(p,r) <= TRANSCAP(p,r)
 
 minimize(MinCost)                       ! Solve the problem

 printtable                              ! Solution printout
 
end-model

© 2001-2025 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.