xpress_interface.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 #ifndef CASADI_XPRESS_INTERFACE_HPP
26 #define CASADI_XPRESS_INTERFACE_HPP
27 
28 #include <string>
29 #include <vector>
30 #include "casadi/core/conic_impl.hpp"
31 #include <casadi/interfaces/xpress/casadi_conic_xpress_export.h>
32 
33 #include <xprs.h>
34 
35 namespace casadi {
36  #include "xpress_runtime.hpp"
37 }
38 
52 
53 namespace casadi {
54 
55  struct XpressMemory : public ConicMemory {
56  // Problem data structure
58  // IIS cache: populated right after an infeasible solve when compute_iis=true
59  std::vector<casadi_int> iis_rows, iis_cols;
60  std::string iis_row_types, iis_col_bound_types;
61  bool iis_valid = false;
62  };
63 
72  class XpressInterface : public Conic {
73  public:
75  static Conic* creator(const std::string& name,
76  const std::map<std::string, Sparsity>& st) {
77  return new XpressInterface(name, st);
78  }
79 
81  explicit XpressInterface(const std::string& name,
82  const std::map<std::string, Sparsity>& st);
83 
85  ~XpressInterface() override;
86 
87  // Get name of the plugin
88  const char* plugin_name() const override { return "xpress";}
89 
90  // Get name of the class
91  std::string class_name() const override { return "XpressInterface";}
92 
94 
95  static const Options options_;
96  const Options& get_options() const override { return options_;}
98 
99  void set_xpress_prob();
100  void set_xpress_prob(CodeGenerator& g) const;
101 
103  void codegen_body(CodeGenerator& g) const override;
104 
106  void codegen_init_mem(CodeGenerator& g) const override;
107 
109  void codegen_free_mem(CodeGenerator& g) const override;
110 
112  std::string codegen_mem_type() const override { return "struct casadi_xpress_data"; }
113 
115  bool codegen_needs_mem() const override { return true; }
116 
117  // Initialize the solver
118  void init(const Dict& opts) override;
119 
120  // Initialize dependant read-only members of class
121  void init_dependent();
122 
124  void* alloc_mem() const override { return new XpressMemory();}
125 
127  int init_mem(void* mem) const override;
128 
130  void free_mem(void *mem) const override;
131 
133  void set_work(void* mem, const double**& arg, double**& res,
134  casadi_int*& iw, double*& w) const override;
135 
137  Dict get_stats(void* mem) const override;
138 
139  // Solve the QP
140  int solve(const double** arg, double** res,
141  casadi_int* iw, double* w, void* mem) const override;
142 
144  static const std::string meta_doc;
145 
147  Dict opts_;
148 
149  // Use x0 as a MIP start hint [default false]
150  bool mip_start_;
151 
152  // If non-empty, call XPRSsetlogfile to write solver output to this path
153  std::string log_file_;
154 
155  // Compute IIS after infeasible solves and cache in memory [default true]
156  bool compute_iis_;
157 
158  void serialize_body(SerializingStream &s) const override;
159 
161  static ProtoFunction* deserialize(DeserializingStream& s) { return new XpressInterface(s); }
162 
164  bool integer_support() const override { return true; }
165 
167  bool psd_support() const override { return true; }
168 
169  protected:
171  explicit XpressInterface(DeserializingStream& s);
172 
173  private:
174 
175  // Memory structure
177 
178  // Linear constraint matrix in CSC form (as int, since Xpress takes int)
179  std::vector<int> colinda_, rowa_;
180 
181  // Upper-triangular triplet form of the Hessian H_ in column-major order.
182  // Entry k corresponds to H_->row()[k_full], H_->colind()[..] elements
183  // filtered to row<=col, in the order they appear in H_'s nonzero array.
184  // The mapping from H_-nz-index to triplet-index is identity for the
185  // upper-triangular nonzeros (we drop the strict lower triangle since
186  // CasADi's H_ is typically symmetric and we expect both triangles, or
187  // we could expect upper-triangular only; here we accept both and pick
188  // upper). qobj_nz_idx_[k] gives the H_ nz index for the k-th triplet.
189  std::vector<int> qobj_col1_, qobj_col2_;
190  std::vector<int> qobj_nz_idx_;
191 
192  // Per-column type ('I' for integer/discrete, 'C' for continuous).
193  // Empty if no discrete variables.
194  std::vector<char> coltype_;
195 
196  // SOS data, flattened from sos_groups/sos_weights/sos_types options.
197  // settype_[k] is '1' or '2'; setstart_[k]..setstart_[k+1] indexes into
198  // setind_/refval_ for the k-th group.
199  std::vector<char> sos_settype_;
200  std::vector<int> sos_setstart_, sos_setind_;
201  std::vector<double> sos_refval_;
202 
203  // SOCP support: precomputed mapping between CasADi's Q/P input pair and
204  // per-cone constraint blocks. Populated when Q_.nnz() > 0.
205  bool has_socp_;
207  // Backing storage for socp_ (must outlive socp_)
208  std::vector<casadi_int> socp_r_;
209  std::vector<casadi_int> socp_mq_colind_, socp_mq_row_, socp_mq_data_;
210  std::vector<casadi_int> socp_map_P_;
211 
212  // Build socp_ from the result of Conic::sdp_to_socp_init
213  void build_socp_config();
214  };
215 } // end namespace casadi
217 #endif // CASADI_XPRESS_INTERFACE_HPP
The casadi namespace.
Definition: archiver.hpp:32
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.