Пример #1
0
/* A test to see if multiple observers/loggers can be wrapped around a problem. */
void multiple_observers(void) {

  coco_suite_t *suite;
  coco_observer_t *observer_inner, *observer_middle, *observer_outer;
  coco_problem_t *problem_inner, *problem_middle, *problem_outer;

  suite = coco_suite("bbob-biobj", "year: 2016", "dimensions: 2 function_indices: 1-3 instance_indices: 1-2");

  observer_inner = coco_observer("toy", "");
  observer_middle = coco_observer("bbob-biobj", "log_nondominated: final log_decision_variables: none");
  observer_outer = coco_observer("toy", "");

  while ((problem_inner = coco_suite_get_next_problem(suite, observer_inner)) != NULL) {

    problem_middle = coco_problem_add_observer(problem_inner, observer_middle);
    problem_outer = coco_problem_add_observer(problem_middle, observer_outer);

    my_optimizer(problem_outer);

    problem_middle = coco_problem_remove_observer(problem_outer, observer_outer);
    problem_inner = coco_problem_remove_observer(problem_middle, observer_middle);
  }

  coco_observer_free(observer_inner);
  coco_observer_free(observer_middle);
  coco_observer_free(observer_outer);

  coco_suite_free(suite);
}
Пример #2
0
/* Each time: run the benchmark and delete the output folder */
void run_once(char *observer_options) {

  coco_suite_t *suite;
  coco_observer_t *observer;
  coco_problem_t *problem;

  printf("Running experiment with options %s\n", observer_options);
  fflush(stdout);

  suite = coco_suite("bbob-biobj", NULL, "dimensions: 2 function_indices: 5-6 instance_indices: 2-3");
  observer = coco_observer("bbob-biobj", observer_options);

  while ((problem = coco_suite_get_next_problem(suite, observer)) != NULL) {

    my_optimizer(problem);

  }

  coco_observer_free(observer);
  coco_suite_free(suite);

  wait_in_seconds(2); /* So that the directory removal is surely finished */

  printf("DONE!\n");
  fflush(stdout);
}
Пример #3
0
/**
 * The archive always contains the two extreme solutions
 */
coco_archive_t *coco_archive(const char *suite_name,
                             const size_t function,
                             const size_t dimension,
                             const size_t instance) {

  coco_archive_t *archive = coco_archive_allocate();
  int output_precision = 15;
  coco_suite_t *suite;
  char *suite_instance = coco_strdupf("instances: %lu", (unsigned long) instance);
  char *suite_options = coco_strdupf("dimensions: %lu function_indices: %lu",
  		(unsigned long) dimension, (unsigned long) function);
  coco_problem_t *problem;
  char *text;
  int update;

  suite = coco_suite(suite_name, suite_instance, suite_options);
  if (suite == NULL) {
    coco_error("coco_archive(): cannot create suite '%s'", suite_name);
    return NULL; /* Never reached */
  }
  problem = coco_suite_get_next_problem(suite, NULL);
  if (problem == NULL) {
    coco_error("coco_archive(): cannot create problem f%02lu_i%02lu_d%02lu in suite '%s'",
    		(unsigned long) function, (unsigned long) instance, (unsigned long) dimension, suite_name);
    return NULL; /* Never reached */
  }

  /* Store the ideal and nadir points */
  archive->ideal = coco_duplicate_vector(problem->best_value, 2);
  archive->nadir = coco_duplicate_vector(problem->nadir_value, 2);

  /* Add the extreme points to the archive */
  text = coco_strdupf("0\t%.*e\t%.*e\n", output_precision, archive->nadir[0], output_precision, archive->ideal[1]);
  update = coco_archive_add_solution(archive, archive->nadir[0], archive->ideal[1], text);
  coco_free_memory(text);
  assert(update == 1);

  text = coco_strdupf("0\t%.*e\t%.*e\n", output_precision, archive->ideal[0], output_precision, archive->nadir[1]);
  update = coco_archive_add_solution(archive, archive->ideal[0], archive->nadir[1], text);
  coco_free_memory(text);
  assert(update == 1);

  archive->extreme1 = archive->tree->head;
  archive->extreme2 = archive->tree->tail;
  assert(archive->extreme1 != archive->extreme2);

  coco_free_memory(suite_instance);
  coco_free_memory(suite_options);
  coco_suite_free(suite);

  return archive;
}
Пример #4
0
/**
 * Tests the function coco_suite_get_next_problem_index.
 */
static void test_coco_suite_encode_problem_index(void **state) {

  coco_suite_t *suite;
  size_t index;
  size_t function_idx = 13, dimension_idx = 0, instance_idx = 10;

  suite = coco_suite("bbob", NULL, NULL);

  expect_value(__wrap_coco_suite_encode_problem_index, function_idx, 13);
  expect_value(__wrap_coco_suite_encode_problem_index, dimension_idx, 0);
  expect_value(__wrap_coco_suite_encode_problem_index, instance_idx, 10);
  will_return(__wrap_coco_suite_encode_problem_index, 205);

  index = coco_suite_encode_problem_index(suite, function_idx, dimension_idx, instance_idx);

  assert_true(index == 205);

  coco_suite_free(suite);

  (void)state; /* unused */
}
/*
 * Class:     CocoJNI
 * Method:    cocoGetSuite
 * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)J
 */
JNIEXPORT jlong JNICALL Java_org_moeaframework_problem_BBOB2016_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;
}
Пример #6
0
int main(int argc, char **argv) {
  int header_shown = 0, number_of_failures = 0, shown_failures = 0;
  size_t number_of_testvectors = 0, i, j;
  int number_of_testcases = 0;
  testvector_t *testvectors = NULL;
  long previous_problem_index = -1;
  size_t problem_index_old, problem_index;
  int testvector_id, ret;
  coco_problem_t *problem = NULL;
  char suit_name[128];
  FILE *testfile = NULL;
  coco_suite_t *suite;

  if (argc != 2) {
    usage(argv[0]);
    goto err;
  }

  testfile = fopen(argv[1], "r");
  if (testfile == NULL) {
    fprintf(stderr, "Failed to open testcases file %s.\n", argv[1]);
    goto err;
  }

  ret = fscanf(testfile, "%127s", suit_name);
  if (ret != 1) {
    fprintf(stderr, "Failed to read suite name from testcases file.\n");
    goto err;
  }

  ret = fscanf(testfile, "%30lu", &number_of_testvectors);
  if (ret != 1) {
    fprintf(stderr,
            "Failed to read number of test vectors from testcases file.\n");
    goto err;
  }

  testvectors = malloc(number_of_testvectors * sizeof(*testvectors));
  if (NULL == testvectors) {
    fprintf(stderr, "Failed to allocate memory for testvectors.\n");
    goto err;
  }

  for (i = 0; i < number_of_testvectors; ++i) {
    for (j = 0; j < 40; ++j) {
      ret = fscanf(testfile, "%30lf", &testvectors[i].x[j]);
      if (ret != 1) {
        fprintf(stderr, "ERROR: Failed to parse testvector %lu element %lu.\n",
        		(unsigned long) i + 1, (unsigned long) j + 1);
      }
    }
  }

  suite = coco_suite("bbob", "instances: 1-15", NULL);

  while (1) {
    double expected_value, *x, y;
    ret = fscanf(testfile, "%30lu\t%30lu\t%30i\t%30lf", &problem_index_old, &problem_index, &testvector_id,
                 &expected_value);
    if (ret != 4)
      break;
    ++number_of_testcases;
    /* We cache the problem object to save time. Instantiating
     * some functions is expensive because we have to generate
     * large rotation matrices.
     */
    if (previous_problem_index != (long) problem_index) {
      if (NULL != problem)
        coco_problem_free(problem);
      if (problem_index > coco_suite_get_number_of_problems(suite) - 1) {
        fprintf(stdout, "problem index = %lu, maximum index = %lu \n", (unsigned long) problem_index,
        		(unsigned long) coco_suite_get_number_of_problems(suite) - 1);
      }
      problem = coco_suite_get_problem(suite, problem_index);
      previous_problem_index = (long) problem_index;
    }
    x = testvectors[testvector_id].x;

    coco_evaluate_function(problem, x, &y);
    if (!about_equal(expected_value, y)) {
      ++number_of_failures;
      if (!header_shown) {
        fprintf(stdout, "Problem Testcase Status Message\n");
        header_shown = 1;
      }
      if (shown_failures < 100) {
        fprintf(stdout,
                "%8lu %8i FAILED expected=%.8e observed=%.8e problem_id=%s\n",
                problem_index, testvector_id, expected_value, y,
                coco_problem_get_id(problem));
        fflush(stdout);
        ++shown_failures;
      } else if (shown_failures == 100) {
        fprintf(stdout, "... further failed tests suppressed ...\n");
        fflush(stdout);
        ++shown_failures;
      }
    }
  }
  fclose(testfile);
  /* Output summary statistics */
  fprintf(stderr, "%i of %i tests passed (failure rate %.2f%%)\n",
          number_of_testcases - number_of_failures, (int)number_of_testcases,
          (100.0 * number_of_failures) / number_of_testcases);

  /* Free any remaining allocated memory */
  if (NULL != problem)
    coco_problem_free(problem);
  free(testvectors);

  coco_suite_free(suite);

  return number_of_failures == 0 ? 0 : 1;

err:
  if (testfile != NULL)
    fclose(testfile);
  if (testvectors != NULL)
    free(testvectors);
  return 2;
}