Update procedures
[shapes.git] / source / quadratic_programs.cc
blobe4aaead839be76ba6993d43948667e077c775529
1 /* This file is part of Shapes.
3 * Shapes is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * any later version.
8 * Shapes is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with Shapes. If not, see <http://www.gnu.org/licenses/>.
16 * Copyright 2009 Henrik Tidefelt
19 #include "quadratic_programs.h"
20 #include <iomanip>
21 #include <fstream>
22 #include <stdint.h>
24 using namespace Shapes;
26 const char *
27 Computation::QPSolverStatus::code_str( ) const
29 switch( code )
31 case QP_OK:
32 return "OK";
33 case QP_WARNING:
34 return "Warning";
35 case QP_ERROR:
36 return "Error";
37 case QP_FAIL:
38 return "Failure";
39 case QP_BAD:
40 return "Bad problem";
42 return "(QPSolverStatus::code_str: enum switch out of range)";
45 const char *
46 Computation::QPSolverStatus::sub_str( ) const
48 switch( code )
50 case QP_OK:
51 switch( termination )
53 case QP_OK_LM:
54 return "No negative lagrange multipliers.";
55 case QP_OK_GAP_ABS:
56 return "Cost absolute gap less than tolerance.";
57 case QP_OK_GAP_REL:
58 return "Cost relative gap less than tolerance.";
59 case QP_OK_UPPER:
60 return "Cost sufficiently bounded from above.";
61 case QP_OK_LOWER:
62 return "Cost sufficiently bounded from below.";
64 break;
65 case QP_WARNING:
66 switch( warning )
68 case QP_WARNING_NEGATIVE_LM:
69 return "Negative Lagrange multiplier.";
70 case QP_WARNING_EARLY_EXIT:
71 return "Early exit.";
73 break;
74 case QP_ERROR:
75 switch( error )
77 case QP_ERROR_UNDEFINED:
78 return "Undefined error.";
79 case QP_ERROR_ALLOCATE:
80 return "Out of memory.";
81 case QP_ERROR_DIMENSIONS_X:
82 return "Wrong dimension of output x.";
83 case QP_ERROR_DIMENSIONS_LM:
84 return "Wrong dimension of output lm.";
86 break;
87 case QP_FAIL:
88 switch( failure )
90 case QP_FAIL_ITER:
91 return "Maximum number of iterations exceeded.";
93 break;
94 case QP_BAD:
95 switch( bad )
97 case QP_BAD_INCONSISTENT_EQ:
98 return "Inconsistent equality constraints.";
99 case QP_BAD_INCONSISTENT_INEQ:
100 return "Inconsistent inequality constraints.";
101 case QP_BAD_INCONSISTENT_BOX:
102 return "Inconsistent box constraints.";
103 case QP_BAD_INCONSISTENT_ACTIVE:
104 return "Encountered inconsistent constraints.";
105 case QP_BAD_POSITIVE_DEFINITE:
106 return "Objective function not positive definite.";
107 case QP_BAD_LINEAR_DEPENDENT:
108 return "Linearly dependent constraints.";
109 case QP_BAD_INITIAL_INFEASIBLE:
110 return "Infeasible initial point.";
112 break;
114 return "(QPSolverStatus::str: enum switch out of range)";
118 void
119 Computation::polytope_distance_generator_form_write_data( std::ostream & os,
120 const size_t dim,
121 const size_t n1, const double * g1,
122 const size_t n2, const double * g2 )
124 os << "Polytope distance problem in generator form." << std::endl
125 << "===" << std::endl
126 << dim << " " << n1 << " " << n2 << std::endl
127 << std::fixed << std::setprecision(6) ;
129 const double * begin = g1;
130 const double * end = begin + dim * n1;
131 for( const double * src = begin; src != end; ++src )
133 if( src != begin )
135 os << " " ;
137 os << *src ;
139 os << std::endl ;
142 const double * begin = g2;
143 const double * end = begin + dim * n2;
144 for( const double * src = begin; src != end; ++src )
146 if( src != begin )
148 os << " " ;
150 os << *src ;
152 os << std::endl ;
157 void
158 Computation::polytope_distance_generator_form_write_binary_data( const char * oFilename,
159 const size_t dim,
160 const size_t n1, const double * g1,
161 const size_t n2, const double * g2 )
163 std::ofstream oFile( oFilename, std::ios::out | std::ios::binary );
164 uint32_t tmp;
165 tmp = dim;
166 oFile.write( reinterpret_cast< const char * >( & tmp ), sizeof( tmp ) );
167 tmp = n1;
168 oFile.write( reinterpret_cast< const char * >( & tmp ), sizeof( tmp ) );
169 tmp = n2;
170 oFile.write( reinterpret_cast< const char * >( & tmp ), sizeof( tmp ) );
172 const double * begin = g1;
173 const double * end = begin + dim * n1;
174 for( const double * src = begin; src != end; ++src )
176 oFile.write( reinterpret_cast< const char * >( src ), sizeof( *src ) );
180 const double * begin = g2;
181 const double * end = begin + dim * n2;
182 for( const double * src = begin; src != end; ++src )
184 oFile.write( reinterpret_cast< const char * >( src ), sizeof( *src ) );