File: qp_box.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 (73 lines) | stat: -rw-r--r-- 1,561 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
// 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_box.cpp}
{xrst_spell
   rl
}

abs_normal qp_box: Example and Test
###################################

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

.. math::

   \begin{array}{rl}
   \R{minimize}      & x_0 - x_1 \; \R{w.r.t} \; x \in \B{R}^2 \\
   \R{subject \; to} & -2 \leq x_0 \leq +2 \; \R{and} \; -2 \leq x_1 \leq +2
   \end{array}

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

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

bool qp_box(void)
{  bool ok = true;
   typedef CppAD::vector<double> vector;
   //
   size_t n = 2;
   size_t m = 0;
   vector a(n), b(n), c(m), C(m), g(n), G(n*n), xin(n), xout(n);
   a[0] = -2.0;
   a[1] = -2.0;
   b[0] = +2.0;
   b[1] = +2.0;
   g[0] = +1.0;
   g[1] = -1.0;
   for(size_t i = 0; i < n * n; i++)
      G[i] = 0.0;
   //
   // (0, 0) is feasible.
   xin[0] = 0.0;
   xin[1] = 0.0;
   //
   size_t level   = 0;
   double epsilon = 99.0 * std::numeric_limits<double>::epsilon();
   size_t maxitr  = 20;
   //
   ok &= CppAD::qp_box(
      level, a, b, c, C, g, G, epsilon, maxitr, xin, xout
   );
   //
   // check optimal value for x
   ok &= std::fabs( xout[0] + 2.0 ) < epsilon;
   ok &= std::fabs( xout[1] - 2.0 ) < epsilon;
   //
   return ok;
}
// END C++