📄 mas_ams.cpp
字号:
//////////////////////////////////////////////////////////////////////
// Title: Geographic Load Balancing for Cellular Networks
// by emulating the behavior of air bubbles
//
// Description: This project is for dynamically balancing the traffic load
// over a cellular network with fully adaptive antennas by
// emulating the behaviours of a bubble array. Since
// we assume fully adaptive base station antenna in this
// version, antenna agent and simulator are not needed.
//
// Copyright: Copyright (c) 2003
// Company: Elec. Eng. Dept., Queen Mary, University of London
// @author Lin Du (lin.du@elec.qmul.ac.uk)
// @version 1.0
//
//////////////////////////////////////////////////////////////////////
// MAS_AMS.cpp: implementation of the MAS_AMS class.
//
//////////////////////////////////////////////////////////////////////
#include "MAS_AMS.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
MAS_AMS::MAS_AMS() {
}
MAS_AMS::~MAS_AMS() {
}
//////////////////////////////////////////////////////////////////////
// start MAS
//////////////////////////////////////////////////////////////////////
int MAS_AMS::startMAS(char *scen_file) {
BSAgent *p_bsa;
int i;
// optimise the network by utility-based method
for( i=0; i<BS_NUMBER; i++) {
p_bsa = tb->bs[i]->getAgent();
// need to reset the BS agent to start a new circle (for new scenario only)
p_bsa->reset();
// init the bubble oscillation (utility-based method)
p_bsa->run();
}
// need to reset the network be4 commit
tb->reset();
// apply the changes
for( i=0; i<BS_NUMBER; i++) {
p_bsa = tb->bs[i]->getAgent();
// commit the changes to our virtual network.
p_bsa->commit();
}
// Calculate the capcity and blocked users (simulate without assignment)
tb->simulate(false);
#ifdef _DEBUG
double cap = tb->getSystemCapacity();
double blk = tb->getBlockedNum();
char buf[256];
sprintf(buf, "%s_init_bub.plt", scen_file);
tb->writeGPResult(buf);
sprintf(buf, "init%sbub.m", scen_file);
tb->writeMatlabResult(buf);
#endif
// Start the bubble oscillation.
// Note: use MAX_OSC_NUM currently, maybe other stop conditions later, such as amplitude or improvement
bool active = true; // check if there is any agent running, if not, stop oscillation
double capacity[MAX_OSC_NUM], blocked[MAX_OSC_NUM];
for(int osc=0; active && osc<MAX_OSC_NUM; osc++) {
active = false;
for( i=0; i<BS_NUMBER; i++) {
BaseStation *p_bs = tb->bs[i];
// Calculate the blocked TU in possible TU
double blk_bs = p_bs->getBlocked( );
if (blk_bs>0) {
// Run BS agents
p_bs->getAgent()->run();
active = true;
}
} // end of for loop
// need to reset the network be4 commit
tb->reset();
// apply the changes
for( i=0; i<BS_NUMBER; i++) {
p_bsa = tb->bs[i]->getAgent();
// commit the changes to our virtual network.
p_bsa->commit();
}
// Calculate the capcity and blocked users (simulate without assignment)
tb->simulate(false);
capacity[osc] = tb->getSystemCapacity();
blocked[osc] = tb->getBlockedNum();
#ifdef _DEBUG
cout << "Oscillation:" << osc << " " << capacity[osc] << " " << blocked[osc] << endl;
// output the scenario of each oscillation
char buf[256];
sprintf(buf, "%s_osc_%d.plt", scen_file, osc);
tb->writeGPResult(buf);
sprintf(buf, "osc%d.m", osc);
tb->writeMatlabResult(buf);
#endif
// check if it has reached the stable state (convergent).
if(osc>30 && abs(capacity[osc] - capacity[osc-10]) < 1 && abs(capacity[osc] - capacity[osc-20]) < 1 ) { // converged, stop oscillation
break;
}
}
return osc-1;
}
//////////////////////////////////////////////////////////////////////
// Recover the env from saved file
//////////////////////////////////////////////////////////////////////
int MAS_AMS::recover(char *file_name) {
int len, scen_num;
U_INT32 uniID;
try {
// Open recover data file.
ifstream fin(file_name, ios::binary);
// get the scenario number
fin.read( (char *)&scen_num, sizeof(int) );
// get the uniqueID
fin.read( (char *)&uniID, sizeof(U_INT32) );
// set the uniqueID in MAS_DF
tb->df->setUniqueID(uniID);
// Get the length of the state array of RNG
fin.read( (char *)&len, sizeof(int) );
// Get the states info. of RNG
char *state_ptr = new char[len];
fin.read (state_ptr, len);
// restore the state array of RNG
setstate(state_ptr);
// Get the pattern info. from recover file
for(int i=0; i<BS_NUMBER; i++) {
P_TU_V &curr = tb->bs[i]->getAgent()->getCurrScheme();
curr.clear();
fin.read( (char *)&len, sizeof(int) );
int *tu_id_buf = new int[len];
fin.read ((char *)tu_id_buf, len*sizeof(int));
// Set the scheme
for(long j=0; j<len; j++) {
TrafficUnit *p_tu = tb->df->findTU(tu_id_buf[j]);
curr.push_back(p_tu);
}
delete []tu_id_buf;
}
// reset the call status info. of all the mobiles
CallGenerator *cg;
len = sizeof(bool) + 3*sizeof(double);
char *buf = new char[len];
for(long tu_i=0; tu_i<TU_NUMBER; tu_i++) {
cg = tb->tu[tu_i]->getCallGenerator();
fin.read(buf, len);
cg->reset(*((bool *)buf), // isTalking
*((double *)(buf+sizeof(bool))), // prevTime
*((double *)(buf+sizeof(bool)+sizeof(double))), // arrivalTime
*((double *)(buf+sizeof(bool)+2*sizeof(double)))); // holdingTime
}
delete []buf;
// close the file
fin.close();
// delete []state_ptr;
}
catch(...){
cout << "Fatal Error with recover file: " << file_name << endl;
exit(1);
}
return scen_num;
}
//////////////////////////////////////////////////////////////////////
// Save necessary info. into a file in order to recover from here.
//////////////////////////////////////////////////////////////////////
void MAS_AMS::checkpoint(int scen_num, char *file_name) {
try {
// Open recover data file.
ofstream fout(file_name, ios::binary);
// Save the scenario number
fout.write( (char *)&scen_num, sizeof(int) );
// Save the uniqueID stored in MAS_DF
U_INT32 uniID = tb->df->getUniqueID();
fout.write( (char *)&uniID, sizeof(U_INT32) );
// Get the states info. of RNG
int len;
char *state_ptr = getstate(&len);
// Save them
fout.write( (char *)&len, sizeof(int) );
fout.write(state_ptr, len);
// Get the pattern info. from antenna agents
for(int i=0; i<BS_NUMBER; i++) {
P_TU_V &curr = tb->bs[i]->getAgent()->getCurrScheme();
len = curr.size();
// write the length first
fout.write( (char *)&len, sizeof(int) );
int *tu_id_buf = new int[len];
for(int j=0; j<len; j++) {
tu_id_buf[j] = curr[j]->getID();
}
// Write it to file
fout.write ((char *)tu_id_buf, len*sizeof(int));
delete []tu_id_buf;
}
// Get the call status info. from all the mobiles
CallGenerator *cg;
len = sizeof(bool) + 3*sizeof(double);
char *buf = new char[len];
for(long tu_i=0; tu_i<TU_NUMBER; tu_i++) {
cg = tb->tu[tu_i]->getCallGenerator();
cg->getState((bool *)buf, // isTalking
(double *)(buf+sizeof(bool)), // prevTime
(double *)(buf+sizeof(bool)+sizeof(double)), // arrivalTime
(double *)(buf+sizeof(bool)+2*sizeof(double))); // holdingTime
fout.write(buf, len);
}
delete []buf;
// close the file
fout.close();
}
catch(...){
cout << "Fatal Error with recover file: " << file_name << endl;
exit(1);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -