#######################################################
# Mosel User Guide Example Problems
# =================================
#
# file ugiodense.py
# `````````````````
# Exchanging dense data between Python and a model.
#
# (c) 2026 Fair Isaac Corporation
# author: B. Vieira, 2026
########################################################
import moselpy as mp
vdata = [15, 100, 90, 60, 40, 15, 10, 1] # VALUE
wdata = [ 2, 20, 20, 30, 40, 30, 60, 10] # WEIGHT
mp.compile_model("burglar8p.mos", "burglar8p.bim")
model = mp.load_model("burglar8p.bim")
output = model.run(
exec_params={"DATASOURCE": "moselpy:"},
input_data={
"VALUE": {i+1: v for i, v in enumerate(vdata)},
"WEIGHT": {i+1: w for i, w in enumerate(wdata)},
})
print(f"Objective value: {model.objective_value}")
for i in range(8):
print(f" take({i+1}): {output['soltake'][i+1]}")
|
#######################################################
# Mosel User Guide Example Problems
# =================================
#
# file ugiosparse.py
# ``````````````````
# Exchanging sparse data between Python and a model.
#
# (c) 2026 Fair Isaac Corporation
# author: B. Vieira, 2026
########################################################
import moselpy as mp
mp.compile_model("burglar9p.mos", "burglar9p.bim")
model = mp.load_model("burglar9p.bim")
output = model.run(
exec_params={"DATASOURCE": "moselpy:"},
input_data={
"VALUE": {"camera": 15, "necklace": 100, "vase": 90,
"picture": 60, "tv": 40, "video": 15,
"chest": 10, "brick": 1},
"WEIGHT": {"camera": 2, "necklace": 20, "vase": 20,
"picture": 30, "tv": 40, "video": 30,
"chest": 60, "brick": 10},
})
print(f"Objective value: {model.objective_value}")
for item, val in output["soltake"].items():
print(f" take({item}): {val}")
|
(!******************************************************
Mosel User Guide Example Problems
=================================
file burglar8p.mos
``````````````````
Model as in burglar.mos, with data input/output
from/to calling application via a parameterized
data source.
(c) 2026 Fair Isaac Corporation
author: S. Heipcke, Mar. 2006, rev. B. Vieira 2026
*******************************************************!)
model Burglar8p
uses "mmxprs"
parameters
DATASOURCE = 'burglard.dat' ! Data source (overridden to 'moselpy:' from Python)
WTMAX = 102 ! Maximum weight allowed
end-parameters
declarations
ITEMS = 1..8 ! Index range 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 DATASOURCE
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
! Output solution to calling application
forall(i in ITEMS) soltake(i):= getsol(take(i))
initializations to DATASOURCE
soltake
end-initializations
end-model
|
(!******************************************************
Mosel User Guide Example Problems
=================================
file burglar9p.mos
``````````````````
Same as burglar2.mos, with input from/output to
calling application via a parameterized data source.
(c) 2026 Fair Isaac Corporation
author: S. Heipcke, Mar. 2006, rev. B. Vieira 2026
*******************************************************!)
model Burglar9p
uses "mmxprs"
parameters
DATASOURCE = 'burglar.dat' ! Data source (overridden to 'moselpy:' from Python)
WTMAX = 102 ! Maximum weight allowed
end-parameters
declarations
ITEMS: set of string ! Index set for items
VALUE: array(ITEMS) of real ! Value of items
WEIGHT: array(ITEMS) of real ! Weight of items
soltake: array(ITEMS) of real ! Solution values
end-declarations
initializations from DATASOURCE
VALUE WEIGHT
end-initializations
declarations
take: array(ITEMS) of mpvar ! 1 if we take item i; 0 otherwise
end-declarations
! 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
! Output solution to calling application
forall(i in ITEMS) soltake(i):= getsol(take(i))
initializations to DATASOURCE
soltake
end-initializations
end-model
|