Skip to content

Commit

Permalink
NSGA with inline and external fitfun
Browse files Browse the repository at this point in the history
  • Loading branch information
phadjido committed Feb 16, 2018
1 parent 473cce5 commit b829593
Show file tree
Hide file tree
Showing 12 changed files with 163 additions and 41 deletions.
19 changes: 13 additions & 6 deletions multiobj/nsga2/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,25 @@ CFLAGS+= -D_XOPEN_SOURCE=700 -D_BSD_SOURCE
LIBS+= `torc_libs` -lm # -L/users/sverma/usr/torc/lib -ltorc
CFLAGS+=`torc_cflags` # -I/users/sverma/usr/torc/include

OBJS:=allocate.o auxiliary.o crossover.o crowddist.o decode.o display.o dominance.o eval.o fillnds.o fitfun.o initialize.o list.o merge.o mutation.o nsga2r.o problemdef.o rand.o rank.o report.o sort.o tourselect.o
MAIN=nsga2r
all:$(MAIN)
OBJS1:=allocate.o auxiliary.o crossover.o crowddist.o decode.o display.o dominance.o eval.o fillnds.o fitfun_inline.o initialize.o list.o merge.o mutation.o nsga2r.o problemdef.o rand.o rank.o report.o sort.o tourselect.o
MAIN1=nsga2r

$(MAIN):$(OBJS)
$(LD) $(LDFLAGS) $(OBJS) -o $(MAIN) $(LIBS)
OBJS2:=allocate.o auxiliary.o crossover.o crowddist.o decode.o display.o dominance.o eval.o fillnds.o fitfun_external.o initialize.o list.o merge.o mutation.o nsga2r.o problemdef.o rand.o rank.o report.o sort.o tourselect.o
MAIN2=nsga2r_ext

all:$(MAIN1) $(MAIN2)

$(MAIN1):$(OBJS1)
$(LD) $(LDFLAGS) $(OBJS1) -o $(MAIN1) $(LIBS)

$(MAIN2):$(OBJS2)
$(LD) $(LDFLAGS) $(OBJS2) -o $(MAIN2) $(LIBS)

%.o: %.c global.h rand.h
$(CC) $(CFLAGS) -c $<

clean:
$(RM) $(OBJS) $(MAIN)
$(RM) $(OBJS1) $(MAIN1) $(OBJS2) $(MAIN2)

clear:
$(RM) eval_db_*.txt curgen_db_*.txt *.out
9 changes: 9 additions & 0 deletions multiobj/nsga2/bin/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash -l

# load here whatever module for simulation needs
# e.g. module load matlab/7.14;

# run the simulation
./simcode


18 changes: 6 additions & 12 deletions multiobj/nsga2/fitfun_external.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ static pthread_mutex_t fork_mutex = PTHREAD_MUTEX_INITIALIZER;
#define CONSTRFILE "constr_viol.txt"
#define REMOVEDIRS 0

// REMOVEDIRS=1 removes the directories... =0 keeps them
// REMOVEDIRS=1 removes the directories..., =0 keeps them

void fitfun(double /*const*/ *x, int N, void *output, int *winfo, double *result, int ncon, double *constraints)
{
// 0.
int n = N;
int me = getpid(); /* spanwer_id : worker_id */
int me = getpid();
char line[1024];
char *largv[64];
char taskname[256];
Expand All @@ -41,7 +41,6 @@ void fitfun(double /*const*/ *x, int N, void *output, int *winfo, double *result
job_restart:

sprintf(taskname, "tmpdir.%d.%d.%d.%d", gen, chain, step, task);
//mkdir(taskname, S_IRWXU);
mkdir(taskname, S_IRWXU | S_IRWXG | S_IRWXO);

{
Expand Down Expand Up @@ -96,18 +95,14 @@ void fitfun(double /*const*/ *x, int N, void *output, int *winfo, double *result
abort();
}


// 1.3 write input parametes to the simulation's input file (first 3)
const double scale[6] = {1, 1, 1, 1, 1, 1}; // 6 just to be safe
// 1.3 write input parametes to the simulation's input file
FILE *finp = fopen(PARAMSFILE, "w");
int i;
for (i = 0; i < n; i++) fprintf(finp, "%.16lf\n", scale[i]*x[i]);
//for (i = 0; i < n; i++) fprintf(finp, "%.16lf\n", x[i]);
for (i = 0; i < n; i++) fprintf(finp, "%.16lf\n", x[i]);
fclose(finp);

/* 2. run simulation */
sprintf(line, "./run.sh");
//printf("parameters are \t %e \t %e \t %e \t %e \t %e \t %e \t %e\n",x[0],x[1],x[2],x[3],x[4],x[5],x[6]);
parse(line, largv);

#if 1
Expand Down Expand Up @@ -162,7 +157,6 @@ void fitfun(double /*const*/ *x, int N, void *output, int *winfo, double *result
}
else
{
//while (!feof(pFile))
fscanf(pFile, "%lf", &fitness1 );
fscanf(pFile, "%lf", &fitness2 );

Expand Down Expand Up @@ -224,10 +218,10 @@ void fitfun(double /*const*/ *x, int N, void *output, int *winfo, double *result
result[1] = res2;

job_try++;
if ((res1==1e12 || res2==1e12 || isnan(res1) || isnan(res2) || (t1-t0)<50.0) && (job_try < 5)) // i.e., if fitness.txt doesn't exist, or if the sim ended in less than 50 seconds real time, and retries have been less than 5.
if ((res1==1e12 || res2==1e12 || isnan(res1) || isnan(res2) || (t1-t0)<0.0) && (job_try < 5)) // i.e., if fitness.txt doesn't exist, or if the sim ended in less than 0 seconds real time, and retries have been less than 5.
goto job_restart;


return; //res;
return;
}

2 changes: 1 addition & 1 deletion multiobj/nsga2/fitfun.c → multiobj/nsga2/fitfun_inline.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

void fitfun(double *x, int nx, void *output, int *winfo, double *result, int ncon, double *constraints) // ny must be also available
{
// usleep(100*1000);
usleep(10*1000);

// result[0] = pow((x[0]+2.0),2.0)-10;
// result[1] = pow((x[0]-2.0),2.0)+20;
Expand Down
19 changes: 0 additions & 19 deletions multiobj/nsga2/fitfun_simple.c

This file was deleted.

3 changes: 1 addition & 2 deletions multiobj/nsga2/runme.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
#./nsga2r 0.5 < test.in
./nsga2r test.in
#mpirun -n 4 ./nsga2r 0.5 < test.in
#mpirun -n 4 ./nsga2r test.in
2 changes: 2 additions & 0 deletions multiobj/nsga2/runme_ext.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
./nsga2r_ext test.in
#mpirun -n 4 ./nsga2r_ext test.in
15 changes: 15 additions & 0 deletions multiobj/nsga2/simulation/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CC = gcc

all: simcode install

simcode: simcode.c
$(CC) -o simcode simcode.c -lm

install:
mkdir -p ../bin
cp -f simcode ../bin
cp -f run.sh ../bin

clean:
rm -f simcode params.txt fitness.txt constr_viol.txt

9 changes: 9 additions & 0 deletions multiobj/nsga2/simulation/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash -l

# load here whatever module for simulation needs
# e.g. module load matlab/7.14;

# run the simulation
./simcode


9 changes: 9 additions & 0 deletions multiobj/nsga2/simulation/run_example.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# load here whatever module for simulation needs
# e.g. module load matlab/7.14;

# run the simulation
echo "0.5" > params.txt
echo "0.9" >> params.txt
./simcode


97 changes: 97 additions & 0 deletions multiobj/nsga2/simulation/simcode.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#include <stdio.h>
#include <sys/time.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <math.h>

#define PROBDIM 1
#define NOBJ 2
#define NCONSTR 1

#define PARAMSFILE "params.txt"
#define OUTPUTFILE "fitness.txt"
#define CONSTRFILE "constr_viol.txt"

void fitfun(double *x, int *pnx, double *obj, int *pno, double *constr, int *pncon)
{
int nx = *pnx;
int no = *pno;
int ncon = *pncon;

double y = 0;
for (int i = 0; i < nx; i++) y += x[i];

obj[0] = pow((y+2.0),2.0)-10;
obj[1] = pow((y-2.0),2.0)+20;

constr[0] = obj[1] - 25;

return;
}

int main(int argc, char *argv[])
{
int nx = PROBDIM;
double x[PROBDIM];
int no = NOBJ;
double obj[NOBJ];
int ncon = NCONSTR;
double constr[NCONSTR+1];

/* print argv numbers */
for (int i = 0; i < argc; i++) {
printf("simcode: arg %d argv %s\n", i, argv[i]); fflush(0);
}

/* read input parameters (argv) into TP */

FILE *fp = fopen(PARAMSFILE, "r");
if (fp == NULL) {
printf("Missing %s file. Exiting...\n", PARAMSFILE);
exit(1);
}

for (int i = 0; i < nx; i++)
fscanf(fp, "%lf", &x[i]);
fclose(fp);

/* run the "simulation" */
for (int i = 0; i < nx; i++) {
printf("input parameter %d = %f\n", i, x[i]);
}

fitfun (x, &nx, obj, &no, constr, &ncon);

/* write the results in the "OUTPUT" file */
char foname[256];
FILE *fo;
strcpy(foname,OUTPUTFILE);
fo = fopen(foname, "w");
if (fo == NULL) {
printf("Could not create %s file. Exiting...\n", OUTPUTFILE);
exit(1);
}

for (int i = 0; i < no; i++)
fprintf(fo, "%.16lf\n", obj[i]);
fclose(fo);


/* write the constraints violations in the "CONSTRFILE" file */
char fcname[256];
FILE *fc;
strcpy(fcname,CONSTRFILE);
fc = fopen(fcname, "w");
if (fc == NULL) {
printf("Could not create %s file. Exiting...\n", CONSTRFILE);
exit(1);
}

for (int i = 0; i < ncon; i++)
fprintf(fc, "%.16lf\n", constr[i]);
fclose(fc);


return 0;
}
2 changes: 1 addition & 1 deletion multiobj/nsga2/spawner.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ int copy_from_dir(char *name)
while ((ent = readdir (dir)) != NULL) {
//if (ent->d_type == DT_REG) {
//printf ("%s (%d)\n", ent->d_name, ent->d_type);
char source[256], dest[256];
char source[4096], dest[4096];

sprintf(source, "%s/%s", name, ent->d_name);
sprintf(dest, "./%s", ent->d_name);
Expand Down

0 comments on commit b829593

Please sign in to comment.