Esempio n. 1
0
    def _compress_domain(self):
        from FSP.sunkarautil import simple_compress

        self.domain_states, self.p = simple_compress(
            self._solver.domain_states.T, self._solver.y[0],
            self.step_error * 0.5)
        self.domain_states = self.domain_states.T

        self._state_enum = state_enum.StateEnum(self.domain_states)
        order = np.lexsort(self.domain_states)
        self.p = self.p[order]
        self._steps_to_compress = 0
        self._make_solver
Esempio n. 2
0
    def set_initial_states(self, domain_states, p):
        """
		@brief initialise the solver if the initial density is not a point mass.
		@param domain_states 	: numpy.ndarray, shape = (num of species x num of states)
		@param p 				: numpy.ndarray, shape = (num of states,)
		"""
        self.t = t
        if domain_states.shape[1] == p.shape[0]:
            domain_states = domain_states.T
        order = np.lexsort(domain_states)
        self.p = p[order].flatten()
        self.domain_states = domain_states[:, order]
        self.initial_state_enum = state_enum.StateEnum(self.domain_states)

        self._make_solver()
Esempio n. 3
0
def Hybrid_FSP(model, X, w, h, position, valid, stoc_vector, expander, tau,
               residue):
    # We take a false step and if there is soo much probability lost, we grow the domain.

    from scipy.integrate import ode
    from Support_Expander import positions

    from n_term import simple_compress as compress
    #from n_term import compress_by_marginal as compress

    from Compute_A import compute_A

    # Growth Flag
    grew = False

    Energy = np.sum(w)

    new_p_0 = np.zeros((X.shape[1], ))

    new_p_0 = w[:]

    def f_sparse(t, y, A):
        return A.dot(y)

    A_sparse = compute_A(X, model.propensities, position, valid)
    ode_obj = ode(f_sparse).set_integrator('lsoda', method='bdf')
    ode_obj.set_initial_value(new_p_0, 0).set_f_params(A_sparse)

    #pdb.set_trace()
    while ode_obj.successful() and ode_obj.t < h:
        ode_obj.integrate(h)
    sink = Energy - np.sum(ode_obj.y)

    if sink + residue > tau:
        # there is soo much outflow, we need to grow.

        residue = 0.0  # reset the residual
        #pdb.set_trace()
        new_X, garbage1, garbage2 = expander.expand(domain_states=X, p=w)

        # new domain enum.

        new_domain_enum = state_enum.StateEnum(new_X, stoc_vector)
        # now we need new position and valid vectors and a new probability vector.
        #print(" Growing the Hybrid Domain by %d states."%(new_X.shape[1] - X.shape[1]) )

        grew = True

        new_X = new_domain_enum.ordered_full_dom

        new_w = np.zeros(
            (new_X.shape[1], ))  # need to add one state for the sink state.
        old_domain_indices = new_domain_enum.indices(X)

        new_w[old_domain_indices] = w

        valid, position = positions(new_X, model.transitions, stoc_vector,
                                    new_domain_enum)

        A_sparse = compute_A(new_X, model.propensities, position, valid)

        ode_obj = ode(f_sparse).set_integrator('lsoda', method='bdf')
        ode_obj.set_initial_value(new_w, 0).set_f_params(A_sparse)

        while ode_obj.successful() and ode_obj.t < h:
            ode_obj.integrate(h)

        sink = Energy - np.sum(ode_obj.y)
        '''
		if sink > tau:
			#raise ValueError(str(ode_obj.y[-1]) + " flowed out, Tau :" + str(tau)) 
			print(" Too much out flow : "+  str(sink) + " flowed out, Tau :" + str(tau) )
		'''
        # We need to remove some redundant states.

        #pdb.set_trace()
        w_dot = f_sparse(1, ode_obj.y, A_sparse)

        # where the derivative is zero.
        #negative_dot = (w_dot <= 0)[:-1]
        # REWRITE THE NEGATIVE DOT COMPRESSION DOESNT WORK
        negative_dot = np.array([True] * (len(w_dot) - 1))
        positive_dot = np.invert(negative_dot)

        ## Baised compress
        #keep_states, keep_prob = compress( new_X[:,negative_dot],ode_obj.y[:-1][negative_dot], 0.0*tau) # No reduction.
        #keep_states, keep_prob = compress( new_X[:,negative_dot],ode_obj.y[:-1][negative_dot], tau)

        ## non biased compress##
        keep_states, keep_prob = compress(new_X, ode_obj.y, 0.1 * tau)

        #pdb.set_trace()
        if keep_states.shape[1] < np.sum(negative_dot):
            #if shrunck_X.shape[1] < new_X.shape[1]:

            #print(" Reducing the Hybrid Domain by : %d states."%(np.sum(negative_dot) - keep_states.shape[1]))

            # we have a reduction, so we adjust the indicies.
            shrunck_X = keep_states  #np.concatenate((new_X[:,positive_dot],keep_states), axis=1)

            shrunck_w = keep_prob  #np.concatenate((ode_obj.y[positive_dot],keep_prob))

            # need to reindex everything and generate new positions.

            new_domain_enum = state_enum.StateEnum(shrunck_X, stoc_vector)

            new_X = new_domain_enum.ordered_full_dom

            new_w = shrunck_w[new_domain_enum.order]

            valid, position = positions(new_X, model.transitions, stoc_vector,
                                        new_domain_enum)

            wdot = w_dot

        else:

            #print('No Reduction')
            new_w = ode_obj.y
    else:
        #residue += ode_obj.y[-1]
        residue += sink
        new_X = X.copy()
        new_w = ode_obj.y

        new_domain_enum = False

    #print("existing with residue" + str(residue))
    return new_X, new_w, position, valid, residue, grew, new_domain_enum
Esempio n. 4
0
 def _initialise_state_space_(self):
     self.domain_states = domain.from_iter((self.model.initial_state, ))
     self._state_enum = state_enum.StateEnum(self.domain_states)
     self.p = self._state_enum.pack_distribution(
         {self.model.initial_state: 1.0})