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

from linopy import Model
import numpy as np

M = Model()
x = M.add_variables(lower=0.0,name="x")
y = M.add_variables(lower=0.0,upper=10.0,name="y")
z = M.add_variables(lower=0.0,name="z")
w = M.add_variables(lower=0.0,name="w")

M.add_constraints(3.0 * x +       y + 2.0 * z           == 30.0, name="C1")
M.add_constraints(2.0 * x +       y + 3.0 * z +       w >= 15.0, name="C2")
M.add_constraints(          2.0 * y           + 3.0 * w <= 25.0, name="C3")
M.add_constraints(x <= np.inf, name="C4")

M.add_objective( 3.0 * x + y + 5.0 * z + w, sense='max' )

print(M)

# Save the model to a LP file
M.to_file('lo1.lp')

M.solve(solver_name="mosek",
        io_api="direct",
        log_fn="lo1.log",                    # Mosek log file 
        basis_fn="lo1.bas",
        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)
