/* * 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; }
/* 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]; } }
/** * @brief Evaluates the transformation. */ static void transform_vars_affine_evaluate(coco_problem_t *problem, const double *x, double *y) { size_t i, j; transform_vars_affine_data_t *data; coco_problem_t *inner_problem; if (coco_vector_contains_nan(x, coco_problem_get_dimension(problem))) { coco_vector_set_to_nan(y, coco_problem_get_number_of_objectives(problem)); return; } data = (transform_vars_affine_data_t *) coco_problem_transformed_get_data(problem); inner_problem = coco_problem_transformed_get_inner_problem(problem); for (i = 0; i < inner_problem->number_of_variables; ++i) { /* data->M has problem->number_of_variables columns and inner_problem->number_of_variables rows. */ const double *current_row = data->M + i * problem->number_of_variables; data->x[i] = data->b[i]; for (j = 0; j < problem->number_of_variables; ++j) { data->x[i] += x[j] * current_row[j]; } } coco_evaluate_function(inner_problem, data->x, y); assert(y[0] + 1e-13 >= problem->best_value[0]); }
/** * Return a problem that stacks the output of two problems, namely * of coco_evaluate_function and coco_evaluate_constraint. The accepted * input remains the same and must be identical for the stacked * problems. * * This is particularly useful to generate multiobjective problems, * e.g. a biobjective problem from two single objective problems. * * Details: regions of interest must either agree or at least one * of them must be NULL. Best parameter becomes somewhat meaningless. */ coco_problem_t *coco_stacked_problem_allocate(coco_problem_t *problem1, coco_problem_t *problem2, void *userdata, coco_stacked_problem_free_data_t free_data) { const size_t number_of_variables = coco_problem_get_dimension(problem1); const size_t number_of_objectives = coco_problem_get_number_of_objectives(problem1) + coco_problem_get_number_of_objectives(problem2); const size_t number_of_constraints = coco_problem_get_number_of_constraints(problem1) + coco_problem_get_number_of_constraints(problem2); size_t i; char *s; const double *smallest, *largest; coco_stacked_problem_data_t *data; coco_problem_t *problem; /* the new coco problem */ assert(coco_problem_get_dimension(problem1) == coco_problem_get_dimension(problem2)); problem = coco_problem_allocate(number_of_variables, number_of_objectives, number_of_constraints); s = coco_strconcat(coco_problem_get_id(problem1), "__"); problem->problem_id = coco_strconcat(s, coco_problem_get_id(problem2)); coco_free_memory(s); s = coco_strconcat(coco_problem_get_name(problem1), " + "); problem->problem_name = coco_strconcat(s, coco_problem_get_name(problem2)); coco_free_memory(s); problem->evaluate_function = coco_stacked_problem_evaluate; if (number_of_constraints > 0) problem->evaluate_constraint = coco_stacked_problem_evaluate_constraint; /* set/copy "boundaries" and best_parameter */ smallest = problem1->smallest_values_of_interest; if (smallest == NULL) smallest = problem2->smallest_values_of_interest; largest = problem1->largest_values_of_interest; if (largest == NULL) largest = problem2->largest_values_of_interest; for (i = 0; i < number_of_variables; ++i) { if (problem2->smallest_values_of_interest != NULL) assert(smallest[i] == problem2->smallest_values_of_interest[i]); if (problem2->largest_values_of_interest != NULL) assert(largest[i] == problem2->largest_values_of_interest[i]); if (smallest != NULL) problem->smallest_values_of_interest[i] = smallest[i]; if (largest != NULL) problem->largest_values_of_interest[i] = largest[i]; if (problem->best_parameter) /* bbob2009 logger doesn't work then anymore */ coco_free_memory(problem->best_parameter); problem->best_parameter = NULL; if (problem->best_value) coco_free_memory(problem->best_value); problem->best_value = NULL; /* bbob2009 logger doesn't work */ } /* setup data holder */ data = coco_allocate_memory(sizeof(*data)); data->problem1 = problem1; data->problem2 = problem2; data->data = userdata; data->free_data = free_data; problem->data = data; problem->free_problem = coco_stacked_problem_free; /* free self->data and coco_problem_free(self) */ return problem; }