Mosel programming examples
The directory Programming contains a set of examples that
demonstrate how Mosel can be used for writing programs.
The files in this directory implement certain well-known algorithms
that, a priori, have nothing to do with the formulation and solution
of optimization problems.
|
| Largest common divisor: Recursive function calls; using 'readln'
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
Find the largest common divisor of two integer numbers
- recursive function calls
- if-then-elif-then-else statement
- alternative formulation using a 'while' loop
|
| File(s): |
lcdiv.mos |
|
|
| Smallest and largest value in a set: The 'if-then-elif-then' selection statement
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
Find the smallest and the largest value in a set of integers
- use of round, random, min, max
- if-then-elif-then statement
|
| File(s): |
minmax.mos |
|
|
| Perfect numbers: Using the 'forall' loop
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
A perfect number is defined as a number for which the
sum of all the divisors adds up to the number itself.
This examples calculates all perfect numbers up to a given upper bound.
- nested 'forall' loops
- use of 'mod'
|
| File(s): |
perfect.mos |
|
|
| Prime numbers using the Sieve of Eratosthenes: Nested loops: repeat-until, while, while-do
|
| |
|
| Type: |
Programming |
| Rating: |
3 (intermediate) |
| Description: |
This example calculat all prime numbers between 2 and a given upper bound shows the following features:
- working with sets
- nested loops: repeat-until, while, while-do
Algorithm 'Sieve of Eratosthenes': Starting with the smallest one, the algorithm
takes every element of a set of numbers SNumbers
(positive numbers between 2 and some upper limit that may be
specified when running the model), adds it to the set of prime
numbers SPrime and removes
the number and all its multiples from the set SNumbers.
|
| File(s): |
prime.mos |
|
|
| Read two tables and a set from a text data file: File access, using 'readln', 'fskipline'
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
Read two tables and a set from a free format data file (text file).
- opening and closing a file
- skipping comments and empty lines
- using 'read' and 'readln'
- using loops: forall, repeat-until, while
|
| File(s): |
readfile.mos |
| Data file(s): |
datafile.dat |
|
|
| Parsing a CSV-format data file: Text parsing functionality
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
Parsing a CSV-format data file line by line.
- opening and closing a file
- using 'trim', 'readtextline', 'nextfield'
- using 'parseint', 'parsereal', 'parsetext', 'parseextn'
- using loops: while
|
| File(s): |
readcsv.mos |
|
|
| Sort an array of numbers using Shell's method: Nested loops: repeat-until, forall-do, while-do
|
| |
|
| Type: |
Programming |
| Rating: |
3 (intermediate) |
| Description: |
Sort an array of numbers into numerical order using Shell's method.
- nested loops: repeat-until, forall-do, while-do
The idea of the Shell sort algorithm is to first sort, by straight insertion, small groups of numbers. Then several
small groups are combined and sorted. This step is repeated until the whole list of numbers is sorted.
|
| File(s): |
shsort.mos |
|
|
| Sort an array of numbers using Quicksort: Recursive subroutine calls, overloading of subroutines
|
| |
|
| Type: |
Programming |
| Rating: |
3 (intermediate) |
| Description: |
Sort an array of numbers into numerical order using Quicksort
- nested loops: repeat-until, forall, while
- recursive calls of procedures
- overloading of subroutines
The idea of the quick sort algorithm is to partition the array that is to be sorted into two parts. The `left' part
containing all values smaller than the partitioning value and the `right' part all the values that are larger than this value.
The partitioning is then applied to the two subarrays, and so on, until all values are sorted.
|
| File(s): |
qsort.mos |
|
|
| Defining a package to handle binary trees: Definition of subroutines, data structures list and record, type definition
|
| |
|
| Type: |
Programming |
| Rating: |
4 (medium-difficult) |
| Description: |
- definition of subroutines (function, procedure)
- data structures 'list' and 'record'
- type definitions
The package 'bintree' defines the new types 'tree'
(to represent a binary tree data structure) and
'btree_node' (a tree node, building blocks of trees
storing the actual data) for the Mosel language and also
a set of subroutines for working with trees, such as
inserting a
value into a tree, locating a value, getting the tree size,
printing out a tree, and converting a tree to a list.
The model file 'exbtree.mos' shows how to create a tree
and work with it using the functionality of this package.
To run this example, you first need to compile the package
(resulting in the BIM file 'bintree.bim'). You may then
execute the example in the same directory that contains
this BIM file.
|
| File(s): |
bintree.mos, exbtree.mos |
|
|
| Encapsulate binary file into a BIM: Submodel included in model source, datablock, deployment as executable
|
| |
|
| Type: |
Programming |
| Rating: |
3 (intermediate) |
| Description: |
The 'bpack' tool encapsulates a user-specified binary file into a BIM file using the 'datablock' functionality. The binary
file gets extracted into the working directory when running the resulting BIM file.
The model can be deployed as an executable (requires a C compiler):
mosel comp bpack.mos -o deploy.exe:bpack
bpack name_of_file_to_include
or run as a standard Mosel model, e.g. from the command line:
mosel bpack.mos -- name_of_file_to_include
The name of the binary file to be included needs to be specified via a
runtime parameter. A file myfile.extension will result in the
creation of a BIM file named myfile_extension.bim from which the original binary file is extracted when running the BIM, e.g via the command
mosel run myfile_extension.bim |
| File(s): |
bpack.mos |
|
|
| Generate an executable from a Mosel source file: deployment as executable, deployment via scripts
|
| |
|
| Type: |
Programming |
| Rating: |
3 (intermediate) |
| Description: |
The 'mosdeploy' tool generates an executable from a Mosel
source or bim file using the 'deploy' module.
The result is either a binary file (produced thanks to a C-compiler)
or a self-contained script embedding the bim file.
Usage (generation of a shell script, the type is determined depending on the detected operating system):
mosel mosdeploy.mos -- myfile.mos
or by first generating a shell script from this file:
mosel mosdeploy.mos -- mosdeploy.mos
mosdeploy myfile.mos
Alternatively, it is also possible to generate an executable from this file (requires a C-compiler):
mosel mosdeploy.mos -- -exe mosdeploy.mos
Other options include the specification of an output filename:
mosel mosdeploy.mos -- -o runfile myfile.mos
and a preselection of the output format (bat/cmd/sh) independent of the detected operating system:
mosel mosdeploy.mos -- -cmd myfile.mos
|
| File(s): |
mosdeploy.mos |
|
|
| Using the automatic translation system: Using xprnls, generating dictionary files
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
This example employs message markup for translation (such as _c, writeln_) and also demnonstrates the use of annotations relating
to the handling of messages.
The provided template file translate.pot (Portable Object Template) has been generated with the compilation option '-x':
mosel comp -x translate.mos -o translate.pot
For each supported language a separate PO (Portable Object) file needs to be generated using the xprnls command:
xprnls init -o translate.es.po translate.pot
The resulting PO file needs to be edited, completing its header by specifying the language and the encoding, and entering
the desired translations.
Run the command below to generate the dictionaries or message catalogues (*.mo Machine Object files) from the provided translations (*.po files):
xprnls mogen -d locale *.po
The output will be contained in the subdirectory 'locale'. |
| File(s): |
translate.mos |
| Data file(s): |
translate.de.po, translate.fr.po, translate.pot |
|
|
| Debugging models via the Remote Invocation Protocol: Remote model execution in debugging mode, implementation of a debugger
|
| |
|
| Type: |
Programming |
| Rating: |
5 (difficult) |
| Description: |
This model implements a debugger for Mosel models that are executing remotely, including
- an interactive command interpreter supporting navigation commands like end, cont(inue), next, step, continue up to, and display
commands such as model status, listing symbols, or help
- evaluation of expressions
- handling of breakpoints
- navigating in the stack
|
| File(s): |
mdbg.mos |
|
|
| Profiling and code coverage via the Remote Invocation Protocol: Remote model execution in profiling mode, decoding of profiling and coverage results
|
| |
|
| Type: |
Programming |
| Rating: |
4 (medium-difficult) |
| Description: |
- Start remote model execution in profiling mode
- Retrieve and decode profiling information (mprf.mos)
- Retrieve and decode code overage statistics (mcov.mos)
|
| File(s): |
mprf.mos, mcov.mos |
|
|
| Creating and saving XML documents: Simple access routines - adding nodes, recursive display of subtrees
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
- Creation of an XML document
- Adding element nodes
- Specifying node attributes
- Recursive display of the subtree under a given XML node
|
| File(s): |
xmltest.mos |
|
|
| Using XML-format files as databases: Retrieving XML nodes using xpath search expressions; modifying and formatting XML files
|
| |
|
| Type: |
Programming |
| Rating: |
3 (intermediate) |
| Description: |
- Using xpath search expressions to retrieve (lists of) nodes
- Editing an XML file: adding, copying, deleting nodes
- Configuring the appearance (e.g. spacing) of XML output files
The example 'booksearch.mos' works with a database file documenting the example models from the book 'Applications of optimization
with Xpress-MP'.
The example 'xmlrefexpl.mos' works with a personnel database, retrieving and modifying some of its entries.
|
| File(s): |
booksearch.mos, xmlrefexpl.mos |
| Data file(s): |
bookexamples.xml, refexample.xml |
|
|
| Parsing XML and JSON documents: Parsing documents with unknown structure
|
| |
|
| Type: |
Programming |
| Rating: |
4 (medium-difficult) |
| Description: |
The parsing routines 'xmlparse' and 'jsonparse' expect tables of (optional) functions implemented by the Mosel model in order
to return detailed information about each node of the document without any prior knowledge of the document structure.
Within Mosel, JSON documents are represented as XML trees.
|
| File(s): |
xparse.mos, jparse.mos |
| Data file(s): |
refexample.xml |
|
|
| Converting WCNF to Mosel format: Processing a text file into a different text format, generating Mosel code, deployment as executable
|
| |
|
| Type: |
Programming |
| Rating: |
3 (intermediate) |
| Description: |
The 'cnf2mos' tool transforms a SAT or MAXSAT problem instance in
CNF or WCNF format to a Mosel SAT/MAXSAT formulation. It also accepts compressed files (gzip format).
The tool can be deployed as an executable (requires a C compiler):
mosel comp cnf2mos.mos -o deploy.exe:cnf2mos
THe excutable needs to be invoked by specifying the name of the file
to be converted as a required argument. Alternatively the program
can be run as a standard Mosel model, e.g. from the command line:
mosel cnf2mos.mos F=name_of_file_to_convert
The resulting Mosel file can be executed in all usual ways, it requires
the Xpress Optimizer for solving the MAXSAT problem via MIP functionality.
|
| File(s): |
cnf2mos.mos |
| Data file(s): |
Rounded_BTWBNSL_asia_100_1_3.scores_TWBound_2.wcnf.gz |
|
|
| Defining a package to read JSON documents of unknown structure: Definition of subroutines, data structures of union types, type definition, JSON parsing
|
| |
|
| Type: |
Programming |
| Rating: |
4 (medium-difficult) |
| Description: |
The package 'json' defines the new types 'jobj',
'jarr', 'jval' and 'jnull' for representing JSON documents
of unknown structure and provides parsing functionality for
reading JSON documents into these structures. Like jparse.mos it relies on the callback-based 'jsonparse' functionality of mmxnl.
The model file 'readjson.mos' shows three different methods
of reading the JSON database file 'bookexamplesl.json' (documenting the
example models from the book 'Applications of optimization with Xpress-MP')
into Mosel structures, namely
(1) representation as 'xmldoc' using mmxml functionality for loading JSON documents,
(2) representation via specific user-defined record structures that are populated by a call to the mmhttp routine 'jsonread',
and
(3) using the types defined by the package 'json'.
To run this example, you first need to compile the package
(resulting in the BIM file 'json.bim'). You may then
execute the example in the same directory that contains
this BIM file.
|
| File(s): |
json.mos, readjson.mos |
| Data file(s): |
bookexamplesl.json |
|
|
|