build: add compilation to lib archive, argp executable
authorametama <ametama@wafflesoft.org>
Mon, 19 Jan 2026 11:30:57 +0000 (12:30 +0100)
committerametama <ametama@wafflesoft.org>
Mon, 19 Jan 2026 11:30:57 +0000 (12:30 +0100)
.gitignore
Makefile
include/de.h
src/main.c

index 28f02b871b2bf2fecb2c83dbd677b72cb14053b3..f899faa1cdfc070bd6958ff6b53a72c4deb89fe2 100644 (file)
@@ -1,3 +1,4 @@
-apso
+cgo
+libcgo.a
 compile_flags.txt
 *.gnu
index 7185bbe5bac231124f5174f59c5272e00204c8b1..d0113385d483fb9d1979e91d689f9a4218fe51b3 100644 (file)
--- 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
index cb95e53c2e182e432c51136060ca55b350bb4d1c..57c4a8c65560ddc8f009620307fafb5cfec069a2 100644 (file)
@@ -1,4 +1,4 @@
-#include "swarm.h"
+#include "rand.h"
 
 typedef struct {
   int dim;
index c56f59003f214ed7c9653527eed13ce3560ce522..b75439c040723e5ae8ae8cb128997d0095a34d12 100644 (file)
@@ -1,61 +1,49 @@
-#include <math.h>
-#include <stdio.h>
-#include "vector.h"
-#include "de.h"
+#include <argp.h>
+#include <string.h>
 
-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;
 }