/** * A random search optimizer. */ void my_optimizer(coco_problem_t *problem) { const size_t budget = 2; coco_random_state_t *rng = coco_random_new(0xdeadbeef); const double *lbounds = coco_problem_get_smallest_values_of_interest(problem); const double *ubounds = coco_problem_get_largest_values_of_interest(problem); size_t dimension = coco_problem_get_dimension(problem); size_t number_of_objectives = coco_problem_get_number_of_objectives(problem); double *x = coco_allocate_vector(dimension); double *y = coco_allocate_vector(number_of_objectives); double range; size_t i, j; for (i = 0; i < budget; ++i) { for (j = 0; j < dimension; ++j) { range = ubounds[j] - lbounds[j]; x[j] = lbounds[j] + coco_random_uniform(rng) * range; } coco_evaluate_function(problem, x, y); } coco_random_free(rng); coco_free_memory(x); coco_free_memory(y); }
/* * Class: CocoJNI * Method: cocoProblemGetSmallestValuesOfInterest * Signature: (J)[D */ JNIEXPORT jdoubleArray JNICALL Java_org_moeaframework_problem_BBOB2016_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; }
/* The gateway function */ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { size_t *ref; coco_problem_t *problem = NULL; size_t nb_dim, i; const double *res; double *v; /* intermediate variable that aloows to set plhs[0] */ /* check for proper number of arguments */ if(nrhs!=1) { mexErrMsgIdAndTxt("cocoProblemGetSmallestValuesOfInterest:nrhs","One input required."); } /* get the problem */ ref = (size_t *)mxGetData(prhs[0]); problem = (coco_problem_t *)(*ref); nb_dim = coco_problem_get_dimension(problem); plhs[0] = mxCreateDoubleMatrix(1, nb_dim, mxREAL); v = mxGetPr(plhs[0]); /* call coco_problem_get_smallest_values_of_interest(...) */ res = coco_problem_get_smallest_values_of_interest(problem); for (i = 0; i < nb_dim; i++){ v[i] = res[i]; } }