Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup of the python interface #159

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
237 changes: 112 additions & 125 deletions examples/python/example_dense_qcqp_getting_started.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,130 +37,117 @@
import numpy as np
import time
import sys
import os



# check that env.sh has been run
env_run = os.getenv('ENV_RUN')
if env_run!='true':
print('ERROR: env.sh has not been sourced! Before executing this example, run:')
print('source env.sh')
sys.exit(1)

travis_run = os.getenv('TRAVIS_RUN')
#travis_run = 'true'


# define flags
warm_start = 0; # set to 1 to warm-start the primal variable


# dim
nv = 2 # number of variables
nq = 1 # number of quadratic inequality constraints


dim = hpipm_dense_qcqp_dim()

dim.set('nv', nv)
dim.set('nq', nq)

# print to shell
# dim.print_C_struct()

H = np.array([[1,0],
[0,1]])
g = np.array([[0],[0]])
Hq = np.array([[2,0],[0,2]])
gq = np.array([[-2],[-2]])
uq = -1

# qp
qcqp = hpipm_dense_qcqp(dim)
# data
qcqp.set('H', H)
qcqp.set('g', g)
qcqp.set('Hq', Hq)
qcqp.set('gq', gq)
qcqp.set('uq', uq)

# print to shell
# qp.print_C_struct()

# qp sol
qcqp_sol = hpipm_dense_qcqp_sol(dim)
# set up solver arg
#mode = 'speed_abs'
mode = 'speed'
#mode = 'balance'
#mode = 'robust'
# create and set default arg based on mode
arg = hpipm_dense_qcqp_solver_arg(dim, mode)
# create and set default arg based on mode
arg.set('mu0', 1e4)
arg.set('iter_max', 30)
arg.set('tol_stat', 1e-5)
arg.set('tol_eq', 1e-5)
arg.set('tol_ineq', 1e-5)
arg.set('tol_comp', 1e-5)
arg.set('reg_prim', 1e-12)

# if warm_start=1, then the primal variable is initialized from qp_sol
arg.set('warm_start', warm_start)
qcqp_sol.set('v', np.array([0.2929, 0.2929]))

# set up solver
solver = hpipm_dense_qcqp_solver(dim, arg)
start_time = time.time()
solver.solve(qcqp, qcqp_sol)
end_time = time.time()
if(travis_run!='true'):
print('solve time {:e}'.format(end_time-start_time))

v = qcqp_sol.get('v')
pi = qcqp_sol.get('pi')
lam_lb = qcqp_sol.get('lam_lb')
lam_ub = qcqp_sol.get('lam_ub')
lam_lg = qcqp_sol.get('lam_lg')
lam_ug = qcqp_sol.get('lam_ug')
lam_uq = qcqp_sol.get('lam_uq')
print('v = {}'.format(v.flatten()))
print('pi = {}'.format(pi.flatten()))
print('lam_lb = {}'.format(lam_lb.flatten()))
print('lam_ub = {}'.format(lam_ub.flatten()))
print('lam_lg = {}'.format(lam_lg.flatten()))
print('lam_ug = {}'.format(lam_ug.flatten()))
print('lam_uq = {}'.format(lam_uq.flatten()))
#qcqp_sol.print_C_struct()

# get solver statistics
status = solver.get('status')
res_stat = solver.get('max_res_stat')
res_eq = solver.get('max_res_eq')
res_ineq = solver.get('max_res_ineq')
res_comp = solver.get('max_res_comp')
iters = solver.get('iter')
stat = solver.get('stat')
if(travis_run!='true'):
print('\nsolver statistics:\n')
print('ipm return = {0:1d}\n'.format(status))
print('ipm max res stat = {:e}\n'.format(res_stat))
print('ipm max res eq = {:e}\n'.format(res_eq))
print('ipm max res ineq = {:e}\n'.format(res_ineq))
print('ipm max res comp = {:e}\n'.format(res_comp))
print('ipm iter = {0:1d}\n'.format(iters))
print('stat =')
print('\titer\talpha_aff\tmu_aff\t\tsigma\t\talpha_prim\talpha_dual\tmu\t\tres_stat\tres_eq\t\tres_ineq\tres_comp')
for ii in range(iters+1):
print('\t{:d}\t{:e}\t{:e}\t{:e}\t{:e}\t{:e}\t{:e}\t{:e}\t{:e}\t{:e}\t{:e}'.format(ii, stat[ii][0], stat[ii][1], stat[ii][2], stat[ii][3], stat[ii][4], stat[ii][5], stat[ii][6], stat[ii][7], stat[ii][8], stat[ii][9]))
print('')

if status==0:
print('\nsuccess!\n')
else:
print('\nSolution failed, solver returned status {0:1d}\n'.format(status))

sys.exit(int(status))
from utils import check_env, is_travis_run


def main(travis_run: bool):

# define flags
warm_start = 0; # set to 1 to warm-start the primal variable

# dim
nv = 2 # number of variables
nq = 1 # number of quadratic inequality constraints

dim = hpipm_dense_qcqp_dim()
dim.set('nv', nv)
dim.set('nq', nq)

# print to shell
# dim.print_C_struct()

H = np.eye(nv)
g = np.array([[0],[0]])
Hq = np.array([[2,0],[0,2]])
gq = np.array([[-2],[-2]])
uq = -1

# qcqp
qcqp = hpipm_dense_qcqp(dim)
qcqp.set('H', H)
qcqp.set('g', g)
qcqp.set('Hq', Hq)
qcqp.set('gq', gq)
qcqp.set('uq', uq)

# print to shell
# qp.print_C_struct()

# qcqp sol
qcqp_sol = hpipm_dense_qcqp_sol(dim)

# set up solver arg
# mode = 'speed_abs'
mode = 'speed'
# mode = 'balance'
# mode = 'robust'

# create and set default arg based on mode
arg = hpipm_dense_qcqp_solver_arg(dim, mode)

arg.set('mu0', 1e4)
arg.set('iter_max', 30)
arg.set('tol_stat', 1e-5)
arg.set('tol_eq', 1e-5)
arg.set('tol_ineq', 1e-5)
arg.set('tol_comp', 1e-5)
arg.set('reg_prim', 1e-12)

# if warm_start = 1, then the primal variable is initialized from qcqp_sol
arg.set('warm_start', warm_start)
qcqp_sol.set('v', np.array([0.2929, 0.2929]))

# set up solver
solver = hpipm_dense_qcqp_solver(dim, arg)
start_time = time.time()
solver.solve(qcqp, qcqp_sol)
end_time = time.time()

v = qcqp_sol.get('v')
pi = qcqp_sol.get('pi')
lam_lb = qcqp_sol.get('lam_lb')
lam_ub = qcqp_sol.get('lam_ub')
lam_lg = qcqp_sol.get('lam_lg')
lam_ug = qcqp_sol.get('lam_ug')
lam_uq = qcqp_sol.get('lam_uq')

print('v = {}'.format(v.flatten()))
print('pi = {}'.format(pi.flatten()))
print('lam_lb = {}'.format(lam_lb.flatten()))
print('lam_ub = {}'.format(lam_ub.flatten()))
print('lam_lg = {}'.format(lam_lg.flatten()))
print('lam_ug = {}'.format(lam_ug.flatten()))
print('lam_uq = {}'.format(lam_uq.flatten()))

# qcqp_sol.print_C_struct()

# get solver statistics
status = solver.get('status')
# res_stat = solver.get('max_res_stat')
# res_eq = solver.get('max_res_eq')
# res_ineq = solver.get('max_res_ineq')
# res_comp = solver.get('max_res_comp')
# iters = solver.get('iter')

if not travis_run:
print('solve time {:e}'.format(end_time-start_time))
solver.print_stats()

if status == 0:
print('\nsuccess!\n')
else:
print('\nSolution failed, solver returned status {0:1d}\n'.format(status))

return status



if __name__ == '__main__':

check_env()
travis_run = is_travis_run()

status = main(travis_run)

sys.exit(int(status))

Loading