From: ametama Date: Mon, 19 Jan 2026 11:30:57 +0000 (+0100) Subject: build: add compilation to lib archive, argp executable X-Git-Url: https://git.wafflesoft.org/?a=commitdiff_plain;h=ba79920155bfddf42a8e2b77bb3b1a7489c0295e;p=cgo.git build: add compilation to lib archive, argp executable --- diff --git a/.gitignore b/.gitignore index 28f02b8..f899faa 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ -apso +cgo +libcgo.a compile_flags.txt *.gnu diff --git a/Makefile b/Makefile index 7185bbe..d011338 100644 --- a/Makefile +++ b/Makefile @@ -1,16 +1,19 @@ -build: compile_flags executable +build: compile_flags lib executable -compile_flags_gtk: - echo "-Iinclude `pkg-config --cflags gtk4` -lm -fopenmp `pkg-config --libs gtk4`" | perl -pe 's|(-.*?)\s|\1\n|g' > compile_flags.txt - echo "-o apso" >> compile_flags.txt +#compile_flags_gtk: +# echo "-Iinclude `pkg-config --cflags gtk4` -lm -fopenmp `pkg-config --libs gtk4`" | perl -pe 's|(-.*?)\s|\1\n|g' > compile_flags.txt +# echo "-o cgo" >> compile_flags.txt compile_flags: echo "-Iinclude -lm -fopenmp" | perl -pe 's|(-.*?)\s|\1\n|g' > compile_flags.txt - echo "-o apso" >> compile_flags.txt + +lib: $(wildcard src/*.c) + gcc $(shell cat compile_flags.txt) -c $(shell ls src/*.c | grep -v main.c) + ar rcs libcgo.a *.o + rm *.o executable: $(wildcard src/*.c) - gcc $(shell cat compile_flags.txt) $(shell ls src/*.c) + gcc $(shell cat compile_flags.txt) -o cgo $(shell ls src/*.c) clear: - rm apso - rm compile_flags.txt + rm lib/libcgo.a diff --git a/include/de.h b/include/de.h index cb95e53..57c4a8c 100644 --- a/include/de.h +++ b/include/de.h @@ -1,4 +1,4 @@ -#include "swarm.h" +#include "rand.h" typedef struct { int dim; diff --git a/src/main.c b/src/main.c index c56f590..b75439c 100644 --- a/src/main.c +++ b/src/main.c @@ -1,61 +1,49 @@ -#include -#include -#include "vector.h" -#include "de.h" +#include +#include -double b0(double *x) { - return -x[0]; -} +enum algorithm { + de, + pso +}; -double b1(double *x) { - return -x[1]; -} +static char doc[] = "cgo: collection of generic optimizers"; +static char args_doc[] = "FILE"; +static struct argp_option options[] = { + {"algorithm", 'a', "{de,pso}", 0, "optimization algorithm (default: de)"} +}; +struct arguments { + enum algorithm algorithm; + char *outfile; +}; -double b2(double *x) { - return x[0] - 10; +static error_t parse_opt(int key, char *arg, struct argp_state *state) { + struct arguments *args = state->input; + switch (key) { + case 'a': + if (strcmp(arg, "de") == 0) args->algorithm = de; + else if (strcmp(arg, "pso") == 0) args->algorithm = pso; + else argp_usage(state); + break; + case ARGP_KEY_ARG: + if (state->arg_num > 0) argp_usage(state); + args->outfile = arg; + break; + case ARGP_KEY_END: + // if (state->arg_num < 2) argp_usage(state); + break; + default: + return ARGP_ERR_UNKNOWN; + } + return 0; } -double b3(double *x) { - return x[1] - 15; -} - -double f(double *x) { - return exp(b0(x)) + exp(b1(x)) + exp(b2(x)) + exp(b3(x)) + x[0] - x[1]; -} +static struct argp argp = { options, parse_opt, args_doc, doc }; int main(int argc, char *argv[]) { - range rrange[2] = { - (range) { -100.0, 100.0 }, - (range) { -100.0, 100.0 } - }; - /* - swarm_parameters par = { - 2, - 100, - rrange, - f, - pow(2, 40), - 0.8, - 2.05, - 2.05 - }; - */ - de_parameters par = { - 2, - 100, - rrange, - f, - 0.9, - 0.8 - }; - // particle_state best = { vec_alloc(par.dim), 0.0 }; - double res[2]; - //swarm_optimize(&par, &best); - de_optimize(&par, res); - printf("apso: converged in x="); - vec_print(par.dim, res); - printf(" with fitness f(x)=%f\n", f(res)); - // GtkApplication *app = gui_init(); - // return g_application_run(G_APPLICATION(app), argc, argv); - return 0; + struct arguments args; + args.algorithm = de; + args.outfile = "out.log"; + argp_parse(&argp, argc, argv, 0, 0, &args); + + return 0; }