nlp_tools.hpp
1 /*
2  * This file is part of CasADi.
3  *
4  * CasADi -- A symbolic framework for dynamic optimization.
5  * Copyright (C) 2010-2023 Joel Andersson, Joris Gillis, Moritz Diehl,
6  * KU Leuven. All rights reserved.
7  * Copyright (C) 2011-2014 Greg Horn
8  *
9  * CasADi is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 3 of the License, or (at your option) any later version.
13  *
14  * CasADi is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with CasADi; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 
26 #ifndef CASADI_NLP_TOOLS_HPP
27 #define CASADI_NLP_TOOLS_HPP
28 
29 #include "casadi/core/function.hpp"
30 
31 namespace casadi {
32 
34 
51  CASADI_EXPORT void detect_simple_bounds(const SX& xX, const SX& p,
52  const SX& g, const SX& lbg, const SX& ubg,
53  std::vector<casadi_int>& SWIG_OUTPUT(gi),
54  SX& SWIG_OUTPUT(lbx), SX& SWIG_OUTPUT(ubx),
55  Function& SWIG_OUTPUT(lam_forward),
56  Function& SWIG_OUTPUT(lam_backward));
57  CASADI_EXPORT void detect_simple_bounds(const MX& xX, const MX& p,
58  const MX& g, const MX& lbg, const MX& ubg,
59  std::vector<casadi_int>& SWIG_OUTPUT(gi),
60  MX& SWIG_OUTPUT(lbx), MX& SWIG_OUTPUT(ubx),
61  Function& SWIG_OUTPUT(lam_forward),
62  Function& SWIG_OUTPUT(lam_backward));
64 
65 /*
66  CASADI_EXPORT void detect_simple_bounds(const SX& xX,
67  const SX& g, const SX& lbg, const SX& ubg,
68  std::vector<casadi_int>& SWIG_OUTPUT(gi),
69  DM& SWIG_OUTPUT(lbx), DM& SWIG_OUTPUT(ubx),
70  Function& SWIG_OUTPUT(lam_forward),
71  Function& SWIG_OUTPUT(lam_backward));
72 */
73 
74 
78  template <class T>
79  void check_sos(casadi_int nx, const std::vector< std::vector<T> >& groups,
80  std::vector< std::vector<double> >& weights,
81  std::vector< casadi_int >& types) {
82  // Checks on presence
83  if (groups.empty()) {
84  casadi_assert(weights.empty(), "Missing sos_groups.");
85  casadi_assert(types.empty(), "Missing sos_groups.");
86  }
87 
88  // Checks on dimensions
89  casadi_int sos_num = groups.size();
90 
91  casadi_assert(weights.empty() || weights.size()==sos_num,
92  "sos_weights has incorrect size");
93 
94  // Set default types
95  if (!groups.empty() && types.empty())
96  types.resize(sos_num, 1);
97 
98  // Set default weights
99  if (weights.empty()) {
100  for (const auto& e : groups) {
101  std::vector<double> w(e.size());
102  for (casadi_int i=0;i<w.size();++i) w[i] = i;
103  weights.push_back(w);
104  }
105  }
106 
107  casadi_assert(types.size()==sos_num,
108  "sos_types has incorrect size");
109 
110  // Group-wise dimension check
111  for (casadi_int i=0;i<weights.size();++i) {
112  casadi_assert(weights[i].size()==groups[i].size(),
113  "Dimension mismatch in weights for group " + str(i) + ": "
114  "Expected " + str(groups[i].size()) + ", got " + str(weights[i].size()));
115  }
116 
117  // Checks on contents
118  for (casadi_int t : types) casadi_assert(t==1 || t==2, "SOS type must be either 1 or 2.");
119  for (const auto& v : groups) casadi_assert(in_range(v, 0, nx), "Index out of bound");
120  }
121 
122 } // namespace casadi
123 
124 #endif // CASADI_NLP_TOOLS_HPP
Function object.
Definition: function.hpp:60
MX - Matrix expression.
Definition: mx.hpp:84
Sparse matrix class. SX and DM are specializations.
Definition: matrix_decl.hpp:92
The casadi namespace.
void check_sos(casadi_int nx, const std::vector< std::vector< T > > &groups, std::vector< std::vector< double > > &weights, std::vector< casadi_int > &types)
Check sos structure and generate defaults.
Definition: nlp_tools.hpp:79
CASADI_EXPORT void detect_simple_bounds(const SX &xX, const SX &p, const SX &g, const SX &lbg, const SX &ubg, std::vector< casadi_int > &gi, SX &lbx, SX &ubx, Function &lam_forward, Function &lam_backward)
Detect simple bounds from general constraints.
bool in_range(const std::vector< T > &v, casadi_int upper)
Check if for each element of v holds: v_i < upper.