Example #1
0
def parameters_of_type(parameter_type, seed):
    np.random.seed(seed)
    while True:
        if parameter_type is None:
            yield None
        else:
            yield Parameter({k: np.random.random(v) for k, v in parameter_type.items()})
Example #2
0
    def sample_randomly(self, count=None, random_state=None, seed=None):
        """Randomly sample |Parameters| from the space.

        Parameters
        ----------
        count
            `None` or number of random parameters (see below).
        random_state
            :class:`~numpy.random.RandomState` to use for sampling.
            If `None`, a new random state is generated using `seed`
            as random seed, or the :func:`default <pymor.tools.random.default_random_state>`
            random state is used.
        seed
            If not `None`, a new radom state with this seed is used.

        Returns
        -------
        If `count` is `None`, an inexhaustible iterator returning random
        |Parameters|.
        Otherwise a list of `count` random |Parameters|.
        """
        assert not random_state or seed is None
        ranges = self.ranges
        random_state = get_random_state(random_state, seed)
        get_param = lambda: Parameter(((k, random_state.uniform(ranges[k][0], ranges[k][1], shp))
                                       for k, shp in self.parameter_type.items()))
        if count is None:
            def param_generator():
                while True:
                    yield get_param()
            return param_generator()
        else:
            return [get_param() for _ in range(count)]
Example #3
0
 def map_vertex_to_mu(self, vertex):
     values = self.ranges[:, 0] + self.dimensions * list(map(float, vertex))
     mu = Parameter({})
     for k, shape in self.parameter_type.items():
         count = np.prod(shape, dtype=int)
         head, values = values[:count], values[count:]
         mu[k] = np.array(head).reshape(shape)
     return mu
Example #4
0
 def sample_randomly(self, count=None, random_state=None, seed=None):
     """Iterator sampling random |Parameters| from the space."""
     assert not random_state or seed is None
     c = 0
     ranges = self.ranges
     random_state = random_state or new_random_state(seed)
     while count is None or c < count:
         yield Parameter(
             ((k, random_state.uniform(ranges[k][0], ranges[k][1], shp))
              for k, shp in self.parameter_type.iteritems()))
         c += 1
Example #5
0
 def sample_uniformly(self, counts):
     """Uniformly sample |Parameters| from the space."""
     if isinstance(counts, dict):
         pass
     elif isinstance(counts, (tuple, list, np.ndarray)):
         counts = {k: c for k, c in zip(self.parameter_type, counts)}
     else:
         counts = {k: counts for k in self.parameter_type}
     linspaces = tuple(np.linspace(self.ranges[k][0], self.ranges[k][1], num=counts[k]) for k in self.parameter_type)
     iters = tuple(product(ls, repeat=max(1, np.zeros(sps).size))
                   for ls, sps in zip(linspaces, self.parameter_type.values()))
     return [Parameter(((k, np.array(v).reshape(shp))
                        for k, v, shp in zip(self.parameter_type, i, self.parameter_type.values())))
             for i in product(*iters)]
Example #6
0
    def sample_randomly(self, count=None, random_state=None, seed=None):
        """Randomly sample |Parameters| from the space.

        .. warning::

            When neither `random_state` nor `seed` are specified,
            repeated calls to this method will return the same sequence
            of parameters!


        Parameters
        ----------
        count
            `None` or number of random parameters (see below).
        random_state
            :class:`~numpy.random.RandomState` to use for sampling.
            If `None`, a new random state is generated using `seed`
            as random seed.
        seed
            Random seed to use. If `None`, the
            :func:`default <pymor.tools.random.new_random_state>` random seed
            is used.

        Returns
        -------
        If `count` is `None`, an inexhaustible iterator returning random
        |Parameters|.
        Otherwise a list of `count` random |Parameters|.
        """
        assert not random_state or seed is None
        ranges = self.ranges
        random_state = random_state or new_random_state(seed)
        get_param = lambda: Parameter(
            ((k, random_state.uniform(ranges[k][0], ranges[k][1], shp))
             for k, shp in sorted(self.parameter_type.items())))
        # sorted is needed to ensure that the sampling of parameter components happens in an
        # deterministic order
        if count is None:

            def param_generator():
                while True:
                    yield get_param()

            return param_generator()
        else:
            return [get_param() for _ in range(count)]
Example #7
0
    def _solve(self, mu=None):
        mu = self.parse_parameter(mu).copy() if self.parametric else Parameter(
            {})

        # explicitly checking if logging is disabled saves the expensive str(mu) call
        if not self.logging_disabled:
            self.logger.info('Solving {} for {} ...'.format(self.name, mu))

        mu['_t'] = 0
        U0 = self.initial_data.as_vector(mu)
        return self.time_stepper.solve(operator=self.operator,
                                       rhs=self.rhs,
                                       initial_data=U0,
                                       mass=self.mass,
                                       initial_time=0,
                                       end_time=self.T,
                                       mu=mu,
                                       num_values=self.num_values)
Example #8
0
 def parse_parameter(self, mu):
     return Parameter.from_parameter_type(mu, self.parameter_type)
Example #9
0
 def parse_parameter(self, mu):
     return Parameter.from_parameter_type(mu, self.parameter_type)