MOSEK Optimization Suite

11.1.11 · active · verified Wed Apr 15

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

Install

Imports

Quickstart

This quickstart demonstrates a basic Linear Programming (LP) problem using MOSEK's Optimizer API. It initializes a MOSEK environment and task, defines variables and constraints, sets the objective, solves the problem, and retrieves the solution. A MOSEK license is required to run this code; ensure your license file is correctly configured, typically via the `MOSEKLM_LICENSE_FILE` environment variable or by placing `mosek.lic` in `~/.mosek/` (or `%USERPROFILE%\mosek\` on Windows).

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}")

view raw JSON →