Page MenuHomec4science

trained_model_nearest_neighbor.py
No OneTemporary

File Metadata

Created
Fri, May 31, 16:23

trained_model_nearest_neighbor.py

# Copyright (C) 2018-2020 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 rrompy.reduction_methods.base.trained_model.trained_model import (
TrainedModel)
from rrompy.utilities.numerical.compress_matrix import compressMatrix
from rrompy.utilities.base.types import Np1D, paramList, sampList
from rrompy.utilities.base import verbosityManager as vbMng
from rrompy.utilities.exception_manager import RROMPyException, RROMPyWarning
from rrompy.sampling import sampleList
__all__ = ['TrainedModelNearestNeighbor']
class TrainedModelNearestNeighbor(TrainedModel):
"""
ROM approximant evaluation for nearest neighbor approximant.
Attributes:
Data: dictionary with all that can be pickled.
"""
def compress(self, collapse : bool = False, tol : float = 0., *args,
**kwargs):
if not collapse and tol <= 0.: return
if hasattr(self.data, "_is_C_quadratic") and self.data._is_C_quadratic:
raise RROMPyException(("Cannot compress model with quadratic "
"output."))
RMat = self.data.projMat
if not collapse:
if hasattr(self.data, "_compressTol"):
RROMPyWarning(("Recompressing already compressed model is "
"ineffective. Aborting."))
return
self.data.projMat, RMat, _ = compressMatrix(RMat, tol, *args,
**kwargs)
for j, suppj in enumerate(self.data.supp):
self.data.vals[j] = RMat[:, suppj : suppj + len(self.data.vals[j])
].dot(self.data.vals[j])
self.data.supp[j] = [0]
super().compress(collapse, tol)
def getNearestNeighbor(self, mu : paramList = []) -> Np1D:
"""
Find nearest neighbor to arbitrary parameter.
Args:
mu: Target parameter.
"""
mu = self.checkParameterList(mu)
vbMng(self, "INIT", "Finding nearest neighbor to mu = {}.".format(mu),
22)
nn = []
intM = np.array(self.data.NN(mu), dtype = int)
for iM in intM:
val, supp = self.data.vals[iM], self.data.supp[iM]
nn += [np.pad(val, (supp, self.data.projMat.shape[1]
- supp - len(val)), 'constant')]
nn = sampleList(nn)
vbMng(self, "DEL", "Done finding nearest neighbor.", 22)
return nn
def getApproxReduced(self, mu : paramList = []) -> sampList:
"""
Evaluate reduced representation of approximant at arbitrary parameter.
Args:
mu: Target parameter.
"""
mu = self.checkParameterList(mu)
if (not hasattr(self, "lastSolvedApproxReduced")
or self.lastSolvedApproxReduced != mu):
vbMng(self, "INIT",
"Evaluating approximant at mu = {}.".format(mu), 12)
self.uApproxReduced = self.getNearestNeighbor(mu)
vbMng(self, "DEL", "Done evaluating approximant.", 12)
self.lastSolvedApproxReduced = mu
return self.uApproxReduced
def getPoles(self, *args, **kwargs) -> Np1D:
"""Obtain approximant poles."""
return np.empty(0)

Event Timeline