def run_regular_sparse_grid(self, gridTypeStr, level, maxGridSize, boundaryLevel=1, isFull=False, out=False, plot=False): np.random.seed(1234567) gridType = Grid.stringToGridType(gridTypeStr) results = {'surrogate': 'sg', 'grid_type': gridType, 'is_full': False, 'time_steps': self.toi, 'setting': self.setting, 'num_dims': self.numDims, 'qoi': self.qoi, 'max_grid_size': maxGridSize, 'boundary_level': boundaryLevel, 'refinement': None, 'results': {}} while True: print("-" * 80) print("level = %i, grid type = %s" % (level, gridTypeStr)) builder = UQBuilder() self.defineUQSetting(builder) uqSetting = builder.andGetResult() uqManager = TestEnvironmentSG().buildSetting(self.params, level=level, gridType=gridType, deg=20, maxGridSize=maxGridSize, isFull=isFull, boundaryLevel=boundaryLevel, qoi=self.qoi, toi=self.toi, saveAfterN=-1, uqSetting=uqSetting, uqSettingRef=self.uqSettings['ref']) if uqManager.sampler.getSize() > maxGridSize: print("DONE: %i > %i" % (uqManager.sampler.getSize(), maxGridSize)) break # ---------------------------------------------- oldSize = uqManager.uqSetting.getSize() while uqManager.hasMoreSamples(): uqManager.runNextSamples() # ---------------------------------------------------------- # specify ASGC estimator analysis = ASGCAnalysisBuilder().withUQManager(uqManager)\ .withAnalyticEstimationStrategy()\ .andGetResult() analysis.setVerbose(False) # ---------------------------------------------------------- label = "sg_l%i_%s" % (level, gridTypeStr) self.runAnalysis(analysis, uqManager, "sg", label, out, plot, results) level += 1 # update results results["max_grid_size"] = uqManager.getGrid().getSize() if out: # store results filename = os.path.join(self.pathResults, "%s-qoi%s_%s_d%i_%s_Nmax%i_r%i_N%i.pkl" % (self.radix, self.qoi, "sg" if not isFull else "fg", self.numDims, gridTypeStr, maxGridSize, False, uqManager.getGrid().getSize())) fd = open(filename, "w") pkl.dump(results, fd) fd.close()
def run_adaptive_sparse_grid(self, gridTypeStr, level, maxGridSize, refinement, boundaryLevel=None, out=False, plot=False): np.random.seed(1234567) gridType = Grid.stringToGridType(gridTypeStr) results = {'surrogate': 'asg', 'grid_type': gridType, 'is_full': False, 'time_steps': self.toi, 'setting': self.setting, 'num_dims': self.numDims, 'qoi': self.qoi, 'max_grid_size': maxGridSize, 'boundary_level': boundaryLevel, 'refinement': refinement, 'refinement_technique': 'refinement', 'adaptPoints': 5, 'adaptRate': 0.05, 'adpatEps': 1e-10, 'results': {}} # ---------------------------------------------------------- # define the learner # ---------------------------------------------------------- builder = UQBuilder() self.defineUQSetting(builder) uqSetting = builder.andGetResult() uqManager = TestEnvironmentSG().buildSetting(self.params, level=level, gridType=gridType, deg=20, maxGridSize=maxGridSize, isFull=False, adaptive=refinement, adaptPoints=5, # adaptRate=0.05, epsilon=1e-10, boundaryLevel=boundaryLevel, qoi=self.qoi, toi=self.toi, saveAfterN=-1, # dont save at all uqSetting=uqSetting, uqSettingRef=self.uqSettings['ref']) # ---------------------------------------------- # first run oldSize = uqManager.uqSetting.getSize() while uqManager.hasMoreSamples(): uqManager.runNextSamples() # ---------------------------------------------------------- # specify ASGC estimator analysis = ASGCAnalysisBuilder().withUQManager(uqManager)\ .withAnalyticEstimationStrategy()\ .andGetResult() analysis.setVerbose(False) # ---------------------------------------------------------- label = "asg_l%i_%s" % (level, gridTypeStr) self.runAnalysis(analysis, uqManager, "sg", label, out, plot, results) # update results results["max_grid_size"] = uqManager.getGrid().getSize() if out: # store results filename = os.path.join(self.pathResults, "%s-qoi%s_%s_d%i_%s_Nmax%i_r%s_N%i.pkl" % (self.radix, self.qoi, "asg", self.numDims, gridTypeStr, maxGridSize, refinement, uqManager.getGrid().getSize())) fd = open(filename, "w") pkl.dump(results, fd) fd.close()
def run_adaptive_sparse_grid(self, gridType, level, maxGridSize, refinement, boundaryLevel=None, isFull=False, out=False, plot=False): test_samples, test_values = self.getTestSamples() # ---------------------------------------------------------- # define the learner # ---------------------------------------------------------- uqManager = TestEnvironmentSG().buildSetting(self.params, self.simulation, level, gridType, deg=20, maxGridSize=maxGridSize, isFull=isFull, adaptive=refinement, adaptPoints=10, adaptRate=0.05, epsilon=1e-10, boundaryLevel=boundaryLevel) # ---------------------------------------------- # first run while uqManager.hasMoreSamples(): uqManager.runNextSamples() # ---------------------------------------------------------- # specify ASGC estimator # ---------------------------------------------------------- analysis = ASGCAnalysisBuilder().withUQManager(uqManager)\ .withAnalyticEstimationStrategy()\ .andGetResult() analysis.setVerbose(False) # ---------------------------------------------------------- # expectation values and variances sg_mean, sg_var = analysis.mean(), analysis.var() stats = {} iterations = uqManager.getKnowledge().getAvailableIterations() for k, iteration in enumerate(iterations): # ---------------------------------------------------------- # estimated anova decomposition anova = analysis.getAnovaDecomposition(iteration=iteration, nk=len(self.params)) # estimate the l2 error grid, alpha = uqManager.getKnowledge().getSparseGridFunction(iteration=iteration) test_values_pred = evalSGFunction(grid, alpha, test_samples) l2test, l1test, maxErrorTest, meanError, varError = \ self.getErrors(test_values, test_values_pred, sg_mean[iteration][0], sg_var[iteration][0]) # ---------------------------------------------------------- # main effects sobol_indices = anova.getSobolIndices() total_effects = computeTotalEffects(sobol_indices) print("-" * 60) print("iteration=%i, N=%i" % (iteration, grid.getSize())) print("E[x] = %g ~ %g (err=%g)" % (self.E_ana[0], sg_mean[iteration]["value"], np.abs(self.E_ana[0] - sg_mean[iteration]["value"]))) print("V[x] = %g ~ %g (err=%g)" % (self.V_ana[0], sg_var[iteration]["value"], np.abs(self.V_ana[0] - sg_var[iteration]["value"]))) stats[grid.getSize()] = {'num_model_evaluations': grid.getSize(), 'l2test': l2test, 'l1test': l1test, 'maxErrorTest': maxErrorTest, 'mean_error': meanError, 'var_error': varError, 'mean_estimated': sg_mean[iteration]["value"], 'var_estimated': sg_var[iteration]["value"], 'sobol_indices_estimated': sobol_indices, 'total_effects_estimated': total_effects} if plot: self.plotResultsSG(grid, alpha, level, maxGridSize, refinement, iteration, out) if out: # store results filename = os.path.join(self.pathResults, "%s_%s_d%i_%s_l%i_Nmax%i_r%s_N%i.pkl" % (self.radix, "sg" if not isFull else "fg", self.numDims, grid.getTypeAsString(), level, maxGridSize, refinement, grid.getSize())) fd = open(filename, "w") pkl.dump({'surrogate': 'sg', 'model': "full" if self.numDims == 4 else "reduced", 'num_dims': self.numDims, 'grid_type': grid.getTypeAsString(), 'level': level, 'max_grid_size': maxGridSize, 'is_full': isFull, 'refinement': refinement, 'mean_analytic': self.E_ana[0], 'var_analytic': self.V_ana[0], 'results': stats}, fd) fd.close()
def run_regular_sparse_grid(self, gridType, level, maxGridSize, boundaryLevel=1, isFull=False, out=False, plot=False): np.random.seed(1234567) test_samples, test_values = self.getTestSamples() stats = {} while True: print("-" * 80) print("level = %i" % level) uqManager = TestEnvironmentSG().buildSetting(self.params, self.simulation, level, gridType, deg=20, maxGridSize=maxGridSize, isFull=isFull, boundaryLevel=boundaryLevel) if uqManager.sampler.getSize() > maxGridSize: print("DONE: %i > %i" % (uqManager.sampler.getSize(), maxGridSize)) break # ---------------------------------------------- # first run while uqManager.hasMoreSamples(): uqManager.runNextSamples() # ---------------------------------------------------------- # specify ASGC estimator analysis = ASGCAnalysisBuilder().withUQManager(uqManager)\ .withAnalyticEstimationStrategy()\ .andGetResult() analysis.setVerbose(False) # ---------------------------------------------------------- # expectation values and variances sg_mean, sg_var = analysis.mean(), analysis.var() # ---------------------------------------------------------- # estimate the l2 error grid, alpha = uqManager.getKnowledge().getSparseGridFunction() test_values_pred = evalSGFunction(grid, alpha, test_samples) l2test, l1test, maxErrorTest, meanError, varError = \ self.getErrors(test_values, test_values_pred, sg_mean["value"], sg_var["value"]) print("-" * 60) print("test: |.|_2 = %g" % l2test) print("E[x] = %g ~ %g (err=%g)" % (self.E_ana[0], sg_mean["value"], np.abs(self.E_ana[0] - sg_mean["value"]))) print("V[x] = %g ~ %g (err=%g)" % (self.V_ana[0], sg_var["value"], np.abs(self.V_ana[0] - sg_var["value"]))) # ---------------------------------------------------------- # estimated anova decomposition if self.inputSpace != "sgde": anova = analysis.getAnovaDecomposition(nk=len(self.params)) sobol_indices = anova.getSobolIndices() total_effects = computeTotalEffects(sobol_indices) else: sobol_indices = {} total_effects = {} # ---------------------------------------------------------- stats[level] = {'num_model_evaluations': grid.getSize(), 'l2test': l2test, 'l1test': l1test, 'maxErrorTest': maxErrorTest, 'mean_error': meanError, 'var_error': varError, 'mean_estimated': sg_mean["value"], 'var_estimated': sg_var["value"], 'sobol_indices_estimated': sobol_indices, 'total_effects_estimated': total_effects} if plot: self.plotResultsSG(grid, alpha, level, maxGridSize, False, 0, out) level += 1 if out: # store results filename = os.path.join(self.pathResults, "%s_%s_d%i_%s_Nmax%i_r%i_N%i.pkl" % (self.radix, "sg" if not isFull else "fg", self.numDims, grid.getTypeAsString(), maxGridSize, False, grid.getSize())) fd = open(filename, "w") pkl.dump({'surrogate': 'sg', 'num_dims': self.numDims, 'grid_type': grid.getTypeAsString(), 'max_grid_size': maxGridSize, 'level': level, 'boundaryLevel': boundaryLevel, 'is_full': isFull, 'refinement': False, 'mean_analytic': self.E_ana[0], 'var_analytic': self.V_ana[0], 'results': stats}, fd) fd.close()
def run_adaptive_sparse_grid(self, gridTypeStr, level, maxGridSize, boundaryLevel=1, refinement="l2", out=False): np.random.seed(1234567) test_samples, test_values = self.getTestSamples() gridType = Grid.stringToGridType(gridTypeStr) print("-" * 80) print("level = %i, boundary level = %i" % (level, boundaryLevel)) print("-" * 80) uqManager = TestEnvironmentSG().buildSetting( self.params, self.simulation, level, gridType, deg=20, maxGridSize=maxGridSize, adaptive=refinement, adaptRate=0.1, adaptPoints=20, epsilon=1e-15, boundaryLevel=min(level, boundaryLevel), knowledgeTypes=[KnowledgeTypes.SIMPLE, KnowledgeTypes.SQUARED]) # ---------------------------------------------- # first run while uqManager.hasMoreSamples(): uqManager.runNextSamples() # ---------------------------------------------------------- # specify ASGC estimator # ---------------------------------------------------------- analysis = ASGCAnalysisBuilder().withUQManager(uqManager)\ .withMonteCarloEstimationStrategy(n=1000, npaths=10)\ .andGetResult() analysis.setVerbose(False) # ---------------------------------------------------------- # expectation values and variances stats = {} iterations = uqManager.getKnowledge().getAvailableIterations() for k, iteration in enumerate(iterations): # ---------------------------------------------------------- # estimate the l2 error grid, alpha = uqManager.getKnowledge().getSparseGridFunction( iteration=iteration) test_values_pred = evalSGFunction(grid, alpha, test_samples) l2test, l1test, maxErrorTest = \ self.getErrors(test_values, test_values_pred) print("-" * 60) print("iteration=%i, N=%i" % (iteration, grid.getSize())) print("test: |.|_2 = %g" % l2test) # sg_mean, sg_var = analysis.mean(iterations=[iteration]), analysis.var(iterations=[iteration]) # ---------------------------------------------------------- stats[grid.getSize()] = { 'num_model_evaluations': grid.getSize(), 'l2test': l2test, 'l1test': l1test, 'maxErrorTest': maxErrorTest, 'mean_estimated': None, # sg_mean["value"], 'var_estimated': None # sg_var["value"] } if out: # store results radix = "%s_sg_d%i_%s_Nmax%i_r%s_N%i_b%i" % ( self.radix, self.numDims, grid.getTypeAsString(), maxGridSize, refinement, grid.getSize(), boundaryLevel) if self.rosenblatt: radix += "_rosenblatt" filename = os.path.join(self.pathResults, "%s.pkl" % radix) fd = open(filename, "w") pkl.dump( { 'surrogate': 'sg', 'num_dims': self.numDims, 'grid_type': grid.getTypeAsString(), 'max_grid_size': maxGridSize, 'is_full': False, 'refinement': refinement, 'rosenblatt': self.rosenblatt, 'boundaryLevel': boundaryLevel, 'results': stats }, fd) fd.close()
def run_regular_sparse_grid(self, gridTypeStr, level, maxGridSize, boundaryLevel=1, out=False): np.random.seed(1234567) test_samples, test_values = self.getTestSamples() gridType = Grid.stringToGridType(gridTypeStr) stats = {} while True: print("-" * 80) print("level = %i, boundary level = %i" % (level, boundaryLevel)) print("-" * 80) uqManager = TestEnvironmentSG().buildSetting( self.params, self.simulation, level, gridType, deg=20, maxGridSize=maxGridSize, boundaryLevel=min(level, boundaryLevel), knowledgeTypes=[KnowledgeTypes.SIMPLE]) if uqManager.sampler.getSize() > maxGridSize: print("DONE: %i > %i" % (uqManager.sampler.getSize(), maxGridSize)) break # ---------------------------------------------- # first run while uqManager.hasMoreSamples(): uqManager.runNextSamples() # ---------------------------------------------------------- if False: grid, alpha = uqManager.knowledge.getSparseGridFunction() samples = DataMatrix(grid.getSize(), self.numDims) grid.getStorage().getCoordinateArrays(samples) samples = self.dist.ppf(samples.array()) fig = plt.figure() plotFunction2d(self.simulation, color_bar_label=r"$u(\xi_1, \xi_2)$") plt.scatter( samples[:, 0], samples[:, 1], color=load_color(3), label=r"SG (CC-bound., $\ell=%i, \ell^{\text{b}}=%i$)" % (level, boundaryLevel)) plt.xlabel(r"$\xi_1$") plt.xlabel(r"$\xi_2$") lgd = insert_legend(fig, loc="bottom", ncol=1) savefig(fig, "plots/genz_with_grid_l%i_b%i" % (level, boundaryLevel), lgd, tikz=False) # ---------------------------------------------------------- # specify ASGC estimator analysis = ASGCAnalysisBuilder().withUQManager(uqManager)\ .withMonteCarloEstimationStrategy(n=1000, npaths=10)\ .andGetResult() analysis.setVerbose(False) # ---------------------------------------------------------- # expectation values and variances sg_mean, sg_var = analysis.mean(), analysis.var() # ---------------------------------------------------------- # estimate the l2 error grid, alpha = uqManager.getKnowledge().getSparseGridFunction() test_values_pred = evalSGFunction(grid, alpha, test_samples) l2test, l1test, maxErrorTest = \ self.getErrors(test_values, test_values_pred) print("-" * 60) print("test: |.|_2 = %g" % l2test) # ---------------------------------------------------------- stats[level] = { 'num_model_evaluations': grid.getSize(), 'l2test': l2test, 'l1test': l1test, 'maxErrorTest': maxErrorTest, 'mean_estimated': sg_mean["value"], 'var_estimated': sg_var["value"] } level += 1 if out: # store results radix = "%s_sg_d%i_%s_Nmax%i_N%i_b%i" % ( self.radix, self.numDims, grid.getTypeAsString(), maxGridSize, grid.getSize(), boundaryLevel) if self.rosenblatt: radix += "_rosenblatt" filename = os.path.join(self.pathResults, "%s.pkl" % radix) fd = open(filename, "w") pkl.dump( { 'surrogate': 'sg', 'num_dims': self.numDims, 'grid_type': grid.getTypeAsString(), 'max_grid_size': maxGridSize, 'is_full': False, 'refinement': False, 'rosenblatt': self.rosenblatt, 'boundaryLevel': boundaryLevel, 'results': stats }, fd) fd.close()
def run_regular_sparse_grid_boundary(self, gridTypeStr, level, maxGridSize, boundaryLevel=1, out=False): np.random.seed(1234567) test_samples, test_values = self.getTestSamples() gridType = Grid.stringToGridType(gridTypeStr) stats = {} while boundaryLevel <= level: print("-" * 80) print("level = %i, boundary level = %i" % (level, boundaryLevel)) print("-" * 80) uqManager = TestEnvironmentSG().buildSetting( self.params, self.simulation, level, gridType, deg=20, maxGridSize=maxGridSize, boundaryLevel=boundaryLevel, knowledgeTypes=[KnowledgeTypes.SIMPLE]) # ---------------------------------------------- # first run while uqManager.hasMoreSamples(): uqManager.runNextSamples() # ---------------------------------------------------------- # specify ASGC estimator analysis = ASGCAnalysisBuilder().withUQManager(uqManager)\ .withMonteCarloEstimationStrategy(n=1000, npaths=10)\ .andGetResult() analysis.setVerbose(False) # ---------------------------------------------------------- # expectation values and variances sg_mean, sg_var = analysis.mean(), analysis.var() # ---------------------------------------------------------- # estimate the l2 error grid, alpha = uqManager.getKnowledge().getSparseGridFunction() test_values_pred = evalSGFunction(grid, alpha, test_samples) l2test, l1test, maxErrorTest = \ self.getErrors(test_values, test_values_pred) print("-" * 60) print("test: |.|_2 = %g" % l2test) # ---------------------------------------------------------- # ---------------------------------------------------------- stats[boundaryLevel] = { 'num_model_evaluations': grid.getSize(), 'l2test': l2test, 'l1test': l1test, 'maxErrorTest': maxErrorTest, 'mean_estimated': sg_mean["value"], 'var_estimated': sg_var["value"] } boundaryLevel += 1 if out: # store results filename = os.path.join( self.pathResults, "%s_sg_d%i_%s_Nmax%i_N%i_b%i.pkl" % (self.radix, self.numDims, grid.getTypeAsString(), maxGridSize, grid.getSize(), boundaryLevel)) fd = open(filename, "w") pkl.dump( { 'surrogate': 'sg', 'num_dims': self.numDims, 'grid_type': grid.getTypeAsString(), 'max_grid_size': maxGridSize, 'is_full': False, 'refinement': False, 'results': stats }, fd) fd.close()