📄 costratio.java
字号:
package sim;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.io.LineNumberReader;import java.io.PrintWriter;import java.util.Properties;public class CostRatio extends Simulation { double costsBefore = 0.0; double costsAfter = 0.0; int measurement = 1; public CostRatio(Properties properties){ super(properties); //System.out.println("CostRatio 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); net.scaleCommunicationCosts(); // 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 = { 637914908441171402L, 1851046980501914809L, 2267373128214660142L, -7475711907906274736L, 5858882038793557940L, 5981475690592163279L, 4605301685547242780L, 5541660706755147983L, -1556510386829236803L, -8017942196272276787L, -1480446948813079093L, -6412326819586479968L, -7234655225937912016L, 6819726538237931068L, -1706289823929421468L, -6203239957709210845L, 2658515201416716119L, 221694206590644801L, -756087561468771737L, -6829211567160812095L, 8920576552725250813L, 8818164450246100203L, -7746915004390830005L, -3632263933692452676L, -1107023520871145352L}; static long[] applicationSeeds = { 3199605987968731667L, 6694891956071340688L, -6950833586872066338L, -4888452993237746567L, -3404116281154533791L, -8629744083859569725L, 6018426566511410040L, 8119731845876345647L, 2139406930939586173L, 1746967469443950816L, 8103662135501390262L, 8837428424511077790L, -5859765306760740067L, -6321560494403750306L, 5502955778780511019L, -8797736371164135603L, 4705936275785431707L, 599398870155865435L, -293090011283522920L, -916718008347020666L, -7690026469580558032L, -2259533354835153317L, -400753926332486866L, 6804267937162938144L, 5274711154794302075L}; static long[] brokerSeeds = { 3980437187721579501L, 8959812494406526487L, 1100274008539502170L, -5393072342997385477L, 4904959800344921149L, -2366239106647743065L, 5644608704808184720L, -1373862481107988870L, -263890913290409799L, -7955258785162578148L, -7982379103841073863L, -372947401204810956L, 7908363822790370632L, -5831129815687332768L, 7685098956370373441L, -8302164667678230869L, 3534300637535531448L, 7983965904266388982L, 7053862509723773471L, -441436330505195764L, 2143837986998362049L, 2872283928454661260L, 1743895825615811683L, 2750842423711865141L, -7631759998960377546L}; static double[] ratio = {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}; static String heuristic = "COSTS_AND_INTERESTS"; static String heuristicShort = "cai"; /** * @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/ratio/"+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: cost ratio "+(i*5)); out.println(); out.println("# general:"); out.println("simulation=sim.CostRatio"); 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="+(20.0d*(1.0d-ratio[i]))); out.println("minProcessingCosts=0"); out.println("maxProcessingCosts="+(20.0d*ratio[i])); out.println("minLoad=1"); out.println("maxLoad=10"); out.println("communicationDelayConstant=5"); out.println("communicationDelayFactor=0"); 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=0.75"); 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/ratio/"+heuristicShort+"/"; out = new PrintWriter(new FileWriter(expDir+"RESULTS"),true); out.println("# [ratio]\t[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(ratio[i]+"\t"+(100d-p)); } out.flush(); out.close(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -