Coder Social home page Coder Social logo

Possible Bug in Setting Contraints about hpipm HOT 5 CLOSED

giaf avatar giaf commented on August 18, 2024
Possible Bug in Setting Contraints

from hpipm.

Comments (5)

giaf avatar giaf commented on August 18, 2024

First of all, thanks for appreciating HPIPM :)
Yes you are right with the bug report, thanks for spotting and finding the exact issue, I'll fix now!

from hpipm.

giaf avatar giaf commented on August 18, 2024

The issue you report should be fixed here db2fe4d

However, this should not affect the function d_ocp_qp_set() that you mention was failing for the field idxbx...
I guess there is still something else...

from hpipm.

reichardtj avatar reichardtj commented on August 18, 2024

Excellent! Thanks for the quick fix. It does not solve the problem with set('idxbx', ...) though as you already expected. I've reinstalled after your fix from both the latest master of Blasfeo and HPIPM, however, the problem persists.

Here is a minimal example showing the problem. From looking at the python interface, I doubt it's a python issue:

import os
# this is needed only on windows where the library searches the path instead of LD_LIBRARY_PATH
#os.environ['PATH'] = os.environ['PATH'] + 'D:\\hpipm\\lib;D:\\github_repos\\blasfeo\\lib'

# On Mac and presumably on Linux this is needed instead to find the library
from ctypes import CDLL
_ = CDLL('/opt/hpipm/lib/libhpipm.so')

import numpy as np

import hpipm_python as hpipm

T = 5

u_lower = np.array([-2, -1])
u_upper = np.array([ 2,  1])

A = np.array([
    [1, 0, 0.1, 0],
    [0, 1, 0, 0.1],
    [0, 0, 1, 0],
    [0, 0, 0, 1]
])
B = np.array([
    [0, 0],
    [0, 0],
    [0.1, 0],
    [0, 0.1]
])

nx = A.shape[0]
nu = B.shape[1]

dim = hpipm.hpipm_ocp_qp_dim(T)

dim.set('nx', nx, 0, T)   # number of states
dim.set('nu', nu, 0, T-1) # number of inputs

dim.set('nbx', 0, 0, T)
dim.set('nbx', nx, 0)     # initial condition

dim.set('nbu', nu, 0, T-1)  # for upper and lower bounds on u

qp = hpipm.hpipm_ocp_qp(dim)

qp_sol = hpipm.hpipm_ocp_qp_sol(dim)

mode = 'speed'
arg = hpipm.hpipm_ocp_qp_solver_arg(dim, mode)

# set up solver
solver = hpipm.hpipm_ocp_qp_solver(dim, arg)

#qp.set('idxbx', np.arange(nx).astype(int), 0) ## ====> this does not work
qp.set('Jbx', np.eye(nx), 0) ## ======> This works

qp.set('lbx', np.zeros(nx), 0)
qp.set('ubx', np.zeros(nx), 0)

qp.set('idxbu', np.arange(nu).astype(int), 0, T-1) #=====> this does work
#qp.set('Jbu', np.eye(nu), 0, T-1)  ## This works, too!
qp.set('lbu', u_lower, 0, T-1)
qp.set('ubu', u_upper, 0, T-1)

x = np.zeros((T + 1, nx))
u = np.clip(np.random.randn(T, nu) * 0.1, u_lower, u_upper)

x[0] = np.array([1, 1, 0, 0])
for i in range(T):
    x[i + 1] = A @ x[i] + B @ u[i]
    qp.set('A', A, i)
    qp.set('B', B, i)
    qp.set('b', np.zeros(nx), i)

    qp.set('Q', np.zeros((nx, nx)), i)
    qp.set('S', np.zeros((nu, nx)), i)
    qp.set('R', np.eye(nu) * 0.01, i)
    qp.set('r', np.zeros(nu), i)
    qp.set('q', np.zeros(nx), i)

    qp.set('lbu', u_lower - u[i], i)
    qp.set('ubu', u_upper - u[i], i)


qp.set('Q', np.eye(nx), T)
qp.set('S', np.zeros((nu, nx)), T)
qp.set('R', np.zeros((nu, nu)), T)
qp.set('r', np.zeros(nu), T)
qp.set('q', np.zeros(nx), T)


# solve qp    
solver.solve(qp, qp_sol)

On my Windows install, the solver crashes with an error message quoting some address it's not supposed to read from. On my Mac, the whole Jupyter Kernel dies without error message. The curious thing is that set("idxbu",...) does not not cause a problem, only set("idxbx",...).

As a first check, it would be good to see if other people can reproduce the problem.

from hpipm.

giaf avatar giaf commented on August 18, 2024

Hi, thanks for the example, it was very useful to find the bug!

The issue was that the integer arrays were converted into double arrays in the python interface, this should fix it 672150d
BTW it was wrong also for idxbu, since internally the C code was getting [0 0] instead of [0 1] in this case.

from hpipm.

reichardtj avatar reichardtj commented on August 18, 2024

Great. I've tried that and that all seems to work. Thanks for fixing this so quickly!

from hpipm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.