Page MenuHomec4science

local_sparse_grid.py
No OneTemporary

File Metadata

Created
Thu, Sep 26, 14:17

local_sparse_grid.py

# Copyright (C) 2018 by the RROMPy authors
#
# This file is part of RROMPy.
#
# RROMPy is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# RROMPy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with RROMPy. If not, see <http://www.gnu.org/licenses/>.
#
import numpy as np
from copy import deepcopy as copy
from rrompy.utilities.base.types import List, paramList
from rrompy.parameter import checkParameterList
from rrompy.utilities.exception_manager import RROMPyException, RROMPyAssert
__all__ = ['localSparseGrid']
class localSparseGrid:
_allowedKinds = ["UNIFORM"]
def __init__(self, lims:paramList, kind : str = "UNIFORM",
scalingExp : List[float] = None):
self.lims = lims
self.kind = kind
self.scalingExp = scalingExp
self.reset()
def name(self) -> str:
return self.__class__.__name__
def __str__(self) -> str:
return "{}[{}_{}]_{}".format(self.name(), self.lims[0],
self.lims[1], self.kind)
def __repr__(self) -> str:
return self.__str__() + " at " + hex(id(self))
@property
def npar(self):
"""Number of parameters."""
return self._lims.shape[1]
@property
def npoints(self):
"""Number of points."""
return len(self.points)
@property
def lims(self):
"""Value of lims."""
return self._lims
@lims.setter
def lims(self, lims):
lims = checkParameterList(lims)[0]
if len(lims) != 2:
raise RROMPyException("2 limits must be specified.")
self._lims = lims
@property
def kind(self):
"""Value of kind."""
return self._kind
@kind.setter
def kind(self, kind):
if kind.upper() not in self._allowedKinds:
raise RROMPyException("Generator kind not recognized.")
self._kind = kind.upper()
if self.kind == "UNIFORM":
self._phiD = lambda x: x
self._phiI = lambda x: x
@property
def scalingExp(self):
"""Value of scalingExp."""
return self._scalingExp
@scalingExp.setter
def scalingExp(self, scalingExp):
if scalingExp is None:
scalingExp = [1.] * self.npar
if not hasattr(scalingExp, "__len__"): scalingExp = [scalingExp]
RROMPyAssert(self.npar, len(scalingExp), "Number of scaling terms")
self._scalingExp = np.array(scalingExp)
def reset(self):
self.deltalims = .5 * np.abs(self.lims.data[1] ** self.scalingExp
- self.lims.data[0] ** self.scalingExp)
self.points = checkParameterList(self._phiD(.5 * (
self.lims[0] ** self.scalingExp
+ self.lims[1] ** self.scalingExp)
) ** (1. / self.scalingExp), self.npar)[0]
self.depth = np.zeros((1, self.npar))
self.deltadepth = np.zeros(1)
def refine(self, active : List[int] = None) -> List[int]:
if active is None: active = np.arange(self.npoints)
newIdxs = []
for act in active:
pnt, dpt = self.points[act], self.depth[act]
ddp = self.deltadepth[act]
for jdelta in range(self.npar):
for signdelta in [-1., 1.]:
if np.isclose(dpt[jdelta], 1.):
gradj = (
self._phiI(pnt[jdelta] ** self.scalingExp[jdelta])
- self._phiI(self.points[0, jdelta] ** self.scalingExp[jdelta]))
if signdelta * gradj > 0:
continue
pntNew = copy(pnt)
pntNew[jdelta] = self._phiD(
self._phiI(pntNew[jdelta] ** self.scalingExp[jdelta])
+ .5 ** (dpt[jdelta] + ddp) * self.deltalims[jdelta]
* signdelta) ** (1. / self.scalingExp[jdelta])
dist = np.sum(np.abs(self.points.data
- pntNew.reshape(1, -1)), axis = 1)
if np.any(np.isclose(dist, 0.)): continue
newIdxs += [self.npoints]
self.points.append(pntNew)
self.depth = np.append(self.depth, [dpt], 0)
self.depth[-1, jdelta] += 1 + ddp
self.deltadepth = np.append(self.deltadepth, [0])
self.deltadepth[act] += 1
return newIdxs

Event Timeline