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

📄 locality.java

📁 发布/订阅系统路由重配算法,可应用于ad hoc环境
💻 JAVA
字号:
package sim;import java.util.*;import java.io.*;public class Locality extends Simulation {	double costsBefore = 0.0;	double costsAfter = 0.0;	int measurement = 1;		public Locality(Properties properties){		super(properties);		//System.out.println("Locality started.");	}		public void setup() {				// create the overlay and broker network		try{			net = Network.loadOverlay(this);		}catch(Exception e){			System.out.println(e);			e.printStackTrace();			System.exit(-1);		}		net.scaleCommunicationCosts();		net.assignProcessingCosts();		net.scaleProcessingCosts();		net.assignLoad();		net.scaleLoad();		net.createRandomTree();		// create the applications		application = Application.createApplication(this);		application.assignTo(net);		// schedule Measurement Events		scheduleEventAt(1000, new MeasureStartEvent());		scheduleEventAt(1750, new Event(){			public void handle(){				net.clearCaches();			}		});		scheduleEventAt(2001, new MeasureStopEvent());		scheduleEventAt(2002, new Event() {			public void handle() {				net.startHeuristic();			}		});		scheduleEventAt(24000, new Event() {			public void handle() {				net.stopHeuristic();			}		});		scheduleEventAt(25000, new MeasureStartEvent());		scheduleEventAt(26001, new MeasureStopEvent());		scheduleEventAt(26002, new StopEvent());	}		public void measure(){		if(measurement==1){			costsBefore = costs;			measurement++;		}else{			costsAfter = costs;			out.println("# [costs before]\t[costs after]");			out.println(costsBefore+"\t"+costsAfter);		}		super.measure();	}		// STATIC PART	static String networkDir = "top/";	static String[] networks = {"top_100_a",		                        "top_100_b",		                        "top_100_c",		                        "top_100_d",		                        "top_100_e",    	                        "top_100_f",    	                        "top_100_g",    	                        "top_100_h",    	                        "top_100_i",    	                        "top_100_j",    	                        "top_100_k",    	                        "top_100_l",    	                        "top_100_m",    	                        "top_100_n",								"top_100_o",    	                        "top_100_p",		                        "top_100_q",		                        "top_100_r",		                        "top_100_s",		                        "top_100_t",		                        "top_100_u",		                        "top_100_v",		                        "top_100_w",		                        "top_100_x",		                        "top_100_y"};	static long[] networkSeeds = {-9078953577908471080L,								   5433412283071382765L,		                           3955972063971381461L,		                           9219323240742299298L,		                          -6776660846947045003L,       	                          -4047722161577689285L,       	                          -9116592706975931155L,       	                          -3107799936986450066L,       	                           1813649587813452518L,       	                          -5144154740242356384L,       	                          -6354732053909741982L,       	                           4080303647031548237L,       	                           5631215225213963119L,       	                           -746753286110509261L,       	                           3717086496231513483L, 	                               9148271295188868005L, 	                              -6123073196505740484L, 	                               -107095398169648298L, 	                               1306472749942953965L, 	                              -3356807378296153053L, 	                              -7553544304997569568L, 	                              -5806643494494444396L, 	                              -9003392080241211402L, 	                               -839138380548073334L, 	                               7825790344239596550L};		static long[] applicationSeeds = {-6354263370347159354L,		                               5328701503880152405L,		                              -5814785161659028417L,		                               1015308290430115210L,		                               -719413168782365446L,	                                  -7439002273665459780L,	                                   2421957848390321832L,	                                   4642254198788325153L,	                                  -3091580600972198935L,	                                   5208618209186019660L,	                                  -4569580152030430087L,	                                  -7119310385591428480L,	                                  -1698169473927350670L,	                                   7758850273768894672L,	                                   5632761443448575764L,	                                  -4200315674590278535L,	                                  -6735893619037547133L,	                                   5589610014448885819L,	                                   8428915115008595010L,	                                  -3684099157051183990L,	                                  -1191361979719338929L,	                                   6153361736325112496L,	                                   1407950722415332951L,	                                  -1689422298730143752L,	                                   7915262446233895764L};		static long[] brokerSeeds = {-2207879420765661839L,		                         -1560790230001725132L,		                         -5251364803416946779L,		                          2380959933682283611L,		                         -2054822537291505085L,	                             -7295546160301164555L,	                             -8971651611524005311L,	                              5867294068755015857L,	                             -4001090655650145765L,	                              8896068755900460249L,	                             -7489835451517513095L,	                              3671149251860383544L,	                              8399593583255060754L,	                              6982035123712667218L,	                             -8256130151490745063L,	                              5835961877606957216L,                                 -5089087742396887498L,	                              7777157343628273832L,	                              4124760862544047748L,	                              2245463445731290893L,	                             -8747835545641414594L,	                              8973465067990137334L,	                               792296308602895681L,	                              2747281367751293745L,	                             -2941900507802982951L};		static String heuristic = "COSTS_AND_INTERESTS";	static String heuristicShort = "cai";		static double[] locality= {0.00,0.05,0.10,0.15,0.20,0.25,0.30,0.35,0.40,0.45,0.50,0.55,0.60,0.65,0.70,0.75,0.80,0.85,0.90,0.95,1.00};			/**	 * @param args	 */	public static void main(String[] args) {		// TODO Auto-generated method stub				try{			if(args.length>=2){				if(args[1].equalsIgnoreCase("MIN")){					heuristic="COSTS";					heuristicShort="min";				}else if(args[1].equalsIgnoreCase("MAX")){					heuristic="INTERESTS";					heuristicShort="max";				}			}			if(args[0].equalsIgnoreCase("MAKE_EXPERIMENT")){				makeExperiment();			}else if(args[0].equalsIgnoreCase("COLLECT_RESULTS")){				collectResults();			}		}catch(Exception e){			System.err.println(e);			e.printStackTrace();			System.exit(-1);		}	}			private static void makeExperiment() throws Exception{		File f;		String expDir;		String expFile;		PrintWriter out;				expDir = "exp/locality/"+heuristicShort+"/";		f = new File(expDir);		f.mkdirs();				for(int net=0; net<networks.length; net++){								for(int i=0; i<21; i++){								expFile = expDir+networks[net]+"_"+heuristicShort+"_"+(i*5);				out = new PrintWriter(new FileWriter(expFile+".exp"),true);				out.println("# experiment: locality "+(i*5));				out.println();				out.println("# general:");				out.println("simulation=sim.Locality");				out.println("outFile="+expFile+".rst");				out.println("netFile="+networkDir+networks[net]);				out.println("measureInterval=1000");				out.println();				out.println("# random seeds");				out.println("networkSeed="+networkSeeds[net]);				out.println("applicationSeed="+applicationSeeds[net]);				out.println("brokerSeed="+brokerSeeds[net]);				out.println();				out.println("# network");				out.println("minCommunicationCosts=0");				out.println("maxCommunicationCosts=10");				out.println("minProcessingCosts=0");				out.println("maxProcessingCosts=10");				out.println("minLoad=1");				out.println("maxLoad=10");				out.println("communicationDelayConstant=0");				out.println("communicationDelayFactor=1");				out.println("processingDelayConstant=0");				out.println("processingDelayFactor=0.001");				out.println();				out.println("# application");				out.println("numberOfJobs=50");				out.println("numberOfSubscribers=9");				out.println("publicationInterval=EXPONENTIAL");				out.println("publicationConstant=0.2");				out.println("reassignmentInterval=CONSTANT");				out.println("reassignmentConstant=100000");				out.println("locality="+locality[i]);				out.println("localityPower=2");				out.println();				out.println("# broker");				out.println("heuristic="+heuristic);				out.println("cacheSize=8192");				out.println("filterSize=100000");				out.println("broadcastInterval=250");				out.println("updateInterval=400");				out.println("order=UNORDERED");				out.println("environmentSize=4");				out.println("numberOfHashs=5");				out.flush();				out.close();			}		}			}			private static void collectResults() throws Exception{		String expDir;		String expFile;		PrintWriter out;		double before;		double after;		LineNumberReader in;		String line;		String[]  tokens;		double b;		double a;		double percent;		double p;		int n = networks.length;				expDir = "exp/locality/"+heuristicShort+"/";		out = new PrintWriter(new FileWriter(expDir+"RESULTS"),true);		out.println("# [maxLoad] [improvement]");		for(int i=0; i<21; i++){					percent = before = after = 0;			for(int net = 0; net < n; net++){				expFile = expDir+networks[net]+"_"+heuristicShort+"_"+(i*5)+".rst";				in = new LineNumberReader(new FileReader(expFile));				b=a=0;				while((line=in.readLine())!=null){					if(line.startsWith("#") || line.equals("")){						continue;					}					tokens = line.split("\t");					b = Double.parseDouble(tokens[0]);					a = Double.parseDouble(tokens[1]);				}				before += b;				after += a;				percent += (a*100.0d/b);								in.close();			}			p = percent/((double)n);			out.println(locality[i]+"\t"+(100d-p));		}		out.flush();		out.close();	}}

⌨️ 快捷键说明

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