⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 challoc.cpp

📁 一种OFDMA低复杂度的比例资源分配策略仿真程序,C语言编程的
💻 CPP
字号:
/***************************************************************************
Author: Ian C. Wong and Robert Mullenix   
Copyright (C) 2004   Ian C. Wong

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

You may reach the author at wongic@mail.utexas.edu.
Or visit his website at www.ece.utexas.edu/~iwong
******************************************************************************/

#include "SIPS.h"

void Shen_Challoc(SNRArray *SNR, IDXArray *IDX, int user, int *Shen_Assignment, 
				float *Proportionality, int *Hmin)
{
	int i, n, k;
	float* Data_rate;
	int *curIDX;
	
	curIDX = (int *) malloc(user*sizeof(int));	
	memset(curIDX, 0, user*sizeof(int));
	Data_rate = (float *) malloc(user*sizeof(float));

//  First round, have each user select the channel with the best SNR
	for (k = 0; k < user; ++k) {
		// sort in descending order
		qsort((float *)SNR[k], (int *)IDX[k], N);
		//n = Best_SNR(k, Shen_Assignment, IDX, curIDX); // assign the index with largest value, w/c is first element
		n = Best_SNR(Shen_Assignment, IDX[k], &curIDX[k]);
		Shen_Assignment[n] = k;
		Data_rate[k] = log2f(1 + p*(float) SNR[k][n]);
	}
//  Second round, give the worst user the next pick until all subchannels distributed
	for (i = 0; i < N - user; ++i) {
		k = Worst_user(user, Data_rate, Proportionality);
		//n = Best_SNR(k, Shen_Assignment, IDX, curIDX);
		n = Best_SNR(Shen_Assignment, IDX[k], &curIDX[k]);		
		Shen_Assignment[n] = k;
		Hmin[k] = n;
		Data_rate[k] += log2f(1 + p*(float) SNR[k][n]);					
	}
	free(Data_rate);
	free(curIDX);
}

void Wong_Challoc(SNRArray *SNR, IDXArray *IDX, int user, int *Wong_Assignment, 
				float *Proportionality, int *Hmin)
{
	int i, n, k;
	float* Data_rate;
	int *Nk;
	char *Exclude;	
	int *curIDX;
	register int Num_users = user;
	int N_star = N;
	register int tmp;
	
	curIDX = (int *) malloc(user*sizeof(int));	
	Data_rate = (float *) malloc(user*sizeof(float));
	Nk = (int *)malloc(user*sizeof(int));	
	Exclude = (char *)malloc(user*sizeof(char));	
	memset(curIDX, 0, user*sizeof(int));
	memset(Exclude, 0, user*sizeof(char));
	
	for (k = 0; k < user; ++k) {
		tmp = (int)(Proportionality[k]*N);
		N_star -= tmp;
		Nk[k] = tmp;
	}

//  First round, have each user select the channel with the best SNR
	for (k = 0; k < user; ++k) {
		// sort in descending order
		qsort((float *)SNR[k], (int *)IDX[k], N);
		n = Best_SNR(Wong_Assignment, IDX[k], &curIDX[k]);
		Wong_Assignment[n] = k;
		Data_rate[k] = log2f(1 + p*(float) SNR[k][n]);
		Nk[k]--;
	}
//  Second round, give the worst user the next pick until all subchannels distributed
	while(Num_users) {
		k = Worst_user(user, Data_rate, Proportionality);			
		if (Nk[k] > 0) {
			n = Best_SNR(Wong_Assignment, IDX[k], &curIDX[k]);
			Wong_Assignment[n] = k;
			Hmin[k] = n;
			Data_rate[k] += log2f(1 + p*(float) SNR[k][n]);					
			Nk[k]--;
		}
		else {
			Data_rate[k] = FLT_MAX;
			Num_users--;
		}
	}
	for (i = 0; i < N; ++i) {
		if (Wong_Assignment[i]==-1) {
			k = Best_user(user, SNR, Exclude, i);
			Wong_Assignment[i] = k;
			Exclude[k] = 1;
		}
	}
	free(Data_rate);
	free(curIDX);
	free(Nk);
	free(Exclude);
}

int Best_user(int user, SNRArray *SNR, char *Exclude, int n) {
	register int k;
	register float max_usr = 0;
	register int max_usr_idx;
	for (k = 0; k < user; ++k) {
		if ((SNR[k][n] > max_usr) && !Exclude[k]) {
			max_usr = SNR[k][n];
			max_usr_idx = k;
		}
	}
	return max_usr_idx;
}


int Best_SNR(int *Assignment, int *IDX, int *curIDX)
{
	int max_idx;
   
	while(Assignment[(int)IDX[*curIDX]] != -1)
		(*curIDX)++;	 
	max_idx = (int)IDX[*curIDX];
	(*curIDX)++;
	return max_idx;
}

//  Returns the user index with the lowest data rate
int Worst_user(int user, float *Data_rate, float *Proportionality)
{
	int i, lowest = 0;
	float lowest_val = FLT_MAX;
	for (i = 0; i < user; i++)
	{
//  If the data rate, proportionality ratio for the user is smaller than the lowest, reassign lowest 
		if ((Data_rate[i]/Proportionality[i]) < lowest_val) {
			lowest = i;
			lowest_val = Data_rate[i]/Proportionality[i];
		}
	}
	return lowest;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -