# cqp\_basic/app.cpp

/*******************************************************************************
    Copyright (c) 2023 Qualcomm Technologies, Inc.
    All rights reserved.
    
    Redistribution and use in source and binary forms, with or without
    modification, are permitted (subject to the limitations in the disclaimer
    below) provided that the following conditions are met:
    
    * Redistributions of source code must retain the above copyright notice, this
    list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.
    * Neither the name of Qualcomm Technologies, Inc. nor the names of its
    contributors may be used to endorse or promote products derived from this
    software without specific prior written permission.
    
    NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
    THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
    CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
    NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
    PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
    OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
    OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
    ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    
    @brief
    Program to run the FastADAS Convex Quadratic Programming solver with the
    following values:
    
        min f(x) = a^T x + 1/2 x^T G x
    
        G =  |  1 -1  1 |   a^T = [ 2 -3 1 ]
             | -1  2 -2 |
             |  1 -2  4 |
    
        s.t.
            x_1  +  x_2  +  x_3  = 0.5
    
            lb = 0
            ub = 1
    
        s.t.
            x_1 +  0   +  0    >=  0
            0   +  x_2 +  0    >=  0
            0   +  0   +  x_3  >=  0
    
            x_1 +  0   +  0    <=  1
            0   +  x_2 +  0    <=  1
            0   +  0   +  x_3  <=  1
    
            C^T x
    
        FADAS uses the form:
    
            C x   <= b
            C_eq x = b_eq
    
    *******************************************************************************/
    
    #include &lt;math.h&gt;
    #include &lt;stdio.h&gt;
    #include &lt;stdlib.h&gt;
    #include &lt;string.h&gt;
    #include &lt;iostream&gt;
    
    #include &lt;fadas.h&gt;
    #include "../util/util.h"
    
    using namespace std;

    int main( int argc, char** argv )
    {
        if( FadasInit( nullptr ) != FADAS_ERROR_NONE )
            UTIL_ERROR( "FadasInit failed\n" );
    
        FadasHandle_t hCQP = nullptr;
    
        FadasError_e status = FADAS_ERROR_NONE;
        bool ans = true;
        const uint32_t balign = 128;
    
        uint32_t n = 3;
        uint32_t m_eq = 1;  // num. eq. constraints
        uint32_t m = 1;     // num. ineq. constraints + eq. constraints
    
        float64_t G[3][3] = { {  1, -1,  1 },
                              { -1,  2, -2 },
                              {  1, -2,  4 } };
        float64_t a[3] = { 2, -3, 1 };
        float64_t C[3] = { 1, 1, 1 };
        float64_t c[1] = { 0.5 };
        float64_t xl[3] = { 0, 0, 0 };
        float64_t xu[3] = { 1, 1, 1 };
        float64_t xv[3] = { 0 };
        float64_t obj[1] = { 0 };
        uint32_t iter[2] = { 0 };
    
        status = FadasRegBuf( FADAS_BUF_TYPE_IN, G, sizeof( G ) );
        status = FadasRegBuf( FADAS_BUF_TYPE_IN, a, sizeof( a ) );
        status = FadasRegBuf( FADAS_BUF_TYPE_IN, C, sizeof( C ) );
        status = FadasRegBuf( FADAS_BUF_TYPE_IN, c, sizeof( c ) );
        status = FadasRegBuf( FADAS_BUF_TYPE_IN, xl, sizeof( xl ) );
        status = FadasRegBuf( FADAS_BUF_TYPE_IN, xu, sizeof( xu ) );
        status = FadasRegBuf( FADAS_BUF_TYPE_OUT, xv, sizeof( xv ) );
        status = FadasRegBuf( FADAS_BUF_TYPE_OUT, obj, sizeof( obj ) );
        status = FadasRegBuf( FADAS_BUF_TYPE_OUT, iter, sizeof( iter ) );
    
        status = FadasCQP_Create( n, m, m_eq, 0, nullptr, &hCQP, true );
    
        FadasCQPSolverResult_t qpResults{};
        qpResults.optimization_result = xv;
        qpResults.obj_result = obj;
        qpResults.required_iterations = iter;
        qpResults.return_status = FADAS_CQP_INIT;
    
        status = FadasCQP_Run( hCQP, &G[0][0], a, C, c, xl, xu, 100, 1e-15, 1e-30, &qpResults );
    
        cout << "  expected x^T: [ 0  0.5  0 ]\n";
        cout << "calculated x^T: [ " << xv[0] << " " << xv[1] << " " << xv[2] << " ]" << endl;
    
        float64_t x_gt[3] = { 0, 0.5, 0.0 };
    
        if( qpResults.return_status == FADAS_CQP_SUCCESS )
        {
            for( uint32_t i = 0; i < n; i++ )
            {
                if( fabs( qpResults.optimization_result[i] - x_gt[i] ) > 1e-10 /*(2.0 * std::numeric_limits&lt;float64_t&gt;::epsilon())*/ )
                    UTIL_ERROR( "Error 1" );
            }
        }
        else
            UTIL_ERROR( "Error 3" );
    
        status = FadasCQP_Destroy( hCQP );
    
        (void)FadasDeregBuf( G );
        (void)FadasDeregBuf( a );
        (void)FadasDeregBuf( C );
        (void)FadasDeregBuf( c );
        (void)FadasDeregBuf( xl );
        (void)FadasDeregBuf( xu );
    
        FadasDeInit();
    }
    Copy to clipboard

Last Published: Sep 30, 2024

[Previous Topic
one/one.cpp](https://docs.qualcomm.com/bundle/publicresource/80-63309-1/topics/one.md) [Next Topic
cqp/app.cpp](https://docs.qualcomm.com/bundle/publicresource/80-63309-1/topics/cqp.md)