1
+ /* *
2
+ NL Writer Python bindings.
3
+
4
+ Copyright (C) 2024 AMPL Optimization Inc.
5
+
6
+ Permission to use, copy, modify, and distribute this software and its
7
+ documentation for any purpose and without fee is hereby granted,
8
+ provided that the above copyright notice appear in all copies and that
9
+ both that the copyright notice and this permission notice and warranty
10
+ disclaimer appear in supporting documentation.
11
+
12
+ The author and AMPL Optimization Inc disclaim all warranties with
13
+ regard to this software, including all implied warranties of
14
+ merchantability and fitness. In no event shall the author be liable
15
+ for any special, indirect or consequential damages or any damages
16
+ whatsoever resulting from loss of use, data or profits, whether in an
17
+ action of contract, negligence or other tortious action, arising out
18
+ of or in connection with the use or performance of this software.
19
+
20
+ Author: Gleb Belov
21
+ */
22
+
1
23
#include < string>
2
24
3
25
#include < pybind11/pybind11.h>
@@ -16,12 +38,12 @@ struct NLWPY_ColData {
16
38
// / Num vars
17
39
int num_col_;
18
40
// / lower bounds
19
- py:: array_t < const double > lower_;
41
+ std::vector< double > lower_;
20
42
// / upper bounds
21
- py:: array_t < const double > upper_;
43
+ std::vector< double > upper_;
22
44
// / type: NLW2_VarType...
23
45
// / Set to NULL if all continuous.
24
- py:: array_t < const int > type_;
46
+ std::vector< int > type_;
25
47
};
26
48
27
49
// / Sparse matrix.
@@ -36,33 +58,36 @@ struct NLWPY_SparseMatrix {
36
58
// / Nonzeros
37
59
size_t num_nz_;
38
60
// / Row / col starts
39
- py:: array_t < const size_t > start_;
61
+ std::vector< size_t > start_;
40
62
// / Entry index
41
- py:: array_t < const int > index_;
63
+ std::vector< int > index_;
42
64
// / Entry value
43
- py:: array_t < const double > value_;
65
+ std::vector< double > value_;
44
66
};
45
67
46
68
// / NLWPY_NLModel.
47
- // / TODO check array lengths etc.
69
+ // / @todo check array lengths etc.
70
+ // /
71
+ // / @note In contrast to C/C++, NLWPY copies all data
72
+ // / so the provided arrays/views can be deleted straightaway.
48
73
class NLWPY_NLModel {
49
74
public:
50
75
// / Construct
51
- NLWPY_NLModel (const char * nm=nullptr )
52
- : prob_name_(nm ? nm : " NLWPY_Model " ),
53
- nlme_ (prob_name_)
76
+ NLWPY_NLModel (std::string nm={} )
77
+ : prob_name_(std::move(nm) ),
78
+ nlme_ (prob_name_.c_str() )
54
79
{ }
55
80
56
81
// / Add variables (all at once.).
57
- // / TODO ty can be None.
82
+ // / @todo ty can be None.
58
83
void SetCols (int n,
59
- py:: array_t < const double > lb,
60
- py:: array_t < const double > ub,
61
- py:: array_t < const int > ty) {
84
+ std::vector< double > lb,
85
+ std::vector< double > ub,
86
+ std::vector< int > ty) {
62
87
vars_.num_col_ = n;
63
- vars_.lower_ = lb ;
64
- vars_.upper_ = ub ;
65
- vars_.type_ = ty ;
88
+ vars_.lower_ = std::move (lb) ;
89
+ vars_.upper_ = std::move (ub) ;
90
+ vars_.type_ = std::move (ty) ;
66
91
nlme_.SetCols ({n,
67
92
vars_.lower_ .data (),
68
93
vars_.upper_ .data (),
@@ -71,28 +96,31 @@ class NLWPY_NLModel {
71
96
}
72
97
73
98
// / Add variable names
74
- void SetColNames (std::vector<const char * > nm) {
99
+ void SetColNames (std::vector<std::string > nm) {
75
100
var_names_=std::move (nm);
76
- nlme_.SetColNames (var_names_.data ());
101
+ var_names_c_.resize (var_names_.size ());
102
+ for (auto i=var_names_.size (); i--; )
103
+ var_names_c_[i] = var_names_[i].c_str ();
104
+ nlme_.SetColNames (var_names_c_.data ());
77
105
}
78
106
79
107
// / Add linear constraints (all at once).
80
108
// / Only rowwise matrix supported.
81
109
void SetRows (
82
110
int nr,
83
- py:: array_t < const double > rlb, py:: array_t < const double > rub,
111
+ std::vector< double > rlb, std::vector< double > rub,
84
112
int format, // TODO enum
85
113
size_t nnz,
86
- py:: array_t < const size_t > st,
114
+ std::vector< size_t > st,
87
115
// / Entry index
88
- py:: array_t < const int > ind,
116
+ std::vector< int > ind,
89
117
// / Entry value
90
- py:: array_t < const double > val
118
+ std::vector< double > val
91
119
) {
92
- num_row_=nr; row_lb_=rlb; row_ub_=rub;
120
+ num_row_=nr; row_lb_=std::move ( rlb) ; row_ub_=std::move ( rub) ;
93
121
A_={
94
122
nr, format,
95
- nnz, st, ind, val
123
+ nnz, std::move (st), std::move ( ind), std::move ( val)
96
124
};
97
125
nlme_.SetRows (nr, row_lb_.data (), row_ub_.data (),
98
126
{
@@ -103,17 +131,20 @@ class NLWPY_NLModel {
103
131
}
104
132
105
133
// / Add constraint names
106
- void SetRowNames (std::vector<const char * > nm) {
134
+ void SetRowNames (std::vector<std::string > nm) {
107
135
row_names_=std::move (nm);
108
- nlme_.SetRowNames (row_names_.data ());
136
+ row_names_c_.resize (row_names_.size ());
137
+ for (auto i=row_names_.size (); i--; )
138
+ row_names_c_[i] = row_names_[i].c_str ();
139
+ nlme_.SetRowNames (row_names_c_.data ());
109
140
}
110
141
111
142
// / Add linear objective (only single objective supported.)
112
143
// / Sense: NLW2_ObjSenseM....
113
144
// / Coefficients: dense vector.
114
145
void SetLinearObjective (int sense, double c0,
115
- py:: array_t < const double > c) {
116
- obj_sense_=sense; obj_c0_=c0; obj_c_=c ;
146
+ std::vector< double > c) {
147
+ obj_sense_=sense; obj_c0_=c0; obj_c_=std::move (c) ;
117
148
nlme_.SetLinearObjective (sense, c0, obj_c_.data ());
118
149
}
119
150
@@ -122,16 +153,16 @@ class NLWPY_NLModel {
122
153
void SetHessian (int nr,
123
154
int format, // TODO enum
124
155
size_t nnz,
125
- py:: array_t < const size_t > st,
156
+ std::vector< size_t > st,
126
157
// / Entry index
127
- py:: array_t < const int > ind,
158
+ std::vector< int > ind,
128
159
// / Entry value
129
- py:: array_t < const double > val
160
+ std::vector< double > val
130
161
) {
131
162
Q_format_ = format;
132
163
Q_={
133
164
nr, 0 ,
134
- nnz, st, ind, val
165
+ nnz, std::move (st), std::move ( ind), std::move ( val)
135
166
};
136
167
nlme_.SetHessian (format, {
137
168
nr, 0 , nnz,
@@ -141,31 +172,33 @@ class NLWPY_NLModel {
141
172
}
142
173
143
174
// / Set obj name
144
- void SetObjName (const char * nm) {
145
- obj_name_=(nm ? nm : " " );
146
- nlme_.SetObjName (obj_name_);
175
+ void SetObjName (std::string nm) {
176
+ obj_name_=std::move (nm);
177
+ nlme_.SetObjName (obj_name_. c_str () );
147
178
}
148
179
149
180
// / Get the model
150
181
const mp::NLModel& GetModel () const { return nlme_; }
151
182
152
183
private:
153
184
// / Store the strings/arrays to keep the memory
154
- const char * prob_name_ {" NLWPY_Model" };
185
+ std::string prob_name_ {" NLWPY_Model" };
155
186
mp::NLModel nlme_;
156
187
NLWPY_ColData vars_ {};
157
- std::vector<const char *> var_names_ {};
188
+ std::vector<std::string> var_names_ {};
189
+ std::vector<const char *> var_names_c_ {};
158
190
NLWPY_SparseMatrix A_ {};
159
191
int num_row_ {};
160
- py::array_t <const double > row_lb_ {};
161
- py::array_t <const double > row_ub_ {};
162
- std::vector<const char *> row_names_ {};
192
+ std::vector<double > row_lb_ {};
193
+ std::vector<double > row_ub_ {};
194
+ std::vector<std::string> row_names_ {};
195
+ std::vector<const char *> row_names_c_ {};
163
196
int obj_sense_ {};
164
197
double obj_c0_ {};
165
- py:: array_t < const double > obj_c_ {};
198
+ std::vector< double > obj_c_ {};
166
199
int Q_format_ {};
167
200
NLWPY_SparseMatrix Q_ {};
168
- const char * obj_name_ {" obj[1]" };
201
+ std::string obj_name_ {" obj[1]" };
169
202
};
170
203
171
204
mp::NLSolution NLW2_Solve (mp::NLSolver& nls,
0 commit comments