def updateStatistics(self): for curLevel in range(len(self.space4Level)): samples = self.y4Level[curLevel] self.variance4level[curLevel] = np.var(samples, 0) compCost = self.space4Level[curLevel].dim() ** 2 / 1000. # Simple for uniform meshes # optSamples = self.constCompCost * np.sqrt( # np.sqrt(np.sqrt(np.mean(np.abs(self.variance4level[curLevel]))) / compCost)) # Same but for general meshes space = self.space4Level[curLevel] domainArea = assemble(interpolate(Constant(1), space) * dx) varianceMean = Function(space) varianceMean.vector().set_local(np.abs(self.variance4level[curLevel])) varianceIntMean = np.sqrt(assemble(varianceMean * dx) / domainArea) optSamples = self.constCompCost * np.sqrt(np.sqrt( varianceIntMean / compCost)) # Alernate from ??? # l = curLevel + 2 # maxL = len(self.space4Level) + 1 # epsOptSamples = 0.1 # optSamples = l ** (2 + 2 * epsOptSamples) * 2 ** (2 * (maxL - l)) self.optNumSamples4Level[curLevel] = np.rint(optSamples).astype(int) self.compCost4lvl[-1] = sum(list(len(samples) * (space.dim() ** 2) for (space, samples) in zip(self.space4Level, self.y4Level)))
def runCycle(self): # initial sampling print("Level {level}".format(level = 0)) self.sampleSolutions() self.updateStatistics() self.sampleSolutions() meanFunction = Function(self.space4Level[0]) meanFunction.vector().set_local(np.mean(self.y4Level[0], 0)) self.mean4level[0] = meanFunction for level in range(1, self.maxLevels): print("Level {level}".format(level = level)) self.addLevel() self.sampleSolutions() self.updateStatistics() self.sampleSolutions() levelFunction = Function(self.space4Level[level]) levelFunction.vector().set_local(np.mean(self.y4Level[level], 0)) self.mean4level.append(levelFunction) for curLevel in range(level): tempFunction = Function(self.space4Level[curLevel]) tempFunction.vector().set_local(np.mean(self.y4Level[curLevel], 0)) self.mean4level[level] += tempFunction