Initializing help system before first use

Basic embedding tasks


Type: Embedding
Rating: 2 (easy-medium)
Description:
  • ugcomp.py: Compiling a model into a BIM file (requires burglar2.mos, burglar.dat)
  • ugarray.py: Accessing modeling objects: sparse arrays (requires transportp.mos, transprt.dat)
  • ugcb.py: Retrieve model output via a callback (requires burglar2.mos, burglar.dat)
  • ugparam.py: Passing parameters to a Mosel model (requires prime.mos)
  • ugsol.py: Accessing modeling objects and solution information (requires burglar3p.mos, burglar.dat)
File(s): ugcomp.py, ugarray.py, ugcb.py, ugsol.py, ugparam.py, burglar2.mos, transportp.mos, prime.mos, burglar3p.mos
Data file(s): burglar.dat, transprt.dat


ugcomp.py
#######################################################
#  Mosel User Guide Example Problems
#  =================================
#
#  file ugcomp.py
#  ``````````````
#  Compiling and executing a model from Python.
#
#  (c) 2026 Fair Isaac Corporation
#      author: B. Vieira, 2026
########################################################

import moselpy as mp

print("Compiling 'burglar2'")
mp.compile_model("burglar2.mos", "burglar2.bim")

print("Loading 'burglar2'")
model = mp.load_model("burglar2.bim")

print("Executing 'burglar2'")
model.run()

print(f"'burglar2' returned: {model.exit_code}")

ugarray.py
#######################################################
#  Mosel User Guide Example Problems
#  =================================
#
#  file ugarray.py
#  ```````````````
#  Enumerating sparse arrays from Python.
#
#  (c) 2026 Fair Isaac Corporation
#      author: B. Vieira, 2026
########################################################

import moselpy as mp

mp.compile_model("transportp.mos", "transportp.bim")
model = mp.load_model("transportp.bim")
model.run()

solflow = model.find_identifier("solflow")  # dict with tuple keys

for (plant, region), val in solflow.items():
    print(f"flow({plant},{region}): {val}")

ugcb.py
#######################################################
#  Mosel User Guide Example Problems
#  =================================
#
#  file ugcb.py
#  ````````````
#  Redirecting the Mosel output in Python.
#
#  (c) 2026 Fair Isaac Corporation
#      author: B. Vieira, 2026
########################################################

import moselpy as mp
from io import StringIO

mp.compile_model("burglar2.mos", "burglar2.bim")
model = mp.load_model("burglar2.bim")

# Capture output to a Python buffer
buf = StringIO()
model.symbols["out"] = buf
model.set_default_stream(mp.StreamType.OUTPUT, "moselpy:out")
model.run()
del model.symbols["out"]

captured = buf.getvalue()
for line in captured.splitlines():
    print(f"Mosel: {line}")

ugsol.py
#######################################################
#  Mosel User Guide Example Problems
#  =================================
#
#  file ugsol.py
#  `````````````
#  Retrieving solution values from Python.
#
#  (c) 2026 Fair Isaac Corporation
#      author: B. Vieira, 2026
########################################################

import moselpy as mp
import sys

mp.compile_model("burglar3p.mos", "burglar3p.bim")
model = mp.load_model("burglar3p.bim")
model.run()

if not model.prob_status.is_solution_optimal:
    sys.exit(1)                          # Stop if no solution found

print(f"Objective value: {model.objective_value}")

soltake = model.find_identifier("soltake")  # dict: {index: solution_value, ...}
value = model.find_identifier("VALUE")      # dict: {index: data_value, ...}

for item in sorted(soltake):
    print(f"take({item}): {soltake[item]}\t (item value: {value[item]})")

ugparam.py
#######################################################
#  Mosel User Guide Example Problems
#  =================================
#
#  file ugparam.py
#  ```````````````
#  Passing parameters and accessing sets from Python.
#
#  (c) 2026 Fair Isaac Corporation
#      author: B. Vieira, 2026
########################################################

import moselpy as mp

LIM = 500

mp.compile_model("prime.mos", "prime.bim")
model = mp.load_model("prime.bim")

model.run(exec_params={"LIMIT": LIM})

print(f"'prime' returned: {model.exit_code}")

s_prime = model.find_identifier("SPrime")   # Returns a Python set

if s_prime:
    print(f"Prime numbers from 2 to {LIM}")
    print(" " + ", ".join(str(p) for p in sorted(s_prime)))

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

   file burglar2.mos
   `````````````````
   Use of index sets.
   
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, 2001, rev. 2006
*******************************************************!)

model Burglar2
 uses "mmxprs"
 
 declarations
  WTMAX = 102                    ! Maximum weight allowed
  ITEMS = {"camera", "necklace", "vase", "picture", "tv", "video", 
           "chest", "brick"}     ! Index set for items
  
  VALUE: array(ITEMS) of real    ! Value of items
  WEIGHT: array(ITEMS) of real   ! Weight of items
  
  take: array(ITEMS) of mpvar    ! 1 if we take item i; 0 otherwise
 end-declarations

 initializations from 'burglar.dat'
  VALUE  WEIGHT
 end-initializations

! Objective: maximize total value
 MaxVal:= sum(i in ITEMS) VALUE(i)*take(i) 

! Weight restriction
 sum(i in ITEMS) WEIGHT(i)*take(i) <= WTMAX

! All variables are 0/1
 forall(i in ITEMS) take(i) is_binary  

 maximize(MaxVal)                 ! Solve the MIP-problem

! Print out the solution
 writeln("Solution:\n Objective: ", getobjval)
 forall(i in ITEMS)  writeln(" take(", i, "): ", getsol(take(i)))
end-model

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

   file transportp.mos
   ````````````````````
   Using dynamic arrays, with solution values stored
   in a real array for retrieval from Python.

   (c) 2026 Fair Isaac Corporation
       author: S. Heipcke, 2001, rev. B. Vieira 2026
*******************************************************!)

model Transportp
 uses "mmxprs"

 public 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
  solflow: dynamic array(PLANT,REGION) of real  ! Solution values
 end-declarations

 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

! Store solution values for retrieval
 forall(p in PLANT, r in REGION | exists(flow(p,r)))
  solflow(p,r):= getsol(flow(p,r))

end-model

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

   file prime.mos 
   ``````````````
   Working with sets.
 
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, 2001, rev. June 2018
*******************************************************!)

model Prime 

 parameters
  LIMIT=100                     ! Search for prime numbers in 2..LIMIT
 end-parameters

 public declarations
  SNumbers: set of integer      ! Set of numbers to be checked
  SPrime: set of integer        ! Set of prime numbers
 end-declarations

 SNumbers:={2..LIMIT} 
 
 writeln("Prime numbers between 2 and ", LIMIT, ":")

 n:=2
 repeat
   while (not(n in SNumbers)) n+=1
   SPrime += {n}                ! n is a prime number
   i:=n
   while (i<=LIMIT) do          ! Remove n and all its multiples
     SNumbers-= {i}
     i+=n
   end-do
 until SNumbers={}    
 
 writeln(SPrime)
 writeln(" (", getsize(SPrime), " prime numbers.)")
 
end-model

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

   file burglar3p.mos
   ``````````````````
   Same as burglar3.mos but with solution values
   stored in a real array for retrieval from Python.

   (c) 2026 Fair Isaac Corporation
       author: S. Heipcke, 2001, rev. B. Vieira 2026
*******************************************************!)

model Burglar3p
 uses "mmxprs"

 public declarations
  WTMAX = 102                    ! Maximum weight allowed
  ITEMS = {"camera", "necklace", "vase", "picture", "tv", "video",
           "chest", "brick"}     ! Index set for items

  VALUE: array(ITEMS) of real    ! Value of items
  WEIGHT: array(ITEMS) of real   ! Weight of items

  take: array(ITEMS) of mpvar    ! 1 if we take item i; 0 otherwise
  soltake: array(ITEMS) of real  ! Solution values
 end-declarations

 initializations from 'burglar.dat'
  VALUE  WEIGHT
 end-initializations

! Objective: maximize total value
 MaxVal:= sum(i in ITEMS) VALUE(i)*take(i)

! Weight restriction
 sum(i in ITEMS) WEIGHT(i)*take(i) <= WTMAX

! All variables are 0/1
 forall(i in ITEMS) take(i) is_binary

 maximize(MaxVal)                  ! Solve the MIP-problem

! Store solution values for retrieval
 forall(i in ITEMS) soltake(i):= getsol(take(i))

end-model

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