Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions TODO-archiving
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@

- [ ] Hypervolume archiver
- [ ] MGA

# NDS Forest

- [ ] Enable timing of query vs update
- [ ] Add stats to Treap
4 changes: 2 additions & 2 deletions c/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ $(ALL_NAMES): %: $(BINDIR)/%$(EXE)

include libhv.mk

$(BINDIR)/archiver$(EXE): main-archiver.o io.o treap_archive.o nondominated.o treap2d_fixed_size.o
$(BINDIR)/archiver$(EXE): main-archiver.o io.o treap_archive.o nondominated.o treap2d_fixed_size.o timer.o
$(BINDIR)/dominatedsets$(EXE): dominatedsets.o nondominated.o treap2d_fixed_size.o
$(BINDIR)/eaf$(EXE): eaf.o eaf_main.o eaf3d.o avl.o
$(BINDIR)/epsilon$(EXE): epsilon.o nondominated.o treap2d_fixed_size.o
Expand Down Expand Up @@ -226,7 +226,7 @@ hvapprox.o: hvapprox.h pow_int.h rng.h mt19937/mt19937.h
hvc3d.o: hv_priv.h sort.h hv3d_priv.h treap2d.h
igd.o: cmdline.h io.h nondominated.h igd.h pow_int.h
io.o: io.h io_priv.h
main-archiver.o: cmdline.h io.h treap_archive.h treap2d.h nd_tree.h libmoocore-config.h
main-archiver.o: cmdline.h io.h treap_archive.h treap2d.h nd_tree.h nds_forest.h libmoocore-config.h
main-hv.o: cmdline.h io.h hv.h timer.h libmoocore-config.h nondominated.h hvapprox.h
main-hvapprox.o: cmdline.h io.h hvapprox.h
main-nondominated.o : cmdline.h io.h nondominated.h nondominated_kung.h
Expand Down
2 changes: 1 addition & 1 deletion c/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ The [**moocore** project](https://github.com/multi-objective/moocore/) collects
* Computation of the Empirical Attainment Function. The empirical attainment
function (EAF) describes the probabilistic distribution of the outcomes
obtained by a stochastic algorithm in the objective space.
* Archivers that allow efficiently storing a nondominated set from points given one-by-one as a sequence.
* Archivers that allow efficiently storing a nondominated set, or sorted nondominated set, from points given one-by-one as a sequence.

The goal of **moocore** is to provide an implementation of these functions that is efficient, thoroughly tested, well-documented, multi-platform (Windows, Linux, MacOS), multi-language (C, R, and Python, so far), with minimal dependencies and reusable from other libraries and packages, thus providing an easy-to-use and high-performance building block.

Expand Down
129 changes: 126 additions & 3 deletions c/main-archiver.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,21 @@
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <limits.h>
#include <unistd.h> // for getopt()
#include <getopt.h> // for getopt_long()

#include "io.h"
#include "archive.h"
#include "nds_forest.h"

#define CMDLINE_COPYRIGHT_YEARS "2026"
#define CMDLINE_AUTHORS "Manuel Lopez-Ibanez <manuel.lopez-ibanez@manchester.ac.uk>\n"
#include "cmdline.h"

static bool union_flag = false;
static bool always_nd_tree_flag = false;
static bool nds_flag = false;
static int verbose_flag = 1;
static char *suffix = NULL;

Expand All @@ -50,13 +54,26 @@ OPTION_HELP_STR
OPTION_VERSION_STR
" -v, --verbose print some information (time, maximum, etc). \n"
" --nd-tree Always use ND-Tree for any dimension.\n"
" --nds NDS-archive (currently NDS-Forest).\n"
" --strategy= NDS-Forest insertion strategy: linear, front-binary, \n"
" previous-rank, zero-then-binary, dominance-guided-binary.\n"
OPTION_UNION_STR
" -s, --suffix=STRING Create an output file for each input file by appending\n"
" this suffix. This is ignored when reading from stdin. \n"
" If missing, output is sent to stdout. \n"
"\n");
}

static bool
check_no_missing_points(NDSForest *archiver, size_t expected_n)
{
size_t nfronts = nds_forest_len(archiver);
size_t total_points = 0;
for (size_t i = 0; i < nfronts; i++)
total_points += nds_forest_front_total_size(archiver, i);
return total_points == expected_n;
}

// FIXME: Add options to print quality metrics after archiving each input point.
static bool
read_and_archive(FILE * input)
Expand Down Expand Up @@ -115,11 +132,97 @@ read_and_archive(FILE * input)
return (status == 0);
}

static bool
read_and_nds_archive(FILE * input, int strategy)
{
dimension_t dim = 0;
double point[MOOCORE_DIMENSION_MAX+1];
// Read the first line as the dimension/template point.
int status = fread_next_double_point(input, point, &dim);
if (status != 1)
fatal_error("reading the first point");
if (dim < 2)
fatal_error("At least 2 dimensions are required");

const bool use_treap = (dim == 2 && !always_nd_tree_flag);
const bool x_is_null = true;
const bool do_timing = (verbose_flag >= 2);
NDSForest * archive = use_treap
? nds_forest_treap_new(strategy, x_is_null, do_timing)
: nds_forest_ndtree_new(dim, strategy, /*max_children=*/0, /*max_bucket_size=*/0, x_is_null, do_timing);

size_t points_read = 0;
// Read point until EOF or empty line.
do {
if (!nds_forest_add(archive, point, /*x=*/NULL))
fatal_error("Memory error!");

points_read++;
if (!check_no_missing_points(archive, points_read))
fatal_error("missing points when adding point %zu =[%g, %g, ...]\n", points_read, point[0], point[1]);

status = fread_next_double_point(input, point, &dim);
} while (status == 1 || (union_flag && status == 0));

if (status != READ_INPUT_FILE_EMPTY && status != 0)
fatal_error("reading point from input");

// We completed one data set
size_t nfronts = nds_forest_len(archive);
// Print result
if (verbose_flag >= 2) {
fprintf(stdout,
"# Query time: %-20.12g\n"
"# Update time: %-20.12g\n"
"# Total points: %zu\n"
"# Unique points: %zu\n"
"# Number of fronts: %zu\n",
archive->query_time, archive->update_time,
nds_forest_total_size(archive),
nds_forest_unique_size(archive),
nfronts);
fprintf(stdout, "# Front total sizes: ");
if (nfronts > 0) {
fprintf(stdout, "%zu", nds_forest_front_total_size(archive, 0));
for (size_t i = 1; i < nfronts; i++)
fprintf(stdout, ", %zu", nds_forest_front_total_size(archive, i));
fprintf(stdout, "\n");
}
fprintf(stdout, "# Front unique sizes: ");
if (nfronts > 0) {
fprintf(stdout, "%zu", nds_forest_front_unique_size(archive, 0));
for (size_t i = 1; i < nfronts; i++)
fprintf(stdout, ", %zu", nds_forest_front_unique_size(archive, i));
fprintf(stdout, "\n");
}
}

_attr_maybe_unused size_t total_points = 0;
for (size_t i = 0; i < nfronts; i++) {
size_t npoint = nds_forest_front_total_size(archive, i);
ASSUME(npoint > 0);
ASSUME(npoint < INT_MAX);
total_points += npoint;
double * z_list = MOOCORE_MALLOC(dim * npoint, *z_list);
nds_forest_front_get_contents(archive, i, z_list, NULL);
// FIXME: Add an option to sort the output.
char prefix[256];
snprintf(prefix, 255, "%zu ", i);
// FIXME: write_sets should take size_t * or unsigned *;
int n_points = (int) npoint;
write_sets(stdout, z_list, dim, &n_points, 1, prefix);
free(z_list);
}
nds_forest_free(archive);
assert(total_points == points_read);
return (status == 0);
}


int main(int argc, char *argv[])
{
// See the man page for getopt_long for an explanation of these fields.
static const char short_options[] = "hVvqUs:N";
static const char short_options[] = "hVvqUs:NnS:";
static const struct option long_options[] = {
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'V'},
Expand All @@ -128,16 +231,30 @@ int main(int argc, char *argv[])
{"union", no_argument, NULL, 'U'},
{"suffix", required_argument, NULL, 's'},
{"nd-tree", no_argument, NULL, 'N'},
{"nds", no_argument, NULL, 'n'},
{"strategy", required_argument, NULL, 'S'},
{NULL, 0, NULL, 0} /* marks end of list */
};

set_program_invocation_short_name(argv[0]);

int nds_forest_insertion_strategy = LINEAR;

int opt; /* it's actually going to hold a char. */
int longopt_index;
while (0 < (opt = getopt_long (argc, argv, short_options,
long_options, &longopt_index))) {
switch (opt) {
case 'S': // --strategy
nds_forest_insertion_strategy = parse_nds_forest_strategy(optarg);
if (nds_forest_insertion_strategy == UNKNOWN_STRATEGY)
fatal_error("unknown value (%s) of --strategy=", optarg);
break;

case 'n': // --nds
nds_flag = true;
break;

case 'N': // --nd-tree
always_nd_tree_flag = true;
break;
Expand Down Expand Up @@ -165,8 +282,14 @@ int main(int argc, char *argv[])

int numfiles = argc - optind;
if (numfiles < 1) { // Read stdin.
while (read_and_archive(stdin)) {
fprintf(stdout, "\n");
if (nds_flag) {
while (read_and_nds_archive(stdin, nds_forest_insertion_strategy)) {
fprintf(stdout, "\n");
}
} else {
while (read_and_archive(stdin)) {
fprintf(stdout, "\n");
}
}
}
return EXIT_SUCCESS;
Expand Down
Loading
Loading