def runOnce(self, MCSampleSize, xmin, args, interventionList, totalBudget, filename): import asd as asd import pickle import numpy as np numInterventions = len(interventionList) scenarioMonteCarloOutput = [] for r in range(0, MCSampleSize): proposalAllocation = np.random.rand(numInterventions) budgetBest, fval, exitflag, output = asd.asd(objectiveFunction, proposalAllocation, args, xmin = xmin, verbose = 0) outputOneRun = OutputClass(budgetBest, fval, exitflag, output.iterations, output.funcCount, output.fval, output.x) scenarioMonteCarloOutput.append(outputOneRun) # find the best bestSample = scenarioMonteCarloOutput[0] for sample in range(0, len(scenarioMonteCarloOutput)): if scenarioMonteCarloOutput[sample].fval < bestSample.fval: bestSample = scenarioMonteCarloOutput[sample] # scale it and make a dictionary bestSampleBudget = bestSample.budgetBest bestSampleBudgetScaled = rescaleAllocation(totalBudget, bestSampleBudget) bestSampleBudgetScaledDict = {} for i in range(0, len(interventionList)): intervention = interventionList[i] bestSampleBudgetScaledDict[intervention] = bestSampleBudgetScaled[i] # put it in a file outfile = open(filename, 'wb') pickle.dump(bestSampleBudgetScaledDict, outfile) outfile.close()
def getOptimisedRegionalBudgetList(self, geoMCSampleSize): import asd import numpy as np xmin = [0.] * self.numRegions # if BOCs not generated, generate them if self.regionalBOCs == None: self.generateAllRegionsBOC() totalBudget = self.getTotalNationalBudget() scenarioMonteCarloOutput = [] for r in range(0, geoMCSampleSize): proposalSpendingList = np.random.rand(self.numRegions) args = { 'regionalBOCs': self.regionalBOCs, 'totalBudget': totalBudget } budgetBest, fval, exitflag, output = asd.asd( geospatialObjectiveFunction, proposalSpendingList, args, xmin=xmin, verbose=2) outputOneRun = OutputClass(budgetBest, fval, exitflag, output.iterations, output.funcCount, output.fval, output.x) scenarioMonteCarloOutput.append(outputOneRun) # find the best bestSample = scenarioMonteCarloOutput[0] for sample in range(0, len(scenarioMonteCarloOutput)): if scenarioMonteCarloOutput[sample].fval < bestSample.fval: bestSample = scenarioMonteCarloOutput[sample] bestSampleScaled = rescaleAllocation(totalBudget, bestSample.budgetBest) optimisedRegionalBudgetList = bestSampleScaled return optimisedRegionalBudgetList
def getOptimisedRegionalBudgetList(self, geoMCSampleSize): import asd import numpy as np xmin = [0.] * self.numRegions # if BOCs not generated, generate them if self.regionalBOCs == None: self.generateAllRegionsBOC() totalBudget = self.getTotalNationalBudget() scenarioMonteCarloOutput = [] for r in range(0, geoMCSampleSize): proposalSpendingList = np.random.rand(self.numRegions) args = {'regionalBOCs':self.regionalBOCs, 'totalBudget':totalBudget} budgetBest, fval, exitflag, output = asd.asd(geospatialObjectiveFunction, proposalSpendingList, args, xmin = xmin, verbose = 2) outputOneRun = OutputClass(budgetBest, fval, exitflag, output.iterations, output.funcCount, output.fval, output.x) scenarioMonteCarloOutput.append(outputOneRun) # find the best bestSample = scenarioMonteCarloOutput[0] for sample in range(0, len(scenarioMonteCarloOutput)): if scenarioMonteCarloOutput[sample].fval < bestSample.fval: bestSample = scenarioMonteCarloOutput[sample] bestSampleScaled = rescaleAllocation(totalBudget, bestSample.budgetBest) optimisedRegionalBudgetList = bestSampleScaled return optimisedRegionalBudgetList
def runOnce(self, MCSampleSize, xmin, args, interventionList, totalBudget, filename): import asd as asd import pickle import numpy as np numInterventions = len(interventionList) scenarioMonteCarloOutput = [] for r in range(0, MCSampleSize): proposalAllocation = np.random.rand(numInterventions) budgetBest, fval, exitflag, output = asd.asd(objectiveFunction, proposalAllocation, args, xmin=xmin, verbose=0) outputOneRun = OutputClass(budgetBest, fval, exitflag, output.iterations, output.funcCount, output.fval, output.x) scenarioMonteCarloOutput.append(outputOneRun) # find the best bestSample = scenarioMonteCarloOutput[0] for sample in range(0, len(scenarioMonteCarloOutput)): if scenarioMonteCarloOutput[sample].fval < bestSample.fval: bestSample = scenarioMonteCarloOutput[sample] # scale it and make a dictionary bestSampleBudget = bestSample.budgetBest bestSampleBudgetScaled = rescaleAllocation(totalBudget, bestSampleBudget) bestSampleBudgetScaledDict = {} for i in range(0, len(interventionList)): intervention = interventionList[i] bestSampleBudgetScaledDict[intervention] = bestSampleBudgetScaled[ i] # put it in a file outfile = open(filename, 'wb') pickle.dump(bestSampleBudgetScaledDict, outfile) outfile.close()
'costCoverageInfo': costCoverageInfo, 'optimise': optimise, 'mothers': mothers, 'timestep': timestep, 'agingRateList': agingRateList, 'agePopSizes': agePopSizes, 'keyList': keyList, 'data': spreadsheetData } for r in range(0, 10): proposalAllocation = np.random.rand(7) budgetBest, fval, exitflag, output = asd.asd(objectiveFunction, proposalAllocation, args, xmin=xmin) #MaxFunEvals = 10 scaledBudgetBest = rescaleAllocation(totalBudget, budgetBest) scaledproposalAllocation = rescaleAllocation(totalBudget, proposalAllocation) budgetDictBefore = {} budgetDictAfter = {} initialCoverage = {} finalCoverage = {} i = 0 for intervention in spreadsheetData.interventionList: budgetDictBefore[intervention] = scaledproposalAllocation[i] #[0] budgetDictAfter[intervention] = scaledBudgetBest[i][0] initialCoverage[intervention] = costCov.function( array([scaledproposalAllocation[i]
for optimise in ['stunting', 'deaths']: print optimise cascadeBudget = {} for cascade in cascadeValues: print 'CASCADE: ' + str(cascade) #filename = 'Bangladesh_cascade_output_'+str(cascade)+'_'+optimise+'.pkl' #outfile = open(filename, 'wb') scenarioMonteCarloOutput = [] for r in range(0, MCSampleSize): print 'SAMPLE: ' + str(r) totalBudget = currentTotalBudget * cascade args = {'totalBudget':totalBudget, 'costCoverageInfo':costCoverageInfo, 'optimise':optimise, 'mothers':mothers, 'timestep':timestep, 'agingRateList':agingRateList, 'agePopSizes':agePopSizes, 'keyList':keyList, 'data':spreadsheetData} proposalAllocation = np.random.rand(numInterventions) budgetBest, fval, exitflag, output = asd.asd(objectiveFunction, proposalAllocation, args, xmin = xmin, verbose = 0) outputOneRun = OutputClass(budgetBest, fval, exitflag, output.iterations, output.funcCount, output.fval, output.x) scenarioMonteCarloOutput.append(outputOneRun) #pickle.dump(outputOneRun, outfile) #outfile.close() # find the best bestSample = scenarioMonteCarloOutput[0] for sample in range(0, len(scenarioMonteCarloOutput)): if scenarioMonteCarloOutput[sample].fval < bestSample.fval: bestSample = scenarioMonteCarloOutput[sample] # scale it and make a dictionary bestSampleBudget = bestSample.budgetBest bestSampleBudgetScaled = rescaleAllocation(totalBudget, bestSampleBudget) bestSampleBudgetScaledDict = {}
totalBudget = currentTotalBudget * cascade args = { 'totalBudget': totalBudget, 'costCoverageInfo': costCoverageInfo, 'optimise': optimise, 'mothers': mothers, 'timestep': timestep, 'agingRateList': agingRateList, 'agePopSizes': agePopSizes, 'keyList': keyList, 'data': spreadsheetData } proposalAllocation = np.random.rand(numInterventions) budgetBest, fval, exitflag, output = asd.asd(objectiveFunction, proposalAllocation, args, xmin=xmin, verbose=0) outputOneRun = OutputClass(budgetBest, fval, exitflag, output.iterations, output.funcCount, output.fval, output.x) scenarioMonteCarloOutput.append(outputOneRun) #pickle.dump(outputOneRun, outfile) #outfile.close() # find the best bestSample = scenarioMonteCarloOutput[0] for sample in range(0, len(scenarioMonteCarloOutput)): if scenarioMonteCarloOutput[sample].fval < bestSample.fval: bestSample = scenarioMonteCarloOutput[sample]
#proposalAllocation = [totalBudget/2., 0., totalBudget/2., 0., 0., 0.,0.] #proposalAllocation = [invest-1000. if invest>2000. else 1000. for invest in initialAllocation] #proposalAllocation = [totalBudget/len(initialAllocation)] * len(initialAllocation) xmin = [0.] * len(initialAllocation) #xmin = [100.] * len(initialAllocation) #xmin = [0.01*invest if invest>100. else 1. for invest in initialAllocation] optimise = 'stunting' # choose between 'deaths' and 'stunting' args = {'totalBudget':totalBudget, 'costCoverageInfo':costCoverageInfo, 'optimise':optimise, 'mothers':mothers, 'timestep':timestep, 'agingRateList':agingRateList, 'agePopSizes':agePopSizes, 'keyList':keyList, 'data':spreadsheetData} for r in range(0, 10): proposalAllocation = np.random.rand(7) budgetBest, fval, exitflag, output = asd.asd(objectiveFunction, proposalAllocation, args, xmin = xmin) #MaxFunEvals = 10 scaledBudgetBest = rescaleAllocation(totalBudget, budgetBest) scaledproposalAllocation = rescaleAllocation(totalBudget, proposalAllocation) budgetDictBefore = {} budgetDictAfter = {} initialCoverage = {} finalCoverage = {} i = 0 for intervention in spreadsheetData.interventionList: budgetDictBefore[intervention] = scaledproposalAllocation[i]#[0] budgetDictAfter[intervention] = scaledBudgetBest[i][0] initialCoverage[intervention] = costCov.function(array([scaledproposalAllocation[i]]), costCoverageInfo[intervention], targetPopSize[intervention]) / targetPopSize[intervention] finalCoverage[intervention] = costCov.function(array([scaledBudgetBest[i]]), costCoverageInfo[intervention], targetPopSize[intervention]) / targetPopSize[intervention] i += 1