def SGpotential(fint,level,var): v = var/100.0 # Coefficients for F(rho) f0 = fint[0].get_coeffs() k0,m0 = fint[0]._data[5],fint[0]._data[7] minCoef0 = f0*(1.0-v) maxCoef0 = f0*(1.0+v) # Coefficients for Rho(r) f1 = fint[1].get_coeffs() k1,m1 = fint[1]._data[5],fint[1]._data[7] minCoef1 = f1*(1.0-v) maxCoef1 = f1*(1.0+v) # Coefficients for r*Phi(r) f2 = fint[2].get_coeffs() k2,m2 = fint[2]._data[5],fint[2]._data[7] minCoef2 = f2*(1.0-v) maxCoef2 = f2*(1.0+v) # Setting up sparse grid ndim = len(f0) # Changes based on the number of coefficients from interpolation X,w = design.sparse_grid(ndim, level, rule='CC') X0 = np.copy(X); X1 = np.copy(X); X2 = np.copy(X) # Copying sparse grid to scale it according to interpolation npoints = len(X) # Scaling nodes (each column in X represent a dimension with npoints values) for i in range(ndim): X0[:,i] = 0.5*(maxCoef0[i]-minCoef0[i])*X[:,i] + 0.5*(maxCoef0[i]+minCoef0[i]) X1[:,i] = 0.5*(maxCoef1[i]-minCoef1[i])*X[:,i] + 0.5*(maxCoef1[i]+minCoef1[i]) X2[:,i] = 0.5*(maxCoef2[i]-minCoef2[i])*X[:,i] + 0.5*(maxCoef2[i]+minCoef2[i]) # Scaling weights and saving to file for i in range(npoints): w[i] = w[i]*(0.5)**ndim fint[0]._data[9][:m0-k0-1] = X0[i,:] fint[1]._data[9][:m1-k1-1] = X1[i,:] fint[2]._data[9][:m2-k2-1] = X2[i,:] writeEAM([fint[0],fint[1],fint[2]],'SGPot/Al99.eam.%d.alloy'%(i)) return w
Author: Ilias Bilionis Date: 3/19/2014 """ import design import numpy as np import matplotlib.pyplot as plt # The number of input dimensions num_dim = 2 # The maximum level of the grid max_level = 4 # Draw the design X, w = design.sparse_grid(num_dim, max_level, rule='GH') print 'After:' print 'Grid points:' print X print 'Weights:' print w # And plot it plt.plot(X[:, 0], X[:, 1], '.', markersize=10) plt.xlabel('$x_1$', fontsize=16) plt.ylabel('$x_2$', fontsize=16) plt.title('Gauss Hermite Open Weakly Nested rule', fontsize=16) plt.show()
def _setup_exp_u(self): """ Set up the following: + self.exp_u_raw: The expected utility of the principal as a Function of e_star and the transfer function parameters a. This is a Function. + self.exp_u: The expected utility of the principal as a function of the transfer function parameters a. This is a common Python function. It also returns the gradient of the exp_u with respect to a. """ # Setup the individual rationality constraints self._setup_irc() # Symbolic parameters of transfer functions (i-k) t_as = [[] for _ in range(self.num_agents)] # Symbolic optimal efforts (i-k) t_e_stars = [[] for _ in range(self.num_agents)] # Symbolic xis (i) t_xis = [] # Symbolic efforts (i-k) t_qs = [[] for _ in range(self.num_agents)] t_ts = [[] for _ in range(self.num_agents)] for i in range(self.num_agents): t_xis.append(T.dvector('xi{%d}' % i)) for k in range(self.agents[i].num_types): t_as[i].append(T.dvector('a{%d,%d}' % (i, k))) t_e_stars[i].append(T.scalar('e_stars{%d,%d}' % (i, k))) q_base = self.agents[i].agent_types[k].q t_qs[i].append( theano.clone(q_base.t_f, replace={ q_base.t_x[0]: t_e_stars[i][k], q_base.t_x[1]: t_xis[i] })) t_ts[i].append( theano.clone(self.t.t_f, replace={ self.t.t_x[0]: t_qs[i][k], self.t.t_x[1]: t_as[i][k] })) # For all possible combinations of agent types # Expected utility functions t_sum_u_over_comb = T.zeros((1, )) for at_comb in self._agent_type_range(): # Loop over agent type combs # Get the value function for this combination of types in theano: t_v_comb = theano.clone(self.v.t_f, replace=dict( (self.v.t_x[i], t_qs[i][at_comb[i]]) for i in range(self.num_agents))) # The payoff to the principal for this combination of types t_pi_comb = t_v_comb - T.sum( [t_ts[i][at_comb[i]] for i in range(self.num_agents)], axis=0) # Multiply with the probability of type happening p_comb = np.prod([ self.agents[i].type_probabilities[at_comb[i]] for i in range(self.num_agents) ]) # The utility of the principal for this combination of types t_u = theano.clone(self.u.t_f, replace={self.u.t_x[0]: t_pi_comb}) # Start summing up t_sum_u_over_comb += p_comb * t_u #theano.printing.pydotprint(t_sum_u_over_comb, outfile='tmp.png') # Take the expectation over the Xi's numerically Z, w_unorm = design.sparse_grid(self.num_agents, self._sg_level, 'GH') Xi = Z * np.sqrt(2.0) w = w_unorm / np.sqrt(np.pi**self.num_agents) t_tmp = theano.clone(t_sum_u_over_comb, replace=dict((t_xis[i], Xi[:, i]) for i in range(self.num_agents))) #theano.printing.pydotprint(t_tmp, outfile='tmp.png') # THEANO OBJECT REPRESENTING THE EXPECTED UTILITY OF THE PRINCIPAL: t_exp_u_raw = T.dot(w, t_tmp) t_e_stars_f = flatten(t_e_stars) t_as_f = flatten(t_as) self._exp_u_raw = Function(t_e_stars_f + t_as_f, t_exp_u_raw) # Take derivative with respect to e_stars self._exp_u_raw_g_e = self._exp_u_raw.grad(t_e_stars_f) # Take derivative with respect to the as self._exp_u_raw_g_a = self._exp_u_raw.grad(t_as_f)
Author: Ilias Bilionis Date: 3/19/2014 """ import design import numpy as np import matplotlib.pyplot as plt # The number of input dimensions num_dim = 2 # The maximum level of the grid max_level = 7 # Draw the design X, w = design.sparse_grid(num_dim, max_level, rule='F1') print 'After:' print 'Grid points:' print X print 'Weights:' print w # And plot it plt.plot(X[:, 0], X[:, 1], '.', markersize=10) plt.xlabel('$x_1$', fontsize=16) plt.ylabel('$x_2$', fontsize=16) plt.title('Fejer 1 Open Fully Nested rule', fontsize=16) plt.show()