ipopt-src 0.2.3+3.14.16

Redistribution of Coin-OR Ipopt as a crate
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
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
/* Copyright (C) 2007 VRTech Industrial Technologies - www.vrtech.com.br.
 * Copyright (C) 2007 Tong Kewei, BeiHang University - www.buaa.edu.cn.
 * All Rights Reserved.
 * This code is published under the Eclipse Public License.
 */

#include <cassert>
#include <jni.h>
#include "IpTNLP.hpp"
#include "IpIpoptApplication.hpp"
#include "org_coinor_Ipopt.h"

using namespace std;
using namespace Ipopt;

#ifdef IPOPT_SINGLE
typedef jfloat jnumber;
typedef jfloatArray jnumberArray;
#define NewNumberArray NewFloatArray
#define GetNumberArrayRegion GetFloatArrayRegion
#define SetNumberArrayRegion SetFloatArrayRegion
#else
typedef jdouble jnumber;
typedef jdoubleArray jnumberArray;
#define NewNumberArray NewDoubleArray
#define GetNumberArrayRegion GetDoubleArrayRegion
#define SetNumberArrayRegion SetDoubleArrayRegion
#endif

/** Main structure for Ipopt JNI implementation.
 *
 * All functions will receive a pointer to this structure as
 * an integer argument (the address in memory of the structure).
 */
class Jipopt: public TNLP
{
public:
   /** constructor */
   Jipopt(
      JNIEnv* env,        /**< JNI environment */
      jobject solver,     /**< Java Ipopt solver object */
      jint    n,          /**< number of variables */
      jint    m,          /**< number of constraints */
      jint    nele_jac,   /**< number of nonzero elements in Jacobian */
      jint    nele_hess,  /**< number of nonzero elements in Hessian */
      jint    index_style /**< whether to use C or Fortran index style (0 for C, 1 for Fortran) */
   );

   /** default destructor */
   virtual ~Jipopt()
   {
   }

   /**@name Overloaded from TNLP */
   ///@{
   /* Method to return some info about the NLP */
   virtual bool get_nlp_info(
      Index&          n,
      Index&          m,
      Index&          nnz_jac_g,
      Index&          nnz_h_lag,
      IndexStyleEnum& index_style
   );

   /* Method to return the bounds for my problem */
   virtual bool get_bounds_info(
      Index   n,
      Number* x_l,
      Number* x_u,
      Index   m,
      Number* g_l,
      Number* g_u
   );

   /* Method to return the starting point for the algorithm */
   virtual 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
   );

   /* Method to return the objective value */
   virtual bool eval_f(
      Index         n,
      const Number* x,
      bool          new_x,
      Number&       obj_value
   );

   /* Method to return the gradient of the objective */
   virtual bool eval_grad_f(
      Index         n,
      const Number* x,
      bool          new_x,
      Number*       grad_f
   );

   /* Method to return the constraint activities */
   virtual bool eval_g(
      Index         n,
      const Number* x,
      bool          new_x,
      Index         m,
      Number*       g
   );

   /* Method to return:
    *  1) The structure of the Jacobian (if "values" is NULL)
    *  2) The values of the Jacobian (if "values" is not NULL)
    */
   virtual bool eval_jac_g(
      Index         n,
      const Number* x,
      bool          new_x,
      Index         m,
      Index         nele_jac,
      Index*        iRow,
      Index*        jCol,
      Number*       values
   );

   /* Method to return:
    *  1) The structure of the Hessian of the Lagrangian (if "values" is NULL)
    *  2) The values of the Hessian of the Lagrangian (if "values" is not NULL)
    */
   virtual bool eval_h(
      Index         n,
      const Number* x,
      bool          new_x,
      Number        obj_factor,
      Index         m,
      const Number* lambda,
      bool          new_lambda,
      Index         nele_hess,
      Index*        iRow,
      Index*        jCol,
      Number*       values
   );

   /* This method is called when the algorithm is complete so the TNLP can store/write the solution */
   virtual 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
   );

   /* Intermediate Callback method for the user. */
   virtual bool intermediate_callback(
      AlgorithmMode              mode,
      Index                      iter,
      Number                     obj_value,
      Number                     inf_pr,
      Number                     inf_du,
      Number                     mu,
      Number                     d_norm,
      Number                     regularization_size,
      Number                     alpha_du,
      Number                     alpha_pr,
      Index                      ls_trials,
      const IpoptData*           ip_data,
      IpoptCalculatedQuantities* ip_cq
   );

   /* Method to return scaling parameters. */
   virtual bool get_scaling_parameters(
      Number&  obj_scaling,
      bool&    use_x_scaling,
      Index    n,
      Number*  x_scaling,
      bool&    use_g_scaling,
      Index    m,
      Number*  g_scaling
   );

   /* Method for quasi-Newton approximation. */
   virtual Index get_number_of_nonlinear_variables();

   virtual bool get_list_of_nonlinear_variables(
      Index  num_nonlin_vars,
      Index* pos_nonlin_vars
   );
   ///@}

public:
   /// The JNI Environment
   JNIEnv* env;
   jobject solver;

   jint n;
   jint m;
   jint nele_jac;
   jint nele_hess;
   jint index_style;

   // some cached arguments
   jnumberArray mult_gj;
   jnumberArray mult_x_Lj;
   jnumberArray mult_x_Uj;

   // the callback arguments
   jnumberArray xj;
   jnumberArray fj;
   jnumberArray grad_fj;
   jnumberArray gj;
   jnumberArray jac_gj;
   jnumberArray hessj;

   jboolean using_scaling_parameters;
   jboolean using_LBFGS;

   SmartPtr<IpoptApplication> application;

   // the callback methods
   jmethodID get_bounds_info_;
   jmethodID get_starting_point_;
   jmethodID eval_f_;
   jmethodID eval_grad_f_;
   jmethodID eval_g_;
   jmethodID eval_jac_g_;
   jmethodID eval_h_;

   jmethodID intermediate_callback_;
   jmethodID get_scaling_parameters_;
   jmethodID get_number_of_nonlinear_variables_;
   jmethodID get_list_of_nonlinear_variables_;

private:
   Jipopt(const Jipopt&);
   Jipopt& operator=(const Jipopt&);
};

Jipopt::Jipopt(
   JNIEnv* env_,
   jobject solver_,
   jint    n_,
   jint    m_,
   jint    nele_jac_,
   jint    nele_hess_,
   jint    index_style_
)
   : env(env_), solver(solver_), n(n_), m(m_), nele_jac(nele_jac_), nele_hess(nele_hess_), index_style(index_style_),
     mult_gj(NULL), mult_x_Lj(NULL), mult_x_Uj(NULL), xj(NULL), fj(NULL), grad_fj(NULL),
     gj(NULL), jac_gj(NULL), hessj(NULL),
     using_scaling_parameters(false), using_LBFGS(false),
     application(new IpoptApplication())
{
   application->RethrowNonIpoptException(false);

   // the solver class
   jclass solverCls = env->GetObjectClass(solver);

   // get the methods
#ifndef IPOPT_SINGLE
   get_bounds_info_    = env->GetMethodID(solverCls, "get_bounds_info", "(I[D[DI[D[D)Z");
   get_starting_point_ = env->GetMethodID(solverCls, "get_starting_point", "(IZ[DZ[D[DIZ[D)Z");
   eval_f_             = env->GetMethodID(solverCls, "eval_f", "(I[DZ[D)Z");
   eval_grad_f_        = env->GetMethodID(solverCls, "eval_grad_f", "(I[DZ[D)Z");
   eval_g_             = env->GetMethodID(solverCls, "eval_g", "(I[DZI[D)Z");
   eval_jac_g_         = env->GetMethodID(solverCls, "eval_jac_g", "(I[DZII[I[I[D)Z");
   eval_h_             = env->GetMethodID(solverCls, "eval_h", "(I[DZDI[DZI[I[I[D)Z");
   get_scaling_parameters_ = env->GetMethodID(solverCls, "get_scaling_parameters", "([DI[DI[D[Z)Z");
   intermediate_callback_ = env->GetMethodID(solverCls, "intermediate_callback", "(IIDDDDDDDDIJJ)Z");
#else
   get_bounds_info_    = env->GetMethodID(solverCls, "get_bounds_info", "(I[F[FI[F[F)Z");
   get_starting_point_ = env->GetMethodID(solverCls, "get_starting_point", "(IZ[FZ[F[FIZ[F)Z");
   eval_f_             = env->GetMethodID(solverCls, "eval_f", "(I[FZ[F)Z");
   eval_grad_f_        = env->GetMethodID(solverCls, "eval_grad_f", "(I[FZ[F)Z");
   eval_g_             = env->GetMethodID(solverCls, "eval_g", "(I[FZI[F)Z");
   eval_jac_g_         = env->GetMethodID(solverCls, "eval_jac_g", "(I[FZII[I[I[F)Z");
   eval_h_             = env->GetMethodID(solverCls, "eval_h", "(I[FZFI[FZI[I[I[F)Z");
   get_scaling_parameters_ = env->GetMethodID(solverCls, "get_scaling_parameters", "([FI[FI[F[Z)Z");
   intermediate_callback_ = env->GetMethodID(solverCls, "intermediate_callback", "(IIFFFFFFFFIJJ)Z");
#endif
   get_number_of_nonlinear_variables_ = env->GetMethodID(solverCls, "get_number_of_nonlinear_variables", "()I");
   get_list_of_nonlinear_variables_   = env->GetMethodID(solverCls, "get_list_of_nonlinear_variables", "(I[I)Z");

   if( get_bounds_info_ == 0 || get_starting_point_ == 0 || eval_f_ == 0
       || eval_grad_f_ == 0 || eval_g_ == 0 || eval_jac_g_ == 0 || eval_h_ == 0
       || get_scaling_parameters_ == 0 || get_number_of_nonlinear_variables_ == 0
       || get_list_of_nonlinear_variables_ == 0 )
   {
      std::cerr << "Expected callback methods missing on JIpopt.java" << std::endl;
   }

   assert(get_bounds_info_    != 0);
   assert(get_starting_point_ != 0);
   assert(eval_f_      != 0);
   assert(eval_grad_f_ != 0);
   assert(eval_g_      != 0);
   assert(eval_jac_g_  != 0);
   assert(eval_h_      != 0);
   assert(get_scaling_parameters_ != 0);
   assert(get_number_of_nonlinear_variables_ != 0);
   assert(get_list_of_nonlinear_variables_   != 0);
}

bool Jipopt::get_nlp_info(
   Index&          n,
   Index&          m,
   Index&          nnz_jac_g,
   Index&          nnz_h_lag,
   IndexStyleEnum& index_style)
{
   n = this->n;
   m = this->m;
   nnz_jac_g = this->nele_jac;
   nnz_h_lag = this->nele_hess;

   index_style = (IndexStyleEnum) this->index_style;

   return true;
}

bool Jipopt::get_bounds_info(
   Index   n,
   Number* x_l,
   Number* x_u,
   Index   m,
   Number* g_l,
   Number* g_u)
{
   jnumberArray x_lj = NULL;
   jnumberArray x_uj = NULL;
   jnumberArray g_lj = NULL;
   jnumberArray g_uj = NULL;

   assert(x_l != NULL);
   assert(x_u != NULL);
   assert(g_l != NULL);
   assert(g_u != NULL);

   x_lj = env->NewNumberArray(n);
   x_uj = env->NewNumberArray(n);
   g_lj = env->NewNumberArray(m);
   g_uj = env->NewNumberArray(m);

   if( !env->CallBooleanMethod(solver, get_bounds_info_, n, x_lj, x_uj, m, g_lj, g_uj) )
   {
      return false;
   }

   // Copy from Java to native value
   env->GetNumberArrayRegion(x_lj, 0, n, x_l);
   env->GetNumberArrayRegion(x_uj, 0, n, x_u);
   env->GetNumberArrayRegion(g_lj, 0, m, g_l);
   env->GetNumberArrayRegion(g_uj, 0, m, g_u);

   return true;
}

bool Jipopt::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)
{
   jnumberArray xj      = this->xj;
   jnumberArray z_lj    = this->mult_x_Lj;
   jnumberArray z_uj    = this->mult_x_Uj;
   jnumberArray lambdaj = this->mult_gj;

   if( !env->CallBooleanMethod(solver, get_starting_point_, n, init_x, xj, init_z, z_lj, z_uj, m, init_lambda, lambdaj) )
   {
      return false;
   }

   /* Copy from Java to native value */
   if( init_x )
   {
      env->GetNumberArrayRegion(xj, 0, n, x);
   }

   if( init_z )
   {
      env->GetNumberArrayRegion(z_lj, 0, n, z_L);
      env->GetNumberArrayRegion(z_uj, 0, n, z_U);
   }

   if( init_lambda )
   {
      env->GetNumberArrayRegion(lambdaj, 0, m, lambda);
   }

   return true;
}

bool Jipopt::eval_f(
   Index         n,
   const Number* x,
   bool          new_x,
   Number&       obj_value)
{
   /* Copy the native double x to the Java double array xj, if new values */
   if( new_x )
   {
      env->SetNumberArrayRegion(xj, 0, n, const_cast<Number*>(x));
   }

   /* Call the java method */
   jboolean new_xj = new_x;
   if( !env->CallBooleanMethod(solver, eval_f_, n, xj, new_xj, fj) )
   {
      return false;
   }

   /* Copy from Java to native value */
   env->GetNumberArrayRegion(fj, 0, 1, &obj_value);

   return true;
}

bool Jipopt::eval_grad_f(
   Index         n,
   const Number* x,
   bool          new_x,
   Number*       grad_f)
{
   /* Copy the native double x to the Java double array xj, if new values */
   if( new_x )
   {
      env->SetNumberArrayRegion(xj, 0, n, const_cast<Number*>(x));
   }

   /* Call the java method */
   jboolean new_xj = new_x;
   if( !env->CallBooleanMethod(solver, eval_grad_f_, n, xj, new_xj, grad_fj) )
   {
      return false;
   }

   env->GetNumberArrayRegion(grad_fj, 0, n, grad_f);

   return true;
}

bool Jipopt::eval_g(
   Index         n,
   const Number* x,
   bool          new_x,
   Index         m,
   Number*       g)
{
   /* Copy the native double x to the Java double array xj, if new values */
   if( new_x )
   {
      env->SetNumberArrayRegion(xj, 0, n, const_cast<Number*>(x));
   }

   /* Call the java method */
   jboolean new_xj = new_x;
   if( !env->CallBooleanMethod(solver, eval_g_, n, xj, new_xj, m, gj) )
   {
      return false;
   }

   /* Copy from Java to native value */
   env->GetNumberArrayRegion(gj, 0, m, g);

   return true;
}

bool Jipopt::eval_jac_g(
   Index         n,
   const Number* x,
   bool          new_x,
   Index         m,
   Index         nele_jac,
   Index*        iRow,
   Index*        jCol,
   Number*       jac_g)
{
   // Copy the native double x to the Java double array xj, if new values
   if( new_x && x != NULL )
   {
      env->SetNumberArrayRegion(xj, 0, n, const_cast<Number*>(x));
   }

   /// Create the index arrays if needed
   jintArray iRowj = NULL;
   jintArray jColj = NULL;
   if( iRow != NULL && jCol != NULL )
   {
      iRowj = env->NewIntArray(nele_jac);
      jColj = env->NewIntArray(nele_jac);
   }

   /* Call the java method */
   jboolean new_xj = new_x;
   if( !env->CallBooleanMethod(solver, eval_jac_g_, n, xj, new_xj, m, nele_jac, iRowj, jColj, jac_g == NULL ? NULL : jac_gj) )
   {
      return false;
   }

   /* Copy from Java to native value */
   if( jac_g != NULL )
   {
      env->GetNumberArrayRegion(jac_gj, 0, nele_jac, jac_g);
   }

   if( iRow != NULL && jCol != NULL )
   {
      if( sizeof(jint) == sizeof(Index) )
      {
         env->GetIntArrayRegion(iRowj, 0, nele_jac, reinterpret_cast<jint*>(iRow));
         env->GetIntArrayRegion(jColj, 0, nele_jac, reinterpret_cast<jint*>(jCol));
      }
      else
      {
         jint* tmp = new jint[nele_jac];

         env->GetIntArrayRegion(iRowj, 0, nele_jac, tmp);
         for( Index i = 0; i < nele_jac; ++i )
         {
            iRow[i] = (Index) tmp[i];
         }

         env->GetIntArrayRegion(jColj, 0, nele_jac, tmp);
         for( Index i = 0; i < nele_jac; ++i )
         {
            jCol[i] = (Index) tmp[i];
         }

         delete[] tmp;
      }
   }

   return true;
}

bool Jipopt::eval_h(
   Index         n,
   const Number* x,
   bool          new_x,
   Number        obj_factor,
   Index         m,
   const Number* lambda,
   bool          new_lambda,
   Index         nele_hess,
   Index*        iRow,
   Index*        jCol,
   Number*       hess)
{
   /* Copy the native double x to the Java double array xj, if new values */
   if( new_x && x != NULL )
   {
      env->SetNumberArrayRegion(xj, 0, n, const_cast<Number*>(x));
   }

   /* Copy the native double lambda to the Java double array lambdaj, if new values */
   if( new_lambda && lambda != NULL )
   {
      env->SetNumberArrayRegion(mult_gj, 0, m, const_cast<Number*>(lambda));
   }

   /* Create the index arrays if needed */
   jintArray iRowj = NULL;
   jintArray jColj = NULL;
   if( iRow != NULL && jCol != NULL )
   {
      iRowj = env->NewIntArray(nele_hess);
      jColj = env->NewIntArray(nele_hess);
   }

   /* Call the java method */
   jboolean new_xj = new_x;
   jboolean new_lambdaj = new_lambda;
   if( !env->CallBooleanMethod(solver, eval_h_, n, xj, new_xj, obj_factor, m, mult_gj, new_lambdaj, nele_hess, iRowj, jColj, hess == NULL ? NULL : hessj) )
   {
      return false;
   }

   /* Copy from Java to native value */
   if( hess != NULL )
   {
      env->GetNumberArrayRegion(hessj, 0, nele_hess, hess);
   }

   if( iRow != NULL && jCol != NULL )
   {
      if( sizeof(jint) == sizeof(Index) )
      {
         env->GetIntArrayRegion(iRowj, 0, nele_hess, reinterpret_cast<jint*>(iRow));
         env->GetIntArrayRegion(jColj, 0, nele_hess, reinterpret_cast<jint*>(jCol));
      }
      else
      {
         jint* tmp = new jint[nele_hess];

         env->GetIntArrayRegion(iRowj, 0, nele_hess, tmp);
         for( Index i = 0; i < nele_hess; ++i )
         {
            iRow[i] = (Index) tmp[i];
         }

         env->GetIntArrayRegion(jColj, 0, nele_hess, tmp);
         for( Index i = 0; i < nele_hess; ++i )
         {
            jCol[i] = (Index) tmp[i];
         }

         delete[] tmp;
      }
   }

   return true;
}

void Jipopt::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*/
)
{
   /* Copy the native arrays to Java double arrays */

   if( x != NULL )
   {
      env->SetNumberArrayRegion(xj, 0, n, const_cast<Number*>(x));
   }

   if( z_L != NULL )
   {
      env->SetNumberArrayRegion(mult_x_Lj, 0, n, const_cast<Number*>(z_L));
   }

   if( z_U != NULL )
   {
      env->SetNumberArrayRegion(mult_x_Uj, 0, n, const_cast<Number*>(z_U));
   }

   if( g != NULL )
   {
      env->SetNumberArrayRegion(gj, 0, m, const_cast<Number*>(g));
   }

   if( lambda != NULL )
   {
      env->SetNumberArrayRegion(mult_gj, 0, m, const_cast<Number*>(lambda));
   }

   env->GetNumberArrayRegion(fj, 0, 1, &obj_value);
}

/* Intermediate Callback method for the user. */
bool Jipopt::intermediate_callback(
   AlgorithmMode              mode,
   Index                      iter,
   Number                     obj_value,
   Number                     inf_pr,
   Number                     inf_du,
   Number                     mu,
   Number                     d_norm,
   Number                     regularization_size,
   Number                     alpha_du,
   Number                     alpha_pr,
   Index                      ls_trials,
   const IpoptData*           ip_data,
   IpoptCalculatedQuantities* ip_cq
)
{
   return env->CallBooleanMethod(solver, intermediate_callback_, (int)mode, iter, obj_value, inf_pr, inf_du, mu, d_norm, regularization_size, alpha_du, alpha_pr, ls_trials, ip_data, ip_cq);
}

bool Jipopt::get_scaling_parameters(
   Number& obj_scaling,
   bool&   use_x_scaling,
   Index   n,
   Number* x_scaling,
   bool&   use_g_scaling,
   Index   m,
   Number* g_scaling)
{
   if( !using_scaling_parameters )
   {
      return false;
   }

   jnumberArray obj_scaling_j = env->NewNumberArray(1);
   jnumberArray x_scaling_j   = env->NewNumberArray(n);
   jnumberArray g_scaling_j   = env->NewNumberArray(m);

   jbooleanArray use_x_g_scaling_j = env->NewBooleanArray(2);

   env->CallBooleanMethod(solver, get_scaling_parameters_, obj_scaling_j, n, x_scaling_j, m, g_scaling_j, use_x_g_scaling_j);

   jboolean* use_x_g_scaling = env->GetBooleanArrayElements(use_x_g_scaling_j, 0);

   /* Copy from Java to native value */
   env->GetNumberArrayRegion(obj_scaling_j, 0, 1, &obj_scaling);

   /* Copy from Java to native value */
   if( use_x_g_scaling[0] )
   {
      env->GetNumberArrayRegion(x_scaling_j, 0, n, x_scaling);
      use_x_scaling = true;
   }
   else
   {
      use_x_scaling = false;
   }

   /* Copy from Java to native value */
   if( use_x_g_scaling[1] )
   {
      env->GetNumberArrayRegion(g_scaling_j, 0, n, g_scaling);
      use_g_scaling = true;
   }
   else
   {
      use_g_scaling = false;
   }

   env->ReleaseBooleanArrayElements(use_x_g_scaling_j, use_x_g_scaling, 0);

   return true;
}

Index Jipopt::get_number_of_nonlinear_variables()
{
   if( using_LBFGS )
   {
      return env->CallIntMethod(solver, get_number_of_nonlinear_variables_);
   }

   return -1;
}

bool Jipopt::get_list_of_nonlinear_variables(
   Index  num_nonlin_vars,
   Index* pos_nonlin_vars)
{
   if( !using_LBFGS )
   {
      return false;
   }

   jintArray pos_nonlin_vars_j = env->NewIntArray(num_nonlin_vars);

   if( !env->CallBooleanMethod(solver, get_list_of_nonlinear_variables_, num_nonlin_vars, pos_nonlin_vars_j) )
   {
      return false;
   }

   if( pos_nonlin_vars != NULL )
   {
      if( sizeof(jint) == sizeof(Index) )
      {
         env->GetIntArrayRegion(pos_nonlin_vars_j, 0, num_nonlin_vars, reinterpret_cast<jint*>(pos_nonlin_vars));
      }
      else
      {
         jint* tmp = new jint[num_nonlin_vars];

         env->GetIntArrayRegion(pos_nonlin_vars_j, 0, num_nonlin_vars, tmp);
         for( Index i = 0; i < num_nonlin_vars; ++i )
         {
            pos_nonlin_vars[i] = (Index) tmp[i];
         }

         delete[] tmp;
      }
   }

   return true;
}

extern "C"
{

   JNIEXPORT jlong JNICALL Java_org_coinor_Ipopt_CreateIpoptProblem(
      JNIEnv* env,
      jobject obj_this,
      jint    n,
      jint    m,
      jint    nele_jac,
      jint    nele_hess,
      jint    index_style)
   {
      /* create the smart pointer to the Ipopt problem */
      SmartPtr<Jipopt>* pproblem = new SmartPtr<Jipopt>;

      /* create the IpoptProblem */
      *pproblem = new Jipopt(env, obj_this, n, m, nele_jac, nele_hess, index_style);

      /* return the smart pointer to our class */
      return (jlong) pproblem;
   }

   JNIEXPORT jint JNICALL Java_org_coinor_Ipopt_OptimizeTNLP(
      JNIEnv*      env,
      jobject      obj_this,
      jlong        pipopt,
      jnumberArray xj,
      jnumberArray gj,
      jnumberArray obj_valj,
      jnumberArray mult_gj,
      jnumberArray mult_x_Lj,
      jnumberArray mult_x_Uj,
      jnumberArray callback_grad_f,
      jnumberArray callback_jac_g,
      jnumberArray callback_hess)
   {
      Jipopt* problem = GetRawPtr(*(SmartPtr<Jipopt>*) pipopt);

      problem->env       = env;
      problem->solver    = obj_this;

      problem->xj        = xj;
      problem->gj        = gj;
      problem->fj        = obj_valj;
      problem->mult_gj   = mult_gj;
      problem->mult_x_Lj = mult_x_Lj;
      problem->mult_x_Uj = mult_x_Uj;

      problem->grad_fj   = callback_grad_f;
      problem->jac_gj    = callback_jac_g;
      problem->hessj     = callback_hess;

      ApplicationReturnStatus status;

      status = problem->application->Initialize();

      if( status != Solve_Succeeded )
      {
         printf("\n\n*** Error during initialization!\n");
         return (int) status;
      }

      /* solve the problem */
      status = problem->application->OptimizeTNLP(problem);

      return (jint) status;
   }

   JNIEXPORT jboolean JNICALL Java_org_coinor_Ipopt_GetCurrIterate(
      JNIEnv*      env,
      jobject      /* obj_this */,
      jlong        pipopt,
      jlong        jip_data,
      jlong        jip_cq,
      jboolean     jscaled,
      jint         jn,
      jnumberArray jx,
      jnumberArray jz_L,
      jnumberArray jz_U,
      jint         jm,
      jnumberArray jg,
      jnumberArray jlambda)
   {
      Jipopt* problem = GetRawPtr(*(SmartPtr<Jipopt>*) pipopt);
      IpoptData* ip_data = (IpoptData*)jip_data;
      IpoptCalculatedQuantities* ip_cq = (IpoptCalculatedQuantities*)jip_cq;

      Index n = jn;
      Index m = jm;

      Number* x = NULL;
      Number* z_L = NULL;
      Number* z_U = NULL;
      Number* g = NULL;
      Number* lambda = NULL;

      if( jx != NULL )
      {
         x = new Number[n];
      }
      if( jz_L != NULL )
      {
         z_L = new Number[n];
      }
      if( jz_U != NULL )
      {
         z_U = new Number[n];
      }
      if( jg != NULL )
      {
         g = new Number[m];
      }
      if( jlambda != NULL )
      {
         lambda = new Number[m];
      }

      bool ok = problem->get_curr_iterate(ip_data, ip_cq, jscaled, n, x, z_L, z_U, m, g, lambda);
      if( ok )
      {
         if( jx != NULL )
         {
            env->SetNumberArrayRegion(jx, 0, n, const_cast<Number*>(x));
         }
         if( jz_L != NULL )
         {
            env->SetNumberArrayRegion(jz_L, 0, n, const_cast<Number*>(z_L));
         }
         if( jz_U != NULL )
         {
            env->SetNumberArrayRegion(jz_U, 0, n, const_cast<Number*>(z_U));
         }
         if( jg != NULL )
         {
            env->SetNumberArrayRegion(jg, 0, m, const_cast<Number*>(g));
         }
         if( jlambda != NULL )
         {
            env->SetNumberArrayRegion(jlambda, 0, m, const_cast<Number*>(lambda));
         }
      }

      delete[] lambda;
      delete[] g;
      delete[] z_U;
      delete[] z_L;
      delete[] x;

      return ok;
   }

   JNIEXPORT jboolean JNICALL Java_org_coinor_Ipopt_GetCurrViolations(
      JNIEnv*      env,
      jobject      /* obj_this */,
      jlong        pipopt,
      jlong        jip_data,
      jlong        jip_cq,
      jboolean     jscaled,
      jint         jn,
      jnumberArray jx_L_violation,
      jnumberArray jx_U_violation,
      jnumberArray jcompl_x_L,
      jnumberArray jcompl_x_U,
      jnumberArray jgrad_lag_x,
      jint         jm,
      jnumberArray jnlp_constraint_violation,
      jnumberArray jcompl_g)
   {
      Jipopt* problem = GetRawPtr(*(SmartPtr<Jipopt>*) pipopt);
      IpoptData* ip_data = (IpoptData*)jip_data;
      IpoptCalculatedQuantities* ip_cq = (IpoptCalculatedQuantities*)jip_cq;

      Index n = jn;
      Index m = jm;

      Number* x_L_violation = NULL;
      Number* x_U_violation = NULL;
      Number* compl_x_L = NULL;
      Number* compl_x_U = NULL;
      Number* grad_lag_x = NULL;
      Number* nlp_constraint_violation = NULL;
      Number* compl_g = NULL;

      if( jx_L_violation != NULL )
      {
         x_L_violation = new Number[n];
      }
      if( jx_U_violation != NULL )
      {
         x_U_violation = new Number[n];
      }
      if( jcompl_x_L != NULL )
      {
         compl_x_L = new Number[n];
      }
      if( jcompl_x_U != NULL )
      {
         compl_x_U = new Number[n];
      }
      if( jgrad_lag_x != NULL )
      {
         grad_lag_x = new Number[n];
      }
      if( jnlp_constraint_violation != NULL )
      {
         nlp_constraint_violation = new Number[m];
      }
      if( jcompl_g != NULL )
      {
         compl_g = new Number[m];
      }

      bool ok = problem->get_curr_violations(ip_data, ip_cq, jscaled, n, x_L_violation, x_U_violation, compl_x_L, compl_x_U, grad_lag_x, m, nlp_constraint_violation, compl_g);
      if( ok )
      {
         if( jx_L_violation != NULL )
         {
            env->SetNumberArrayRegion(jx_L_violation, 0, n, const_cast<Number*>(x_L_violation));
         }
         if( jx_U_violation != NULL )
         {
            env->SetNumberArrayRegion(jx_U_violation, 0, n, const_cast<Number*>(x_U_violation));
         }
         if( jcompl_x_L != NULL )
         {
            env->SetNumberArrayRegion(jcompl_x_L, 0, n, const_cast<Number*>(compl_x_L));
         }
         if( jcompl_x_U != NULL )
         {
            env->SetNumberArrayRegion(jcompl_x_U, 0, n, const_cast<Number*>(compl_x_U));
         }
         if( jgrad_lag_x != NULL )
         {
            env->SetNumberArrayRegion(jgrad_lag_x, 0, n, const_cast<Number*>(grad_lag_x));
         }
         if( jnlp_constraint_violation != NULL )
         {
            env->SetNumberArrayRegion(jnlp_constraint_violation, 0, m, const_cast<Number*>(nlp_constraint_violation));
         }
         if( jcompl_g != NULL )
         {
            env->SetNumberArrayRegion(jcompl_g, 0, m, const_cast<Number*>(compl_g));
         }
      }

      delete[] compl_g;
      delete[] nlp_constraint_violation;
      delete[] grad_lag_x;
      delete[] compl_x_U;
      delete[] compl_x_L;
      delete[] x_U_violation;
      delete[] x_L_violation;

      return ok;
   }

   JNIEXPORT void JNICALL Java_org_coinor_Ipopt_FreeIpoptProblem(
      JNIEnv* /*env*/,
      jobject /*obj_this*/,
      jlong   pipopt
   )
   {
      SmartPtr<Jipopt>* pproblem = (SmartPtr<Jipopt>*)pipopt;

      if( pproblem != NULL && IsValid(*pproblem) )
      {
         /* if OptimizeTNLP has been called, the application holds a SmartPtr to our problem class
          * to resolve this circular dependency we first free the application explicitly
          */
         (*pproblem)->application = NULL;

         /* now free our JIpopt itself */
         *pproblem = NULL;
      }
   }

   JNIEXPORT jboolean JNICALL Java_org_coinor_Ipopt_AddIpoptIntOption(
      JNIEnv*  env,
      jobject  /*obj_this*/,
      jlong    pipopt,
      jstring  jparname,
      jint     jparvalue
   )
   {
      Jipopt* problem = GetRawPtr(*(SmartPtr<Jipopt>*) pipopt);

      const char* pparameterName = env->GetStringUTFChars(jparname, 0);
      string parameterName = pparameterName;

      // Try to apply the integer option
      jboolean ret = problem->application->Options()->SetIntegerValue(parameterName, jparvalue);

      env->ReleaseStringUTFChars(jparname, pparameterName);

      return ret;
   }

   JNIEXPORT jboolean JNICALL Java_org_coinor_Ipopt_AddIpoptNumOption(
      JNIEnv* env,
      jobject /*obj_this*/,
      jlong   pipopt,
      jstring jparname,
      jnumber jparvalue
   )
   {
      Jipopt* problem = GetRawPtr(*(SmartPtr<Jipopt>*) pipopt);

      const char* pparameterName = env->GetStringUTFChars(jparname, 0);
      string parameterName = pparameterName;

      // Try to set the real option
      jboolean ret = problem->application->Options()->SetNumericValue(parameterName, jparvalue);

      env->ReleaseStringUTFChars(jparname, pparameterName);

      return ret;
   }

   JNIEXPORT jboolean JNICALL Java_org_coinor_Ipopt_AddIpoptStrOption(
      JNIEnv* env,
      jobject /*obj_this*/,
      jlong   pipopt,
      jstring jparname,
      jstring jparvalue
   )
   {
      Jipopt* problem = GetRawPtr(*(SmartPtr<Jipopt>*) pipopt);

      const char* pparameterName = env->GetStringUTFChars(jparname, NULL);
      string parameterName = pparameterName;
      const char* pparameterValue = env->GetStringUTFChars(jparvalue, NULL);
      string parameterValue = pparameterValue;

      // parameterValue has been changed to LowerCase in Java!
      if( parameterName == "hessian_approximation" && parameterValue == "limited-memory" )
      {
         problem->using_LBFGS = true;
      }
      else if( parameterName == "nlp_scaling_method" && parameterValue == "user-scaling" )
      {
         problem->using_scaling_parameters = true;
      }

      // Try to apply the string option
      jboolean ret = problem->application->Options()->SetStringValue(parameterName, parameterValue);

      env->ReleaseStringUTFChars(jparname, pparameterName);
      env->ReleaseStringUTFChars(jparname, pparameterValue);

      return ret;
   }

} // extern "C"