Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 70 additions & 5 deletions include/pyoptinterface/knitro_model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

// 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); \
Expand Down Expand Up @@ -103,6 +106,17 @@ struct KNITROFreeProblemT
}
};

struct KNITROFreeLicenseT
{
void operator()(LM_context *lmc) const
{
if (lmc != nullptr)
{
knitro::KN_release_license(&lmc);
}
}
};

enum ObjectiveFlags
{
OBJ_CONSTANT = 1 << 0, // 0x01
Expand Down Expand Up @@ -334,6 +348,35 @@ inline ObjectiveSense knitro_obj_sense(int 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>,
Expand All @@ -346,7 +389,16 @@ class KNITROModel : public OnesideLinearConstraintMixin<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
Expand Down Expand Up @@ -423,6 +475,14 @@ class KNITROModel : public OnesideLinearConstraintMixin<KNITROModel>,
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)
{
Expand Down Expand Up @@ -487,32 +547,37 @@ class KNITROModel : public OnesideLinearConstraintMixin<KNITROModel>,

// 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;

// Member variables
std::unique_ptr<KN_context, KNITROFreeProblemT> m_kc = nullptr;

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;

bool m_is_dirty = true;
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);

Expand Down
Loading