# CVXPY + MOSEK example
#
# Demonstrates how to set MOSEK parameters and dump task files from CVXPY
#
# Adapted from cvxpy.org

import cvxpy as cp
import numpy as np

# Problem data.
m = 30
n = 20
np.random.seed(1)
A = np.random.randn(m, n)
b = np.random.randn(m)

# Construct the problem.
x = cp.Variable(n)
objective = cp.Minimize(cp.sum_squares(A@x - b))
constraints = [0 <= x, x <= 1]
prob = cp.Problem(objective, constraints)

# The optimal objective value is returned by `prob.solve()`.
# mosek_params - dictionary with MOSEK parameters set as in the C API manual (optional)
# save_file    - where to save the data file (optional)
# verbose      - enable full log (optional)
result = prob.solve(solver = cp.MOSEK, 
                    mosek_params = {'MSK_DPAR_OPTIMIZER_MAX_TIME':  100.0,
                                    'MSK_IPAR_INTPNT_SOLVE_FORM':   'MSK_SOLVE_DUAL' },
                    save_file = 'dump.ptf',
                    verbose = True)

# Note! For linear problems an additional argument
#       bfs = True
#       indicates to fetch the basic solution instead of interior.

# The optimal value for x is stored in `x.value`.
print(x.value)

# The optimal Lagrange multiplier for a constraint is stored in
# `constraint.dual_value`.
print(constraints[0].dual_value)
