import java.util.*; import java.io.*; public class MicrogridOCBA { private static int ND; private static int ADD_BUDGET; private static int[] n; private static int[] an; private static int b; public static void ocba(double[] mean,double[] var, int nd,int[] n,int add_budget,int type) /* This subroutine determines how many additional runs each design will should have for next iteration of simulation. s_mean[i]: sample mean of design i, i=0,1,...,ND-1 s_var[i]: sample variance of design i, i=0,1,...,ND-1 nd: the number of designs n[i]: number of simulation replications of design i, i=0,1,...,ND-1 add_budget; the additional simulation budget an[i]: additional number of simulation replications assigned to design i, i=0,1,...,ND-1 type: type of opitmization problem. type=1, MIN problem; type=2, MAX problem */ { int i,s,budget,remainingBudget; double[] ratio=new double[nd]; /* Ni/Ns */ double totalRatio; double temp=0; boolean[] moreRun=new boolean[nd]; boolean moreAllocation=true; if(type==2) /*MAX problem*/ { for(i=0;i<nd;i++) mean[i]=-mean[i]; } b=best(mean,nd); s=second_best(mean,nd,b); ratio[s]=1; for(i=0;i<nd;i++) { if(i!=s && i!=b) { ratio[i]=Math.pow((mean[b]-mean[s])/(mean[b]-mean[i]),2)*var[i]/var[s]; } /* calculate ratio of Ni/Ns */ if(i!=b) temp+=ratio[i]*ratio[i]/var[i]; } ratio[b]=Math.sqrt(var[b]*temp); /* calculate NB */ budget=add_budget; for(i=0;i<nd;i++) { budget+=n[i]; moreRun[i]=true; } remainingBudget=budget; while(moreAllocation) { moreAllocation=false; totalRatio=0; for(i=0;i<nd;i++) { if(moreRun[i]) totalRatio+=ratio[i]; } for(i=0;i<nd;i++) { if(moreRun[i]) an[i]=(int)(remainingBudget*ratio[i]/totalRatio); /*disable those design which have been run too much */ if(an[i]<n[i]) { an[i]=n[i]; moreRun[i]=false; moreAllocation=true; } } if(moreAllocation) { remainingBudget=budget; for(i=0;i<nd;i++) { if(moreRun[i]==false) remainingBudget-=an[i]; } } } /*end of WHILE */ /*calculate the difference */ remainingBudget=an[0]; for(i=1;i<nd;i++) remainingBudget+=an[i]; an[b]+=(budget-remainingBudget); /* give the difference to design b */ for (i=0;i<nd;i++) an[i]-=n[i]; } public static int best(double[] mean, int nd) /* This function determines the best design based on current simulation results */ /* t_s_mean[i]: temporary array for sample mean of design i, i=0,1,...,ND-1 nd: the number of designs */ { int min_index=0; for(int i=0;i<nd;i++) { if(mean[i]<mean[min_index]) { min_index=i; } } return min_index; } public static int second_best(double[] t_s_mean,int nd,int b) /* This function determines the second best design based on current simulation results */ /* t_s_mean[i]: temporary array for sample mean of design i, i=0,1,...,ND-1 nd: the number of designs. b: current best design determined by function best() */ { int i, second_index; if(b==0) second_index=1; else second_index=0; for(i=0;i<nd;i++) { if(t_s_mean[i]<t_s_mean[second_index] && i!=b) { second_index=i; } } return second_index; }