1###################################################################################################
2#                                                                                                 #
3# This file is part of HPIPM.                                                                     #
4#                                                                                                 #
5# HPIPM -- High-Performance Interior Point Method.                                                #
6# Copyright (C) 2019 by Gianluca Frison.                                                          #
7# Developed at IMTEK (University of Freiburg) under the supervision of Moritz Diehl.              #
8# All rights reserved.                                                                            #
9#                                                                                                 #
10# The 2-Clause BSD License                                                                        #
11#                                                                                                 #
12# Redistribution and use in source and binary forms, with or without                              #
13# modification, are permitted provided that the following conditions are met:                     #
14#                                                                                                 #
15# 1. Redistributions of source code must retain the above copyright notice, this                  #
16#    list of conditions and the following disclaimer.                                             #
17# 2. Redistributions in binary form must reproduce the above copyright notice,                    #
18#    this list of conditions and the following disclaimer in the documentation                    #
19#    and/or other materials provided with the distribution.                                       #
20#                                                                                                 #
21# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND                 #
22# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED                   #
23# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE                          #
24# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR                 #
25# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES                  #
26# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;                    #
27# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND                     #
28# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT                      #
29# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS                   #
30# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                                    #
31#                                                                                                 #
32# Author: Gianluca Frison, gianluca.frison (at) imtek.uni-freiburg.de                             #
33#                                                                                                 #
34###################################################################################################
35from ctypes import *
36import ctypes.util
37import numpy as np
38#import faulthandler
39
40#faulthandler.enable()
41
42
43
44class hpipm_ocp_qp_solver_arg:
45	def __init__(self, dim):
46
47		# save dim internally
48		self.dim = dim
49
50		# load hpipm shared library
51		__hpipm   = CDLL('libhpipm.so')
52		self.__hpipm = __hpipm
53
54		# C qp struct
55		arg_struct_size = __hpipm.d_ocp_qp_ipm_arg_strsize()
56		arg_struct = cast(create_string_buffer(arg_struct_size), c_void_p)
57		self.arg_struct = arg_struct
58
59		# C qp internal memory
60		arg_mem_size = __hpipm.d_ocp_qp_ipm_arg_memsize(dim.dim_struct)
61		arg_mem = cast(create_string_buffer(arg_mem_size), c_void_p)
62		self.arg_mem = arg_mem
63
64		# create C qp
65		__hpipm.d_ocp_qp_ipm_arg_create(dim.dim_struct, arg_struct, arg_mem)
66
67		# initialize default arguments
68		__hpipm.d_ocp_qp_ipm_arg_set_default(1, arg_struct) # mode==SPEED
69
70
71	# TODO single setter !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
72
73	def set_mu0(self, mu0):
74		tmp_in = np.zeros((1,1))
75		tmp_in[0][0] = mu0
76		tmp = cast(tmp_in.ctypes.data, POINTER(c_double))
77		self.__hpipm.d_ocp_qp_ipm_arg_set_mu0.argtypes = [POINTER(c_double), c_void_p]
78		self.__hpipm.d_ocp_qp_ipm_arg_set_mu0(tmp, self.arg_struct)
79		return
80
81
82	def set_iter_max(self, iter_max):
83		tmp_in = np.zeros((1,1), dtype=int)
84		tmp_in[0][0] = iter_max
85		tmp = cast(tmp_in.ctypes.data, POINTER(c_int))
86		self.__hpipm.d_ocp_qp_ipm_arg_set_iter_max.argtypes = [POINTER(c_int), c_void_p]
87		self.__hpipm.d_ocp_qp_ipm_arg_set_iter_max(tmp, self.arg_struct)
88		return
89
90
91	def set_tol_stat(self, tol_stat):
92		tmp_in = np.zeros((1,1))
93		tmp_in[0][0] = tol_stat
94		tmp = cast(tmp_in.ctypes.data, POINTER(c_double))
95		self.__hpipm.d_ocp_qp_ipm_arg_set_tol_stat.argtypes = [POINTER(c_double), c_void_p]
96		self.__hpipm.d_ocp_qp_ipm_arg_set_tol_stat(tmp, self.arg_struct)
97		return
98
99
100	def set_tol_eq(self, tol_eq):
101		tmp_in = np.zeros((1,1))
102		tmp_in[0][0] = tol_eq
103		tmp = cast(tmp_in.ctypes.data, POINTER(c_double))
104		self.__hpipm.d_ocp_qp_ipm_arg_set_tol_eq.argtypes = [POINTER(c_double), c_void_p]
105		self.__hpipm.d_ocp_qp_ipm_arg_set_tol_eq(tmp, self.arg_struct)
106		return
107
108
109	def set_tol_ineq(self, tol_ineq):
110		tmp_in = np.zeros((1,1))
111		tmp_in[0][0] = tol_ineq
112		tmp = cast(tmp_in.ctypes.data, POINTER(c_double))
113		self.__hpipm.d_ocp_qp_ipm_arg_set_tol_ineq.argtypes = [POINTER(c_double), c_void_p]
114		self.__hpipm.d_ocp_qp_ipm_arg_set_tol_ineq(tmp, self.arg_struct)
115		return
116
117
118	def set_tol_comp(self, tol_comp):
119		tmp_in = np.zeros((1,1))
120		tmp_in[0][0] = tol_comp
121		tmp = cast(tmp_in.ctypes.data, POINTER(c_double))
122		self.__hpipm.d_ocp_qp_ipm_arg_set_tol_comp.argtypes = [POINTER(c_double), c_void_p]
123		self.__hpipm.d_ocp_qp_ipm_arg_set_tol_comp(tmp, self.arg_struct)
124		return
125
126	def set_reg_prim(self, reg_prim):
127		tmp_in = np.zeros((1,1))
128		tmp_in[0][0] = reg_prim
129		tmp = cast(tmp_in.ctypes.data, POINTER(c_double))
130		self.__hpipm.d_ocp_qp_ipm_arg_set_reg_prim.argtypes = [POINTER(c_double), c_void_p]
131		self.__hpipm.d_ocp_qp_ipm_arg_set_reg_prim(tmp, self.arg_struct)
132		return
133
134	def codegen(self, file_name, mode):
135		file_name_b = file_name.encode('utf-8')
136		mode_b = mode.encode('utf-8')
137		self.__hpipm.d_ocp_qp_ipm_arg_codegen(c_char_p(file_name_b), c_char_p(mode_b), self.dim.dim_struct, self.arg_struct)
138		return
139
140