public static void computeUtility(string[] commandPieces, resultObject Result) { //usage computeutility AS d iteration if (commandPieces.Length < 4) { Console.WriteLine("computeutility [ASN] [dest] [iteration]"); return; } UInt32 ASN, dest; Int32 iter; if (!UInt32.TryParse(commandPieces[1], out ASN) || !UInt32.TryParse(commandPieces[2], out dest) || !Int32.TryParse(commandPieces[3], out iter)) { Console.WriteLine("bad params"); return; } if (iter > Result.state.Count) { Console.WriteLine("iteration too large."); return; } bool[] iterState = Result.state[iter]; foreach (var stub in Result.g.getStubs()) iterState[stub] = true;//turn on the stubs as in the sim SimulatorLibrary.setUtilityComputation(UtilityComputationType.outgoing); GlobalState initial = SimulatorLibrary.initGlobalState(Result.g, Result.earlyAdopters, Result.weightedNodes, short.Parse(Result.k)); Destination d = new Destination(SimulatorLibrary.initMiniDestination(Result.g, dest, false)); d.UpdatePaths(iterState); d.ComputeU(initial.W); Console.WriteLine("Utility for " + ASN + " in iteration: " + iter + " is " + d.U[ASN]); Worker w = new Worker(); int afterFlip= w.ComputeUtility(d.BucketTable, d.Best, d.ChosenParent, d.SecP, iterState, ASN, d.L[ASN], d.BestRelation[ASN], initial.W); Console.WriteLine("Utility for " + ASN + " in iteration: " + iter + " if they flip is " +afterFlip); }
private bool flipU(ref List<Destination> ds, ref NetworkGraph g, ref GlobalState globalState,string command) { Console.WriteLine("Flipping state:"); string[] cpieces = command.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); if (cpieces.Length < 2) { Console.WriteLine("usage: flipu <d> <quiet?>"); return false; } Destination d = new Destination(); UInt32 destNum; if (!UInt32.TryParse(cpieces[1], out destNum)) { Console.WriteLine("invalid destination number"); return false; } foreach (Destination curr in ds) { if (curr.destination == destNum) d = curr; } if (d.BucketTable == null) { Console.WriteLine("null bucket table!"); return false; } Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); List<UInt32> toflip = new List<UInt32>(); bool quiet = false; if (command.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Length > 2) { quiet = true; Console.WriteLine("you have selected quiet mode"); } List<UInt32> nonstubs = g.getNonStubs(); foreach (UInt32 ASN in nonstubs) { if (d.Best[ASN] != null)//make sure this AS has a route to our destination { Worker w = new Worker(); int notASN = w.ComputeUtility(d.BucketTable, d.Best, d.ChosenParent, d.SecP, globalState.S, ASN, d.L[ASN], d.BestRelation[ASN], globalState.W); if (d.U[ASN] < notASN) { if (!quiet) Console.WriteLine("AS: " + ASN + " from " + globalState.S[ASN] + " to " + !globalState.S[ASN]); toflip.Add(ASN);//don't flip on the fly it messes up everyones calculations, do it at the end. } } } foreach (int ASN in toflip) { if (globalState.S[ASN]) Console.WriteLine("AS: " + ASN + " is rolling back!!!"); globalState.S[ASN] = !globalState.S[ASN]; } stopwatch.Stop(); Console.WriteLine("flip u took " + stopwatch.ElapsedMilliseconds + " ms"); Console.WriteLine("it flipped " + toflip.Count + " ASes"); return true; }
private bool computeNotN(ref List<Destination> ds, ref GlobalState globalState, ref Worker w, string command) { string[] cpieces = command.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); if (cpieces.Length < 3) { Console.WriteLine("error: usage getpath <as#> <dstnum>"); return false; } int dstNum; UInt32 ASN; if (!UInt32.TryParse(cpieces[1], out ASN) || !int.TryParse(cpieces[2],out dstNum)) { Console.WriteLine("invalid ASN or destination."); return false; } foreach (Destination d in ds) { if (d.destination == dstNum) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Reset(); stopwatch.Start(); int unotn = w.ComputeUtility(d.BucketTable, d.Best, d.ChosenParent, d.SecP, globalState.S, ASN, d.L[ASN], d.BestRelation[ASN], globalState.W); stopwatch.Stop(); Console.WriteLine("Utility for " + ASN + " if they flip is: " + unotn + " computing this took " + stopwatch.ElapsedMilliseconds); return true; } } Console.WriteLine("could not find destination."); return false; }
private bool flipallU(ref List<Destination> ds, ref NetworkGraph g, ref GlobalState globalState, string command) { Console.WriteLine("Flipping state:"); string[] cpieces = command.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); if (cpieces.Length < 1) { Console.WriteLine("usage: flipallu <quiet?>"); return false; } Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); List<UInt32> toflip = new List<UInt32>(); bool quiet = false; if (command.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Length > 1) { quiet = true; Console.WriteLine("you have selected quiet mode"); } List<UInt32> nonstubs = g.getNonStubs(); Int64[] deltaU = new Int64[Constants._numASNs]; for (int i = 0; i < deltaU.Length; i++) deltaU[i] = 0; foreach (Destination d in ds) { foreach (UInt32 ASN in nonstubs) { if (d.Best[ASN] != null)//make sure this AS has a route to our destination { Worker w = new Worker(); int notASN = w.ComputeUtility(d.BucketTable, d.Best, d.ChosenParent, d.SecP, globalState.S, ASN, d.L[ASN], d.BestRelation[ASN], globalState.W); deltaU[ASN] += (notASN - d.U[ASN]); } } } int flipped = 0; for (int ASN = 0; ASN < deltaU.Length;ASN++ ) { if (deltaU[ASN] > 0)//positive change in utility for this ASN { flipped++; if (globalState.S[ASN]) Console.WriteLine("AS: " + ASN + " is rolling back!!!"); globalState.S[ASN] = !globalState.S[ASN]; if (!quiet) { Console.WriteLine("AS: " + ASN + " had change in utility of " + deltaU[ASN]); } } } stopwatch.Stop(); Console.WriteLine("flip u took " + stopwatch.ElapsedMilliseconds + " ms"); Console.WriteLine("it flipped " + flipped + " ASes"); return true; }
private void runCompareU(List<UInt32> nonstubs,Destination d,ref Worker w,bool onlybenefit,ref GlobalState globalState) { if (d.BucketTable == null) { Console.WriteLine("null bucket table!"); return; } Stopwatch stopwatch = new Stopwatch(); stopwatch.Reset(); stopwatch.Start(); d.UpdatePaths(globalState.S); d.ComputeU(globalState.W);//just in case. stopwatch.Stop(); foreach (UInt32 ASN in nonstubs) { if (d.Best[ASN] != null)//nonstub has a path to d { int notASN = w.ComputeUtility(d.BucketTable, d.Best, d.ChosenParent, d.SecP, globalState.S, ASN, d.L[ASN], d.BestRelation[ASN], globalState.W); if (!onlybenefit || d.U[ASN] < notASN) Console.WriteLine(ASN + " :: " + d.U[ASN] + " : " + notASN); } } Console.WriteLine("update paths and compute u took: " + stopwatch.ElapsedMilliseconds + " ms"); }