-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCocoJNI.c
More file actions
554 lines (447 loc) · 17.4 KB
/
CocoJNI.c
File metadata and controls
554 lines (447 loc) · 17.4 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
/**
* This file contains all necessary interfaces to the COCO code in C. The structures coco_problem_s,
* coco_suite_s and coco_observer_s are accessed by means of "pointers" of type long.
*
* TODO: Check if the casts from pointer to C structure actually work (how can this be done?)
*/
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <jni.h>
#include "coco.h"
#include "coco.c"
#include "CocoJNI.h"
/*
* Class: CocoJNI
* Method: cocoSetLogLevel
* Signature: (Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_CocoJNI_cocoSetLogLevel
(JNIEnv *jenv, jclass interface_cls, jstring jlog_level) {
const char *log_level;
/* This test is both to prevent warning because interface_cls was not used and to check for exceptions */
if (interface_cls == NULL) {
jclass Exception = (*jenv)->FindClass(jenv, "java/lang/Exception");
(*jenv)->ThrowNew(jenv, Exception, "Exception in cocoGetObserver\n");
}
log_level = (*jenv)->GetStringUTFChars(jenv, jlog_level, NULL);
coco_set_log_level(log_level);
return;
}
/*
* Class: CocoJNI
* Method: cocoGetObserver
* Signature: (Ljava/lang/String;Ljava/lang/String;)J
*/
JNIEXPORT jlong JNICALL Java_CocoJNI_cocoGetObserver
(JNIEnv *jenv, jclass interface_cls, jstring jobserver_name, jstring jobserver_options) {
coco_observer_t *observer = NULL;
const char *observer_name;
const char *observer_options;
/* This test is both to prevent warning because interface_cls was not used and to check for exceptions */
if (interface_cls == NULL) {
jclass Exception = (*jenv)->FindClass(jenv, "java/lang/Exception");
(*jenv)->ThrowNew(jenv, Exception, "Exception in cocoGetObserver\n");
}
observer_name = (*jenv)->GetStringUTFChars(jenv, jobserver_name, NULL);
observer_options = (*jenv)->GetStringUTFChars(jenv, jobserver_options, NULL);
observer = coco_observer(observer_name, observer_options);
return (jlong) observer;
}
/*
* Class: CocoJNI
* Method: cocoProblemAddObserver
* Signature: (JJ)J
*/
JNIEXPORT jlong JNICALL Java_CocoJNI_cocoProblemAddObserver
(JNIEnv *jenv, jclass interface_cls, jlong jproblem_pointer, jlong jobserver_pointer) {
coco_observer_t *observer = NULL;
coco_problem_t *problem_before = NULL;
coco_problem_t *problem_after = NULL;
/* This test is both to prevent warning because interface_cls was not used and to check for exceptions */
if (interface_cls == NULL) {
jclass Exception = (*jenv)->FindClass(jenv, "java/lang/Exception");
(*jenv)->ThrowNew(jenv, Exception, "Exception in cocoProblemAddObserver\n");
}
observer = (coco_observer_t *) jobserver_pointer;
problem_before = (coco_problem_t *) jproblem_pointer;
problem_after = coco_problem_add_observer(problem_before, observer);
return (jlong) problem_after;
}
/*
* Class: CocoJNI
* Method: cocoProblemRemoveObserver
* Signature: (JJ)J
*/
JNIEXPORT jlong JNICALL Java_CocoJNI_cocoProblemRemoveObserver
(JNIEnv *jenv, jclass interface_cls, jlong jproblem_pointer, jlong jobserver_pointer) {
coco_observer_t *observer = NULL;
coco_problem_t *problem_before = NULL;
coco_problem_t *problem_after = NULL;
/* This test is both to prevent warning because interface_cls was not used and to check for exceptions */
if (interface_cls == NULL) {
jclass Exception = (*jenv)->FindClass(jenv, "java/lang/Exception");
(*jenv)->ThrowNew(jenv, Exception, "Exception in cocoProblemRemoveObserver\n");
}
observer = (coco_observer_t *) jobserver_pointer;
problem_before = (coco_problem_t *) jproblem_pointer;
problem_after = coco_problem_remove_observer(problem_before, observer);
return (jlong) problem_after;
}
/*
* Class: CocoJNI
* Method: cocoFinalizeObserver
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_CocoJNI_cocoFinalizeObserver
(JNIEnv *jenv, jclass interface_cls, jlong jobserver_pointer) {
coco_observer_t *observer = NULL;
/* This test is both to prevent warning because interface_cls was not used and to check for exceptions */
if (interface_cls == NULL) {
jclass Exception = (*jenv)->FindClass(jenv, "java/lang/Exception");
(*jenv)->ThrowNew(jenv, Exception, "Exception in cocoFinalizeObserver\n");
}
observer = (coco_observer_t *) jobserver_pointer;
coco_observer_free(observer);
return;
}
/*
* Class: CocoJNI
* Method: cocoGetSuite
* Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)J
*/
JNIEXPORT jlong JNICALL Java_CocoJNI_cocoGetSuite
(JNIEnv *jenv, jclass interface_cls, jstring jsuite_name, jstring jsuite_instance, jstring jsuite_options) {
coco_suite_t *suite = NULL;
const char *suite_name;
const char *suite_instance;
const char *suite_options;
/* This test is both to prevent warning because interface_cls was not used and to check for exceptions */
if (interface_cls == NULL) {
jclass Exception = (*jenv)->FindClass(jenv, "java/lang/Exception");
(*jenv)->ThrowNew(jenv, Exception, "Exception in cocoGetSuite\n");
}
suite_name = (*jenv)->GetStringUTFChars(jenv, jsuite_name, NULL);
suite_instance = (*jenv)->GetStringUTFChars(jenv, jsuite_instance, NULL);
suite_options = (*jenv)->GetStringUTFChars(jenv, jsuite_options, NULL);
suite = coco_suite(suite_name, suite_instance, suite_options);
return (jlong) suite;
}
/*
* Class: CocoJNI
* Method: cocoFinalizeSuite
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_CocoJNI_cocoFinalizeSuite
(JNIEnv *jenv, jclass interface_cls, jlong jsuite_pointer) {
coco_suite_t *suite = NULL;
/* This test is both to prevent warning because interface_cls was not used and to check for exceptions */
if (interface_cls == NULL) {
jclass Exception = (*jenv)->FindClass(jenv, "java/lang/Exception");
(*jenv)->ThrowNew(jenv, Exception, "Exception in cocoFinalizeSuite\n");
}
suite = (coco_suite_t *) jsuite_pointer;
coco_suite_free(suite);
return;
}
/*
* Class: CocoJNI
* Method: cocoSuiteGetNextProblem
* Signature: (JJ)J
*/
JNIEXPORT jlong JNICALL Java_CocoJNI_cocoSuiteGetNextProblem
(JNIEnv *jenv, jclass interface_cls, jlong jsuite_pointer, jlong jobserver_pointer) {
coco_problem_t *problem = NULL;
coco_suite_t *suite = NULL;
coco_observer_t *observer = NULL;
/* This test is both to prevent warning because interface_cls was not used and to check for exceptions */
if (interface_cls == NULL) {
jclass Exception = (*jenv)->FindClass(jenv, "java/lang/Exception");
(*jenv)->ThrowNew(jenv, Exception, "Exception in cocoSuiteGetNextProblem\n");
}
suite = (coco_suite_t *) jsuite_pointer;
observer = (coco_observer_t *) jobserver_pointer;
problem = coco_suite_get_next_problem(suite, observer);
if (problem == NULL)
return 0;
return (jlong) problem;
}
/*
* Class: CocoJNI
* Method: cocoSuiteGetProblem
* Signature: (JJ)J
*/
JNIEXPORT jlong JNICALL Java_CocoJNI_cocoSuiteGetProblem
(JNIEnv *jenv, jclass interface_cls, jlong jsuite_pointer, jlong jproblem_index) {
coco_problem_t *problem = NULL;
coco_suite_t *suite = NULL;
/* This test is both to prevent warning because interface_cls was not used and to check for exceptions */
if (interface_cls == NULL) {
jclass Exception = (*jenv)->FindClass(jenv, "java/lang/Exception");
(*jenv)->ThrowNew(jenv, Exception, "Exception in cocoSuiteGetProblem\n");
}
suite = (coco_suite_t *) jsuite_pointer;
problem = coco_suite_get_problem(suite, jproblem_index);
if (problem == NULL)
return 0;
return (jlong) problem;
}
/*
* Class: CocoJNI
* Method: cocoEvaluateFunction
* Signature: (J[D)[D
*/
JNIEXPORT jdoubleArray JNICALL Java_CocoJNI_cocoEvaluateFunction
(JNIEnv *jenv, jclass interface_cls, jlong jproblem_pointer, jdoubleArray jx) {
coco_problem_t *problem = NULL;
double *y = NULL;
double *x = NULL;
int number_of_objectives;
jdoubleArray jy;
/* This test is both to prevent warning because interface_cls was not used and to check for exceptions */
if (interface_cls == NULL) {
jclass Exception = (*jenv)->FindClass(jenv, "java/lang/Exception");
(*jenv)->ThrowNew(jenv, Exception, "Exception in cocoEvaluateFunction\n");
}
problem = (coco_problem_t *) jproblem_pointer;
number_of_objectives = (int) coco_problem_get_number_of_objectives(problem);
/* Call coco_evaluate_function */
x = (*jenv)->GetDoubleArrayElements(jenv, jx, NULL);
y = coco_allocate_vector(number_of_objectives);
coco_evaluate_function(problem, x, y);
/* Prepare the return value */
jy = (*jenv)->NewDoubleArray(jenv, number_of_objectives);
(*jenv)->SetDoubleArrayRegion(jenv, jy, 0, number_of_objectives, y);
/* Free resources */
coco_free_memory(y);
(*jenv)->ReleaseDoubleArrayElements(jenv, jx, x, JNI_ABORT);
return jy;
}
/*
* Class: CocoJNI
* Method: cocoEvaluateConstraint
* Signature: (J[D)[D
*/
JNIEXPORT jdoubleArray JNICALL Java_CocoJNI_cocoEvaluateConstraint
(JNIEnv *jenv, jclass interface_cls, jlong jproblem_pointer, jdoubleArray jx) {
coco_problem_t *problem = NULL;
double *y = NULL;
double *x = NULL;
int number_of_objectives;
jdoubleArray jy;
/* This test is both to prevent warning because interface_cls was not used and to check for exceptions */
if (interface_cls == NULL) {
jclass Exception = (*jenv)->FindClass(jenv, "java/lang/Exception");
(*jenv)->ThrowNew(jenv, Exception, "Exception in cocoEvaluateConstraint\n");
}
problem = (coco_problem_t *) jproblem_pointer;
number_of_objectives = (int) coco_problem_get_number_of_objectives(problem);
/* Call coco_evaluate_constraint */
x = (*jenv)->GetDoubleArrayElements(jenv, jx, NULL);
y = coco_allocate_vector(number_of_objectives);
coco_evaluate_constraint(problem, x, y);
/* Prepare the return value */
jy = (*jenv)->NewDoubleArray(jenv, number_of_objectives);
(*jenv)->SetDoubleArrayRegion(jenv, jy, 0, number_of_objectives, y);
/* Free resources */
coco_free_memory(y);
(*jenv)->ReleaseDoubleArrayElements(jenv, jx, x, JNI_ABORT);
return jy;
}
/*
* Class: CocoJNI
* Method: cocoProblemGetDimension
* Signature: (J)I
*/
JNIEXPORT jint JNICALL Java_CocoJNI_cocoProblemGetDimension
(JNIEnv *jenv, jclass interface_cls, jlong jproblem_pointer) {
coco_problem_t *problem = NULL;
jint jresult;
/* This test is both to prevent warning because interface_cls was not used and to check for exceptions */
if (interface_cls == NULL) {
jclass Exception = (*jenv)->FindClass(jenv, "java/lang/Exception");
(*jenv)->ThrowNew(jenv, Exception, "Exception in cocoProblemGetDimension\n");
}
problem = (coco_problem_t *) jproblem_pointer;
jresult = (jint) coco_problem_get_dimension(problem);
return jresult;
}
/*
* Class: CocoJNI
* Method: cocoProblemGetNumberOfObjectives
* Signature: (J)I
*/
JNIEXPORT jint JNICALL Java_CocoJNI_cocoProblemGetNumberOfObjectives
(JNIEnv *jenv, jclass interface_cls, jlong jproblem_pointer) {
coco_problem_t *problem = NULL;
jint jresult;
/* This test is both to prevent warning because interface_cls was not used and to check for exceptions */
if (interface_cls == NULL) {
jclass Exception = (*jenv)->FindClass(jenv, "java/lang/Exception");
(*jenv)->ThrowNew(jenv, Exception, "Exception in cocoProblemGetNumberOfObjectives\n");
}
problem = (coco_problem_t *) jproblem_pointer;
jresult = (jint) coco_problem_get_number_of_objectives(problem);
return jresult;
}
/*
* Class: CocoJNI
* Method: cocoProblemGetNumberOfConstraints
* Signature: (J)I
*/
JNIEXPORT jint JNICALL Java_CocoJNI_cocoProblemGetNumberOfConstraints
(JNIEnv *jenv, jclass interface_cls, jlong jproblem_pointer) {
coco_problem_t *problem = NULL;
jint jresult;
/* This test is both to prevent warning because interface_cls was not used and to check for exceptions */
if (interface_cls == NULL) {
jclass Exception = (*jenv)->FindClass(jenv, "java/lang/Exception");
(*jenv)->ThrowNew(jenv, Exception, "Exception in cocoProblemGetNumberOfConstraints\n");
}
problem = (coco_problem_t *) jproblem_pointer;
jresult = (jint) coco_problem_get_number_of_constraints(problem);
return jresult;
}
/*
* Class: CocoJNI
* Method: cocoProblemGetSmallestValuesOfInterest
* Signature: (J)[D
*/
JNIEXPORT jdoubleArray JNICALL Java_CocoJNI_cocoProblemGetSmallestValuesOfInterest
(JNIEnv *jenv, jclass interface_cls, jlong jproblem_pointer) {
coco_problem_t *problem = NULL;
const double *result;
jdoubleArray jresult;
jint dimension;
/* This test is both to prevent warning because interface_cls was not used and to check for exceptions */
if (interface_cls == NULL) {
jclass Exception = (*jenv)->FindClass(jenv, "java/lang/Exception");
(*jenv)->ThrowNew(jenv, Exception, "Exception in cocoProblemGetSmallestValuesOfInterest\n");
}
problem = (coco_problem_t *) jproblem_pointer;
dimension = (int) coco_problem_get_dimension(problem);
result = coco_problem_get_smallest_values_of_interest(problem);
/* Prepare the return value */
jresult = (*jenv)->NewDoubleArray(jenv, dimension);
(*jenv)->SetDoubleArrayRegion(jenv, jresult, 0, dimension, result);
return jresult;
}
/*
* Class: CocoJNI
* Method: cocoProblemGetLargestValuesOfInterest
* Signature: (J)[D
*/
JNIEXPORT jdoubleArray JNICALL Java_CocoJNI_cocoProblemGetLargestValuesOfInterest
(JNIEnv *jenv, jclass interface_cls, jlong jproblem_pointer) {
coco_problem_t *problem = NULL;
const double *result;
jdoubleArray jresult;
jint dimension;
/* This test is both to prevent warning because interface_cls was not used and to check for exceptions */
if (interface_cls == NULL) {
jclass Exception = (*jenv)->FindClass(jenv, "java/lang/Exception");
(*jenv)->ThrowNew(jenv, Exception, "Exception in cocoProblemGetLargestValuesOfInterest\n");
}
problem = (coco_problem_t *) jproblem_pointer;
dimension = (int) coco_problem_get_dimension(problem);
result = coco_problem_get_largest_values_of_interest(problem);
/* Prepare the return value */
jresult = (*jenv)->NewDoubleArray(jenv, dimension);
(*jenv)->SetDoubleArrayRegion(jenv, jresult, 0, dimension, result);
return jresult;
}
/*
* Class: CocoJNI
* Method: cocoProblemGetId
* Signature: (J)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_CocoJNI_cocoProblemGetId
(JNIEnv *jenv, jclass interface_cls, jlong jproblem_pointer) {
coco_problem_t *problem = NULL;
const char *result;
jstring jresult;
/* This test is both to prevent warning because interface_cls was not used and to check for exceptions */
if (interface_cls == NULL) {
jclass Exception = (*jenv)->FindClass(jenv, "java/lang/Exception");
(*jenv)->ThrowNew(jenv, Exception, "Exception in cocoProblemGetId\n");
}
problem = (coco_problem_t *) jproblem_pointer;
result = coco_problem_get_id(problem);
jresult = (*jenv)->NewStringUTF(jenv, result);
return jresult;
}
/*
* Class: CocoJNI
* Method: cocoProblemGetName
* Signature: (J)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_CocoJNI_cocoProblemGetName
(JNIEnv *jenv, jclass interface_cls, jlong jproblem_pointer) {
coco_problem_t *problem = NULL;
const char *result;
jstring jresult;
/* This test is both to prevent warning because interface_cls was not used and to check for exceptions */
if (interface_cls == NULL) {
jclass Exception = (*jenv)->FindClass(jenv, "java/lang/Exception");
(*jenv)->ThrowNew(jenv, Exception, "Exception in cocoProblemGetName\n");
}
problem = (coco_problem_t *) jproblem_pointer;
result = coco_problem_get_name(problem);
jresult = (*jenv)->NewStringUTF(jenv, result);
return jresult;
}
/*
* Class: CocoJNI
* Method: cocoProblemGetEvaluations
* Signature: (J)J
*/
JNIEXPORT jlong JNICALL Java_CocoJNI_cocoProblemGetEvaluations
(JNIEnv *jenv, jclass interface_cls, jlong jproblem_pointer) {
coco_problem_t *problem = NULL;
jlong jresult;
/* This test is both to prevent warning because interface_cls was not used and to check for exceptions */
if (interface_cls == NULL) {
jclass Exception = (*jenv)->FindClass(jenv, "java/lang/Exception");
(*jenv)->ThrowNew(jenv, Exception, "Exception in cocoProblemGetIndex\n");
}
problem = (coco_problem_t *) jproblem_pointer;
jresult = (jlong) coco_problem_get_evaluations(problem);
return jresult;
}
/*
* Class: CocoJNI
* Method: cocoProblemGetIndex
* Signature: (J)J
*/
JNIEXPORT jlong JNICALL Java_CocoJNI_cocoProblemGetIndex
(JNIEnv *jenv, jclass interface_cls, jlong jproblem_pointer) {
coco_problem_t *problem = NULL;
jlong jresult;
/* This test is both to prevent warning because interface_cls was not used and to check for exceptions */
if (interface_cls == NULL) {
jclass Exception = (*jenv)->FindClass(jenv, "java/lang/Exception");
(*jenv)->ThrowNew(jenv, Exception, "Exception in cocoProblemGetIndex\n");
}
problem = (coco_problem_t *) jproblem_pointer;
jresult = (jlong) coco_problem_get_suite_dep_index(problem);
return jresult;
}
/*
* Class: CocoJNI
* Method: cocoProblemIsFinalTargetHit
* Signature: (J)I
*/
JNIEXPORT jint JNICALL Java_CocoJNI_cocoProblemIsFinalTargetHit
(JNIEnv *jenv, jclass interface_cls, jlong jproblem_pointer) {
coco_problem_t *problem = NULL;
jint jresult;
/* This test is both to prevent warning because interface_cls was not used and to check for exceptions */
if (interface_cls == NULL) {
jclass Exception = (*jenv)->FindClass(jenv, "java/lang/Exception");
(*jenv)->ThrowNew(jenv, Exception, "Exception in cocoProblemGetIndex\n");
}
problem = (coco_problem_t *) jproblem_pointer;
jresult = (jint) coco_problem_final_target_hit(problem);
return jresult;
}