-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
-#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;
}