File: qp_interior.cpp

package info (click to toggle)
cppad 2026.00.00.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,584 kB
  • sloc: cpp: 112,960; sh: 6,146; ansic: 179; python: 71; sed: 12; makefile: 10
file content (107 lines) | stat: -rw-r--r-- 2,485 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
// SPDX-FileCopyrightText: Bradley M. Bell <bradbell@seanet.com>
// SPDX-FileContributor: 2003-24 Bradley M. Bell
// ----------------------------------------------------------------------------
/*
{xrst_begin qp_interior.cpp}
{xrst_spell
   rlr
}

abs_normal qp_interior: Example and Test
########################################

Problem
*******
Our original problem is

.. math::

   \R{minimize} \; | u - 1| \; \R{w.r.t} \; u \in \B{R}

We reformulate this as the following problem

.. math::

   \begin{array}{rlr}
      \R{minimize}      & v             & \R{w.r.t} \; (u,v) \in \B{R}^2 \\
      \R{subject \; to} &  u - 1 \leq v \\
                         &  1 - u \leq v
   \end{array}

This is equivalent to

.. math::

   \begin{array}{rlr}
      \R{minimize}
      & (0, 1) \cdot (u, v)^T  & \R{w.r.t} \; (u,v) \in \B{R}^2 \\
      \R{subject \; to}
      &
      \left( \begin{array}{cc} 1 & -1 \\ -1 & -1 \end{array} \right)
      \left( \begin{array}{c} u \\ v \end{array} \right)
      +
      \left( \begin{array}{c} -1 \\ 1 \end{array} \right)
      \leq
      0
   \end{array}

which is in the form expected by :ref:`qp_interior-name` .

Source
******
{xrst_literal
   // BEGIN C++
   // END C++
}

{xrst_end qp_interior.cpp}
*/
// BEGIN C++
# include <limits>
# include <cppad/utility/vector.hpp>
# include "qp_interior.hpp"

bool qp_interior(void)
{  bool ok = true;
   typedef CppAD::vector<double> vector;
   //
   size_t n = 2;
   size_t m = 2;
   vector C(m*n), c(m), G(n*n), g(n), xin(n), xout(n), yout(m), sout(m);
   C[ 0 * n + 0 ] =  1.0; // C(0,0)
   C[ 0 * n + 1 ] = -1.0; // C(0,1)
   C[ 1 * n + 0 ] = -1.0; // C(1,0)
   C[ 1 * n + 1 ] = -1.0; // C(1,1)
   //
   c[0]           = -1.0;
   c[1]           =  1.0;
   //
   g[0]           =  0.0;
   g[1]           =  1.0;
   //
   // G = 0
   for(size_t i = 0; i < n * n; i++)
      G[i] = 0.0;
   //
   // If (u, v) = (0,2), C * (u, v) + c = (-2,-2)^T + (1,-1)^T < 0
   // Hence (0, 2) is feasible.
   xin[0] = 0.0;
   xin[1] = 2.0;
   //
   double epsilon = 99.0 * std::numeric_limits<double>::epsilon();
   size_t maxitr  = 10;
   size_t level   = 0;
   //
   ok &= CppAD::qp_interior(
      level, c, C, g, G, epsilon, maxitr, xin, xout, yout, sout
   );
   //
   // check optimal value for u
   ok &= std::fabs( xout[0] - 1.0 ) < epsilon;
   // check optimal value for v
   ok &= std::fabs( xout[1] ) < epsilon;
   //
   return ok;
}
// END C++