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
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))
Example #3
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()
Example #4
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
Example #5
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()
Example #6
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()
Example #7
0
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
 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
Example #9
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))
Example #10
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()
Example #11
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'))
Example #12
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)
Example #13
0
 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)
Example #14
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))
Example #15
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'))
Example #16
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'))
Example #17
0
    def _create_topography_active(self, tag=0, separate_faults=True):
        import adolc

        # Sanitise kwargs
        assert isinstance(tag, int)
        assert tag >= 0
        for control in self.active_controls:
            assert control in self.all_controls, "Active control '{:s}' not recognised.".format(
                control)

        # 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)

        # Mark output as dependent
        if separate_faults:
            for subfault in self.subfaults:
                adolc.dependent(subfault.dtopo.dZ)
        else:
            adolc.dependent(self.fault.dtopo.dZ_a)
        adolc.trace_off()
Example #18
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()
Example #19
0
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()
Example #20
0
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()
Example #21
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,:]
Example #22
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
Example #23
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))
Example #24
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
    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)
def MCdo(b, i):
    user = os.path.expanduser("~")

    tempOut = robj.r("rm(list=ls())")
    tempOut = robj.r("i <- %i"%(i+1))
    tempOut = robj.r("b <- %i"%(i*(1000)+(b+1) ))
    tempOut = robj.r("source('CMLE_unstable_support.r')")
    regrMat = np.array((robj.r("do.call(cbind, regr)")))
    regr = OrderedDict([('SA',np.ones((regrMat.shape[0],0))),('VA',regrMat[:,0:1]),('CB',np.ones((regrMat.shape[0],0))),('barWA',regrMat[:,1:2]),('barWB',regrMat[:,2:4]),('bara',regrMat[:,4:5]),('VB',regrMat[:,5:6])])
    Y = np.array((robj.r("Y")))
    x0 = np.array((robj.r("unname(x0)")))
    xL = np.array((robj.r("unname(xL)")))
    tPL = np.array((robj.r("unname(out.2step$time )")))
    
    def fll(x):
        return LL_jo(x, Y, regr)
    
    def heq(x):
        return  const_cmle(x, Y, regr)
    np.random.seed(b)
    while np.isinf(fll(adouble(x0)).val):
        x0 = np.random.uniform(size=len(x0))
    
    ccd = os.getcwd()
    if not os.path.exists(user + '/Documents/adolc%i_%i'%(i, b)):
        os.makedirs(user + '/Documents/adolc%i_%i'%(i, b))
    
    os.chdir(user + '/Documents/adolc%i_%i'%(i, b))
    
    
    
    adolc.trace_on(1)
    ax = adolc.adouble(np.zeros(len(x0)))
    adolc.independent(ax)
    ay = fll(ax)
    adolc.dependent(ay)
    adolc.trace_off()
    
    # trace constraint function
    adolc.trace_on(2)
    ax = adolc.adouble(x0)
    adolc.independent(ax)
    ay = heq(ax)
    adolc.dependent(ay)
    adolc.trace_off()
    
    npar = len(x0)
    
    def adFun(x):
        return adolc.function(1, x)
    
    def grFun(x): 
        return adolc.gradient(1, x)
    
    def  const_adolc(x):
        return adolc.function(2,x)
    
    def jac_adolc(x):
        return adolc.jacobian(2,x)
    
    
    def lagrangian(x, lagrange, obj_factor):
        return  obj_factor*fll(x) + np.dot(lagrange, heq(x))

    #Jacobian
    
    
    #### initalize it
    class jac_c_adolc:
        
        def __init__(self, x):
            options = None
            result = adolc.colpack.sparse_jac_no_repeat(2,x,options)
            
            self.nnz  = result[0]     
            self.rind = np.asarray(result[1],dtype=int)
            self.cind = np.asarray(result[2],dtype=int)
            self.values = np.asarray(result[3],dtype=float)
            
        def __call__(self, x, flag, user_data=None):
            if flag:
                return (self.rind, self.cind)
            else:
                result = adolc.colpack.sparse_jac_repeat(2, x, self.nnz, self.rind,
                    self.cind, self.values)
                return result[3]
    
    ##### create the function
    Jac_c_adolc = jac_c_adolc(x0)
    
    
    ###Hessian
  
        
    # trace lagrangian function
    adolc.trace_on(3)
    ax = adolc.adouble(x0)
    adolc.independent(ax)
    ay = lagrangian(ax, xL, np.array([1.0]))
    adolc.dependent(ay)
    adolc.trace_off()
    

    M = Y.shape[1]
    nreal = npar-M
    given = {'rind': np.concatenate((np.kron(np.arange(nreal), np.ones(npar,dtype='int')), np.arange(nreal, npar))),
             'cind': np.concatenate((np.tile(np.arange(npar), nreal), np.arange(nreal, npar)))}
    mask = np.where(given['rind'] <= given['cind'])
    given['rind'] = given['rind'][mask]
    given['cind'] = given['cind'][mask]
    
    
    def hessLag_adolc(x, lagrange, obj_factor, flag, user_data=None):
        if flag:
            result = (given['rind'], given['cind'])
        else:
             result = np.ravel(adolc.hessian(3, x)[given['rind'],given['cind']], order="C")
        return result
    
    
    H2 = hessLag_adolc(x0, xL, 1.0, False)
    H2a = hessLag_adolc(x0, xL, 1.0, True)
    nnzh = len(given['rind'])
   

    ##Optimization
    #PRELIMS: other things to pass to IPOPT
    nvar = len(x0) #number of variables in the problem
    x_L = np.array([-np.inf]*nvar, dtype=float) #box contraints on variables (none)
    x_U = np.array([np.inf]*nvar, dtype=float)
     
    #PRELIMS:define the (in)equality constraints
    ncon = heq(ax).shape[0] #number of constraints
    g_L = np.array([0]*ncon, dtype=float) #constraints are to equal 0
    g_U = np.array([0]*ncon, dtype=float) #constraints are to equal 0
    
    
    #PRELIMS: define the number of nonzeros in the jacobian 
    val = Jac_c_adolc(x0, False) 
    nnzj = len(val)            
    
      
    # create the nonlinear programming model
    nlp2 = pyipopt.create(
    nvar, 
    x_L,
    x_U,
    ncon,
    g_L,
    g_U,
    nnzj,
    nnzh,
    adFun,
    grFun,
    const_adolc,
    Jac_c_adolc,
    hessLag_adolc
    )
    
    nlp2.num_option('expect_infeasible_problem_ctol', 1e-15)
    nlp2.int_option('max_iter', 100)
    nlp2.num_option('dual_inf_tol', 1e-3)
    nlp2.num_option('constr_viol_tol', 1e-3)
    nlp2.num_option('tol', 1e-6)
    nlp2.int_option('print_level', 0)
    
    t1 = time()
    out = nlp2.solve(x0)
    t1 = time()-t1
    t1 = t1 + tPL
    
    # free the model
    nlp2.close()

    
    os.chdir(ccd)
    shutil.rmtree(user + '/Documents/adolc%i_%i'%(i, b)) 
    output = np.concatenate((out[0][:6], t1, np.array([out[5]])))
    return output
Example #27
0
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)


   
def fll(x):
    return LL_jo(x, Y, regr)

def heq(x):
    return const_cmle(x, Y, regr)

ccd = os.getcwd()
if not os.path.exists(user + '/Documents/adolcSanctions'):
    os.makedirs(user + '/Documents/adolcSanctions')

os.chdir(user + '/Documents/adolcSanctions')



adolc.trace_on(1)
ax = adolc.adouble(np.zeros(len(x0)))
adolc.independent(ax)
ay = fll(ax)
adolc.dependent(ay)
adolc.trace_off()

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

npar = len(x0)
Example #29
0
        for d in range(D):
            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'
    ## 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.) 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)

    ### check that both derivatives give the same result
        ny = adolc.zos_forward(self.tag_ext_fct,2,2,1,x)
        nz = adolc.fov_reverse(self.tag_ext_fct,2,2,q,u)
        np.copyto(z,nz)
        return 1


if __name__ == '__main__':
    tag_full = 1
    tag_part = 2
    tag_ext_fct = 3
    n = 2
    m = 2
    t0 = 0.0
    tf = 1.0
    conp = [ 1.0, 1.0 ]
    adolc.trace_on(tag_full)
    con = adolc.as_adouble(conp)
    for c in con:
        c.declareIndependent()
    y = con
    ynew = adolc.as_adouble([ 0.0, 0.0 ])
    for i in range(steps):
        euler_step_act(n,y,m,ynew)
        y = ynew
    f = y[0] + y[1]
    f.declareDependent()
    adolc.trace_off()
    grad = adolc.gradient(tag_full,conp)
    print(" full taping:\n gradient=(",grad[0],", ",grad[1],")\n\n")

    # now taping external function
    def record_tape(self):
        #import numpy as np
        tag = 0
        Q = self.Q.copy()
        self.Q = ad.adouble(self.Q)

        R = self.R.copy()
        self.R = ad.adouble(self.R)

        U = self.U.copy()
        self.U = ad.adouble(self.U)

        F = self.F.copy()
        self.F = ad.adouble(self.F)

        Fv = self.Fv.copy()
        self.Fv = ad.adouble(self.Fv)

        S = self.S.copy()
        self.S = ad.adouble(self.S)

        mut = self.mut.copy()
        self.mut = ad.adouble(self.mut)

        b = self.b.copy()
        self.b = ad.adouble(self.b)

        rhotau = self.rhotau.copy()
        self.rhotau = ad.adouble(self.rhotau)

        ex = self.ex.copy()
        self.ex = ad.adouble(self.ex)

        Ul = self.Ul.copy()
        self.Ul = ad.adouble(self.Ul)

        Ur = self.Ur.copy()
        self.Ur = ad.adouble(self.Ur)

        Ux_face = self.Ux_face.copy()
        self.Ux_face = ad.adouble(self.Ux_face)
        Ux_center = self.Ux_center.copy()
        self.Ux_center = ad.adouble(self.Ux_center)

        ad.trace_on(tag)
        ad.independent(self.Q)
        self.calc_residual()
        ad.dependent(self.R)
        ad.trace_off()
        print(ad.tapestats(0))
        self.Q = Q
        self.U = U
        self.R = R
        self.Ul = Ul
        self.Ur = Ur
        self.F = F
        self.Fv = Fv
        self.S = S
        self.Ux_face = Ux_face
        self.Ux_center = Ux_center
        self.ex = ex
        self.b = b
        self.rhotau = rhotau
        self.mut = mut
Example #33
0
	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])
	av[2].is_independent(q[0])
	ay = dFdp(av[:Np],av[Np:],ts,Sigma,etas)
        values[0] += lagrange[1] * 2
        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