#!/usr/bin/gawk -f BEGIN{ Output = "model_"O"_"R"_"M".c" srand(Seed); #Struct info print "#include \"model.h\"\n\n#define M_COUNT "M"\n#define O_COUNT "O"\n#define R_COUNT "R"\n\nstruct ddpStruct\n{\n\tfloat oWeight[O_COUNT+1];\n\tfloat oAttainment[O_COUNT+1];\n\tfloat oAtRiskProp[O_COUNT+1];\n\tfloat rAPL[R_COUNT+1];\n\tfloat rAggrevatedImpact[R_COUNT+1];\n\tfloat rLikelihood[R_COUNT+1];\n\tfloat mCost[M_COUNT+1];\n\tfloat roImpact[R_COUNT+1][O_COUNT+1];\n\tfloat mrEffect[M_COUNT+1][R_COUNT+1];\n};\n\nddpStruct *ddpData;\n\nvoid setupModel(void)\n{\n\tddpData = (ddpStruct *) malloc(sizeof(ddpStruct));" > Output #setupModel function for(i=1;irAPL["i"]=1;\n\tddpData->rAggrevatedImpact["i"]=1;" > Output } populateMCosts(O,M); populateOWeights(O); Risks1[0]=""; Objectives[0]=""; Mitigations[0]=""; Risks2[0]=""; populateROI(R,O,Risks1,Objectives); populateMRE(M,R,Mitigations,Risks2,Negatives); print "}\nvoid model(float *cost, float *att, float m[])\n{\n\tfloat costTotal, attTotal;" > Output #model function for(j=1;jrLikelihood["j"] = ddpData->rAPL["j"];" > Output } print "\n\t/* Mitigation effects on Risk likelihoods */" > Output populateRLikelihood(R,M,Mitigations,Risks2,Negatives); print "\t/* Risk impacts on Objective attainment proportions */" > Output populateOAtRiskProp(O,R,Objectives,Risks1); print "\t/* Objective attainments */" > Output for(k=1;koAttainment["k"] = ddpData->oWeight["k"] * (1 - minValue(1, ddpData->oAtRiskProp["k"]));" > Output } att="\tattTotal ="; for(l=1;loAttainment["l"] +"; } att=substr(att,0,length(att)-2); print att";" > Output cost="\tcostTotal ="; for(m=1;mmCost["m"] +"; } cost=substr(cost,0,length(cost)-2); print cost";" > Output print "\t*cost = costTotal;\n\t*att = attTotal;\n}" > Output } function round(P, i,j,size,digits,rounded){ #Rounds numbers to most significant digit split(P,digits,""); size=length(P); if(digits[2]>=5) digits[1]++; rounded=digits[1]; for(j=0;j=5) digits[3]++; if(digits[3]==10) rounded=1.0 else rounded=digits[1]digits[2]digits[3] if(rounded<0.1) rounded=0.1; return rounded; } function populateMCosts(O,M, p,p2,k,cost){ #Not exact, but costs for other models generally seem to fall around $10,000 * num goals upper=10000*O; for(k=1;k0.20) p=int(p*0.25); cost = round(p); #Print to file. print "\tddpData->mCost["k"]= "cost";" > Output } } function populateOWeights(O, p,i,weight){ #Goal weights fall between 0 and 100 for(i=1;ioWeight["i"]= "weight";" > Output } } function populateROI(R,O,Risks,Objectives, p,p2,p3,p4,i,j,impact,curR,curO){ #Risk->Objective Impact, falls between 0 and 1 curR=1; curO=1; #At least one impact per risk. for(i=1;iO) p=O; #1-40 objectives impacted per risk for(j=1;j<=p;j++){ #Which objective is affected? p3=int((O+1)*rand()); if(p3==0) p3=1; #ensures that every objective is used at some point. unused=0; while(unused==0){ allused=1; for(k=1;k0.33) p3=int(p3/2)+1; if(p3==0) p3=1; } } #ensures no repeats per risk found=0; while(found==0){ for(k=1;k<=j;k++){ if(closed[i","k]==p3){ unique=0; break; } else{ unique=1; } } if(unique==1||closed[1,1]==""){ found=1; closed[i","j]=p3 } else{ p3=int((M+1)*rand()); p6=rand(); if(p6>0.33) p3=int(p3/2)+1; if(p3==0) p3=1; } } Used[p3]=1; Risks[curR]=i; Objectives[curO]=p3; curR++; curO++; #and by how much? p4=rand(); impact=roundDecimals(p4); print "\tddpData->roImpact["i"]["p3"]= "impact";" > Output } } } function populateMRE(M,R,Mitigations,Risks,Negatives, p,p2,p3,p4,p5,p6,i,j,effect,curM,curR){ #Mititgation->Risk Effect, falls between 0 and 1 curM=1; curR=1; #At least one mitigation per risk. for(i=1;iM) p=M; #1-20 mitigations affect each risk for(j=1;j<=p;j++){ #Which mitigation affects this risk? p3=int((M+1)*rand()); p6=rand(); if(p6>0.33) p3=int(p3/2)+1; if(p3==0) p3=1; #ensures that every mitigation is used at some point. unused=0; while(unused==0){ allused=1; for(k=1;k0.33) p3=int(p3/2)+1; if(p3==0) p3=1; } } #ensures no repeat mitigations per risk found=0; while(found==0){ for(k=1;k<=j;k++){ if(closed[i","k]==p3){ unique=0; break; } else{ unique=1; } } if(unique==1||closed[1,1]==""){ found=1; closed[i","j]=p3 } else{ p3=int((M+1)*rand()); p6=rand(); if(p6>0.33) p3=int(p3/2)+1; if(p3==0) p3=1; } } Used[p3]=1; Risks[curR]=i; Mitigations[curM]=p3; curR++; curM++; #and by how much? p4=rand(); effect=roundDecimals(p4); #at some small probabilty, make that a negative effect p5=rand(); if(p5>0.99){ #at some small probability, make it a large negative p7=rand(); if(p7>0.66){ effect=effect+1; effect=-effect; Negatives[p3","i]=effect; } else{ effect=-effect; Negatives[p3","i]=effect; } } print "\tddpData->mrEffect["p3"]["i"]= "effect";" > Output } } } function populateRLikelihood(R,M,Mitigations,Risks,Negatives, p,r,r2,i,j,k,l,m,n,o,q,s,t,x,y,a,b,isneg,negQueue,posQueue,out,negout,mits,phasemits){ #determine size and contents of each phase if(M>5){ p[1]=int((M/3)*rand()); p[2]=int((M/3)*rand()); p[3]=int((M/3)*rand()); p[4]=int((M/3)*rand()); p[5]=M-p[1]-p[2]-p[3]-p[4]; if(p[5]<0) p[5]=0; }else{ p[1]=M; p[2]=0; p[3]=0; p[4]=0; p[5]=0; } print p[1]"/"p[2]"/"p[3]"/"p[4]"/"p[5]; for(i=1;i<6;i++){ for(j=1;j0.33) r=int(r/2)+1; if(r==0) r=1; #ensures no repeat mitigations found=0; while(found==0){ if(closed[r]==""){ found=1; closed[r]=1; } else{ r=int((M+1)*rand()); r2=rand(); if(r2>0.33) r=int(r/2)+1; if(r==0) r=1; } } mits[i","j]=r; } } #for each phase for(l=1;l<6;l++){ print "rLikelihoods - Phase "l; print "\t/* Phase "l" Mitigation effects */" > Output nummits=0; #extract mitigations for(m=1;mrLikelihood["q"]"; negout="ddpData->rAggrevatedImpact["q"] = ddpData->rAggrevatedImpact["q"]"; #what mitigations? for(t=1;tmrEffect["Mitigations[x]"]["q"]))"; } else{ #change in likelihood negout="ddpData->rLikelihood["q"] = minValue(1, (ddpData->rLikelihood["q"] - m["Mitigations[x]"] * ddpData->mrEffect["Mitigations[x]"]["q"]))"; } } else{ #likelihood equation out=out" * (1 - m["Mitigations[x]"] * ddpData->mrEffect["Mitigations[x]"]["q"])"; } } x++; } } #Add to appropriate queue if(isneg==1){ negQueue[nN]=negout; nN++; } else{ posQueue[nO]=out; nO++; } } } } #Print to file. for(a=1;a Output; delete negQueue; for(b=1;b Output; delete posQueue; } } function populateOAtRiskProp(O,R,Objectives,Risks, i,x,out){ #For each Objective for(i=1;irLikelihood["Risks[x]"] * ddpData->rAggrevatedImpact["Risks[x]"] * ddpData->roImpact["Risks[x]"]["i"]) +"; } x++; } out = substr(out,0,length(out)-2); print "\t"out";" > Output } }