#include <stdio.h>
#include <math.h>
#include "ripopt.h"
static int eval_f(int n, const double *x, int new_x,
double *obj, void *user_data)
{
(void)n; (void)new_x; (void)user_data;
*obj = 9.0 - 8.0*x[0] - 6.0*x[1] - 4.0*x[2]
+ 2.0*x[0]*x[0] + 2.0*x[1]*x[1] + x[2]*x[2]
+ 2.0*x[0]*x[1] + 2.0*x[0]*x[2];
return 1;
}
static int eval_grad_f(int n, const double *x, int new_x,
double *grad, void *user_data)
{
(void)n; (void)new_x; (void)user_data;
grad[0] = -8.0 + 4.0*x[0] + 2.0*x[1] + 2.0*x[2];
grad[1] = -6.0 + 2.0*x[0] + 4.0*x[1];
grad[2] = -4.0 + 2.0*x[0] + 2.0*x[2];
return 1;
}
static int eval_g(int n, const double *x, int new_x,
int m, double *g, void *user_data)
{
(void)n; (void)m; (void)new_x; (void)user_data;
g[0] = x[0] + x[1] + 2.0*x[2];
return 1;
}
static int eval_jac_g(int n, const double *x, int new_x,
int m, int nele_jac,
int *iRow, int *jCol, double *values,
void *user_data)
{
(void)n; (void)x; (void)m; (void)nele_jac; (void)new_x; (void)user_data;
if (values == NULL) {
iRow[0]=0; jCol[0]=0;
iRow[1]=0; jCol[1]=1;
iRow[2]=0; jCol[2]=2;
} else {
values[0] = 1.0;
values[1] = 1.0;
values[2] = 2.0;
}
return 1;
}
static int eval_h(int n, const double *x, int new_x,
double obj_factor,
int m, const double *lambda, int new_lambda,
int nele_hess,
int *iRow, int *jCol, double *values,
void *user_data)
{
(void)n; (void)x; (void)m; (void)nele_hess; (void)new_x; (void)new_lambda;
(void)lambda; (void)user_data;
if (values == NULL) {
iRow[0]=0; jCol[0]=0;
iRow[1]=1; jCol[1]=0;
iRow[2]=1; jCol[2]=1;
iRow[3]=2; jCol[3]=0;
iRow[4]=2; jCol[4]=2;
} else {
values[0] = obj_factor * 4.0;
values[1] = obj_factor * 2.0;
values[2] = obj_factor * 4.0;
values[3] = obj_factor * 2.0;
values[4] = obj_factor * 2.0;
}
return 1;
}
int main(void)
{
int n = 3, m = 1;
double x_l[3] = {0.0, 0.0, 0.0};
double x_u[3] = {HUGE_VAL, HUGE_VAL, HUGE_VAL};
double g_l[1] = {-HUGE_VAL};
double g_u[1] = {3.0};
int nele_jac = 3;
int nele_hess = 5;
RipoptProblem nlp = ripopt_create(
n, x_l, x_u,
m, g_l, g_u,
nele_jac, nele_hess, 0,
eval_f, eval_grad_f, eval_g, eval_jac_g, eval_h);
if (!nlp) {
fprintf(stderr, "ripopt_create failed\n");
return 1;
}
ripopt_add_int_option(nlp, "print_level", 0);
ripopt_add_num_option(nlp, "tol", 1e-8);
double x[3] = {0.5, 0.5, 0.5};
double obj_val = 0.0;
double g[1] = {0.0};
double mult_g[1] = {0.0};
double mult_xl[3] = {0.0, 0.0, 0.0};
double mult_xu[3] = {0.0, 0.0, 0.0};
int status = ripopt_solve(nlp, x, g, &obj_val,
mult_g, mult_xl, mult_xu, NULL);
double expected_obj = 1.0/9.0;
printf("=== HS035 (inequality + bounds) ===\n");
printf("Status : %d (0 = Optimal)\n", status);
printf("Obj : %.10f (expected %.10f)\n", obj_val, expected_obj);
printf("x : [%.6f, %.6f, %.6f]\n", x[0], x[1], x[2]);
printf(" (expected [1.3333, 0.7778, 0.4444])\n");
printf("g(x) : [%.6f] (should be <= 3)\n", g[0]);
printf("mult_g : [%.6f] (constraint multiplier)\n", mult_g[0]);
printf("mult_x_L : [%.6f, %.6f, %.6f] (bound multipliers)\n",
mult_xl[0], mult_xl[1], mult_xl[2]);
int pass = ((status == 0 || status == 1) &&
fabs(obj_val - expected_obj) < 1e-3);
printf("Test : %s\n", pass ? "PASSED" : "FAILED");
ripopt_free(nlp);
return pass ? 0 : 1;
}