#!/usr/bin/gawk -f BEGIN { FS = OFS = ","; Seed = 1; Best = 10; TotalMitigations = 2; } END { #start init stuff srand(Seed); m[0] = TotalMitigations; Results[0] = 2; #set all mitigations to non-fixed value of -1 for (mCounter = 1; mCounter <= m[0]; mCounter++) FixedMitigations[mCounter] = -1; #end init stuff for (round = 1; round <= TotalMitigations; round++) { TotalInstances = 0; MinCost = 10^20; MaxCost = -(10^20); MinAtt = 10^20; MaxAtt = -(10^20); for (run = 1; run <= 100; run++) { for (mCounter = 1; mCounter <= m[0]; mCounter++) { #if in the previous runs the mitigation is fixed to a certain value (0 or 1) then use that value. otherwise, select it at random if (FixedMitigations[mCounter] == -1) m[mCounter] = select(0,1); else m[mCounter] = FixedMitigations[mCounter]; } #find the cost and att using these mitigations model(Results); #print m[1],m[2],Results[1],Results[2]; #store the current instance addInstance(Results); } #find the sweet spot and distances from sweet spot for each distance sweetSpot(); #rank the mitigations and find the mitigation that should be fixed rankMitigations(); #prepare for the next round delete Data; delete Distance; } } function rankMitigations(tempDistanceArray,tempBestFreqCount,tempRestFreqCount,tempScoreOff,tempScoreOn) { asort(Distance,tempDistanceArray); for (instanceCounter = 1; instanceCounter <= TotalInstances; instanceCounter++) { #if it is in the Best distance from the sweet spot, count the frequency of each mitigation (0 and 1) for the best instances if (Distance[instanceCounter] <= tempDistanceArray[Best]) { #print "best:"Distance[instanceCounter],Data[instanceCounter,3],Data[instanceCounter,4]; for (mCounter = 1; mCounter <= m[0]; mCounter++) { #keep track of the mitigation's counts if it is not already fixed if (FixedMitigations[mCounter] == -1) { if (Data[instanceCounter,mCounter+2] == 0) tempBestFreqCount[mCounter,0]++; else if (Data[instanceCounter,mCounter+2] == 1) tempBestFreqCount[mCounter,1]++; } } } #else it is in the Rest distance from the sweet spot and so count the frequency of each mitigation (0 and 1) for the rest instances else { #print "rest:"Distance[instanceCounter],Data[instanceCounter,3],Data[instanceCounter,4]; for (mCounter = 1; mCounter <= m[0]; mCounter++) { #keep track of the mitigation's counts if it is not already fixed if (FixedMitigations[mCounter] == -1) { if (Data[instanceCounter,mCounter+2] == 0) tempRestFreqCount[mCounter,0]++; else if (Data[instanceCounter,mCounter+2] == 1) tempRestFreqCount[mCounter,1]++; } } } } maxScore = -(10^20); maxScoredMitigation = 0; maxScoredMitigationStatus = -1; #normalize each frequency count by dividing it by the total number of instances and score each mitigation using the best^2/(best+rest) and keep min and max for (mCounter = 1; mCounter <= m[0]; mCounter++) { #do this only if mitigation is not fixed already if (FixedMitigations[mCounter] == -1) { #find the score of the mitigation when it is off best = tempBestFreqCount[mCounter,0]/TotalInstances; rest = tempRestFreqCount[mCounter,0]/TotalInstances; tempScoreOff[mCounter] = best^2/(best+rest); #print "m"mCounter " with 0 has best: "best " and rest: "rest; #keep its information if it is the max score seen so far if (tempScoreOff[mCounter] > maxScore) { maxScore = tempScoreOff[mCounter]; maxScoredMitigation = mCounter; maxScoredMitigationStatus = 0; } #find the score of the mitigation when it is on best = tempBestFreqCount[mCounter,1]/TotalInstances; rest = tempRestFreqCount[mCounter,1]/TotalInstances; tempScoreOn[mCounter] = best^2/(best+rest); #print "m"mCounter " with 1 has best: "best " and rest: "rest; #keep its information if it is the max score seen so far if (tempScoreOn[mCounter] > maxScore) { maxScore = tempScoreOn[mCounter]; maxScoredMitigation = mCounter; maxScoredMitigationStatus = 1; } #print "mitigation: "mCounter " for being 0 has score: " tempScoreOff[mCounter] " and for being 1 has score: "tempScoreOn[mCounter]; } } print "mitigation "maxScoredMitigation " with status " maxScoredMitigationStatus " has score " maxScore; FixedMitigations[maxScoredMitigation] = maxScoredMitigationStatus; } function sweetSpot() { #normalize the att and cost using their for (instanceCounter = 1; instanceCounter <= TotalInstances; instanceCounter++) { #calculate the normalized cost and att normalizedCost = (Data[instanceCounter,1] - MinCost)/(MaxCost - MinCost); normalizedAtt = (Data[instanceCounter,2] - MinAtt)/(MaxAtt - MinAtt); #distance of each instance is measured from (cost=0,att=1) which is the sweet spot Distance[instanceCounter] = sqrt((normalizedCost - 0)^2 + (normalizedAtt - 1)^2); } } function addInstance(resultsArray) { TotalInstances++; #this is the cost Data[TotalInstances,1] = resultsArray[1]; #this is the att Data[TotalInstances,2] = resultsArray[2]; for (mCounter = 1; mCounter <= m[0]; mCounter++) Data[TotalInstances,mCounter+2] = m[mCounter]; if (MinCost > Data[TotalInstances,1]) MinCost = Data[TotalInstances,1]; if (MaxCost < Data[TotalInstances,1]) MaxCost = Data[TotalInstances,1]; if (MinAtt > Data[TotalInstances,2]) MinAtt = Data[TotalInstances,2]; if (MaxAtt < Data[TotalInstances,2]) MaxAtt = Data[TotalInstances,2]; } function select(val1,val2) { if (rand() < 0.5) return val1; else return val2; } function model(resultsArray) { # m[1]=0; # m[2]=1; mCost[1] = 11; mCost[2] = 22; rAPL[1] = 1; rAPL[2] = 1; oWeight[1] = 1; oWeight[2] = 2; oWeight[3] = 3; roImpact[1,1] = 0.1; roImpact[1,2] = 0.3; roImpact[2,1] = 0.2; mrEffect[1,1] = 0.9; mrEffect[1,2] = 0.3; mrEffect[2,1] = 0.4; rLikelihood[1] = rAPL[1] * (1 - m[1] * mrEffect[1,1]) * (1 - m[2] * mrEffect[2,1]); rLikelihood[2] = rAPL[2] * (1 - m[1] * mrEffect[1,2]); oAtRiskProp[1] = (rLikelihood[1] * roImpact[1,1]) + (rLikelihood[2] * roImpact[2,1]); oAtRiskProp[2] = (rLikelihood[1] * roImpact[1,2]); oAtRiskProp[3] = 0; oAttainment[1] = oWeight[1] * (1 - minValue(1, oAtRiskProp[1])); oAttainment[2] = oWeight[2] * (1 - minValue(1, oAtRiskProp[2])); oAttainment[3] = oWeight[3] * (1 - minValue(1, oAtRiskProp[3])); attTotal = oAttainment[1] + oAttainment[2] + oAttainment[3]; costTotal = m[1] * mCost[1] + m[2] * mCost[2]; resultsArray[1] = costTotal; resultsArray[2] = attTotal; } function minValue(val1,val2) { if (val1 < val2) return val1; else return val2; }