00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "ne_mut_lib.h"
00024 #include "ne_evo.h"
00025 #include <igraph.h>
00026 #include <gsl/gsl_rng.h>
00027 #include <gsl/gsl_randist.h>
00028 #include <string.h>
00029
00030
00031 extern gsl_rng* neRng;
00032
00033
00034 ne_mut_t * ne_mut_rewire_alloc (ne_real_t expMean)
00035 {
00036 ne_mut_t *mut;
00037 ne_real_t *params;
00038
00039 if ((mut = (ne_mut_t *)malloc(sizeof(ne_mut_t))) == NULL) {
00040 ne_error("ne_mut_rewire_alloc: Could not allocate memory for ne_mut.");
00041 return NULL;
00042 }
00043
00044
00045 if ((params = (ne_real_t *)malloc(sizeof(ne_real_t))) == NULL) {
00046 ne_error("ne_mut_rewire_alloc: Could not allocate memory for ne_mut.params.");
00047 free(mut);
00048 return NULL;
00049 }
00050 params[0] = expMean;
00051
00052
00053 mut->type = NE_MUT_TOP;
00054 mut->params = params;
00055 mut->fn = &ne_mut_rewire;
00056
00057 return mut;
00058 }
00059
00060
00061 ne_err_code_t ne_mut_rewire (ne_sys_t *sys, ne_real_t *params, char *strMut)
00062 {
00063 int i, nNum, v1Add, v2Add, eNum, eDel, rewirings = 0;
00064 igraph_es_t esDel;
00065 char strBuffer[100];
00066
00067
00068 nNum = (int)ne_sys_nodes(sys);
00069 eNum = (int)ne_sys_edges(sys);
00070
00071
00072 rewirings = (int)gsl_ran_exponential(neRng, params[0]);
00073
00074
00075 if ( rewirings <= 0 )
00076 rewirings = 1;
00077
00078
00079 for (i=0; i<rewirings; i++) {
00080
00081
00082 eDel = (int)(gsl_rng_get(neRng)%eNum);
00083 esDel = igraph_ess_1((igraph_integer_t)eDel);
00084
00085
00086 do {
00087 v1Add = (int)(gsl_rng_get(neRng)%nNum);
00088 do {
00089 v2Add = (int)(gsl_rng_get(neRng)%nNum);
00090 } while ( v1Add == v2Add );
00091 } while ( ne_sys_edge_exists(sys, v1Add, v2Add) == TRUE );
00092
00093
00094 igraph_delete_edges(sys->graph, esDel);
00095 ne_evo_del_edge(strBuffer, (int)IGRAPH_FROM(sys->graph, eDel), (int)IGRAPH_TO(sys->graph, eDel));
00096 strcat(strMut, strBuffer);
00097 igraph_add_edge(sys->graph, v1Add, v2Add);
00098 ne_evo_add_edge(strBuffer, v1Add, v2Add, 0);
00099 strcat(strMut, strBuffer);
00100 }
00101
00102 return NE_SUCCESS;
00103 }
00104