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

📄 almah_tree_analyzer.cc

📁 为NS2仿真中的一个overlay多播协议的协议模块
💻 CC
字号:
 /***************************************************************************                                                      -------------------    begin                : mar mar 16 2004    copyright            : (C) 2004 by    email                : ***************************************************************************//*************************************************************************** *                                                                         * *   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.                                   * *                                                                         * ***************************************************************************/#include "almah_tree_analyzer.h"#include <iostream>#include <random.h>#include "god.h"#define max(a,b)        ( (a) > (b) ? (a) : (b) )#define CURRENT_TIME    Scheduler::instance().clock()static class ALMAH_Analizer_Class : public TclClass {public:	ALMAH_Analizer_Class() : TclClass("Agent/AlmahTreeAnalyzer") {}	TclObject* create(int, const char*const*) {		return (new ALMAH_Tree_Analyzer());	}} class_almah_tree_analizer;ALMAH_Tree_Analyzer::ALMAH_Tree_Analyzer() : ttimer(this){  fout_=NULL;  bind("M_cast",&M_cast);  bind("interval",&interval);  sot_=NULL;  efficiency=0;				  efficiency_samples=0;			  mean_ideal_cost=0;			  mean_ALMAH_cost=0;			  efficiency_on=false;}voidALMAH_Tree_AnalyzerTimer::handle(Event*) {          agent->Print_Report();                    Scheduler::instance().schedule(this, &intr, agent->interval);}ALMAH_Tree_Analyzer::~ALMAH_Tree_Analyzer(){	fclose(fout_);	return;}void ALMAH_Tree_Analyzer::start(){	last_sampled_tree_instant=-Agents[0]->TREE_INT;	ttimer.handle((Event*) 0);}void ALMAH_Tree_Analyzer::Print_Report(){      double now = CURRENT_TIME;     int ideal_cost, ALMAH_cost, Count_Tree_Links_;				God::instance()->ComputeRoute();		Count_Tree_Links_=Count_Tree_Links();		ALMAH_cost = Tree_Cost();    ideal_cost = TreeOptimalCost();  		fprintf(fout_,"%f \t %d \t %d \t %d \n",			now,			ideal_cost,			ALMAH_cost,			Count_Tree_Links_		);        				if (efficiency_on && ALMAH_cost<1000) {			if (ideal_cost<1000) {			efficiency_samples++;			mean_ideal_cost=(mean_ideal_cost*(efficiency_samples-1)+ideal_cost)/efficiency_samples;			mean_ALMAH_cost=(mean_ALMAH_cost*(efficiency_samples-1)+ALMAH_cost)/efficiency_samples;			efficiency=mean_ideal_cost/mean_ALMAH_cost;			}		}        return;}int ALMAH_Tree_Analyzer::Tree_Cost(){				double now = CURRENT_TIME;     OBAMP_Overlay_Neighbor* ON;    int costo=0;		for(int i=0;i<this->M_cast;i++){              ON = Agents[i]->nbhead.lh_first;              OBAMP_Overlay_Neighbor *nbn;			  			OBAMP_Overlay_Neighbor *nb_r;              if(ON!=0)                for(; ON; ON = nbn) {                    nbn = ON->nb_link.le_next;                    int cost_ = God::instance()->hops(i,ON->nb_addr);										if (cost_>1000) cost_=1000;										costo+=cost_;										nb_r=Agents[ON->nb_addr]->nb_lookup(i);										if (nb_r==0) printf("%.2f tree inconsistece from %d to %d\n",now,i,(int) ON->nb_addr);                }        }    costo=(int)(1.0*costo/2.0);    return costo;}int ALMAH_Tree_Analyzer::command(int argc, const char*const* argv){    Tcl& tcl = Tcl::instance();		if (strcmp(argv[1], "start") == 0){         this->start();         return (TCL_OK);    }    	// Andrea Broadcast	if (strcmp(argv[1], "get-efficiency") == 0){			efficiency_on=true;			tcl.resultf("%f", efficiency);         return (TCL_OK);    }    	if (strcmp(argv[1], "agent-attach")==0) {                ALMAH_Agent* ALMAH_ag = (ALMAH_Agent*)TclObject::lookup(argv[2]);        Agents[ALMAH_ag->index] = ALMAH_ag;        return (TCL_OK);	}    if ((argc == 4) && (strcmp(argv[1], "attach-file") == 0))	  {  	    fout_=fopen(argv[2],argv[3]);        if (fout_ == NULL) return(TCL_ERROR);		return (TCL_OK);	  }   return (TclObject::command(argc, argv));}int ALMAH_Tree_Analyzer::TreeOptimalCost(){    double now = Scheduler::instance().clock();	  int costo;		lista* ls = new lista();		for(int i=0; i<M_cast; i++)			for(int j=0; j<M_cast; j++){				if(i!=j) {						costo = God::instance()->hops(i, j);						if (costo>1000) costo=1000;						}				else					costo = 0;				if(i<j)					ls->inserisci(i,j,costo);			}    Prim *pr = new Prim();	  lista* ls_ = new lista();	  ls_ = pr->esegui(ls);//torna la lista minima secondo Prim    ls_->Set_costo_tot();    int tree_cost;    ls_->get_costo_tot(tree_cost);	  if (now>=last_sampled_tree_instant+Agents[0]->TREE_INT) {			if (sot_!=NULL) delete sot_;				sot_=new lista(ls_);				last_sampled_tree_instant=now;			}	  delete pr;    delete ls_;    delete ls;    return tree_cost;}int ALMAH_Tree_Analyzer::SampledTreeOptimalCost(){    int costo=0;	tree_analyzer_list_elem* el;	if (sot_!=NULL && sot_->numero_elementi>0) {		el = sot_->primo;		for( ;el;el=el->next) {			el->COSTO=God::instance()->hops(el->S, el->D);		}	}	sot_->Set_costo_tot();    sot_->get_costo_tot(costo);    return costo;}int ALMAH_Tree_Analyzer::Count_Tree_Links(){        lista* ls = new lista();        OBAMP_Overlay_Neighbor* ON[/*M_cast*/50];        for(int i=0;i<this->M_cast;i++){              ON[i] = Agents[i]->nbhead.lh_first;              OBAMP_Overlay_Neighbor *nbn;              if(ON[i]!=0)                for(; ON[i]; ON[i] = nbn) {                    nbn = ON[i]->nb_link.le_next;                        ls->inserisci(i,ON[i]->nb_addr,God::instance()->hops(i,ON[i]->nb_addr));                    }        }        Prim* pr = new Prim();        int numero_elementi, costo;        ls->get_numero_elementi(numero_elementi);        if(numero_elementi>0){                pr->riduci(ls);                ls->Set_costo_tot();                ls->get_numero_elementi(numero_elementi);        }        else            numero_elementi = 0;        ls->get_costo_tot(costo);        delete ls;        delete pr;        return numero_elementi;}                  

⌨️ 快捷键说明

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