• Main Page
  • Data Structures
  • Files
  • File List
  • Globals

ne_mut_lib.c

00001 /*===========================================================================
00002  NetEvo Foundation Library
00003  Copyright (C) 2009, 2010 Thomas E. Gorochowski <tgorochowski@me.com>
00004  Bristol Centre for Complexity Sciences, University of Bristol, Bristol, UK
00005  ---------------------------------------------------------------------------- 
00006  NetEvo is a computing framework designed to allow researchers to investigate 
00007  evolutionary aspects of dynamical complex networks. By providing tools to 
00008  easily integrate each of these factors in a coherent way, it is hoped a 
00009  greater understanding can be gained of key attributes and features displayed 
00010  by complex systems.
00011  
00012  NetEvo is open-source software released under the Open Source Initiative 
00013  (OSI) approved Non-Profit Open Software License ("Non-Profit OSL") 3.0. 
00014  Detailed information about this licence can be found in the COPYING file 
00015  included as part of the source distribution.
00016  
00017  This library is distributed in the hope that it will be useful, but
00018  WITHOUT ANY WARRANTY; without even the implied warranty of
00019  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
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 /* Global random number generator */
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    /* Allocate and set parameters */
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    /* Set fields */
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    /* Count number of nodes and edges */
00068    nNum = (int)ne_sys_nodes(sys);
00069    eNum = (int)ne_sys_edges(sys);
00070    
00071    /* Select number of rewirings to perform (exp dist) */
00072    rewirings = (int)gsl_ran_exponential(neRng, params[0]);
00073    
00074    /* Ensure that at least a single rewiring is performed */
00075    if ( rewirings <= 0 ) 
00076       rewirings = 1;
00077    
00078    /* Perform the chosen number of rewirings */
00079    for (i=0; i<rewirings; i++) {
00080       
00081       /* Pick an existing edge to remove */
00082       eDel = (int)(gsl_rng_get(neRng)%eNum);
00083       esDel = igraph_ess_1((igraph_integer_t)eDel);
00084 
00085       /* Pick two valid nodes to add a new edge between */
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       /* Rewire network */
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 

Generated on Thu Aug 26 2010 11:04:24 for NetEvo by  doxygen 1.7.1