Skip to main content

mosox

An LP matrix generator for GMPL (MathProg) and (by extension) AMPL.

Can be used in two ways:

  • Compile an MPS file for solving elsewhere
  • Solve directly with the HiGHS integration, no other binary needed

Currently a work-in-progress.

It works for a subset of GMPL (specifically the subset required to run Osemosys). See Known limitations section below.

There are a number of examples of varying complexity in the examples/ directory.

Developed by and for Climate Compatible Growth.

Quickstart

Installation

Using Cargo

cargo install mosox

Using Homebrew (macOS)

brew tap carderne/mosox-tap
brew install mosox

Pre-built binaries (macOS, Windows, Linux)

Binaries are built for a small set of systems and architectures. Available to download (compressed) from the Releases page. Please choose the appropriate archive.

Usage overview

Usage overview:

mosox help

MathProg Translation Kit

Usage: mosox <COMMAND>

Commands:
  compile    Load and output to MPS
  solve      Solve with HiGHS
  normalize  Normalize an MPS file for diffing
  compare    Compare two normalized MPS files with epsilon tolerance
  help       Print this message or the help of the given subcommand(s)

Options:
  -h, --help     Print help
  -V, --version  Print version

Compile MPS

Compile MPS for one of the examples in this repo. Output will be written to to the provided file:

mosox compile examples/basic/model.mod -o output.mps

Or an example with a separate data file, piping MPS to a file:

mosox compile examples/sets/model.mod examples/sets/data.dat -o output.mps

Solve a model

Results will be saved to the output file:

mosox solve examples/basic/model.mod -o output.txt

# Loading model from model.mod
# Generating matrix
# Matrix compiled in 666.625µs
# Matrix: 3 rows, 2 cols, 6 nonzero
# Solving matrix with HiGHS
# Solved in 1.939083ms
# Objective value: 2200
# Results output to f.txt

Python

Data can be passed from Python as pandas DataFrames (or Series, or pyarrow Tables) instead of a .dat file. Columns are read by position: for a param, the index columns then the value; for a set, the index columns (if indexed) then the members.

import pandas as pd
import mosox

model = mosox.Model.from_file("examples/2d_params/model.mod")
solution = model.solve({"supply": pd.DataFrame({"plant": ["P1", "P2"], "value": [100, 150]}), ...})
solution.objective
solution["ship"]  # DataFrame with columns p, w, value, marginal

Development

Please install cargo-make:

cargo install cargo-make

The most useful dev commands are listed in Makefile.toml.

You can view available commands by running cargo make.

Formatting, linting

cargo make fmt
cargo make lint

Testing

In addition to other tests, this will run mosox against all the examples under examples/ and confirm that the output is identical to the existing MPS files.

cargo make test

This will additionally run a regression test against examples/osemosys_large if present.

cargo make testlarge

Python bindings

Requires uv. uv run builds the extension (unoptimised) as needed.

cargo make py  # ruff format, ruff check, ty check, pytest

Benchmarks

Run performance benchmarks across all examples (except osemosys_large):

cargo make bench

Include osemosys_large (requires the example to be present):

cargo make benchlarge

Run with glpsol comparison (requires glpsol on PATH):

cargo make benchglpsol

Performance vs glpsol (median, release build, Macbook Air M2)

Note that some of the performance advantage over glpsol may be caused by the limitations enumerated below.

Example rows/cols/nonzero N glpsol mosox Speedup
osemosys_small 6k/7k/15k 50 68ms 17ms 4.0x
osemosys_atlantis 180k/230k/510k 10 2.4s 373ms 6.5x
osemosys_large 1M/5M/12M 4 130s 20s 6.5x

Memory usage

The matrix generator uses very roughly 2,000 bytes per non-zero. This is a significant overhead over the 12 bytes bytes per non-zero that would be needed in a format like CSC. However, as the table below shows, the matrix generator will rarely (if ever) use more memory than the solver.

Example Mosox matrix Glpsol matrix HiGHS solver Glpsol solver
osemosys_small 13 MB 12 MB 24 MB 16 MB
osemosys_atlantis 244 MB 287 MB 386 MB 416 MB
osemosys_large 5 GB 5.6 GB 6.5 GB ?

Known limitations

This list of limitations is made with reference to the GNU MathProg Language Reference which can be viewed here or downloaded from the original here.

It is intended that all of these will ultimately be supported, and most of them are "trivial" to add.

Functions

  • The following functions: abs, atan, ceil, cos, exp, floor, gmtime, length, log, prod, round, sin, sqrt, str2time, trunc, Irand224, Uniform, Normal.

Expressions

  • These arithmetic operators: less, div, mod
  • These symbolic operators: & (string concatenation)
  • These set expressions: conditional set expressions
  • String ordering comparisons (<, > etc. between symbolic values)

Sets

  • within and dimen are not enforced on computed sets that fail to evaluate at load (eg sets over tuple domains)

Parameters

  • Type specifiers, relational conditions and superset (in) expressions are only enforced on explicit data and constant defaults, not on computed params (:= expressions) or expression defaults
  • Plain-format data for multi-dimensional params without a slice (eg param p := a x 1 b y 2;) is not supported (it is caught as out-of-domain data)

Variables

  • Bounds specified as expressions (currently only constant accepted)

Constraints

  • Multiple expressions (comma-separated)

Other statements

Support for these statements is not planned (results should instead be parsed from the solver output): display, printf, for.

Metadata

Release files for mosox 0.7.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mosox 0.7.0
File Size Uploaded Provenance Actions
mosox-0.7.0.tar.gz 537.1 kB Without provenance Details for mosox-0.7.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mosox 0.7.0
File Interpreter ABI Platform Actions
mosox-0.7.0-cp311-abi3-macosx_11_0_arm64.whl CPython 3.11 abi3 macOS 11.0+ ARM64 Details for mosox-0.7.0-cp311-abi3-macosx_11_0_arm64.whl

Total release size: 10.5 MB

Release files / mosox-0.7.0.tar.gz

Download URL mosox-0.7.0.tar.gz
Size 537.1 kB
Tags Source
SHA-256 checksum
How to use checksums
f648b43b9fdeb18afaacf5be648c7f2dfdcf71511f8e43cac91261abb912e3b8
BLAKE2b-256 checksum
How to use checksums
0ae7c3c2b89c88ef9010ee8979a01bb540a91cad10ea99ac378a58549e632060
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / mosox-0.7.0-cp311-abi3-macosx_11_0_arm64.whl

Download URL mosox-0.7.0-cp311-abi3-macosx_11_0_arm64.whl
Size 9.9 MB
Tags CPython 3.11 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
094617699c9f020637c93c4c7e065184f8990b784e5ce005b038c9d01ac4d71d
BLAKE2b-256 checksum
How to use checksums
8706b13e6369c2bc79b727307de0f502fbb76e2feb247de5b6c4587eecefc76d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for mosox 0.7.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page