| 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
|
|