Код: Выделить всё
#include
#include
using namespace Ipopt;
/**
* @brief Trivial quadratic problem for testing.
*/
class Dummy_NLP : public TNLP
{
public:
Dummy_NLP()
{
}
virtual ~Dummy_NLP() = default;
bool get_nlp_info(Index &number_of_variables, Index &number_of_constraints,
Index &number_of_nonzero_jacobian_entries, Index &number_of_nonzero_hessian_entries,
IndexStyleEnum &index_style) override
{
number_of_variables = 1;
number_of_constraints = 1;
number_of_nonzero_jacobian_entries = 1;
number_of_nonzero_hessian_entries = 1;
index_style = TNLP::C_STYLE;
return true;
}
bool get_bounds_info(Index n, Number *x_l, Number *x_u, Index m, Number *g_l, Number *g_u) override
{
g_l[0] = -2;
g_u[0] = -2;
return true;
}
bool get_starting_point(Index n, bool init_x, Number *x, bool init_z, Number *z_L, Number *z_U, Index m,
bool init_lambda, Number *lambda) override
{
for (Index i = 0; i < n; i++) {
x[i] = 0;
}
return true;
}
bool eval_f(Index n, const Number *x, bool new_x, Number &obj_value) override
{
obj_value = std::abs(x[0] * x[0] - 5);
return true;
}
bool eval_grad_f(Index n, const Number *x, bool new_x, Number *grad_f) override
{
grad_f[0] = 2 * x[0];
return true;
}
bool eval_g(Index n, const Number *x, bool new_x, Index m, Number *g) override
{
g[0] = x[0] * x[0];
return true;
}
bool eval_jac_g(Index n, const Number *x, bool new_x, Index m, Index nele_jac, Index *iRow, Index *jCol,
Number *values) override
{
if (values == nullptr) {
iRow[0] = 0;
jCol[0] = 0;
} else {
values[0] = 2 * x[0];
}
return true;
}
void finalize_solution(SolverReturn status, Index n, const Number *x, const Number *z_L, const Number *z_U, Index m,
const Number *g, const Number *lambda, Number obj_value, const IpoptData *ip_data,
IpoptCalculatedQuantities *ip_cq) override
{
}
};
int main(int argv, char **argc)
{
SmartPtr mynlp = new Dummy_NLP();
SmartPtr app = IpoptApplicationFactory();
app->Options()->SetNumericValue("tol", 3.82e-6);
app->Options()->SetStringValue("mu_strategy", "adaptive");
app->Options()->SetStringValue("output_file", "ipopt.out");
ApplicationReturnStatus status;
status = app->Initialize();
if (status != Solve_Succeeded) {
std::cout
Подробнее здесь: [url]https://stackoverflow.com/questions/79002581/segmentation-fault-when-calling-ipoptapplicationinitialize[/url]