# Linopy + MOSEK example (https://github.com/PyPSA/linopy)
# 
# Demonstrates how to use MOSEK from LinoPy to solve a small quadratic
# optimization problem.

from linopy import Model

M = Model()

x = M.add_variables(lower=0.0, name="x")
y = M.add_variables(lower=0.0, name="y")
z = M.add_variables(lower=0.0, name="z")

M.add_constraints(x + y + z <= 1.0, name="C")

M.add_objective(-1.0 * y + 2 * x*x - 2 * x*z + 0.2 * y*y + 2 * z*z, sense='min')

print(M)

# Save the model to an LP file
M.to_file('qo1.lp')   

M.solve(solver_name="mosek",
        log_fn="qo1.log",                    # Mosek log file
        io_api='direct',
        MSK_DPAR_OPTIMIZER_MAX_TIME=100.0,   # Setting Mosek parameters using generic names
        MSK_IPAR_INTPNT_SOLVE_FORM='MSK_SOLVE_DUAL')

print("Solution:",M.solution)

