Skip to content

Instantly share code, notes, and snippets.

@EdgeCaseBerg
Last active December 29, 2015 23:39
Show Gist options
  • Select an option

  • Save EdgeCaseBerg/7744572 to your computer and use it in GitHub Desktop.

Select an option

Save EdgeCaseBerg/7744572 to your computer and use it in GitHub Desktop.
Compile like: cc network_load.c -lm Run with ./a.out
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <math.h> /* link with -lm */
#define NETWORK_WIDTH 5
#define NETWORK_HEIGHT 4
#define NUMBER_NETWORKS 80
#define ALPHA 0.89
#define SIGMA(x,alpha) (0.5 * (x) * (alpha) / (1 + abs((x)*(alpha))) + 0.5) /* Replace this with some sigmoidal function */
/*
0 1 2 3 4
+-----------+
0| x x x x x |
1| x x x x x |
2| x x x x x |
3| x x x x x |
+-----------+
0,0 represents the 0th node's weighted edge to the 0th node (self loop)
0,1 represents the 0th node's weighted edge to the 1st node
These weights multiply the value of the node (which is 0 or 1) and
feed the value forward to the next layer.
I forget the calculation and my memory might be off but it was something
like this that we did.
*/
int main(){
float network_edges[NETWORK_WIDTH][NETWORK_HEIGHT];
int i,j,k;
char bipedStr[32];
FILE *fp;
/* Just initializing the values */
srand(time(NULL));
for(i=0;i<NETWORK_HEIGHT;++i)
for(j=0;j<NETWORK_WIDTH;++j)
network_edges[i][j] = (float)rand()/(float)(RAND_MAX);
bzero(bipedStr,sizeof bipedStr);
/* Let's say we write each network's weights out to it's own file: */
for(k=0; k < NUMBER_NETWORKS; ++k){
/* Create a network data file, might be good to put this in it's own directory */
bzero(bipedStr,sizeof bipedStr);
snprintf(bipedStr,sizeof bipedStr,"network%i.data", k);
fp = fopen(bipedStr,"w"); /* overwrite mode. */
if( fp == NULL)
return 1; /* Somethings wrong */
/* You choose how to store the values, but make it simple, like: */
for(i=0; i < NETWORK_HEIGHT; ++i){
for(j=0; j<NETWORK_WIDTH; ++j)
fprintf(fp, "%f ", network_edges[i][j] );
fprintf(fp, "\n" );
/* Print one row, \n, then the next... */
}
fclose(fp);
}
/* Now you have a bunch of initial values (you should only really generate them once )
* so now you just need to read them out into your array, run a step or generation
* and perturb or whatever you'd like, then write them back out to the files.
*/
/* Just choosing a random one as an example:
In code dealing with all the generated networks, you could loop and
fork the process to create child processes, one per network that would
run their course and then finish up.
Alternatively, you could use threads and join the threads together at the
end, but since ODE would be running in the theoritical child process, it'd
probably be best to give it its own process space by forking.
Once you have ran however many generations for each child (which could be
done in each child process). Detect that all the children have ceased and
then you could reveiw the results of each of the children by loading up their
networks and watching the last run in the simulator.
*/
k= 7;
bzero(bipedStr,sizeof bipedStr);
snprintf(bipedStr,sizeof bipedStr,"network%i.data", k);
fp = fopen(bipedStr,"r");
if( fp == NULL )
return 1;
for(i=0; i < NETWORK_HEIGHT; ++i)
for(j=0; j < NETWORK_WIDTH; ++j)
fscanf(fp,"%f", &network_edges[i][j]);
fclose(fp);
/* Perturb and run simulation for this network */
fprintf(stderr, "%s\n", "Before Perturbation");
for(i=0; i < NETWORK_HEIGHT; ++i){
for(j=0; j < NETWORK_WIDTH; ++j)
fprintf(stdout,"%f ", network_edges[i][j]);
fprintf(stdout, "\n" );
}
for(i=0; i < NETWORK_HEIGHT; ++i){
for(j=0; j < NETWORK_WIDTH; ++j)
if((float)rand()/(float)(RAND_MAX) < 0.5)
network_edges[i][j] = SIGMA(network_edges[i][j] + (float)rand()/(float)(RAND_MAX), ALPHA );
else
network_edges[i][j] = SIGMA(network_edges[i][j] - (float)rand()/(float)(RAND_MAX), ALPHA);
}
fprintf(stderr, "%s\n", "After Perturbation");
for(i=0; i < NETWORK_HEIGHT; ++i){
for(j=0; j < NETWORK_WIDTH; ++j)
fprintf(stdout,"%f ", network_edges[i][j]);
fprintf(stdout, "\n" );
}
/* If It's good, then write it out overwriting the old file,
if it's not better then discard it by simply not writing out
over the old file.
*/
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment