forked from metab0t/PyOptInterface
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathknitro_model.hpp
More file actions
778 lines (697 loc) · 23.3 KB
/
knitro_model.hpp
File metadata and controls
778 lines (697 loc) · 23.3 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
#pragma once
#include <deque>
#include <memory>
#include <optional>
#include <variant>
#include "solvers/knitro/knitro.h"
#include "pyoptinterface/core.hpp"
#include "pyoptinterface/container.hpp"
#include "pyoptinterface/cppad_interface.hpp"
#define USE_NLMIXIN
#include "pyoptinterface/solver_common.hpp"
#include "pyoptinterface/dylib.hpp"
// Define Knitro C APIs to be dynamically loaded
#define APILIST \
B(KN_checkout_license); \
B(KN_release_license); \
B(KN_new_lm); \
B(KN_new); \
B(KN_free); \
B(KN_update); \
B(KN_get_param_id); \
B(KN_get_param_type); \
B(KN_set_int_param); \
B(KN_set_char_param); \
B(KN_set_double_param); \
B(KN_get_int_param); \
B(KN_get_double_param); \
B(KN_add_vars); \
B(KN_add_var); \
B(KN_add_cons); \
B(KN_add_con); \
B(KN_set_var_lobnd); \
B(KN_set_var_upbnd); \
B(KN_get_var_lobnd); \
B(KN_get_var_upbnd); \
B(KN_set_var_type); \
B(KN_get_var_type); \
B(KN_set_var_name); \
B(KN_get_var_name); \
B(KN_set_con_lobnd); \
B(KN_set_con_upbnd); \
B(KN_set_con_eqbnd); \
B(KN_get_con_lobnd); \
B(KN_get_con_upbnd); \
B(KN_set_con_name); \
B(KN_get_con_name); \
B(KN_set_obj_goal); \
B(KN_get_obj_goal); \
B(KN_set_var_primal_init_value); \
B(KN_add_obj_constant); \
B(KN_del_obj_constant); \
B(KN_add_obj_linear_struct); \
B(KN_del_obj_linear_struct); \
B(KN_del_obj_linear_struct_all); \
B(KN_add_obj_quadratic_struct); \
B(KN_del_obj_quadratic_struct); \
B(KN_del_obj_quadratic_struct_all); \
B(KN_chg_obj_linear_term); \
B(KN_add_con_constant); \
B(KN_add_con_linear_struct); \
B(KN_add_con_linear_term); \
B(KN_add_con_quadratic_struct); \
B(KN_add_con_quadratic_term); \
B(KN_chg_con_linear_term); \
B(KN_add_eval_callback); \
B(KN_set_cb_user_params); \
B(KN_set_cb_grad); \
B(KN_set_cb_hess); \
B(KN_del_obj_eval_callback_all); \
B(KN_solve); \
B(KN_get_var_primal_value); \
B(KN_get_var_dual_value); \
B(KN_get_con_value); \
B(KN_get_con_dual_value); \
B(KN_get_obj_value); \
B(KN_get_number_iters); \
B(KN_get_mip_number_nodes); \
B(KN_get_mip_relaxation_bnd); \
B(KN_get_mip_rel_gap); \
B(KN_get_solve_time_real); \
B(KN_get_release);
namespace knitro
{
#define B DYLIB_EXTERN_DECLARE
APILIST
#undef B
bool is_library_loaded();
bool load_library(const std::string &path);
} // namespace knitro
struct KNITROFreeProblemT
{
void operator()(KN_context *kc) const
{
if (kc != nullptr)
{
knitro::KN_free(&kc);
}
}
};
struct KNITROFreeLicenseT
{
void operator()(LM_context *lmc) const
{
if (lmc != nullptr)
{
knitro::KN_release_license(&lmc);
}
}
};
enum ObjectiveFlags
{
OBJ_CONSTANT = 1 << 0, // 0x01
OBJ_LINEAR = 1 << 1, // 0x02
OBJ_QUADRATIC = 1 << 2, // 0x04
OBJ_NONLINEAR = 1 << 3 // 0x08
};
enum ConstraintSenseFlags
{
CON_LOBND = 1 << 0, // 0x01
CON_UPBND = 1 << 1, // 0x02
};
struct CallbackPattern
{
std::vector<KNINT> indexCons;
std::vector<KNINT> objGradIndexVars;
std::vector<KNINT> jacIndexCons;
std::vector<KNINT> jacIndexVars;
std::vector<KNINT> hessIndexVars1;
std::vector<KNINT> hessIndexVars2;
};
template <typename V>
struct CallbackEvaluator
{
static inline const std::string jac_coloring_ = "cppad";
static inline const std::string hess_coloring_ = "cppad.symmetric";
std::vector<KNINT> indexVars;
std::vector<KNINT> indexCons;
CppAD::ADFun<V> fun;
std::vector<size_t> fun_rows;
CppAD::sparse_rc<std::vector<size_t>> jac_pattern_;
CppAD::sparse_rcv<std::vector<size_t>, std::vector<V>> jac_;
CppAD::sparse_jac_work jac_work_;
CppAD::sparse_rc<std::vector<size_t>> hess_pattern_;
CppAD::sparse_rc<std::vector<size_t>> hess_pattern_symm_;
CppAD::sparse_rcv<std::vector<size_t>, std::vector<V>> hess_;
CppAD::sparse_hes_work hess_work_;
std::vector<V> x;
std::vector<V> w;
void setup()
{
fun.optimize();
CppAD::sparse_rc<std::vector<size_t>> jac_pattern_in(fun.Range(), fun_rows.size(),
fun_rows.size());
for (size_t k = 0; k < fun_rows.size(); k++)
{
jac_pattern_in.set(k, fun_rows[k], fun_rows[k]);
}
fun.rev_jac_sparsity(jac_pattern_in, false, false, true, jac_pattern_);
jac_pattern_in.resize(fun.Domain(), fun.Domain(), fun.Domain());
for (size_t i = 0; i < fun.Domain(); i++)
{
jac_pattern_in.set(i, i, i);
}
CppAD::sparse_rc<std::vector<size_t>> jac_pattern_out;
fun.for_jac_sparsity(jac_pattern_in, false, false, true, jac_pattern_out);
std::vector<bool> select_rows(fun.Range(), false);
for (size_t k = 0; k < fun_rows.size(); k++)
{
select_rows[fun_rows[k]] = true;
}
fun.rev_hes_sparsity(select_rows, false, true, hess_pattern_);
for (size_t k = 0; k < hess_pattern_.nnz(); k++)
{
size_t row = hess_pattern_.row()[k];
size_t col = hess_pattern_.col()[k];
if (row <= col)
{
hess_pattern_symm_.push_back(row, col);
}
}
x.resize(fun.Domain(), 0.0);
w.resize(fun.Range(), 0.0);
jac_ = CppAD::sparse_rcv<std::vector<size_t>, std::vector<V>>(jac_pattern_);
hess_ = CppAD::sparse_rcv<std::vector<size_t>, std::vector<V>>(hess_pattern_symm_);
}
void eval_fun(const V *req_x, V *res_y, bool aggregate = false)
{
for (size_t i = 0; i < indexVars.size(); i++)
{
x[i] = req_x[indexVars[i]];
}
auto y = fun.Forward(0, x);
for (size_t k = 0; k < fun_rows.size(); k++)
{
if (aggregate)
{
res_y[0] += y[fun_rows[k]];
}
else
{
res_y[k] = y[fun_rows[k]];
}
}
}
void eval_jac(const V *req_x, V *res_jac)
{
for (size_t i = 0; i < indexVars.size(); i++)
{
x[i] = req_x[indexVars[i]];
}
fun.sparse_jac_rev(x, jac_, jac_pattern_, jac_coloring_, jac_work_);
auto& jac = jac_.val();
for (size_t i = 0; i < jac_.nnz(); i++)
{
res_jac[i] = jac[i];
}
}
void eval_hess(const V *req_x, const V *req_w, V *res_hess, bool aggregate = false)
{
for (size_t i = 0; i < indexVars.size(); i++)
{
x[i] = req_x[indexVars[i]];
}
for (size_t k = 0; k < fun_rows.size(); k++)
{
if (aggregate)
{
w[fun_rows[k]] = req_w[0];
}
else
{
w[fun_rows[k]] = req_w[indexCons[k]];
}
}
fun.sparse_hes(x, w, hess_, hess_pattern_, hess_coloring_, hess_work_);
auto& hess = hess_.val();
for (size_t i = 0; i < hess_.nnz(); i++)
{
res_hess[i] = hess[i];
}
}
CallbackPattern get_callback_pattern() const
{
CallbackPattern pattern;
pattern.indexCons = indexCons;
auto& jac_rows = jac_pattern_.row();
auto& jac_cols = jac_pattern_.col();
if (indexCons.empty())
{
for (size_t k = 0; k < jac_pattern_.nnz(); k++)
{
pattern.objGradIndexVars.push_back(indexVars[jac_cols[k]]);
}
}
else
{
for (size_t k = 0; k < jac_pattern_.nnz(); k++)
{
pattern.jacIndexCons.push_back(indexCons[jac_rows[k]]);
pattern.jacIndexVars.push_back(indexVars[jac_cols[k]]);
}
}
auto& hess_rows = hess_pattern_symm_.row();
auto& hess_cols = hess_pattern_symm_.col();
for (size_t k = 0; k < hess_pattern_symm_.nnz(); k++)
{
pattern.hessIndexVars1.push_back(indexVars[hess_rows[k]]);
pattern.hessIndexVars2.push_back(indexVars[hess_cols[k]]);
}
return pattern;
}
};
struct Outputs
{
std::vector<size_t> obj_idxs;
std::vector<size_t> con_idxs;
std::vector<ConstraintIndex> cons;
};
inline bool is_name_empty(const char *name)
{
return name == nullptr || name[0] == '\0';
}
inline int knitro_var_type(VariableDomain domain)
{
switch (domain)
{
case VariableDomain::Continuous:
return KN_VARTYPE_CONTINUOUS;
case VariableDomain::Integer:
return KN_VARTYPE_INTEGER;
case VariableDomain::Binary:
return KN_VARTYPE_BINARY;
default:
throw std::runtime_error("Unknown variable domain");
}
}
inline int knitro_obj_goal(ObjectiveSense sense)
{
switch (sense)
{
case ObjectiveSense::Minimize:
return KN_OBJGOAL_MINIMIZE;
case ObjectiveSense::Maximize:
return KN_OBJGOAL_MAXIMIZE;
default:
throw std::runtime_error("Unknown objective sense");
}
}
inline ObjectiveSense knitro_obj_sense(int goal)
{
switch (goal)
{
case KN_OBJGOAL_MINIMIZE:
return ObjectiveSense::Minimize;
case KN_OBJGOAL_MAXIMIZE:
return ObjectiveSense::Maximize;
default:
throw std::runtime_error("Unknown objective goal");
}
}
inline void knitro_throw(int error)
{
if (error != 0)
{
throw std::runtime_error(fmt::format("KNITRO error code: {}", error));
}
}
class KNITROEnv
{
public:
KNITROEnv(bool empty = false);
KNITROEnv(const KNITROEnv &) = delete;
KNITROEnv &operator=(const KNITROEnv &) = delete;
KNITROEnv(KNITROEnv &&) = default;
KNITROEnv &operator=(KNITROEnv &&) = default;
void start();
bool empty() const;
std::shared_ptr<LM_context> get_lm() const;
void close();
private:
void _check_error(int code) const;
std::shared_ptr<LM_context> m_lm = nullptr;
};
class KNITROModel : public OnesideLinearConstraintMixin<KNITROModel>,
public TwosideLinearConstraintMixin<KNITROModel>,
public OnesideQuadraticConstraintMixin<KNITROModel>,
public TwosideQuadraticConstraintMixin<KNITROModel>,
public TwosideNLConstraintMixin<KNITROModel>,
public LinearObjectiveMixin<KNITROModel>,
public PPrintMixin<KNITROModel>,
public GetValueMixin<KNITROModel>
{
public:
// Constructor/Init/Close
KNITROModel();
KNITROModel(const KNITROEnv &env);
KNITROModel(const KNITROModel &) = delete;
KNITROModel &operator=(const KNITROModel &) = delete;
KNITROModel(KNITROModel &&) = default;
KNITROModel &operator=(KNITROModel &&) = default;
void init();
void init(const KNITROEnv &env);
void close();
// Model information
double get_infinity() const;
std::string get_solver_name() const;
std::string get_release() const;
// Variable functions
VariableIndex add_variable(VariableDomain domain = VariableDomain::Continuous,
double lb = -KN_INFINITY, double ub = KN_INFINITY,
const char *name = nullptr);
void delete_variable(const VariableIndex &variable);
double get_variable_lb(const VariableIndex &variable) const;
double get_variable_ub(const VariableIndex &variable) const;
void set_variable_lb(const VariableIndex &variable, double lb);
void set_variable_ub(const VariableIndex &variable, double ub);
void set_variable_bounds(const VariableIndex &variable, double lb, double ub);
double get_variable_value(const VariableIndex &variable) const;
void set_variable_start(const VariableIndex &variable, double start);
std::string get_variable_name(const VariableIndex &variable) const;
void set_variable_name(const VariableIndex &variable, const std::string &name);
void set_variable_domain(const VariableIndex &variable, VariableDomain domain);
double get_variable_rc(const VariableIndex &variable) const;
std::string pprint_variable(const VariableIndex &variable) const;
// Constraint functions
ConstraintIndex add_linear_constraint(const ScalarAffineFunction &f, ConstraintSense sense,
double rhs, const char *name = nullptr);
ConstraintIndex add_linear_constraint(const ScalarAffineFunction &f,
const std::tuple<double, double> &interval,
const char *name = nullptr);
ConstraintIndex add_quadratic_constraint(const ScalarQuadraticFunction &f,
ConstraintSense sense, double rhs,
const char *name = nullptr);
ConstraintIndex add_quadratic_constraint(const ScalarQuadraticFunction &f,
const std::tuple<double, double> &interval,
const char *name = nullptr);
ConstraintIndex add_second_order_cone_constraint(const Vector<VariableIndex> &variables,
const char *name, bool rotated);
ConstraintIndex add_single_nl_constraint(ExpressionGraph &graph, const ExpressionHandle &result,
const std::tuple<double, double> &interval,
const char *name = nullptr);
ConstraintIndex add_single_nl_constraint_sense_rhs(ExpressionGraph &graph,
const ExpressionHandle &result,
ConstraintSense sense, double rhs,
const char *name = nullptr);
void delete_constraint(const ConstraintIndex &constraint);
void set_constraint_name(const ConstraintIndex &constraint, const std::string &name);
std::string get_constraint_name(const ConstraintIndex &constraint) const;
double get_constraint_primal(const ConstraintIndex &constraint) const;
double get_constraint_dual(const ConstraintIndex &constraint) const;
void set_normalized_rhs(const ConstraintIndex &constraint, double rhs);
double get_normalized_rhs(const ConstraintIndex &constraint) const;
void set_normalized_coefficient(const ConstraintIndex &constraint,
const VariableIndex &variable, double coefficient);
// Objective functions
void set_objective(const ScalarAffineFunction &f, ObjectiveSense sense);
void set_objective(const ScalarQuadraticFunction &f, ObjectiveSense sense);
void set_objective(const ExprBuilder &expr, ObjectiveSense sense);
void add_single_nl_objective(ExpressionGraph &graph, const ExpressionHandle &result);
double get_obj_value() const;
void set_obj_sense(ObjectiveSense sense);
ObjectiveSense get_obj_sense() const;
void set_objective_coefficient(const VariableIndex &variable, double coefficient);
// Solve functions
void optimize();
// Solve information
size_t get_number_iterations() const;
size_t get_mip_node_count() const;
double get_obj_bound() const;
double get_mip_relative_gap() const;
double get_solve_time() const;
// Model state
bool dirty() const;
bool empty() const;
// Solve status
int get_solve_status() const;
// Parameter management
template <typename T>
void set_raw_parameter(const std::string &name, T value)
{
int param_id = _get_value<const char *, int>(knitro::KN_get_param_id, name.c_str());
set_raw_parameter<T>(param_id, value);
}
template <typename T>
void set_raw_parameter(int param_id, T value)
{
if constexpr (std::is_same_v<T, int>)
{
_set_value<int, T>(knitro::KN_set_int_param, param_id, value);
}
else if constexpr (std::is_same_v<T, double>)
{
_set_value<int, T>(knitro::KN_set_double_param, param_id, value);
}
else if constexpr (std::is_same_v<T, std::string> || std::is_same_v<T, const char *>)
{
if constexpr (std::is_same_v<T, std::string>)
{
_set_value<int, const char *>(knitro::KN_set_char_param, param_id, value.c_str());
}
else
{
_set_value<int, const char *>(knitro::KN_set_char_param, param_id, value);
}
}
else
{
static_assert(std::is_same_v<T, int> || std::is_same_v<T, double> ||
std::is_same_v<T, std::string> || std::is_same_v<T, const char *>,
"T must be int, double, std::string, or const char*");
}
}
template <typename T>
T get_raw_parameter(const std::string &name)
{
int param_id = _get_value<const char *, int>(knitro::KN_get_param_id, name.c_str());
return get_raw_parameter<T>(param_id);
}
template <typename T>
T get_raw_parameter(int param_id)
{
if constexpr (std::is_same_v<T, int>)
{
return _get_value<int, T>(knitro::KN_get_int_param, param_id);
}
else if constexpr (std::is_same_v<T, double>)
{
return _get_value<int, T>(knitro::KN_get_double_param, param_id);
}
else
{
static_assert(std::is_same_v<T, int> || std::is_same_v<T, double>,
"T must be int or double for get_raw_parameter");
}
}
// Internal helpers
void _check_error(int error) const;
void _mark_dirty();
void _unmark_dirty();
void _check_dirty() const;
KNINT _variable_index(const VariableIndex &variable) const;
KNINT _constraint_index(const ConstraintIndex &constraint) const;
size_t n_vars = 0;
size_t n_cons = 0;
size_t n_lincons = 0;
size_t n_quadcons = 0;
size_t n_coniccons = 0;
size_t n_nlcons = 0;
private:
// Member variables
std::shared_ptr<LM_context> m_lm = nullptr;
std::unique_ptr<KN_context, KNITROFreeProblemT> m_kc = nullptr;
std::unordered_map<KNINT, std::variant<KNINT, std::pair<KNINT, KNINT>>> m_soc_aux_cons;
std::unordered_map<KNINT, uint8_t> m_con_sense_flags;
uint8_t m_obj_flag = 0;
std::unordered_map<ExpressionGraph *, Outputs> m_pending_outputs;
std::vector<std::unique_ptr<CallbackEvaluator<double>>> m_evaluators;
bool m_need_to_add_callbacks = false;
int m_solve_status = 0;
bool m_is_dirty = true;
private:
void _init();
void _reset_state();
std::tuple<double, double> _sense_to_interval(ConstraintSense sense, double rhs);
void _update_con_sense_flags(const ConstraintIndex &constraint, ConstraintSense sense);
void _set_linear_constraint(const ConstraintIndex &constraint, const ScalarAffineFunction &f);
void _set_quadratic_constraint(const ConstraintIndex &constraint,
const ScalarQuadraticFunction &f);
void _set_second_order_cone_constraint(const ConstraintIndex &constraint,
const Vector<VariableIndex> &variables);
void _set_second_order_cone_constraint_rotated(const ConstraintIndex &constraint,
const Vector<VariableIndex> &variables);
void _set_linear_objective(const ScalarAffineFunction &f);
void _set_quadratic_objective(const ScalarQuadraticFunction &f);
void _reset_objective();
void _add_graph(ExpressionGraph &graph);
void _add_callbacks();
void _add_constraint_callback(ExpressionGraph *graph, const Outputs &outputs);
void _add_objective_callback(ExpressionGraph *graph, const Outputs &outputs);
void _update();
void _pre_solve();
void _solve();
void _post_solve();
template <typename F>
ConstraintIndex _add_constraint_impl(ConstraintType type,
const std::tuple<double, double> &interval,
const char *name, size_t *np, const F &setter)
{
KNINT indexCon;
int error = knitro::KN_add_con(m_kc.get(), &indexCon);
_check_error(error);
IndexT index = indexCon;
ConstraintIndex constraint(type, index);
double lb = std::get<0>(interval);
double ub = std::get<1>(interval);
error = knitro::KN_set_con_lobnd(m_kc.get(), indexCon, lb);
_check_error(error);
error = knitro::KN_set_con_upbnd(m_kc.get(), indexCon, ub);
_check_error(error);
setter(constraint);
if (!is_name_empty(name))
{
error = knitro::KN_set_con_name(m_kc.get(), indexCon, name);
_check_error(error);
}
m_con_sense_flags[indexCon] = CON_UPBND;
n_cons++;
if (np != nullptr)
{
(*np)++;
}
m_is_dirty = true;
return constraint;
}
template <typename S, typename F>
ConstraintIndex _add_constraint_with_sense(const S &function, ConstraintSense sense, double rhs,
const char *name, const F &add)
{
auto interval = _sense_to_interval(sense, rhs);
auto constraint = add(function, interval, name);
_update_con_sense_flags(constraint, sense);
return constraint;
}
template <typename F>
void _set_objective_impl(ObjectiveSense sense, const F &setter)
{
_reset_objective();
setter();
set_obj_sense(sense);
m_is_dirty = true;
}
template <typename F, typename G, typename H>
void _register_callback(CallbackEvaluator<double> *evaluator, const F f, const G g, const H h)
{
CB_context *cb = nullptr;
auto p = evaluator->get_callback_pattern();
int error;
error = knitro::KN_add_eval_callback(m_kc.get(), p.indexCons.empty(), p.indexCons.size(),
p.indexCons.data(), f, &cb);
_check_error(error);
error = knitro::KN_set_cb_user_params(m_kc.get(), cb, evaluator);
_check_error(error);
error = knitro::KN_set_cb_grad(m_kc.get(), cb, p.objGradIndexVars.size(),
p.objGradIndexVars.data(), p.jacIndexCons.size(),
p.jacIndexCons.data(), p.jacIndexVars.data(), g);
_check_error(error);
error = knitro::KN_set_cb_hess(m_kc.get(), cb, p.hessIndexVars1.size(),
p.hessIndexVars1.data(), p.hessIndexVars2.data(), h);
_check_error(error);
}
template <typename T, typename F, typename G, typename H>
void _add_callback_impl(const ExpressionGraph &graph, const std::vector<size_t> &rows,
const std::vector<ConstraintIndex> cons, const T &trace, const F f,
const G g, const H h)
{
auto evaluator_ptr = std::make_unique<CallbackEvaluator<double>>();
auto *evaluator = evaluator_ptr.get();
evaluator->fun = trace(graph);
evaluator->fun_rows = rows;
evaluator->indexVars.resize(graph.n_variables());
for (size_t i = 0; i < graph.n_variables(); i++)
{
evaluator->indexVars[i] = _variable_index(graph.m_variables[i]);
}
evaluator->indexCons.resize(cons.size());
for (size_t i = 0; i < cons.size(); i++)
{
evaluator->indexCons[i] = _constraint_index(cons[i]);
}
evaluator->setup();
_register_callback(evaluator, f, g, h);
m_evaluators.push_back(std::move(evaluator_ptr));
}
template <typename V>
using Getter = std::function<int(KN_context *, V *)>;
template <typename V>
using Setter = std::function<int(KN_context *, V)>;
template <typename V>
V _get_value(Getter<V> get) const
{
V value;
int error = get(m_kc.get(), &value);
_check_error(error);
return value;
}
template <typename V>
void _set_value(Setter<V> set, V value)
{
int error = set(m_kc.get(), value);
_check_error(error);
}
template <typename K, typename V>
using GetterMap = std::function<int(KN_context *, K, V *)>;
template <typename K, typename V>
using SetterMap = std::function<int(KN_context *, K, V)>;
template <typename K, typename V>
V _get_value(GetterMap<K, V> get, K key) const
{
V value;
int error = get(m_kc.get(), key, &value);
_check_error(error);
return value;
}
template <typename K, typename V>
void _set_value(SetterMap<K, V> set, K key, V value)
{
int error = set(m_kc.get(), key, value);
_check_error(error);
}
template <typename F>
std::string _get_name(F get, KNINT index, const char *prefix) const
{
char name[1024];
name[0] = '\0';
int error = get(m_kc.get(), index, 1024, name);
_check_error(error);
if (name[0] != '\0')
{
return std::string(name);
}
else
{
return fmt::format("{}{}", prefix, index);
}
}
template <typename F>
void _set_name(F set, KNINT index, const std::string &name)
{
int error = set(m_kc.get(), index, name.c_str());
_check_error(error);
}
template <typename I>
KNINT _get_index(const I &index) const
{
return index.index;
}
};