コード例 #1
0
ファイル: random.cpp プロジェクト: Aleyasen/Alaki
void srand4(void) {
	
	long s=(long)time(NULL);
	ran4(false, s);
	
	
	
}
コード例 #2
0
void sample_graph(ostream & pout, deque<pair<int, int> > & all_edges_deque, double sample_probability, ostream & multiplexout, int layer_index) {

    for(int i=0; i<int(all_edges_deque.size()); i++) {
        if(ran4()<sample_probability) {
            pout<<all_edges_deque[i].first<<" "<<all_edges_deque[i].second<<endl;
            multiplexout<<layer_index<<" "<<all_edges_deque[i].first<<" "<<all_edges_deque[i].second<<" 1"<<endl;
            multiplexout<<layer_index<<" "<<all_edges_deque[i].second<<" "<<all_edges_deque[i].first<<" 1"<<endl;
        }
    }
}
コード例 #3
0
ファイル: combinatorics.cpp プロジェクト: Aleyasen/Alaki
int shuffle_and_set(int *due, int dim) {		// it sets due as a random sequence of integers from 0 to dim-1
	
	multimap <double, int> uno;
	for (int i=0; i<dim; i++)
		uno.insert(make_pair(ran4(), i));
	

	multimap<double, int>::iterator it;
	
	int h=0;
	for (it=uno.begin(); it!=uno.end(); it++)
		due[h++]=it->second;
	


	return 0;

}
コード例 #4
0
int build_subgraphs(deque<set<int> > & E, const deque<deque<int> > & member_matrix, deque<deque<int> > & member_list, 
                    deque<deque<int> > & link_list, const deque<int> & internal_degree_seq, const deque<int> & degree_seq, const bool excess, const bool defect) {
	
	
	
	E.clear();
	member_list.clear();
	link_list.clear();
	
	int num_nodes=degree_seq.size();
	
	//printm(member_matrix);
    
	
	
	
	{
    
    deque<int> first;
    for (int i=0; i<num_nodes; i++)
        member_list.push_back(first);
	
	}
	
	
	
	for (int i=0; i<member_matrix.size(); i++)
		for (int j=0; j<member_matrix[i].size(); j++)
			member_list[member_matrix[i][j]].push_back(i);
	
	
	//printm(member_list);
	
	for (int i=0; i<member_list.size(); i++) {
		
		deque<int> liin;
        
		
		for (int j=0; j<member_list[i].size(); j++) {
			
			compute_internal_degree_per_node(internal_degree_seq[i], member_list[i].size(), liin);
			liin.push_back(degree_seq[i] - internal_degree_seq[i]);
            
		}
		
		link_list.push_back(liin);
		
	}
    
	
	
    
	// now there is the check for the even node (it means that the internal degree of each group has to be even and we want to assure that, otherwise the degree_seq has to change) ----------------------------
    
	
	
	
	// ------------------------ this is done to check if the sum of the internal degree is an even number. if not, the program will change it in such a way to assure that. 
	
	
    
	for (int i=0; i<member_matrix.size(); i++) {
        
		
		int internal_cluster=0;
		for (int j=0; j<member_matrix[i].size(); j++) {
			
			int right_index= lower_bound(member_list[member_matrix[i][j]].begin(), member_list[member_matrix[i][j]].end(), i) - member_list[member_matrix[i][j]].begin();
			
            
			
			internal_cluster+=link_list[member_matrix[i][j]][right_index];
		}
		
		
		if(internal_cluster % 2 != 0) {
			
			
			
			bool default_flag=false;
			
			if(excess)
				default_flag=true;
			else if(defect)
				default_flag=false;
			else if (ran4()>0.5)
				default_flag=true;
			
			if(default_flag) {
				
				// if this does not work in a reasonable time the degree sequence will be changed
				
				for (int j=0; j<member_matrix[i].size(); j++) {		
					
					
					int random_mate=member_matrix[i][irand(member_matrix[i].size()-1)];
					
					int right_index= lower_bound(member_list[random_mate].begin(), member_list[random_mate].end(), i) - member_list[random_mate].begin();
					
					
					if ((link_list[random_mate][right_index]<member_matrix[i].size()-1) && (link_list[random_mate][link_list[random_mate].size()-1] > 0 )) {
						
						link_list[random_mate][right_index]++;
						link_list[random_mate][link_list[random_mate].size()-1]--;
                        
						break;
						
					}
                    
				}
                
                
			}
			
			
			else {
				
				for (int j=0; j<member_matrix[i].size(); j++) {
                    
					int random_mate=member_matrix[i][irand(member_matrix[i].size()-1)];
                    
					int right_index= lower_bound(member_list[random_mate].begin(), member_list[random_mate].end(), i) - member_list[random_mate].begin();
					
					if (link_list[random_mate][right_index] > 0 ) {
						
						link_list[random_mate][right_index]--;
						link_list[random_mate][link_list[random_mate].size()-1]++;
                        
						break;
						
					}
                    
				}
                
                
			}
			
			
			
            
		}
        
        
	}
	
	
	// ------------------------ this is done to check if the sum of the internal degree is an even number. if not, the program will change it in such a way to assure that. 
	
	
	
	
	{
	
    set<int> first;
    for(int i=0; i<num_nodes; i++)
        E.push_back(first);
	
	}
	
	for (int i=0; i<member_matrix.size(); i++) {
		
		
		deque<int> internal_degree_i;
		for (int j=0; j<member_matrix[i].size(); j++) {
            
            
			int right_index= lower_bound(member_list[member_matrix[i][j]].begin(), member_list[member_matrix[i][j]].end(), i) - member_list[member_matrix[i][j]].begin();
            
			internal_degree_i.push_back(link_list[member_matrix[i][j]][right_index]);
            
		}
		
		
		
		
		if(build_subgraph(E, member_matrix[i], internal_degree_i)==-1)
			return -1;
        
        
	}
    
    
    
    
	return 0;
	
}
コード例 #5
0
int internal_degree_and_membership (double mixing_parameter, int overlapping_nodes, int max_mem_num, int num_nodes, deque<deque<int> >  & member_matrix, 
                                    bool excess, bool defect,  deque<int> & degree_seq, deque<int> &num_seq, deque<int> &internal_degree_seq, bool fixed_range, int nmin, int nmax, double tau2) {
	
	
	
	
	
	if(num_nodes< overlapping_nodes) {
		
		cerr<<"\n***********************\nERROR: there are more overlapping nodes than nodes in the whole network! Please, decrease the former ones or increase the latter ones"<<endl;
		return -1;
	}
	
	
	// 
	member_matrix.clear();
	internal_degree_seq.clear();
	
	deque<double> cumulative;
	
	// it assigns the internal degree to each node -------------------------------------------------------------------------
	int max_degree_actual=0;		// maximum internal degree
    
	for (int i=0; i<degree_seq.size(); i++) {
		
		double interno=(1-mixing_parameter)*degree_seq[i];
		int int_interno=int(interno);
		
		
		if (ran4()<(interno-int_interno))
			int_interno++;
		
		if (excess) {
			
			while (   (  double(int_interno)/degree_seq[i] < (1-mixing_parameter) )  &&   (int_interno<degree_seq[i])   )
				int_interno++;
            
            
		}
		
		
		if (defect) {
			
			while (   (  double(int_interno)/degree_seq[i] > (1-mixing_parameter) )  &&   (int_interno>0)   )
				int_interno--;
            
            
		}
        
		
		
		
		internal_degree_seq.push_back(int_interno);
		
		
		if (int_interno>max_degree_actual)
			max_degree_actual=int_interno;
		
        
	}
	
	
	// it assigns the community size sequence -----------------------------------------------------------------------------
	
	powerlaw(nmax, nmin, tau2, cumulative);
	
	
	if (num_seq.empty()) {
		
		int _num_=0;
		if (!fixed_range && (max_degree_actual+1)>nmin) {
            
			_num_=max_degree_actual+1;			// this helps the assignment of the memberships (it assures that at least one module is big enough to host each node)
			num_seq.push_back(max_degree_actual+1);
            
		}
		
		
		while (true) {
			
			
			int nn=lower_bound(cumulative.begin(), cumulative.end(), ran4())-cumulative.begin()+nmin;
			
			if (nn+_num_<=num_nodes + overlapping_nodes * (max_mem_num-1) ) {
				
				num_seq.push_back(nn);				
				_num_+=nn;
                
			}
			else
				break;
			
			
		}
		
		num_seq[min_element(num_seq.begin(), num_seq.end()) - num_seq.begin()]+=num_nodes + overlapping_nodes * (max_mem_num-1) - _num_;
		
	}
	
	
	//cout<<"num_seq"<<endl;
	//prints(num_seq);
	
	int ncom=num_seq.size();
	
	//cout<<"\n----------------------------------------------------------"<<endl;
    
	/*
     cout<<"community sizes"<<endl;
     for (int i=0; i<num_seq.size(); i++)
     cout<<num_seq[i]<<" ";
     cout<<endl<<endl;
     //*/
	
    
	/*
     deque <int> first;
     for (int i=0; i<ncom; i++)
     member_matrix.push_back(first);
     
     
     
     // it puts the overlapping_nodes inside
     cout<<ncom<<endl;
     for (int i=degree_seq.size() - overlapping_nodes; i<degree_seq.size(); i++) {
     
     cout<<i<<endl;
     set<int> members;
     int hh=0;
     
     while(members.size()<max_mem_num) {
     
     int random_module=irand(ncom-1);
     
     if(member_matrix[random_module].size()!=num_seq[random_module])
     members.insert(random_module);
     
     hh++;
     
     if(hh>3*num_nodes) {
     cerr<<"it seems that the overlapping nodes need more communities that those I provided. Please increase the number of communities or decrease the number of overlapping nodes"<<endl;
     return -1;				
     }
     
     }
     
     
     for (set<int>::iterator its=members.begin(); its!=members.end(); its++)
     member_matrix[*its].push_back(i);
     
     }
     
     
     
     // it decides the memberships for the not overlapping nodes		
     
     int moment_module=0;
     for (int i=0; i<num_nodes - overlapping_nodes; i++) {
     
     while(member_matrix[moment_module].size()==num_seq[moment_module])
     moment_module++;
     
     member_matrix[moment_module].push_back(i);
     
     }
     
     
     
     */
	
	// I have to assign the degree to the nodes
	
	
	deque<int> member_numbers;
	for(int i=0; i<overlapping_nodes; i++)
		member_numbers.push_back(max_mem_num);
	for(int i=overlapping_nodes; i<degree_seq.size(); i++)
		member_numbers.push_back(1);
	
	//prints(member_numbers);
	//prints(num_seq);
	
	if(build_bipartite_network(member_matrix, member_numbers, num_seq)==-1) {
		
		cerr<<"it seems that the overlapping nodes need more communities that those I provided. Please increase the number of communities or decrease the number of overlapping nodes"<<endl;
		return -1;			
        
	}
    
	//printm(member_matrix);
	
	//cout<<"degree_seq"<<endl;
	//prints(degree_seq);
	
	//cout<<"internal_degree_seq"<<endl;
	//prints(internal_degree_seq);
    
	deque<int> available;
	for (int i=0; i<num_nodes; i++)
		available.push_back(0);
	
	for (int i=0; i<member_matrix.size(); i++) {
		for (int j=0; j<member_matrix[i].size(); j++)
			available[member_matrix[i][j]]+=member_matrix[i].size()-1;
	}
	
	//cout<<"available"<<endl;
	//prints(available);
	
	
	deque<int> available_nodes;
	for (int i=0; i<num_nodes; i++)
		available_nodes.push_back(i);
	
	
	deque<int> map_nodes;				// in the position i there is the new name of the node i
	for (int i=0; i<num_nodes; i++)
		map_nodes.push_back(0);
    
	
	for (int i=degree_seq.size()-1; i>=0; i--) {
		
		int & degree_here=internal_degree_seq[i];
		int try_this = irand(available_nodes.size()-1);
		
		int kr=0;
		while (internal_degree_seq[i] > available[available_nodes[try_this]]) {
            
			kr++;
			try_this = irand(available_nodes.size()-1);
			if(kr==3*num_nodes) {
                
				if(change_community_size(num_seq)==-1) {
					
					cerr<<"\n***********************\nERROR: this program needs more than one community to work fine"<<endl;
					return -1;
                    
				}
				
				cout<<"it took too long to decide the memberships; I will try to change the community sizes"<<endl;
                
				cout<<"new community sizes"<<endl;
				for (int i=0; i<num_seq.size(); i++)
					cout<<num_seq[i]<<" ";
				cout<<endl<<endl;
				
				return (internal_degree_and_membership(mixing_parameter, overlapping_nodes, max_mem_num, num_nodes, member_matrix, excess, defect, degree_seq, num_seq, internal_degree_seq, fixed_range, nmin, nmax, tau2));
                
                
			}
			
			
		}
		
		
		
		map_nodes[available_nodes[try_this]]=i;
		
		available_nodes[try_this]=available_nodes[available_nodes.size()-1];
		available_nodes.pop_back();
		
        
        
	}
	
	
	for (int i=0; i<member_matrix.size(); i++) {
		for (int j=0; j<member_matrix[i].size(); j++)
			member_matrix[i][j]=map_nodes[member_matrix[i][j]];	
	}
	
	
	
	for (int i=0; i<member_matrix.size(); i++)
		sort(member_matrix[i].begin(), member_matrix[i].end());
    
    
	return 0;
    
}
コード例 #6
0
ファイル: random.cpp プロジェクト: Aleyasen/Alaki
void srand5(int rank) {
	
	long s=(long)(rank);
	ran4(false, s);
	
}
コード例 #7
0
ファイル: random.cpp プロジェクト: Aleyasen/Alaki
double ran4() {
	
	return ran4(true, 0);
}
コード例 #8
0
ファイル: random.cpp プロジェクト: Aleyasen/Alaki
int irand(int n) {

	return (int(ran4()*(n+1)));
	
}
コード例 #9
0
ファイル: readparams.c プロジェクト: freemanjustin/modulo
int read_parameter_file(  char *parameter_filename,
																			RUN_TIME_TYPE **pp,
																			NEURONE_TYPE **n_neurone,
																			MATCH_TYPE **match,
																			int *batch_flag )
{
	char string[241], keyword[11];
	FILE    *input_stream;
	long input_weight_offset;
	RUN_TIME_TYPE  *p;
	NEURONE_TYPE  *neurone;
	int card_type=NOTHING,
					n, j, k, r1, c1,
					tpat_count, npat_count, row_count;
	int q;
	char dflag[2],temp_str[2];
	int state_val;
	int random_val;
	int rtype_flag_temp;
	long fp;
	int num_rands = 0;
	//int				local_var;
	int r;
	int count, temp_act;


	if ( (input_stream = fopen( parameter_filename, "r" )) == NULL )
	{
									printf( "\nFatal Error - could not open input file : %s\n",parameter_filename );
									return( F_FALSE );
	}

	//  Read in the cards. The NET card is the first valid card expected.
	//  This should be followed by the TPAT cards.
	//	Then the single NPAT block should be read.

	// First allocate memory for the parameter block.
	p = (RUN_TIME_TYPE *) malloc( sizeof(RUN_TIME_TYPE) );
	*pp = p; // Set for return

	p->weight.flag = MEMORY; // use memory rather than disk
	p->number_of_patterns = 0;
	p->states = 0;
	p->actives = 0;
	p->brain_iterations = 1;
	p->noise_level = 0;
	p->match_perc       = 100.0F;
	p->report.flag = F_FALSE;

	tpat_count = 0;
	npat_count = 0;
	row_count  = 0;


	while ( fgets( string, 240, input_stream ) != NULL )
	{
									//printf("%s", string ) ;

									// Condition the input string for further processing
									uppercase(lr_pack(fix_string(string)));

									// Take care of the NET card case
									if ( strncmp( "NET", string, 3 ) == 0 )
									{
																	if ( card_type != NOTHING ) // Have already read a NET card
																	{
																									printf( "\nFatal Error - a NET card has already been read. Only\n"
																																	"one NET specification is allowed. This should be the\n"
																																	"first valid card in the file.\n");
																									return( F_FALSE );
																	}
																	else if ( sscanf( string, "%s %d %d %d %d %d %d %s %s %s %s",
																																			keyword,
																																			&(p->rows),
																																			&(p->cols),
																																			&(p->states),
																																			&(p->actives),
																																			&(p->update_cycles),
																																			&(p->num_pats),
																																			dflag,
																																			p->input_weight.file,
																																			p->output_weight.file,
																																			p->pattern.file
																																			) != 11 )
																	{
																									printf("\nFatal Error - NET card did not have enough parameters !\n");
																									return( F_FALSE );
																	}

																	// check to see if we have valid input for the int data types ...
																	if ( (p->rows) < 1 || (p->cols) < 1 || (p->num_pats) < 0 || (p->states) < 0 || (p->actives) < 0 )
																	{
																									printf("\nFatal Error - NET has illegal parameters specified !\n" );
																									return( F_FALSE );
																	}

																	// Set dflag and check input ...
																	if   ( *dflag == 'N' ) (p->rtype_flag) = NOT_DREAMING;
																	else if ( *dflag == 'R' ) (p->rtype_flag) = RANDOM_TEST;
																	else
																	{
																									printf("\nFatal Error - NET run parameters must be N or R !!" );
																									return( F_FALSE );
																	}

																	if ( *(p->input_weight.file) == '-' ) // Set up input weight file flags
																									p->input_weight.flag = F_FALSE;
																	else
																	{
																									// Make sure that the input weight file actually exists.
																									if ( (p->input_weight.stream=fopen( p->input_weight.file, "rb" )) == NULL )

																									{
																																	printf("\nFatal Error - %s was not found !!!\n", p->input_weight.file );
																																	return( F_FALSE );
																									}

																									// Make sure of matching dimensions.
																									fread( &r1, sizeof( int ), 1, p->input_weight.stream );
																									fread( &c1, sizeof( int ), 1, p->input_weight.stream );
																									input_weight_offset = ftell( p->input_weight.stream ); // Save
																									if ( r1 != (p->rows)  ||  c1 != (p->cols) )
																									{
																																	printf(  "\nFatal Error - Weight matrix mismatch...."
																																										"\n  NET image has %d rows and %d cols while"
																																										"\n  weight file %s has %d rows and %d cols."
																																										"\n  rows and columns must match !!!\n",
																																										(p->rows), (p->cols), p->input_weight.file, r1, c1);
																																	return( F_FALSE );
																									}
																									p->input_weight.flag = F_TRUE;
																	}

																	if ( *(p->output_weight.file) == '-' ) // ...output weight file flags
																									p->output_weight.flag = F_FALSE;
																	else
																									p->output_weight.flag = F_TRUE;

																	if ( (p->num_pats) == 0  && p->input_weight.flag == F_FALSE )
																	{
																									printf( "\nFatal Error - You have specified a run with no input"
																																	"\n              weight file and and no patterns to learn"
																																	"\n              from (see parameter 5 of the NET image and"
																																	"\n              your TPAT and RPAT images). Brainex needs"
																																	"\n              to get a connection weight matrix from"
																																	"\n              somewhere you ninkempoop.\n" );
																									return( F_FALSE );
																	}

																	if ( *(p->pattern.file) == '-' ) // Set up for learnt patterns
																	{
																									p->pattern.flag = F_FALSE;
																									if ( (p->rtype_flag) == RANDOM_TEST )
																									{
																																	printf( "\nFatal Error - must have pattern file "
																																									"when random testing !!\n" );
																																	return( F_FALSE );
																									}
																	}
																	else
																	{
																									// Make sure that the input pattern file actually exists. */
																									if ( (p->pattern.stream=fopen( p->pattern.file, "a+b" )) == NULL )
																									{
																																	printf("\nFatal Error - %s illegal !!!\n", p->pattern.file );
																																	return( F_FALSE );
																									}

																									fseek( p->pattern.stream, 0L, SEEK_END ); // Set to end of file
																									fp = ftell( p->pattern.stream ); // Where are we ?

																									if ( fp == 0L  &&  (p->rtype_flag) == RANDOM_TEST )
																									{
																																	printf("\nFatal Error - pattern file empty, no report possible!!\n" );
																																	return( F_FALSE );
																									}
																									else if ( fp == 0L  &&  (p->rtype_flag) != RANDOM_TEST )
																									{
																																	fwrite( &(p->rows), sizeof(int), 1, p->pattern.stream );
																																	fwrite( &(p->cols), sizeof(int), 1, p->pattern.stream );
																									}
																									else if ( fp > 0L )
																									{
																																	fseek( p->pattern.stream, 0L, SEEK_SET ); // Go to start of file
																																	// Make sure of matching dimensions
																																	fread( &r1, sizeof(int), 1, p->pattern.stream );
																																	fread( &c1, sizeof(int), 1, p->pattern.stream );
																																	if ( r1 != (p->rows)  ||  c1 != (p->cols) )
																																	{
																																									printf( "\nFatal Error - Pattern file matrix mismatch...."
																																																	"\n  NET image has %d rows and %d cols while"
																																																	"\n  learnt pattern file %s has %d rows and %d cols."
																																																	"\n  Rows and columns must match !!!\n",
																																																	(p->rows), (p->cols), p->pattern.file, r1, c1 );
																																									return( F_FALSE );
																																	}
																									}
																									p->pattern.flag = F_TRUE;
																	}

																	card_type = NET;

																	// set the number of modules in the network
																	p->number_of_neurones = (p->rows) * (p->cols);
																	n = p->number_of_neurones;

																	// If we are here then we must have found a NET card with valid parameters
																	if ( ( neurone = (NEURONE_TYPE *) malloc( sizeof(NEURONE_TYPE) * n ) ) == NULL )
																	{
																									printf( "\nFatal Error - rows and columns specify a neural grid that\n"
																																	"                exceeds the available system memory.\t:(\n" );
																									return( F_FALSE );
																	}

																	*n_neurone = neurone; // Set for return

																	//printf("\ntrying to malloc some memory for the weight file...\n");

																	// get memory for the weights
																	count = 0;
																	for ( j=0; j<n; j++ )
																	{
																									(neurone+j)->id_num = j;
																									(neurone+j)->nw     = n*n*(p->states)*(p->states);// we have n squared weights
																									// malloc the top level:
																									if ( ((neurone+j)->w = (WEIGHT_MATRIX **)malloc(sizeof(WEIGHT_MATRIX)*n)) == NULL)
																									{
																																	printf( "\n Not enough memory for the weight grid\n");
																																	return( F_FALSE );
																									}
																									else // malloc each matrix
																									{
																																	for(k=0; k<n; k++)
																																	{
																																									// malloc rows:
																																									if ( ((neurone+j)->w[k] = (WEIGHT_MATRIX *)malloc(sizeof(WEIGHT_MATRIX)*n)) == NULL)
																																									{
																																																	return( F_FALSE);
																																									}
																																									if (((neurone+j)->w[k]->weight = (int **) malloc( (p->states) * sizeof(int*))) == NULL )
																																									{
																																																	printf( "\n Not enough memory for the weight grid...\n");
																																																	return( F_FALSE );
																																									}
																																									// and finally malloc cols:
																																									for (q = 0; q<(p->states); q++)
																																									{
																																																	if (((neurone+j)->w[k]->weight[q] = malloc( (p->states) * sizeof(int))) == NULL )
																																																	{
																																																									printf( "\n Not enough memory for the weight grid...\n");
																																																									return( F_FALSE );
																																																	}
																																																	// init elements of the weight matrix to zero
																																																	else
																																																	{
																																																									for(r=0; r<(p->states); r++)
																																																									{
																																																																	(neurone+j)->w[k]->weight[q][r] = 0; //initialise to zero
																																																																	count++;
																																																																	//printf("j = %d\tk = %d\tweight matrix: weight[%d][%d] = %d\n",j,k,q,r,(neurone+j)->w[k]->weight[q][r]);
																																																									}
																																																	}
																																									}
																																	}
																									}
																	}

																	//printf("\nmalloc of weight memory done. did it work?\tcount = %d\tshould be %d\n\n",count,n*n*(p->states)*(p->states));
																	// read in the weights from file if requested to do so
																	if ( p->input_weight.flag == F_TRUE )
																	{
																									count = 0;
																									//printf("\nreading in predefined weights from file %s\n",p->input_weight.file);
																									for(j=0; j<n; j++)
																									{
																																	for(k=0; k<n; k++)
																																	{
																																									for(q=0; q< (p->states); q++)
																																									{
																																																	for(r=0; r< (p->states); r++)
																																																	{
																																																									fread(&((neurone+j)->w[k]->weight[q][r]), sizeof(int),1, p->input_weight.stream);
																																																									count++;
																																																									//printf("j = %d\tk = %d\tweight matrix: weight[%d][%d] = %d\n",j,k,q,r,(neurone+j)->w[k]->weight[q][r]);
																																																	}
																																									}
																																	}
																									}
																									// close the file
																									fclose( p->input_weight.stream );
																									//printf("read %d elements from file %s\tshould have read %d elements!\n",count,p->input_weight.file,n*n*(p->states)*(p->states));
																	}

																	// get memory for states per module
																	if (p->states == 0)
																	{
																									printf("p->states == 0. something bad is about to happen...\n");
																									return(F_FALSE);
																	}
																	else
																	{
																									for ( j=0; j<n; j++ )
																									{
																																	if ( ( (neurone+j)->states = (int *) malloc( sizeof(int)*(p->states) ) ) == NULL )
																																	{
																																									printf( "\nNot enough memory for the state vectors.\n");
																																									return( F_FALSE );
																																	}
																																	else
																																	{
																																									for ( k = 0; k < (p->states); k++ )
																																									{
																																																	(neurone+j)->states[k] = 0; // Initialise to 0
																																									}
																																	}
																									}
																	}
									} // end of the NET case

									// *****************************************************************************************

									// Take care of the TPAT/RPAT card case
									if ( strncmp( "TPAT", string, 4 ) == 0 || strncmp( "RPAT", string, 4 ) == 0     )
									{
																	// Investigate the entry status
																	if ( card_type != RPAT  &&  card_type != TPAT  &&  card_type != NET )
																	{
																									printf(  "\nFatal Error - Only a NET, TPAT or RPAT image may\n"
																																		"precede a TPAT or RPAT image. A NET card must precede\n"
																																		"all TPAT/RPAT blocks. Exactly one NET card must appear\n"
																																		"and it must be the first valid card in the parameter\n"
																																		"specification file.\n" );
																									return( F_FALSE );
																	}
																	else if ( card_type == TPAT  &&  row_count != (p->rows) )
																	{
																									printf( "\nFatal Error - TPAT Pattern block %d should have %d rows,"
																																	"\n              only %d rows were actually counted !\n",
																																	tpat_count, (p->rows), row_count );
																									return( F_FALSE );
																	}

																	if ( strncmp( "TPAT", string, 4)== 0)
																									card_type = TPAT;
																	else if ( strncmp( "RPAT", string, 4)== 0)
																									card_type = RPAT;

																	row_count = 0; // This is used to control image access

																	tpat_count++; // Use this to count both TPAT and RPAT images
																	if ( tpat_count > (p->num_pats) )
																	{
																									printf( "\nFatal Error - Too many TPAT/RPAT definitions. The number\n"
																																	"of TPAT/RPAT definitions must match the NET specification.\n" );
																									return( F_FALSE );
																	}
																	for(j=0; j<n; j++) // zero the states for TPAT reading
																	{
																									for(k=0; k<(p->states); k++)
																									{
																																	(neurone+j)->states[k] = 0; // zero the states
																									}
																									(neurone+j)->tag = 0; // and the tags
																	}
									} // end of the TPAT/RPAT case

									// Take care of the NPAT/RANDIN card case
									if ( strncmp( "NPAT", string, 4 ) == 0 || strncmp( "RANDIN", string, 6 ) == 0)
									{
																	if ( tpat_count != (p->num_pats) )
																	{
																									printf( "\nFatal Error - Not enough TPAT/RPAT sets before NPAT!!\n");
																									return( F_FALSE );
																	}
																	else if ( card_type == TPAT  &&  row_count != (p->rows) )
																	{
																									printf( "\nFatal Error - TPAT Pattern block %d should have %d rows,"
																																	"\n                only %d rows were actually counted !\n",
																																	tpat_count, (p->rows), row_count );
																									return( F_FALSE );
																	}
																	else if ( npat_count > 0 )
																	{
																									printf( "\nFatal Error - Only 1 NPAT/RANDIN block is allowed !!\n");
																									return( F_FALSE );
																	}

																	if ( strncmp( "NPAT", string, 4 ) == 0  )
																	{
																									row_count = 0; // We have to count the rows....
																	}

																	if ( strncmp( "RANDIN", string, 4 ) == 0  )
																	{
																									row_count = (p->rows); // Set up as dummy value

																									if ( sscanf(string, "%s %d %d %f %s",
																																					keyword,
																																					&(p->brain_iterations),&(p->noise_level),
																																					&(p->match_perc),
																																					p->report.file        ) != 5 )
																									{
																																	printf("\nFatal Error - RANDIN card, not enough parameters !\n");
																																	return( F_FALSE );
																									}

																									if ( (p->brain_iterations) < 1 )
																									{
																																	printf( "\nFatal Error - RANDIN parameters illegal - "
																																									"\n              Number of random cycles must be >= 1" );
																																	return( F_FALSE );
																									}

																									// Check on the report file.
																									if ( (p->report.stream=fopen( p->report.file, "a+" )) == NULL )
																									{
																																	printf("\nFatal Error - %s illegal !!!\n", p->report.file );
																																	return( F_FALSE );
																									}
																									p->report.flag = F_TRUE; // Enable

																									// Start the report
																									report_header( p->report.stream, p );

																									p->number_of_patterns = get_number_of_patterns( p->pattern.stream );

																									report_patterns( p->report.stream, p->pattern.stream );

																									// Get memory to be used in report of stats
																									*match = (MATCH_TYPE *) malloc( sizeof(MATCH_TYPE) * ( p->number_of_patterns + 1 ) );
																	}
																	card_type = NPAT;
																	npat_count = 1;
									} // end of the NPAT/RANDIN case


									if ( ( card_type == TPAT || card_type == NPAT) && ( strncmp( "0", string, 1 ) == 0 ||
																																																													strncmp( "1", string, 1 ) == 0 || strncmp( "2", string, 1 ) == 0 ||
																																																													strncmp( "3", string, 1 ) == 0 || strncmp( "4", string, 1 ) == 0 ||
																																																													strncmp( "5", string, 1 ) == 0 || strncmp( "6", string, 1 ) == 0 ||
																																																													strncmp( "7", string, 1 ) == 0 || strncmp( "8", string, 1 ) == 0 ||
																																																													strncmp( "9", string, 1 ) == 0   )  )
									{
																	row_count++;
																	if ( row_count > (p->rows) )
																	{
																									printf( "\nFatal Error - Pattern block must have only %d rows!\n",(p->rows));
																									return( F_FALSE );
																	}

																	if ( (int)strlen( string ) != (p->cols) )
																	{
																									printf( "\nFatal Error - Pattern block bad row %d\n\n"
																																	"There are an incorrect number of columns.\n"
																																	"in the pattern string.\n"
																																	"The number of columns found was : %d\n"
																																	"There should be exactly %d columns.\n",
																																	row_count, (int)strlen( string ), (p->cols) );
																									return( F_FALSE );
																	}

																	if ( (int)strspn( string, "0123456789" ) != (p->cols)  )
																	{
																									printf( "\nFatal Error - Pattern block bad at row %d\n\n"
																																	"There are illegal characters in the pattern string.\n"
																																	"Only integers are allowed in a pattern string.\n",
																																	row_count );
																									return( F_FALSE );
																	}

																	// fill in the neural array with the TPAT/NPAT values...
																	//printf("temp_act: NPAT/TPAT\n");
																	for(k=0; k<(p->cols); k++)
																	{
																									strncpy(temp_str, &string[k], sizeof(char));
																									temp_act = atoi(&temp_str[0]);
																									//printf("%d",temp_act);
																									if(temp_act != 0)
																																	(neurone+((row_count-1)*(p->cols)+k))->states[temp_act-1] = 1; // set the state
																	}
																	//printf("\n");



																	//  If the neural array has been filled with the input
																	//	activation values then we can calculate the connecting
																	//	weights (to this point).

																	if ( card_type == TPAT && row_count == (p->rows) )
																	{
																									rtype_flag_temp   = p->rtype_flag;// Preserve for restoration
																									p->rtype_flag   = NOT_DREAMING;// Set for call to update_...

																									update_neural_weights( p, neurone );

																									p->rtype_flag   = rtype_flag_temp;// Restore

																									/*
																									   printf("\nTPAT follows\n");
																									   printf("\n");

																									   for(j=0;j<n;j++)
																									   {
																									   local_var = 0;
																									   for(k=0;k<(p->states);k++)
																									   {
																									   if((neurone+j)->states[k] == 0)
																									   local_var++;
																									   else
																									   {
																									   printf("%d",local_var+1);
																									   break;
																									   }
																									   }
																									   if( local_var == (p->states))
																									   printf("0");
																									   if( (j+1) % (p->cols) == 0)
																									   printf("\n");
																									   }
																									 */
																	}

									}
									else if (  card_type != TPAT && card_type != NPAT &&
																				( strncmp( "0", string, 1 ) == 0 || strncmp( "1", string, 1 ) == 0 ||
																						strncmp( "2", string, 1 ) == 0 || strncmp( "3", string, 1 ) == 0 ||
																						strncmp( "4", string, 1 ) == 0 || strncmp( "5", string, 1 ) == 0 ||
																						strncmp( "6", string, 1 ) == 0 || strncmp( "7", string, 1 ) == 0 ||
																						strncmp( "8", string, 1 ) == 0 || strncmp( "9", string, 1 ) == 0 ))
									{
																	printf("\nFatal Error - Pattern detected without a preceding TPAT!\n" );
																	return( F_FALSE );
									}

									// Now process RPAT. This means the generation of a random pattern.
									if ( card_type == RPAT  &&  row_count == 0 )
									{
																	for(j=0; j<n; j++)
																	{
																									for(k=0; k<(p->states); k++)
																									{
																																	(neurone+j)->states[k] = 0; // zero the states
																									}
																									(neurone+j)->tag = 0; // and the tags
																	}
																	// Fill in the neural array....
																	num_rands = 0;
																	//printf("generating an RPAT...\n");
																	do
																	{
																									random_val = (int)ceil((n-1) * ran4());
																									//printf("random_val = %d\n",random_val);
																									if( (neurone+(int)random_val)->tag != 1 )
																									{
																																	state_val = (int)ceil((p->states)*ran4());
																																	//printf("\tstate_val = %d\n",state_val);
																																	(neurone+(int)random_val)->states[(int)state_val-1] = 1; // -1 because 0 is the first element
																																	(neurone+(int)random_val)->tag=1;
																																	num_rands++;
																									}
																	} while(num_rands != (p->actives));

																	/*
																	   printf("\nRPAT follows\n");
																	   printf("\n");

																	   for(j=0;j<n;j++)
																	   {
																	   local_var = 0;
																	   for(k=0;k<(p->states);k++)
																	   {
																	   if((neurone+j)->states[k] == 0)
																	   local_var++;
																	   else
																	   {
																	   printf("%d",local_var+1);
																	   break;
																	   }
																	   }
																	   if( local_var == (p->states))
																	   printf("0");
																	   if( (j+1) % (p->cols) == 0)
																	   printf("\n");
																	   }
																	 */


																	//  If the neural array has been filled with the input
																	//	activation values then we can calculate the connecting
																	//	weights (to this point)


																	rtype_flag_temp   = p->rtype_flag;// Preserve for restoration
																	p->rtype_flag   = NOT_DREAMING;// Set for call to update_...
																	//printf("\n calculating weights.....\n");
																	update_neural_weights( p, neurone ); // calculate the connecting weights up to this point ***************

																	p->rtype_flag   = rtype_flag_temp;// Restore


																	// We set row_count to force a new TPAT/RPAT before re-entry
																	row_count = (p->rows);
									}
	} // End of the outer while loop

	// Make sure that there were enough patterns
	if ( tpat_count != (p->num_pats) )
	{
									printf(  "\nFatal Error - Incorrect number of TPAT definitions.\n"
																		"The number of TPAT definitions must match the\n"
																		"NET specification.\n\n"
																		"Number of TPAT blocks from the NET specification : %d\n"
																		"Number of TPAT blocks actually found             : %d\n",
																		(p->num_pats), tpat_count );
									return( F_FALSE );
	}
	else if ( npat_count != 1 )
	{
									printf("\nFatal Error - There should be exactly 1 NPAT block!\n" );
									return( F_FALSE );
	}
	else if ( row_count != (p->rows)  &&  (p->update_cycles) > 0 )
	{
									printf(  "\nFatal Error - NPAT pattern block should have %d rows,\n"
																		"                only %d rows were actually counted !\n",
																		(p->rows), row_count );
									return( F_FALSE );
	}

	fclose( input_stream ); // Close the input parameter file
	return( F_TRUE ); // and return to main()
}
コード例 #10
0
int benchmark(bool excess, bool defect, int num_nodes, double  average_k, int  max_degree, double  tau, double  tau2, 
	double  mixing_parameter, int  overlapping_nodes, int  overlap_membership, int  nmin, int  nmax, bool  fixed_range, double ca) {	

	
	
	
	
	double dmin=solve_dmin(max_degree, average_k, -tau);
	if (dmin==-1)
		return -1;
	
	int min_degree=int(dmin);
	
	
	double media1=integer_average(max_degree, min_degree, tau);
	double media2=integer_average(max_degree, min_degree+1, tau);
	
	if (fabs(media1-average_k)>fabs(media2-average_k))
		min_degree++;
	
	
	
	
	
	
		
	// range for the community sizes
	if (!fixed_range) {
	
		nmax=max_degree;
		nmin=max(int(min_degree), 3);
		cout<<"-----------------------------------------------------------"<<endl;
		cout<<"community size range automatically set equal to ["<<nmin<<" , "<<nmax<<"]"<<endl;
		

	}
	
	
	//----------------------------------------------------------------------------------------------------
	
	
	deque <int> degree_seq ;		//  degree sequence of the nodes
	deque <double> cumulative;
	powerlaw(max_degree, min_degree, tau, cumulative);
	
	for (int i=0; i<num_nodes; i++) {
		
		int nn=lower_bound(cumulative.begin(), cumulative.end(), ran4())-cumulative.begin()+min_degree;
		degree_seq.push_back(nn);
	
	}
	
	
	
	sort(degree_seq.begin(), degree_seq.end());
		
	if(deque_int_sum(degree_seq) % 2!=0)
		degree_seq[max_element(degree_seq.begin(), degree_seq.end()) - degree_seq.begin()]--;
	
	
	deque<deque<int> >  member_matrix;
	deque<int> num_seq;
	deque<int> internal_degree_seq;
	
	// ********************************			internal_degree and membership			***************************************************

	
	

	if(internal_degree_and_membership(mixing_parameter, overlapping_nodes, overlap_membership, num_nodes, member_matrix, excess, defect, degree_seq, num_seq, internal_degree_seq, fixed_range, nmin, nmax, tau2)==-1)
		return -1;
	
	
	
	deque<set<int> > E;					// E is the adjacency matrix written in form of list of edges
	deque<deque<int> > member_list;		// row i cointains the memberships of node i
	deque<deque<int> > link_list;		// row i cointains degree of the node i respect to member_list[i][j]; there is one more number that is the external degree

	
	
	cout<<"building communities... "<<endl;
	if(build_subgraphs(E, member_matrix, member_list, link_list, internal_degree_seq, degree_seq, excess, defect)==-1)
		return -1;	
	




	cout<<"connecting communities... "<<endl;
	connect_all_the_parts(E, member_list, link_list);
	


	if(erase_links(E, member_list, excess, defect, mixing_parameter)==-1)
		return -1;

	
	
	
	if(ca!=unlikely) {
		cout<<"trying to approach an average clustering coefficient ... "<<ca<<endl;
		cclu(E, member_list, member_matrix, ca);
	}
	
	
	
	
	cout<<"recording network..."<<endl;	
	print_network(E, member_list, member_matrix, num_seq);

	
	
	
		
	return 0;
	
}