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
Пример #2
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
Пример #3
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))
Пример #4
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()
Пример #5
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()
Пример #6
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()
Пример #7
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()
Пример #8
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
Пример #9
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()
Пример #10
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()
Пример #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 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
Пример #13
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))
 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
 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
Пример #17
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()
Пример #18
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'))
Пример #19
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)
Пример #20
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)
Пример #21
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'))
Пример #22
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'))
Пример #23
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))
Пример #24
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()
Пример #25
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()
Пример #26
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()
Пример #27
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()
Пример #28
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,:]
Пример #29
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
Пример #30
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))
Пример #31
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)
Пример #33
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)
	for m in range(Nm):
		for n in range(Np):
			adolc.dependent(ay[m,n])
	adolc.trace_off()

	# tape the objective function with Algopy
	J = zeros((DM-1,1, Nm, Np))
    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
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.])

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