예제 #1
0
def eqProbs(p, U):
    A = len(p)
    pC = adouble(np.zeros((A, )))
    pF = adouble(np.zeros((A, )))
    for i in xrange(A):
        c = cStar(p[i], U, i)
        g1 = g_jo(c, U, i)
        pC[i] = g1
        pF[i] = h_jo(c, U, i) / g1
    return np.hstack((p, pC, pF)).reshape((-1, 3), order='F')
예제 #2
0
def tape(fID, gID, lID, x0, beta):

    t0 = time.time()

    # trace objective function
    adolc.trace_on(fID)
    ax = adolc.adouble(x0)
    adolc.independent(ax)
    ay = action(ax, beta)
    #ay = actTDtest(ax, beta)
    adolc.dependent(ay)
    adolc.trace_off()

    # trace constraint function
    adolc.trace_on(gID)
    ax = adolc.adouble(x0)
    adolc.independent(ax)
    ay = eval_g(ax)
    adolc.dependent(ay)
    adolc.trace_off()

    # trace lagrangian function
    
    adolc.trace_on(lID)
    # xtmp = [x0, lambdas, obj_factor]
#    xtmp = np.hstack(x0,np.ones(ncon),1.)
    ax = adolc.adouble(x0)
    alagrange = adolc.adouble(np.ones(ncon))
    aobj_factor = adolc.adouble(1.)
    adolc.independent(ax)
    adolc.independent(alagrange)
    adolc.independent(aobj_factor)
    ay = eval_lagrangian(ax, alagrange, aobj_factor, beta)
    adolc.dependent(ay)
    adolc.trace_off()

    t1 = time.time()
    print "tape time = ", t1-t0
    
    eval_jac_g_adolc = Eval_jac_g(x0)

    #eval_h_adolc = Eval_h(x0, np.ones(ncon), 1.)
    eval_h_adolc = Eval_h_dense(x0, np.ones(ncon), 1.)
    t2 = time.time()
    print "Hess time = ", t2-t1


    #




    return eval_jac_g_adolc, eval_h_adolc
예제 #3
0
    def adjoint_solve(self, data, weight_sens=False):
        
        # Evaluate the jacobian of residuals w.r.t. states and augmentation field

        ad.trace_on(1)

        ad_states   = ad.adouble(self.states)
        ad_beta     = ad.adouble(self.beta)

        ad.independent(ad_states)
        ad.independent(ad_beta)

        ad_res = self.evalResidual(ad_states, ad_beta)

        ad.dependent(ad_res)
        ad.trace_off()

        jacres = ad.jacobian(1, np.hstack((self.states, self.beta)))

        Rq = jacres[:,0:np.shape(self.states)[0]]
        Rb = jacres[:,np.shape(self.states)[0]:]
        
        # Obtain the jacobian of objective function w.r.t. states and augmentation field
        Jq, Jb = self.getObjJac(data)
        
        # Solve the discrete adjoint system to obtain sensitivity
        psi  = np.linalg.solve(Rq.T,Jq)
        sens = Jb - np.matmul(Rb.T,psi)

        # Obtain the sensitivity of the objective function w.r.t. NN weights
        if weight_sens==True:

            d_weights = nn.nn.nn_get_weights_sens(np.asfortranarray(self.nn_params["network"]),
                                                  
                                                  self.nn_params["act_fn"],
                                                  self.nn_params["loss_fn"],
                                                  self.nn_params["opt"],
                                                  
                                                  np.asfortranarray(self.nn_params["weights"]),
                                                  np.asfortranarray(self.features),
                                                  
                                                  1,
                                                  np.shape(self.beta)[0],
                                                  
                                                  np.asfortranarray(sens),
                                                  np.asfortranarray(self.nn_params["opt_params_array"]))

            return d_weights

        else:
            
            return sens
예제 #4
0
    def pCostpW_adolc(self, W, p_target):

        tag = self.pCostpW_tag
        if not self.pCostpW_traced:
            aW = adouble(W.flatten(order='F'))
            ap = adouble(p_target)

            adolc.trace_on(tag)
            adolc.independent(aW)
            aW3 = np.reshape(aW, W.shape, order='F')
            acost = cost.inverse_pressure_design(aW3, ap)
            adolc.dependent(acost)
            adolc.trace_off()

        return adolc.gradient(self.pCostpW_tag, W.flatten(order='F'))
예제 #5
0
    def __init__(self,func,x):
        """ Initializes the tape for a function which allows
        the user to then quickly evaluate the function,
        gradient, and hessian at arbitrary points

        func is assumed to be a function from a vector array
        to a single number.

        Also updates the global CUR_ADOLC_TAPE_NUMBER which
        keeps track of the tapes that have been used by
        ADOLC

        Parameters
        ----------
        func: function
            function to be evaluated
        x: ndarray
            ndarray of correct shape and type for the 
            function

        """
        # access the tape number counter
        global CUR_ADOLC_TAPE_NUMBER
        # initialize the ADOLC tape for the function
        adolc.trace_on(CUR_ADOLC_TAPE_NUMBER)
        x = adolc.adouble(x)
        adolc.independent(x)
        y = func(x)
        adolc.dependent(y)
        adolc.trace_off()
        self.tape_number = CUR_ADOLC_TAPE_NUMBER
        CUR_ADOLC_TAPE_NUMBER += 1
예제 #6
0
파일: adolcdev.py 프로젝트: AndySze/cardoon
def condassign(b, c, d):
    """
    Returns the result of (b>0)? c : d 

    Use instead of if statements to force taping both c and d
    expressions.
    """
    if type(b) == ad._adolc.adub:
        ad_a = ad.adouble(0.)
        ad.condassign(ad_a, ad.adouble(b), ad.adouble(c), ad.adouble(d))
        return ad_a
    else:
        if b > 0.:
            return c
        else:
            return d
예제 #7
0
def condassign(b, c, d):
    """
    Returns the result of (b>0)? c : d 

    Use instead of if statements to force taping both c and d
    expressions.
    """
    if type(b) == ad._adolc.adub:
        ad_a = ad.adouble(0.)
        ad.condassign(ad_a, ad.adouble(b), ad.adouble(c), ad.adouble(d))
        return ad_a
    else:
        if b > 0.:
            return c
        else:
            return d
    def calc_residual_jacobian(self, q, dq=1e-25):
        n = np.size(q)
        q_c = q.copy()
        q = ad.adouble(q)
        tag = 0
        ad.trace_on(tag)
        ad.independent(q)
        R = self.calc_residual(q)
        ad.dependent(R)
        ad.trace_off()
        options = np.array([0, 0, 0, 0], dtype=int)
        q = q_c

        dRdq = calc_jacobian(q, tag=tag, shape=(n, n))

        #pat = ad.sparse.jac_pat(tag, q_c, options)
        #if 1:
        #    result = ad.colpack.sparse_jac_no_repeat(tag, q, options)
        #else:
        #    result = ad.colpack.sparse_jac_repeat(tag, q, nnz, ridx, cidx, values)

        #nnz = result[0]
        #ridx = result[1]
        #cidx = result[2]
        #values = result[3]
        #dRdq = sp.csr_matrix((values, (ridx, cidx)), shape=(n, n))

        #dRdq = np.zeros([n, n], dtype=q.dtype)
        #for i in range(n):
        #    q[i] = q[i] + 1j*dq
        #    R = self.calc_residual(q)
        #    dRdq[:,i] = np.imag(R[:])/dq
        #    q[i] = q[i] - 1j*dq
        return dRdq
 def calc_residual(self, q, dtype=None):
     R = np.zeros_like(q)
     if dtype == ad.adouble:
         R = ad.adouble(R)
     n = self.n
     R[0::3], R[1::3], R[2::3] = self.calc_momentum_residual(q), self.calc_k_residual(q), self.calc_omega_residual(q)
     #R = komegaf.calc_residual(self.y.astype(np.float64), q.astype(np.float64), np.float64(self.dp), self.beta.astype(np.float64))
     return R
예제 #10
0
def PyAdolc_dvLJ(x):
    adolc.trace_on(0)
    ad_x = adolc.adouble(np.zeros(np.shape(np.ravel(x)),dtype=float))
    adolc.independent(ad_x)
    ad_y = Adolc_vLJ(ad_x)
    adolc.dependent(ad_y)
    adolc.trace_off()
    return adolc.gradient(0, np.ravel(x))
예제 #11
0
 def _trace_con(self, x):
     if self._con_trace_id is None and self.m > 0:
         self._con_trace_id = self._get_trace_id() + 1
         adolc.trace_on(self._con_trace_id)
         x = adolc.adouble(x)
         adolc.independent(x)
         y = self.cons(x)
         adolc.dependent(y)
         adolc.trace_off()
예제 #12
0
 def _trace_obj(self, x):
     if self._obj_trace_id is None:
         self._obj_trace_id = self._get_trace_id()
         adolc.trace_on(self._obj_trace_id)
         x = adolc.adouble(x)
         adolc.independent(x)
         y = self.obj(x)
         adolc.dependent(y)
         adolc.trace_off()
예제 #13
0
    def pRpW_adolc(self, sim, W, area):

        tag = self.pRpW_tag
        if not self.pRpW_traced:
            aW = adouble(W.flatten(order='F'))
            aarea = adouble(area)

            adolc.trace_on(tag)
            adolc.independent(aW)

            aW3 = np.reshape(aW, W.shape, order='F')
            aresidual = q1d.evaluate_residual(sim, aW3, area)

            aresidual.flatten(order='F')
            adolc.dependent(aresidual)
            adolc.trace_off()

        return adolc.jacobian(self.pRpW_tag, W.flatten(order='F'))
예제 #14
0
    def ppRpWpW_adolc(self, sim, W, area):

        tag = self.ppRpWpW_tag
        if not self.ppRpWpW_traced:
            aW = adouble(W.flatten(order='F'))
            aarea = adouble(area)

            adolc.trace_on(tag)
            adolc.independent(aW)

            aW3 = np.reshape(aW, W.shape, order='F')
            pRpW = self.pRpW_adolc(sim, aW3, area)

            pRpW.flatten(order='F')
            adolc.dependent(pRpW)
            adolc.trace_off()

        return adolc.hessian(self.ppRpWpW_tag, W.flatten(order='F'))
예제 #15
0
def const(p, U):
    A = len(p)
    psi = adouble(np.zeros((A, )))
    for i in xrange(A):
        c = cStar(p[i], U, i)
        g1 = g_jo(c, U, i)
        j = h_jo(c, U, i) / g1
        psi[i] = (p[i] - f_jo(j, U, i))
    return psi
예제 #16
0
    def _trace_lag(self, x, z):
        self._trace_obj(x)
        self._trace_con(x)
        self._trace_cons_pos(x)
        unconstrained = self.m == 0 and self.nbounds == 0
        if self._lag_trace_id is None:
            if unconstrained:
                self._lag_trace_id = self._obj_trace_id
                return

            self._lag_trace_id = self._get_trace_id() + 3
            adolc.trace_on(self._lag_trace_id)
            x = adolc.adouble(x)
            z = adolc.adouble(z)
            adolc.independent(x)
            adolc.independent(z)
            l = self.lag(x, z)
            adolc.dependent(l)
            adolc.trace_off()
예제 #17
0
    def __init__(self, f, x, test = 'f'):
        self.f = f
        self.x = x

        adolc.trace_on(0)
        ax = adolc.adouble(x)
        adolc.independent(ax)
        y = f(ax)
        adolc.dependent(y)
        adolc.trace_off()
예제 #18
0
    def __init__(self, f, x, test='f'):
        self.f = f
        self.x = x

        adolc.trace_on(0)
        ax = adolc.adouble(x)
        adolc.independent(ax)
        y = f(ax)
        adolc.dependent(y)
        adolc.trace_off()
예제 #19
0
    def _create_topography_active_interpolate(self,
                                              tag=0,
                                              separate_faults=False):
        import adolc

        # Sanitise kwargs
        assert isinstance(tag, int)
        assert tag >= 0
        for control in self.active_controls:
            assert control in self.all_controls

        # Initialise tape
        adolc.trace_on(tag)

        # Read parameters and mark active variables as independent
        msg = "INIT: Subfault {:d}: shear modulus {:4.1e} Pa, seismic moment is {:4.1e}"
        for i, subfault in enumerate(self.subfaults):
            for control in self.all_controls:
                if control in self.active_controls:
                    subfault.__setattr__(
                        control,
                        adolc.adouble(self.control_parameters[control][i]))
                    adolc.independent(subfault.__getattribute__(control))
                else:
                    subfault.__setattr__(control,
                                         self.control_parameters[control][i])
            if self.debug and self.debug_mode == 'full':
                try:
                    print(msg.format(i, subfault.mu, subfault.Mo().val))
                except Exception:
                    print(msg.format(i, subfault.mu, subfault.Mo()))

        # Create the topography, thereby calling Okada
        self.print_debug("SETUP: Creating topography using Okada model...")
        self.fault.create_dtopography(verbose=self.debug
                                      and self.debug_mode == 'full',
                                      active=True)

        # Compute quantity of interest
        self.J_subfaults = [0.0 for j in range(self.N)]
        data = self._data_to_interpolate
        for j in range(self.N):
            for i in range(self.N):
                self.J_subfaults[j] += (data[i, j] -
                                        self.fault.dtopo.dZ_a[i, j])**2
            self.J_subfaults[j] /= self.N**2
        self.J = sum(self.J_subfaults)

        # Mark dependence
        if separate_faults:
            for j in range(self.N):
                adolc.dependent(self.J_subfaults[j])
        else:
            adolc.dependent(self.J)
        adolc.trace_off()
예제 #20
0
def run(N,D,dt,beta,x0):
    
    if x0=='start':
        #init = 20.0*np.random.random_sample((N,D))-10.0
        #np.savetxt('initpaths.txt',init)        
        init = np.loadtxt('initpaths.txt')       
    else:
        init = x0.reshape(N,D)

    #init = np.loadtxt("data_D{0}_dt{1}_noP.txt".format(D,dt))[:N,:]
    
    if init.shape != (N,D):
        raise "x is wrong dims!"
  
    epsg = 1e-8
    epsf = 1e-8
    epsx = 1e-8
    maxits = 10000
    

    ### Use ADOL-C to generate trace, used for derivatives and
    ### evaluations
    start = time.time()

    adolc.trace_on(adolcID)
    ax = adolc.adouble(init.flatten())
    adolc.independent(ax)
    af = action(ax, beta)
    adolc.dependent(af)
    adolc.trace_off()

    print "taped =", time.time()-start,"s"


    #Test Gradient numerically 
    #    flat = init.flatten()
    #    grad = np.zeros_like(flat)
    #    cost1 = alglib_func(flat,grad,1)
    #    grad2 = grad.copy()
    #    for i in range(len(flat)):
    #        perturb = flat.copy()
    #        perturb[i] = perturb[i]+0.00001
    #        cost2 =alglib_func(perturb,grad2,1)
    #        numgrad = (cost2-cost1)/0.00001
    #        print numgrad/grad[i]

    m = af.shape[0]
    state = xalglib.minlmcreatevj( m ,list(init.flatten()))
    xalglib.minlmsetcond(state,epsg, epsf,epsx,maxits)
    xalglib.minlmoptimize_vj(state, lm_func, lm_jac)
    final, rep = xalglib.minlbfgsresults(state)
    print "optimized: ", time.time()-start, "s"
    print "Exit flag = ", rep.terminationtype, rep.iterationscount
    print "Action = ", action(final,beta)
    return rep.iterationscount, action(final,beta), final
예제 #21
0
def explicit_euler(x0,f,ts,p,q):
    N = size(ts)
    if isinstance(p[0],adolc._adolc.adouble):
        x = array([adolc.adouble(0) for m in range(Nm)])
    else:
        x = zeros(N)
    x[0] = x0
    for n in range(1,N):
        h = ts[n] - ts[n-1]
        x[n]= x[n-1] + h*f(ts[n-1],x[n-1],p,q)
    return x
예제 #22
0
파일: adolcmodel.py 프로젝트: syarra/nlpy
    def _trace_con(self, x):

        if self._con_trace_id is None and self.m > 0:

            self._con_trace_id = self._get_trace_id() + 1
            adolc.trace_on(self._con_trace_id)
            x = adolc.adouble(x)
            adolc.independent(x)
            y = self.cons(x)
            adolc.dependent(y)
            adolc.trace_off()
def explicit_euler(x0, f, ts, p, q):
    N = size(ts)
    if isinstance(p[0], adolc.adouble):
        x = array([adolc.adouble(0) for m in range(Nm)])
    else:
        x = zeros(N)
    x[0] = x0
    for n in range(1, N):
        h = ts[n] - ts[n - 1]
        x[n] = x[n - 1] + h * f(ts[n - 1], x[n - 1], p, q)
    return x
예제 #24
0
파일: adolcmodel.py 프로젝트: syarra/nlpy
    def _trace_obj(self, x):

        if self._obj_trace_id is None:

            self._obj_trace_id = self._get_trace_id()
            adolc.trace_on(self._obj_trace_id)
            x = adolc.adouble(x)
            adolc.independent(x)
            y = self.obj(x)
            adolc.dependent(y)
            adolc.trace_off()
예제 #25
0
파일: test.py 프로젝트: utke1/pylipsmin
def func(tag):
    n=50
    x=goffin_init(n)
    tag=1
    adolc.trace_on(tag)
    ax = adolc.independent(adolc.adouble(x))
    ay = goffin(ax,n)
    adolc.dependent(ay)
    trace_off()
    y=ay.val
    return y
예제 #26
0
    def getObjJac(self, data):
        
        ad.trace_on(1)

        ad_states   = ad.adouble(self.states)
        ad_beta     = ad.adouble(self.beta)

        ad.independent(ad_states)
        ad.independent(ad_beta)

        ad_obj = self.getObjRaw(ad_states, data, ad_beta)

        ad.dependent(ad_obj)
        ad.trace_off()

        jacobj = ad.jacobian(1, np.hstack((self.states, self.beta)))

        Jq = jacobj[:,0:np.shape(self.states)[0]]
        Jb = jacobj[:,np.shape(self.states)[0]:]

        return Jq[0,:], Jb[0,:]
예제 #27
0
def explicit_euler(x0,f,ts,p,q):
	Nm = size(ts)
	Nx = size(x0)
	if isinstance(p[0],adolc.adouble):
		x = array([[adolc.adouble(0) for n in range(Nx)]  for m in range(Nm)])
	else:
		x = zeros((Nm,Nx))
	x[0,:] = x0[:]
	for m in range(1,Nm):
		h = ts[m] - ts[m-1]
		x[m,:]= x[m-1,:] + h*f(ts[m-1],x[m-1,:],p,q)
	return x
예제 #28
0
    def trace(self, dims):
        # trace function
        t = numpy.zeros(1)
        x = numpy.zeros(dims['x'])
        f = numpy.zeros(dims['x'])
        p = numpy.zeros(dims['p'])
        u = numpy.zeros(dims['u'])

        adolc.trace_on(123)
        at = adolc.adouble(t)
        ax = adolc.adouble(x)
        af = adolc.adouble(f)
        ap = adolc.adouble(p)
        au = adolc.adouble(u)

        adolc.independent(at)
        adolc.independent(ax)
        adolc.independent(ap)
        adolc.independent(au)
        self.ffcn(t, ax, af, ap, au)
        adolc.dependent(af)
        adolc.trace_off()
        self.traced = True
 def calc_delJ_delbeta(self, q, beta):
     n = np.size(beta)
     beta_c = beta.copy()
     beta = ad.adouble(beta)
     tag = 1
     ad.trace_on(tag)
     ad.independent(beta)
     F = self.objective.objective(q, beta)
     #print ad.value(F)
     ad.dependent(F)
     ad.trace_off()
     beta = beta_c
     dJdbeta = calc_jacobian(beta, tag=tag, sparse=False)
     return dJdbeta
    def calc_delJ_delq(self, q, beta):
        n = np.size(q)
        q_c = q.copy()
        q = ad.adouble(q)
        tag = 2

        ad.trace_on(tag)
        ad.independent(q)
        F = self.objective.objective(q, beta)
        ad.dependent(F)
        ad.trace_off()
        q = q_c
        dJdq = calc_jacobian(q, tag=tag, sparse=False)
        return dJdq
 def calc_delR_delbeta(self, q):
     nb = np.size(self.beta)
     n = np.size(q)
     beta_c = self.beta.copy()
     self.beta = ad.adouble(self.beta)
     tag = 3
     ad.trace_on(tag)
     ad.independent(self.beta)
     R = self.calc_residual(q, dtype=ad.adouble)
     ad.dependent(R)
     ad.trace_off()
     self.beta = beta_c
     dRdbeta = calc_jacobian(self.beta, tag=tag, sparse=False)
     return dRdbeta
예제 #32
0
def PyAdolc_vLJ_Optimize(x):
    N = len(x)
    adolc.trace_on(0)
    ad_x = adolc.adouble(np.zeros(np.shape(np.ravel(x)),dtype=float))
    adolc.independent(ad_x)
    ad_y = Adolc_vLJ(ad_x)
    adolc.dependent(ad_y)
    adolc.trace_off()
    
    Adolc_BFGSres = optimize.minimize(PyAdolc_vLJ_for_Optimize, np.ravel(x),      \
                                      method='L-BFGS-B', \
                                      jac = PyAdolc_dvLJ_for_Optimize,         \
                                      options={'disp': False})
    return np.reshape(Adolc_BFGSres.x, (N,D))
예제 #33
0
    def tape_A(self, xtrace):
        """
        Tape the objective function.
        """
        print('Taping action evaluation...')
        tstart = time.time()
        """
        trace objective function
        """
        adolc.trace_on(self.adolcID)
        # set the active independent variables
        ax = adolc.adouble(xtrace)
        adolc.independent(ax)
        # set the dependent variable (or vector of dependent variables)
        af = self.A(ax)
        adolc.dependent(af)
        adolc.trace_off()

        #IPOPT needs the lagrangian functions to be traced
        if self.method == 'IPOPT':
            """
            trace lagrangian unconstrained
            """
            #This adolc numbering could cause problems in the future
            self.lagrangeID = self.adolcID + 1000
            adolc.trace_on(self.lagrangeID)
            ax = adolc.adouble(xtrace)
            aobj_factor = adolc.adouble(1.)
            adolc.independent(ax)
            adolc.independent(aobj_factor)
            ay = self.eval_lagrangian(ax, aobj_factor)
            adolc.trace_off()

        self.taped = True
        print('Done!')
        print('Time = {0} s\n'.format(time.time() - tstart))
예제 #34
0
 def __init__(self, f, x, scaler=False):
     if not isinstance(x, list):
         x_lst = [x]
     else:
         x_lst = x
     self.id = next(self.__class__._ids)
     self.scaler = scaler
     # x = np.random.uniform(x_L, x_U)
     adolc.trace_on(self.id)
     a_x_lst = [adolc.adouble(v) for v in x_lst]
     for ax in a_x_lst:
         adolc.independent(ax)
     ay = f(*a_x_lst)
     adolc.dependent(ay)
     adolc.trace_off()
예제 #35
0
def qr(in_A):
    """
    QR decomposition of A
   
    Q,R = qr(A)
    
    """
    # input checks
    Ndim = numpy.ndim(in_A)
    assert Ndim == 2
    N,M = numpy.shape(in_A)
    assert N==M
    

    # prepare R and QT
    R  = in_A.copy()
    
    if isinstance(in_A[0,0], adolc._adolc.adouble):
        QT = numpy.array([[adolc.adouble(0) for c in range(N)] for r in range(N) ])
    else:
        QT = numpy.zeros((N,N))
    
    for n in range(N):
        QT[n,n]  += 1

    # main algorithm
    for n in range(N):
        for m in range(n+1,N):
            a = R[n,n]
            b = R[m,n]
            r = numpy.sqrt(a**2 + b**2)
            c = a/r
            s = b/r

            for k in range(N):
                Rnk = R[n,k]
    
                R[n,k] = c*Rnk + s*R[m,k]
                R[m,k] =-s*Rnk + c*R[m,k];

                QTnk = QT[n,k]
                QT[n,k] = c*QTnk + s*QT[m,k]
                QT[m,k] =-s*QTnk + c*QT[m,k];
            #print 'QT:\n',QT
            #print 'R:\n',R
            #print '-------------'

    return QT.T,R
예제 #36
0
    def evalJacobian(self, states, beta_inv):
        
        # Evaluate jacobian of residuals w.r.t. states

        ad.trace_on(1)

        ad_states   = ad.adouble(states)

        ad.independent(ad_states)

        ad_res = self.evalResidual(ad_states, beta_inv)

        ad.dependent(ad_res)
        ad.trace_off()

        return ad.jacobian(1, states)
예제 #37
0
 def _create_dtopography_active(self, verbose=False):
     import adolc
     num_subfaults = len(self.subfaults)
     tic = perf_counter()
     msg = "created topography for subfault {:d}/{:d} ({:.1f} seconds)"
     dz = np.zeros(self.dtopo.X.shape)
     dz = adolc.adouble(dz)
     for k, subfault in enumerate(self.subfaults):
         subfault.okada()
         dz += subfault.dtopo.dZ[0, :, :].reshape(dz.shape)
         if k % 10 == 0 and verbose:
             print(msg.format(k+1, num_subfaults, perf_counter() - tic))
             tic = perf_counter()
     self.dtopo.dZ_a = dz
     self.dtopo.dZ = np.array([dzi.val for dzi in np.ravel(dz)]).reshape((1, ) + dz.shape)
     return self.dtopo
    def __init__(self):
        dwl.OptimizationModel.__init__(self)
        self.setDimensionOfState(4)
        self.setDimensionOfConstraints(2)
        self.setNumberOfNonzeroJacobian(8)
        self.setNumberOfNonzeroHessian(10)

        # trace objective function
        x0 = np.array([1.0, 5.0, 5.0, 1.0])
        self.getStartingPoint(x0)
        f = np.array([0.0])
        adolc.trace_on(1)
        ax = adolc.adouble(x0)
        af = adolc.adouble(f)
        adolc.independent(ax)
        self.costFunction(af, ax)
        adolc.dependent(af)
        adolc.trace_off()

        # trace constraint function
        adolc.trace_on(2)
        ax = adolc.adouble(x0)
        g = np.zeros(self.getDimensionOfConstraints())
        ag = adolc.adouble(g)
        adolc.independent(ax)
        self.constraintFunction(ag, ax)
        adolc.dependent(ag)
        adolc.trace_off()

        # trace lagrangian function
        adolc.trace_on(3)
        ax = adolc.adouble(x0)
        alagrange = adolc.adouble([1., 1.])
        aobj_factor = adolc.adouble(1.)
        adolc.independent(ax)
        adolc.independent(alagrange)
        adolc.independent(aobj_factor)
        ay = self.eval_lagrangian(ax, alagrange, aobj_factor)
        adolc.dependent(ay)
        adolc.trace_off()

        x = np.array([1.0, 5.0, 5.0, 1.0])
        hoptions = np.array([0, 0], dtype=int)
        hess_result = adolc.colpack.sparse_hess_no_repeat(3, x, hoptions)
        self.hrow = np.asarray(hess_result[1], dtype=int)
        self.hcol = np.asarray(hess_result[2], dtype=int)
        self.hess_values = np.asarray(hess_result[3], dtype=float)
        # need only upper left part of the Hessian
        self.mask = np.where(self.hcol < 4)
예제 #39
0
    def tape_A(self, xtrace):
        """
        Tape the objective function.
        """
        print('Taping action evaluation...')
        tstart = time.time()

        adolc.trace_on(self.adolcID)
        # set the active independent variables
        ax = adolc.adouble(xtrace)
        adolc.independent(ax)
        # set the dependent variable (or vector of dependent variables)
        af = self.A(ax)
        adolc.dependent(af)
        adolc.trace_off()
        self.taped = True
        print('Done!')
        print('Time = {0} s\n'.format(time.time()-tstart))
예제 #40
0
파일: nn.py 프로젝트: anandpratap/ml_stack
 def dydgamma(self, x, gamma):
     """
     Calculate derivative of the neural network output with respect to the
     hyperparameters gamma.
     """
     gamma_c = gamma.copy()
     gamma = ad.adouble(gamma)
     tag = 11
     ad.trace_on(tag)
     ad.independent(gamma)
     self.set_from_array(gamma)
     y = self.eval(x)
     ad.dependent(y)
     ad.trace_off()
     gamma = gamma_c
     self.set_from_array(gamma_c)
     dJdgamma = calc_jacobian(gamma, tag=tag, sparse=False)
     return dJdgamma.reshape(gamma_c.shape)
예제 #41
0
파일: adolcdev.py 프로젝트: AndySze/cardoon
def create_tape(dev, vPort):
    """
    Generate Adol-C tape

    Normally there is no need to manually call this.
    """
    assert dev.isNonlinear
    try:
        tag = dev._tag
    except AttributeError:
        tag = dev.adolcID
        dev._tag = tag
    ad.trace_on(tag)
    # Create derivative vector
    a_vPort = ad.adouble(vPort)
    ad.independent(a_vPort)
    # perform actual calculation (for now re-tape every time)
    a_out = dev.eval_cqs(a_vPort)
    ad.dependent(a_out)
    ad.trace_off()
예제 #42
0
파일: main.py 프로젝트: b45ch1/pyadolc
import numpy
import math
import adolc

# tape a function evaluation
ax = numpy.array([adolc.adouble(0.) for n in range(2)])
# ay = adolc.adouble(0)
adolc.trace_on(13)
adolc.independent(ax)
ay = numpy.sin(ax[0] + ax[1]*ax[0])
adolc.dependent(ay)
adolc.trace_off()

x = numpy.array([3., 7.])
y = numpy.zeros(1)
adolc.tape_to_latex(13, x, y)

y = adolc.function(13, x)
g = adolc.gradient(13, x)
J = adolc.jacobian(13, x)

print('function y=', y)
print('gradient g=', g)
print('Jacobian J=', J)


예제 #43
0
	title('Trajectory of an ODE')
	xlabel(r'states $x(t),x_{p_1}(t)$ and $x_{p_2}(t)$')
	ylabel(r' time $t$')
	savefig('variational_ode_trajectory.eps')
	
	# generate pseudo measurement data
	p[0]+=3.; 	p[1] += 2.
	x0 = array([v[0], 1., 0.])
	x = explicit_euler(x0,f1,ts,p,q)
	h = measurement_model(x[:,0],p,q)
	etas = h + numpy.random.normal(size=Nm)
	p[0]-= 3.;	p[1] -= 2.

	# taping F
	av = array([adolc.adouble(0) for i in range(Nv)])
	y = zeros(Nm)
	adolc.trace_on(1)
	av[0].is_independent(p[0])
	av[1].is_independent(p[1])
	av[2].is_independent(q[0])
	ay = F(av[:Np],av[Np:],ts,Sigma,etas)
	for m in range(Nm):
		y[m] = adolc.depends_on(ay[m])
	adolc.trace_off()

	# taping dFdp
	av = array([adolc.adouble(0) for i in range(Nv)])
	adolc.trace_on(1)
	av[0].is_independent(p[0])
	av[1].is_independent(p[1])
예제 #44
0
            print 'running runtime tests for A.shape = (D,P,N,N) = %d, %d, %d, %d'%(D,P,N,N)
            A_data = numpy.random.rand(N,N,P,D)
            Qbar_data = numpy.random.rand(1,N,N,P,D)
            Rbar_data = numpy.random.rand(1,N,N,P,D)

            #----------------------------------------------
            # STEP 1:
            # QR decomposition by Givens Rotations
            # using pyadolc for the differentiation
            #----------------------------------------------
            A = A_data[:,:,0,0]
            # trace QR decomposition with adolc
            AP = AdolcProgram()
            AP.trace_on(1)
            aA = adouble(A)
            AP.independent(aA)
            aQ, aR = qr(aA)
            AP.dependent(aQ)
            AP.dependent(aR)
            AP.trace_off()

            for r in range(repetitions):

                A = A_data[:,:,0,0]
                # compute push forward
                VA = A_data[:,:,:,1:]
                tic = time()
                out = AP.forward([A],[VA])
                toc = time()
                runtime_pyadolc_push_forward = toc - tic
예제 #45
0
            symdict[xs[n,d]] = x[n,d]
    return array([[dfs[n,d].subs(symdict).evalf() for d in range(D)] for n in range(N)])

def sym_ddf(x):
    symdict = dict()
    for n in range(N):
        for d in range(D):
            symdict[xs[n,d]] = x[n,d]
    return array([[[[ ddfs[m,e,n,d].subs(symdict).evalf() for d in range(D)] for n in range(N)] for e in range(D)] for m in range(N)],dtype=float)

###################################
# PART 1: computation with PYADOLC
###################################

adolc.trace_on(0)
x = adolc.adouble(numpy.random.rand(*(N,D)))
adolc.independent(x)
y = f(x)
adolc.dependent(y)
adolc.trace_off()


# point at which the derivatives should be evaluated
x = random((N,D))

print '\n\n'
print 'Sympy function = function  check (should be almost zero)'
print f(x) - sym_f(x)

print '\n\n'
print 'Sympy vs Hand Derived Gradient check (should be almost zero)'
예제 #46
0
# OBJECTIVE FUNCTION
# ------------------
def Phi(F):
	return trace( dot(F.T,F))

def ffcn(x):
	 return 0.5*array(
[[(x[0]-17.)*(x[0]-17.), (x[0]-17.)*(x[0]-17.)],
[ x[1]-19. , x[1]-19.]])

# TAPING THE FUNCTIONS
# --------------------
# taping function ffcn
u = 3.; v = 7.
ax = array([adolc.adouble(u), adolc.adouble(v)])
adolc.trace_on(1)
ax[0].is_independent(u)
ax[1].is_independent(v)
ay = ffcn(ax)
for n in range(2):
	for m in range(2):
		adolc.depends_on(ay[n,m])
adolc.trace_off()

# taping matrix functions with algopy
x = array([u,v])
F = ffcn(x)
Fdot = zeros((2,2))
cg = CGraph()
FF = Function(Mtc(F))
예제 #47
0
    ## taping
    start_time = time()
    cg = rm.tape(f, x)
    end_time = time()
    tape_eval_times.append(end_time - start_time)

    ## reverse evaluation
    start_time = time()
    g_reverse2 = rm.gradient_from_graph(cg)
    end_time = time()
    rev_eval_times.append(end_time - start_time)

    ## PyADOLC taping
    start_time = time()
    ax = numpy.array([adolc.adouble(0.0) for i in range(N)])
    adolc.trace_on(0)
    for n in range(N):
        ax[n].is_independent(x[n])
    ay = f(ax)
    adolc.depends_on(ay)
    adolc.trace_off()
    end_time = time()
    adolc_tape_times.append(end_time - start_time)

    ## PyADOLC gradient
    start_time = time()
    adolc_g = adolc.gradient(0, x)
    end_time = time()
    adolc_gradient_times.append(end_time - start_time)
import numpy; from numpy import sin,cos; import adolc
def f(x):
    return sin(x[0] + cos(x[1])*x[0])

adolc.trace_on(1)
x = adolc.adouble([3,7]);  adolc.independent(x)
y = f(x)
adolc.dependent(y); adolc.trace_off()
adolc.tape_to_latex(1,[3,7],[0.])

예제 #49
0
# trace with ALGOPY
start_time = time.time()
cg = CGraph()
x = Function(UTPM(numpy.random.rand(1, 1, N)))
y = f(x)
cg.trace_off()
cg.independentFunctionList = [x]
cg.dependentFunctionList = [y]
end_time = time.time()
time_trace_algopy = end_time - start_time

# trace with PYADOLC
start_time = time.time()
adolc.trace_on(1)
x = adolc.adouble(numpy.random.rand(N))
adolc.independent(x)
y = f(x)
adolc.dependent(y)
adolc.trace_off()
end_time = time.time()
time_trace_adolc = end_time - start_time

# trace with PYADOLC.cgraph
from adolc.cgraph import AdolcProgram

start_time = time.time()
ap = AdolcProgram()
ap.trace_on(2)
x = adolc.adouble(numpy.random.rand(N))
ap.independent(x)
예제 #50
0
		
		atmp = []
		for n in range(N):
			atmp.append(numpy.sin( numpy.sum(ax[:n])))
		ay   = numpy.array( [ ax[0] * numpy.sin( numpy.sum(atmp)) ] )
		f   = pycppad.adfun(ax, ay)
		x   = numpy.random.rand(N)
		w   = numpy.array( [ 1.] ) # compute Hessian of x0 * sin(x1)
		
		cppad_hessian_runtime  = timeit.Timer('f.hessian(x, w)', 'from __main__ import f,x,w').timeit(number=reps)/reps
		cppad_gradient_runtime = timeit.Timer('f.jacobian(x)', 'from __main__ import f,x').timeit(number=reps)/reps
		
		# adolc timing
		x     = numpy.zeros(N, dtype=float)
		adolc.trace_on(0)
		ax = adolc.adouble(x)
		adolc.independent(ax)
		atmp = []
		for n in range(N):
			atmp.append(numpy.sin( numpy.sum(ax[:n])))
		ay   = numpy.array( [ ax[0] * numpy.sin( numpy.sum(atmp)) ] )
		adolc.dependent(ay)
		adolc.trace_off()
		
		x   = numpy.random.rand(N)
		adolc_hessian_runtime = timeit.Timer('adolc.hessian(0, x)', 'import adolc;from __main__ import x').timeit(number=reps)/reps
		adolc_gradient_runtime = timeit.Timer('adolc.gradient(0, x)', 'import adolc;from __main__ import x').timeit(number=reps)/reps
		
		adolc_hessian_runtimes.append(adolc_hessian_runtime)
		cppad_hessian_runtimes.append(cppad_hessian_runtime)
		
예제 #51
0
    return (exp(q[0]*t)-1.)/q[0]


p = array([10.,2.])
q = array([-1.])
v = concatenate((p,q))

Np = size(p)
Nq = size(q)
Nv = size(v)
Nm = 100
ts = linspace(0,10,Nm)

# TAPING THE INTEGRATOR
adolc.trace_on(1)
av = adolc.adouble(v)
adolc.independent(av)
ax = explicit_euler(av[0],f,ts,av[:Np],av[Np:])
adolc.dependent(ax)
adolc.trace_off()

# COMPUTING FUNCTION AND JACOBIAN FROM THE TAPE
y = adolc.zos_forward(1,v,0)
J = adolc.jacobian(1,v)

x_plot = plot(ts, y,'b')
x_analytical_plot = plot(ts,phi(ts,p,q),'b.')

xp0_plot = plot(ts, J[:,0], 'g')
xp0_analytical_plot = plot(ts, phip0(ts,p,q), 'g.')
예제 #52
0
        values[2] += lagrange[1] * 2
        values[5] += lagrange[1] * 2
        values[9] += lagrange[1] * 2
        return values


def apply_new(x):
    return True


x0 = numpy.array([1.0, 5.0, 5.0, 1.0])
pi0 = numpy.array([1.0, 1.0])

# trace objective function
adolc.trace_on(1)
ax = adolc.adouble(x0)
adolc.independent(ax)
ay = eval_f(ax)
adolc.dependent(ay)
adolc.trace_off()

# trace constraint function
adolc.trace_on(2)
ax = adolc.adouble(x0)
adolc.independent(ax)
ay = eval_g(ax)
adolc.dependent(ay)
adolc.trace_off()

# trace lagrangian function
adolc.trace_on(3)