通过似然分析预测丢失的边和虚假的边--的C语言代码实现

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

int kc = 12;
int training_set_size;
int probe_set_size;
int vertax_set_size;
char training_filename[200];
char probe_filename[200];
char vertax_filename[200];

int** training_set;
double** adjacentMatrix;
double* beta_thirdParty;
double obverse_Hamiltonian;
int** probe_set;
double* score_probe;
int** nonExist_set;
int nonExist_set_size;
double* score_nonExist;

double* Hamiltonian_beta_1;
double* Hamiltonian_beta_1_var;
double** input_GA;
int* exist_label;
double* beta_value;

void readTrainingSetFromFile();
void createAdjacentMatrix();
double calculate_Hamiltonian(double** adjacentMatrix, int is_obverse, int);
double Tr(double** adjacentMatrix);
double** calculate_matrix_power(double** adjacentMatrix, int k);
double** calculate_matrix_multiple(double** matrix_1, double** matrix_2);
void setBeta();
void readProbeSetFromFile();
void calcu_score_probe();
void get_non_exist_eage();
void calcu_score_nonExist();
void calcu_AUC();

void calcu_input_GA();
void gradientAscent_alogrithm();
double sigmoid(double);

int main(int argc, char* argv[])
{
	if( argc != 7 )
	{
		printf("\tThis algorithm require 7 parameters"
				"\n\t\t1:the size of training set"
				"\n\t\t2:the filename contain training set"
				"\n\t\t3:the size of all vertax"
				"\n\t\t4:the filename contain vertax-data"
				"\n\t\t5:the size of probe set"
				"\n\t\t6:the filename contain probe set"
				"\n");
		exit(0);
	}
	training_set_size = atoi(argv[1]);
	strcat(training_filename, argv[2]);
	vertax_set_size = atoi(argv[3]);
	strcat(vertax_filename, argv[4]);
	probe_set_size = atoi(argv[5]);
	strcat(probe_filename, argv[6]);

	printf("training size: %d\ttraining filename:%s\tvertax size: %d\tprobe size: %d\tprobe filename:%s\n", training_set_size, training_filename, vertax_set_size, probe_set_size, probe_filename);
	readTrainingSetFromFile();
	createAdjacentMatrix();

	setBeta();

	printf("the Hamiltonain value of obverse is %f\n", calculate_Hamiltonian(adjacentMatrix, 1, 0));

	readProbeSetFromFile();
	calcu_score_probe();
	get_non_exist_eage();
	calcu_score_nonExist();

	calcu_AUC();
	return 0;
}

/*
 * read training set from file
 * */
void readTrainingSetFromFile()
{
	FILE* f_read;
	if( NULL == (f_read = fopen(training_filename, "r")))
	{
		printf("open file(%s) error!\n", training_filename);
		exit(0);
	}
	//create dynamic array for storing training set form file @filename
	training_set = (int**)malloc(sizeof(int*) * (training_set_size + 1));
	if( !training_set )
	{
		printf("malloc error: training_set**\n");
		exit(0);
	}
	int i;
	for( i = 1; i <= training_set_size; i++ )
	{
		training_set[i] = (int*)malloc(sizeof(int) * (2 + 1));
		if( !training_set[i] )
		{
			printf("training_set[%d] malloc error", i);
			exit(0);
		}
	}
	//read training set from file
	for( i = 1; i <= training_set_size; i++ )
	{
		if( 2 != fscanf(f_read, "%d %d", &training_set[i][1], &training_set[i][2]))
		{
			printf("fscanf error: %d\n", i);
			exit(0);
		}
	}
}

/*
 * careat the adjacent matrix
 * */
void createAdjacentMatrix()
{
	//create the dynamic array for saving adjcaent matrix
	adjacentMatrix = (double**)malloc(sizeof(double*) * (vertax_set_size + 1));
	if( !adjacentMatrix )
	{
		printf("adjacentMatrix** malloc error");
		exit(0);
	}
	int i;
	for( i = 1; i <= vertax_set_size; i++ )
	{
		adjacentMatrix[i] = (double*)malloc(sizeof(double) * (vertax_set_size + 1));
		if( !adjacentMatrix[i] )
		{
			printf("adjacentMatrix[%d] malloc error");
			exit(0);
		}
	}
	//initial the value of adjacentMatrix
	int j;
	for( i = 1; i <= vertax_set_size; i++ )
	{
		for( j = 1; j <= vertax_set_size; j++ )
		{
			adjacentMatrix[i][j] = 0.0;
		}
	}

	//set the value for adjacentMatrix 
	for( i = 1; i <= training_set_size; i++ )
	{
		adjacentMatrix[training_set[i][1]][training_set[i][2]] = 1;
		adjacentMatrix[training_set[i][2]][training_set[i][1]] = 1;
	}
}

/*
 * calculate the Hamiltonian of network
 * */
double calculate_Hamiltonian(double** adjacentMatrix, int is_obverse, int isBeta_1)
{
	double** power = (double**)malloc(sizeof(double*) * (vertax_set_size + 1));
	if( !power )
	{
		printf("function calculate_Hamiltonian: power_3** malloc error");
		exit(0);
	}
	int i, j, k;
	for( i = 1; i <= vertax_set_size; i++ )
	{
		power[i] = (double*)malloc(sizeof(double) * (vertax_set_size + 1));
		if( !power[i] )
		{
			printf("function calculate_Hamiltonian: power[%d]* malloc error");
			exit(0);
		}
	}

	power = calculate_matrix_power(adjacentMatrix, 3);

	//calculate the Hamiltonian of obverse network
	double H = 0.0;
	double temp = 0.0;
	for( i = 3; i <= kc; i++ )
	{
		if( Tr(power) > 0 )
			temp = log(Tr(power));
		else
			temp = 0;
		
		if( isBeta_1 == 1 )		//for get beta
		{
			if( is_obverse == 1 )
			{
				Hamiltonian_beta_1[i - 2] = temp;
			}
			else if( is_obverse != 1 )
			{
				Hamiltonian_beta_1_var[i - 2] = temp;
			}
		}
		else if( isBeta_1 != 1 )	//for get Hamiltonian
		{
			H = H + beta_thirdParty[i] * temp;
		}
		if( i < kc )
		{
			power = calculate_matrix_multiple(power, adjacentMatrix);
		}
	}

	if( is_obverse == 1 && isBeta_1 != 1 )	//save the original network Hamiltonian as a global variable
	{
		obverse_Hamiltonian = H;
	}
	//printf(" ------the Hamiltonian value is %10.9f\n", H);
	return H;
}
/*
 * calculate the Tr of a matrix
 * */
double Tr(double** adjacentMatrix)
{
	int i;
	double Tr_value = 0.0;
	for( i = 1; i <= vertax_set_size; i++ )
	{
		Tr_value = Tr_value + adjacentMatrix[i][i];
	}
	return Tr_value;
}
double** calculate_matrix_power(double** adjacentMatrix, int k)
{
	double** result = (double**)malloc(sizeof(double*) * (vertax_set_size + 1));
	if( !result )
	{
		printf("function: calculate_Tr: result** malloc error");
		exit(0);
	}
	int i;
	for( i = 1; i <= vertax_set_size; i++ )
	{
		result[i] = (double*)malloc(sizeof(double) * (vertax_set_size + 1));
		if( !result[i] )
		{
			printf("function: calculate_Tr: result[%d]* malloc error", i);
			exit(0);
		}
	}
	int j;
	for( i = 1; i <= vertax_set_size; i++ )
		for( j = 1; j <= vertax_set_size; j++ )
			result[i][j] = adjacentMatrix[i][j];
	for( i = 1; i < k; i++ )
	{
		//printf("\n-------------------%d----------------------\n", i + 1);
		result = calculate_matrix_multiple((double**)result, (double**)adjacentMatrix);
	}
	return result;
}
double** calculate_matrix_multiple(double** matrix_1, double** matrix_2)
{
	int i, j, k;
	double** result = (double**)malloc(sizeof(double*) * (vertax_set_size + 1));
	if( !result )
	{
		printf("function: matrix_multiplication: result** malloc error");
		exit(0);
	}
	for( i = 1; i <= vertax_set_size; i++ )
	{
		result[i] = (double*)malloc(sizeof(double) * (vertax_set_size + 1));
		if( !result[i] )
		{
			printf("function: matrix_Multiple: result[%d]* malloc error", i);
			exit(0);
		}
	}
	for( i = 1; i <= vertax_set_size; i++ )
		for( j = 1; j <= vertax_set_size; j++ )
			result[i][j] = 0.0;
	for( i = 1; i <= vertax_set_size; i++ )
	{
		for( j = 1; j <= vertax_set_size; j++ )
		{
			for( k = 1; k <= vertax_set_size; k++ )
			{
				//using method 1 when parameter matrix not a poniter, for example in test()
				//result[i][j] = result[i][j] + (*((int*)matrix_1 + i * (vertax_size + 1) + k)) * (*((int*)matrix_2 + (vertax_size + 1) * k + j));
				result[i][j] = result[i][j] + matrix_1[i][k] * matrix_2[k][j];
			}
		}
	}
	return result;
}

/*
 * read parameter \beta from file
 * */
void setBeta()
{
	FILE* fread_beta;
	char filename_beta[20] = "beta_value.data";
	if( NULL == (fread_beta = fopen(filename_beta, "r")))
	{
		printf("function: setBeta: open file(%s) error", filename_beta);
		exit(0);
	}
	beta_thirdParty = (double*)malloc(sizeof(double) * (kc + 1));
	if( !beta_thirdParty )
	{
		printf("function: setBeta: beta_thirdParty error");
		exit(0);
	}
	int i;
	for( i = 1; i <= kc; i++ )
	{
		if( 1 != fscanf(fread_beta, " %lf ", &beta_thirdParty[i]))
		{
			printf("function: setBeta: fscanf error: %d", i);
			exit(0);
		}
	}

	//test
	printf("show the value of beta from third_party_calculate:\n");
	for( i = 1; i <= kc; i++ )
		printf("%f\t", beta_thirdParty[i]);
	printf("\n");
	//test end
}

/*
 * read probe edge
 * */
void readProbeSetFromFile()
{
	FILE* f_read_probe;
	if( NULL == (f_read_probe = fopen(probe_filename, "r")))
	{
		printf("function: readProbeSetFromFile: open file(%s) error", probe_filename);
		exit(0);
	}
	probe_set = (int**)malloc(sizeof(int*) * (probe_set_size + 1));
	if( !probe_set )
	{
		printf("function: readProbeSetFromFile: probe_set** malloc error");
		exit(0);
	}
	int i;
	for( i = 1; i <= probe_set_size; i++ )
	{
		probe_set[i] = (int*)malloc(sizeof(int) * (2 + 1));
		if( !probe_set[i] )
		{
			printf("function:readProbeSetFromFile: probe_set[%d] malloc error", i);
			exit(0);
		}
	}

	//read from file
	for( i = 1; i <= probe_set_size; i++ )
	{
		if( 2 != fscanf(f_read_probe, "%d %d", &probe_set[i][1], &probe_set[i][2]))
		{
			printf("function:readProbeSetFromFile:fscanf error %d\n", i);
			exit(0);
		}
	}
	fclose(f_read_probe);
}

/*
 * calculate the score of probe set 
 * */
void calcu_score_probe()
{
	score_probe = (double*)malloc(sizeof(double) * (probe_set_size + 1));
	if( !score_probe )
	{
		printf("function:calcu_score_probe:score_probe* malloc error");
		exit(0);
	}
	int i;
	double add_link = 0.0;
	printf("the progess of procedure 1: ");
	int progess;
	for( i = 1; i <= probe_set_size; i++ )
	{
		progess = (int)((double)i / probe_set_size * 100);
		printf("%d%%", progess);

		adjacentMatrix[probe_set[i][1]][probe_set[i][2]] = 1;
		adjacentMatrix[probe_set[i][2]][probe_set[i][1]] = 1;
		add_link = calculate_Hamiltonian(adjacentMatrix, 0, 0);
		score_probe[i] = add_link;
		
		adjacentMatrix[probe_set[i][1]][probe_set[i][2]] = 0;
		adjacentMatrix[probe_set[i][2]][probe_set[i][1]] = 0;

		if( progess < 10 )
			printf("\b\b");
		else if( progess < 100 )
			printf("\b\b\b");
	}
	printf("\n");
}

/*
 * get the non-exist edge
 * */
void get_non_exist_eage()
{
	// step 1
	double** temp_adjacent = (double**)malloc(sizeof(double*) * (vertax_set_size + 1));
	if( !temp_adjacent )
	{
		printf("function:get_non_exist_eage: temp_adjacent** malloc error");
		exit(0);
	}
	int i, j;
	for( i = 1; i <= vertax_set_size; i++ )
	{
		temp_adjacent[i] = (double*)malloc(sizeof(double) * (vertax_set_size + 1));
		if( !temp_adjacent[i] )
		{
			printf("function:get_non_exist_eage: temp_adjcacent[%d]* malloc error", i);
			exit(0);
		}
	}
	for( i = 1; i <= vertax_set_size; i++ )
		for( j = 1; j <= vertax_set_size; j++ )
			temp_adjacent[i][j] = adjacentMatrix[i][j];

	// step 2
	for( i = 1; i <= probe_set_size; i++ )
	{
		temp_adjacent[probe_set[i][1]][probe_set[i][2]] = 1;
		temp_adjacent[probe_set[i][2]][probe_set[i][1]] = 1;
	}

	
	// step 3
	nonExist_set_size = vertax_set_size * (vertax_set_size - 1) / 2 - training_set_size - probe_set_size;
	//printf("the size of 0; %d\n", nonExist_set_size);
	nonExist_set = (int**)malloc(sizeof(int*) * (nonExist_set_size + 1));
	if( !nonExist_set )
	{
		printf("function:get_non_exist_eage: nonExist_set** malloc error");
		exit(0);
	}
	for( i = 1; i <= nonExist_set_size; i++ )
	{
		nonExist_set[i] = (int*)malloc(sizeof(int) * (2 + 1));
		if( !nonExist_set[i] )
		{
			printf("function: get_non_exist_eage: nonExist_set[%d] malloc error", i);
			exit(0);
		}
	}
	int counter_nonExist = 0;
	for( i = 1; i <= vertax_set_size; i++ )
	{
		for( j = i + 1; j <= vertax_set_size; j++ )
		{
			if( temp_adjacent[i][j] == 0 )
			{
				counter_nonExist++;
				nonExist_set[counter_nonExist][1] = i;
				nonExist_set[counter_nonExist][2] = j;
			}
		}
	}
}

/*
 * calculate the score of non-exist edge
 * */
void calcu_score_nonExist()
{
	score_nonExist = (double*)malloc(sizeof(double) * (nonExist_set_size + 1));
	if( !score_nonExist )
	{
		printf("function:calcu_score_nonExist: score_nonExist* malloc error");
		exit(0);
	}
	int i;
	double add_link = 0.0;
	printf("the progess of procedure 2: ");
	int progess;
	for( i = 1; i <= nonExist_set_size; i++ )
	{
		progess = (int)((double)i / nonExist_set_size * 100);
		printf("%d%%", progess);
		adjacentMatrix[nonExist_set[i][1]][nonExist_set[i][2]] = 1;
		adjacentMatrix[nonExist_set[i][2]][nonExist_set[i][1]] = 1;
		add_link = calculate_Hamiltonian(adjacentMatrix, 0, 0);
		score_nonExist[i] = add_link;

		adjacentMatrix[nonExist_set[i][1]][nonExist_set[i][2]] = 0;
		adjacentMatrix[nonExist_set[i][2]][nonExist_set[i][1]] = 0;

		if( progess < 10 )
			printf("\b\b");
		else if( progess < 100 )
			printf("\b\b\b");
	}
	printf("\n");
}

void calcu_AUC()
{
	int i, j, k = 0;
	double AUC = 0.0;

	printf("the progess of procedure 3: ");
	int progess;
	for( i = 1; i <= probe_set_size; i++ )
	{
		progess = (int)((double)i / probe_set_size * 100);
		printf("%d%%", progess);
		for( j = 1; j <= nonExist_set_size; j++ )
		{
			if( score_probe[i] > score_nonExist[j] )
				AUC += 1.0;
			else if( score_probe[i] == score_nonExist[j] )
				AUC += 0.5;
			else
				k++;
		}

		if( progess < 10 )
			printf("\b\b");
		else if( progess < 100 )
			printf("\b\b\b");
	}
	printf("\nAUC--------%f \n", AUC);
	printf("AUC = %f\n", AUC / (double)(probe_set_size * nonExist_set_size));
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值