MOSEK Optimization Suite
MOSEK is a high-performance software package for solving large-scale mathematical optimization problems, encompassing linear, quadratic, and conic optimization, as well as mixed-integer programming (LP, QP, SOCP, SDP, MIP, etc.). It provides a Python API, including the lower-level Optimizer API and the object-oriented Fusion API. As a commercial product, it requires a valid license, with free academic and trial licenses available. It follows a major.minor.revision semantic versioning, where major versions may introduce incompatibilities.
Warnings
- breaking MOSEK is commercial software and requires a valid license to function. Even after `pip install mosek`, you must obtain and configure a license file (e.g., `mosek.lic`) by placing it in a default location or setting the `MOSEKLM_LICENSE_FILE` environment variable. Without a license, optimization calls will fail.
- breaking Upgrading to a new major version (e.g., from MOSEK 10 to 11) can introduce API incompatibilities and changes in default parameter behaviors. Users who have tuned solver parameters are recommended to re-evaluate their settings.
- deprecated In the Optimizer API, conic constraints restricted to `x ∈ K` for a variable `x` are deprecated and will be removed in a future major version.
- deprecated The MOSEK conda package is deprecated and may be dropped in a future release.
- gotcha Repeatedly creating and destroying `mosek.Env()` or `mosek.fusion.Model` objects for many small optimization problems can incur significant performance overhead due to the license checkout system.
- gotcha Using the Fusion API, assigning names to all variables, constraints, and other model elements can substantially increase problem setup time. This overhead should be avoided in time-critical applications.
- gotcha Passing NumPy arrays of incorrect integer types (e.g., `int64` where `int32` is expected, or vice versa) to the MOSEK API will trigger warnings and cause the module to make internal copies, potentially impacting performance.
Install
-
pip install mosek
Imports
- mosek
import mosek
- fusion
import mosek.fusion as msk
Quickstart
import mosek
import os
# Configure MOSEK license path. Replace 'path/to/your/mosek.lic' with actual path.
# For academic/trial licenses, it often defaults to ~/.mosek/mosek.lic
# It's recommended to set MOSEKLM_LICENSE_FILE environment variable for production.
# For quickstart, ensure the license file is accessible or MOSEKLM_LICENSE_FILE is set.
# os.environ['MOSEKLM_LICENSE_FILE'] = os.environ.get('MOSEKLM_LICENSE_FILE', 'path/to/your/mosek.lic')
# Define a stream printer to capture MOSEK output (optional)
def streamprinter(text):
# print(text.strip())
pass
try:
# Create a MOSEK environment
with mosek.Env() as env:
# Attach a stream printer to the environment
env.set_Stream(mosek.streamtype.log, streamprinter)
# Create a task for optimization
with env.Task() as task:
task.set_Stream(mosek.streamtype.log, streamprinter)
# Problem: Minimize x + y subject to x >= 0, y >= 0, x + y >= 1
# Append two variables
task.appendvars(2)
# Set variable bounds to be free initially
task.putvarboundlist([0, 1], [mosek.boundkey.fr, mosek.boundkey.fr], [0.0, 0.0], [0.0, 0.0])
# Append one constraint
task.appendcons(1)
# Set constraint bound (x + y >= 1)
task.putconbound(0, mosek.boundkey.lo, 1.0, 1.0)
# Set coefficients for constraint (x + y)
task.putaij(0, 0, 1.0) # Constraint 0, Variable 0, Coefficient 1.0
task.putaij(0, 1, 1.0) # Constraint 0, Variable 1, Coefficient 1.0
# Set objective coefficients (Minimize x + y)
task.putclist([0, 1], [1.0, 1.0])
# Set objective sense to minimize
task.putobjsense(mosek.objsense.minimize)
# Solve the problem
task.optimize()
task.solutionsummary(mosek.streamtype.log)
# Get and print the solution
prosta = task.getprosta(mosek.soltype.itr)
if prosta == mosek.prosta.prim_feas_obj_and_dual_feas_obj:
xx = [0.0] * 2
task.getxx(mosek.soltype.itr, xx)
print(f"Solution: x = {xx[0]}, y = {xx[1]}")
else:
print("Problem status: ", prosta)
except mosek.Error as e:
print(f"MOSEK Error: {e.code} {e.msg}")
except Exception as e:
print(f"General Error: {e}")