Vous avez reçu un message "Your GitLab account has been locked ..." ? Pas d'inquiétude : lisez cet article https://docs.gricad-pages.univ-grenoble-alpes.fr/help/unlock/

Commit 4e79441c authored by EXT José Ignacio Requeno Jarabo's avatar EXT José Ignacio Requeno Jarabo
Browse files

Minor changes

parent 07e8a3d4
Pipeline #47929 failed with stages
in 2 minutes and 54 seconds
......@@ -12,9 +12,18 @@ operations for creating and handling partial ordered sets.
from sortedcontainers import SortedSet
from operator import getitem
import cython
@cython.cclass
class Lattice(object):
key = cython.declare(object)
list_of_sets = cython.declare(list)
# cython.declare(low=tuple, high=tuple)
@cython.locals(dim=cython.ushort)
@cython.returns(cython.void)
def __init__(self,
dim,
key=lambda x: x):
......@@ -24,6 +33,7 @@ class Lattice(object):
# self.list_of_lists = [SortedSet([], key=lambda x, j=i: self.key(x)[j]) for i in range(dim)]
self.list_of_sets = [SortedSet([], key=lambda x, j=i: getitem(self.key(x), j)) for i in range(dim)]
@cython.returns(str)
def _to_str(self):
# type: (Lattice) -> str
"""
......@@ -31,6 +41,7 @@ class Lattice(object):
"""
return str(self.list_of_sets)
@cython.returns(str)
def __repr__(self):
# type: (Lattice) -> str
"""
......@@ -38,6 +49,7 @@ class Lattice(object):
"""
return self._to_str()
@cython.returns(str)
def __str__(self):
# type: (Lattice) -> str
"""
......@@ -45,6 +57,7 @@ class Lattice(object):
"""
return self._to_str()
@cython.returns(cython.bint)
def __eq__(self, other):
# type: (Lattice, Lattice) -> bool
"""
......@@ -52,6 +65,7 @@ class Lattice(object):
"""
return (other.list_of_lists == self.list_of_sets) and (other.key == self.key)
@cython.returns(cython.bint)
def __ne__(self, other):
# type: (Lattice, Lattice) -> bool
"""
......@@ -59,6 +73,7 @@ class Lattice(object):
"""
return not self.__eq__(other)
@cython.returns(int)
def __hash__(self):
# type: (Lattice) -> int
"""
......@@ -66,6 +81,7 @@ class Lattice(object):
"""
return hash((tuple(self.list_of_sets), self.key))
@cython.returns(int)
def __len__(self):
# type: (Lattice) -> int
"""
......@@ -75,6 +91,8 @@ class Lattice(object):
return len(s)
# Lattice properties
@cython.ccall
@cython.returns(cython.ushort)
def dim(self):
# type: (Lattice) -> int
"""
......@@ -94,32 +112,44 @@ class Lattice(object):
"""
return len(self.list_of_sets)
@cython.ccall
@cython.returns(object)
def get_elements(self):
# type: (Lattice) -> iter
# type: (Lattice) -> SortedSet
return self.list_of_sets[0]
@cython.ccall
@cython.returns(cython.void)
def add(self, elem):
# type: (Lattice, object) -> None
for l in self.list_of_sets:
l.add(elem)
@cython.ccall
@cython.returns(cython.void)
def add_list(self, lst):
# type: (Lattice, iter) -> None
for l in self.list_of_sets:
l |= lst
@cython.ccall
@cython.returns(cython.void)
def remove(self, elem):
# type: (Lattice, object) -> None
for l in self.list_of_sets:
l.discard(elem)
@cython.ccall
@cython.returns(cython.void)
def remove_list(self, lst):
# type: (Lattice, iter) -> None
for l in self.list_of_sets:
l -= lst
@cython.ccall
@cython.locals(index=int)
def less(self, elem):
# type: (Lattice, object) -> set
# type: (Lattice, object) -> SortedSet
"""
Elements 'x' of the Lattice having x_i < elem_i for all i with i in [1, dim(elem)].
"""
......@@ -129,8 +159,10 @@ class Lattice(object):
s = s.intersection(l[:index])
return s
@cython.ccall
@cython.locals(index=int)
def less_equal(self, elem):
# type: (Lattice, object) -> set
# type: (Lattice, object) -> SortedSet
"""
Elements 'x' of the Lattice having x_i <= elem_i for all i with i in [1, dim(elem)].
"""
......@@ -140,8 +172,10 @@ class Lattice(object):
s = s.intersection(l[:index])
return s
@cython.ccall
@cython.locals(index=int)
def greater(self, elem):
# type: (Lattice, object) -> set
# type: (Lattice, object) -> SortedSet
"""
Elements 'x' of the Lattice having x_i > elem_i for all i with i in [1, dim(elem)].
"""
......@@ -151,8 +185,10 @@ class Lattice(object):
s = s.intersection(l[index:])
return s
@cython.ccall
@cython.locals(index=int)
def greater_equal(self, elem):
# type: (Lattice, object) -> set
# type: (Lattice, object) -> SortedSet
"""
Elements 'x' of the Lattice having x_i >= elem_i for all i with i in [1, dim(elem)].
"""
......@@ -162,8 +198,10 @@ class Lattice(object):
s = s.intersection(l[index:])
return s
@cython.ccall
@cython.locals(index1=int, index2=int)
def equal(self, elem):
# type: (Lattice, object) -> set
# type: (Lattice, object) -> SortedSet
"""
Elements 'x' of the Lattice having x_i == elem_i for all i with i in [1, dim(elem)].
"""
......
......@@ -17,6 +17,7 @@ optimising vector operations.
import numpy as np
from numpy import linalg as la
from numpy import array as point
import cython
import ParetoLib.Geometry
from ParetoLib._py3k import red
......@@ -25,6 +26,9 @@ from ParetoLib._py3k import red
# Auxiliary functions for computing the algebraic properties
# of a vector (e.g., norm, distance, etc.)
@cython.ccall
@cython.returns(cython.double)
@cython.locals(i=cython.double)
def r(i):
# type: (float) -> float
"""
......@@ -50,7 +54,8 @@ def r(i):
"""
return round(i, ParetoLib.Geometry.__numdigits__)
@cython.ccall
@cython.returns(cython.ushort)
def dim(x):
# type: (point) -> int
"""
......@@ -69,7 +74,7 @@ def dim(x):
"""
return len(x)
@cython.returns(cython.double)
def norm(x):
# type: (point) -> float
"""
......@@ -88,7 +93,8 @@ def norm(x):
"""
return la.norm(x)
@cython.ccall
@cython.returns(cython.double)
def distance(x, xprime):
# type: (point, point) -> float
"""
......@@ -111,6 +117,8 @@ def distance(x, xprime):
return norm(temp)
@cython.returns(cython.double)
@cython.locals(_sum=cython.double)
def hamming_distance(x, xprime):
# type: (point, point) -> float
"""
......@@ -179,6 +187,9 @@ def add(x, xprime):
return x + xprime
# Cythonizing 'mult' produce (0.0, ..., 0.0) as output, even though i != 0
# @cython.returns(tuple)
@cython.locals(i=cython.double)
def mult(x, i):
# type: (point, float) -> point
"""
......@@ -200,6 +211,9 @@ def mult(x, i):
return x * i
# Cythonizing 'div' produce 'Division by zero' error, even though i != 0
# @cython.returns(tuple)
# @cython.locals(i=cython.double)
def div(x, i):
# type: (point, float) -> point
"""
......@@ -222,6 +236,7 @@ def div(x, i):
# Comparison of two points
@cython.returns(cython.bint)
def greater(x, xprime):
# type: (point, point) -> bool
"""
......@@ -243,6 +258,7 @@ def greater(x, xprime):
return all(x > xprime)
@cython.returns(cython.bint)
def greater_equal(x, xprime):
# type: (point, point) -> bool
"""
......@@ -264,6 +280,7 @@ def greater_equal(x, xprime):
return all(x >= xprime)
@cython.returns(cython.bint)
def less(x, xprime):
# type: (point, point) -> bool
"""
......@@ -285,6 +302,7 @@ def less(x, xprime):
return all(x < xprime)
@cython.returns(cython.bint)
def less_equal(x, xprime):
# type: (point, point) -> bool
"""
......@@ -306,6 +324,8 @@ def less_equal(x, xprime):
return all(x <= xprime)
@cython.ccall
@cython.returns(cython.bint)
def equal(x, xprime):
# type: (point, point) -> bool
"""
......@@ -327,6 +347,8 @@ def equal(x, xprime):
return all(x == xprime)
@cython.ccall
@cython.returns(cython.bint)
def incomparables(x, xprime):
# type: (point, point) -> bool
"""
......@@ -350,6 +372,7 @@ def incomparables(x, xprime):
>>> incomparables(x, xprime)
>>> True
"""
return (not greater_equal(x, xprime)) and (not greater_equal(xprime, x))
......@@ -443,6 +466,9 @@ def minimum(x, xprime):
return np.minimum(x, xprime)
@cython.ccall
@cython.locals(i=cython.ushort,
n=cython.ushort, m=cython.ushort)
def subt(i, x, xprime):
# type: (int, point, point) -> point
"""
......@@ -472,6 +498,7 @@ def subt(i, x, xprime):
return out
@cython.locals(n=cython.ushort, m=cython.ushort)
def select(x, xprime):
# type: (point, point) -> point
"""
......@@ -498,6 +525,9 @@ def select(x, xprime):
# Integer to binary notation
@cython.ccall
@cython.returns(list)
@cython.locals(x=cython.ulonglong, pad=cython.ushort, pad_temp=cython.ushort, i=str, temp1=list, temp2=list)
def int_to_bin_list(x, pad=0):
# type: (int, int) -> list
"""
......@@ -524,6 +554,8 @@ def int_to_bin_list(x, pad=0):
return temp2
@cython.ccall
@cython.locals(x=cython.ulonglong, pad=cython.ushort)
def int_to_bin_tuple(x, pad=0):
# type: (int, int) -> point
""" Equivalent to int_to_bin_list(x, pad=0).
......@@ -548,6 +580,8 @@ def int_to_bin_tuple(x, pad=0):
# Domination
@cython.ccall
@cython.returns(cython.bint)
def dominates(x, xprime):
# type: (point, point) -> bool
"""
......@@ -556,6 +590,8 @@ def dominates(x, xprime):
return less_equal(x, xprime)
@cython.ccall
@cython.returns(cython.bint)
def is_dominated(x, xprime):
# type: (point, point) -> bool
"""
......
......@@ -193,7 +193,7 @@ def add(x, xprime):
# Cythonizing 'mult' produce (0.0, ..., 0.0) as output, even though i != 0
# @cython.returns(tuple)
@cython.locals(xi=cython.double)
@cython.locals(i=cython.double, xi=cython.double)
def mult(x, i):
# type: (tuple, float) -> tuple
"""
......
......@@ -24,7 +24,7 @@ import ParetoLib.Geometry.Point as Point
@cython.cclass
class Segment:
class Segment(object):
low = cython.declare(tuple, visibility='public')
high = cython.declare(tuple, visibility='public')
......
......@@ -33,8 +33,10 @@ class OracleMatlab(Oracle):
cython.declare(err=object)
cython.declare(eng=object)
cython.declare(f=object)
cython.declare(d=cython.ushort)
cython.declare(_f=object)
cython.declare(_d=cython.ushort)
cython.declare(_matlab_model_file=str)
@cython.returns(cython.void)
@cython.locals(matlab_model_file=str)
......@@ -53,15 +55,15 @@ class OracleMatlab(Oracle):
# Matlab function 'f' not loaded yet.
# Dimension of the space unknown
self.f = None
self.d = None
self._f = None
self._d = 0
self.matlab_model_file = matlab_model_file
self._matlab_model_file = matlab_model_file
@cython.returns(cython.void)
def _load_matlab_engine(self):
# type: (OracleMatlab) -> None
assert self.matlab_model_file != ''
assert self._matlab_model_file != ''
# Lazy initialization of the Matlab engine
RootOracle.logger.debug('Initializing Matlab engine')
......@@ -76,7 +78,7 @@ class OracleMatlab(Oracle):
@cython.locals(filename=str, file_extension=str, func_name=str)
def _load_function(self):
# type: (OracleMatlab) -> None
assert self.matlab_model_file != '', 'Matlab function not defined'
assert self._matlab_model_file != '', 'Matlab function not defined'
# assert self.eng is not None, 'Matlab engine not started'
RootOracle.logger.debug('Loading Matlab function')
......@@ -85,7 +87,7 @@ class OracleMatlab(Oracle):
self._load_matlab_engine()
# Include the path of the Matlab model in the Matlab workspace
self.eng.addpath(os.path.dirname(self.matlab_model_file))
self.eng.addpath(os.path.dirname(self._matlab_model_file))
# Extract the name of the Matlab function.
# If the Matlab filename is 'matlab_model.m' then:
......@@ -96,45 +98,113 @@ class OracleMatlab(Oracle):
# matlab_model_file = '/path/to/matlab_model.m'
# filename, file_extension = ('/path/to/matlab_model', '.m')
filename, file_extension = os.path.splitext(self.matlab_model_file)
filename, file_extension = os.path.splitext(self._matlab_model_file)
# func_name = 'matlab_model'
func_name = os.path.basename(filename)
# Extract from Matlab the function 'f' that will be used as Oracle
# self.f = self.eng.__getattr__('matlab_model')
self.f = self.eng.__getattr__(func_name)
# self._f = self.eng.__getattr__('matlab_model')
self._f = self.eng.__getattr__(func_name)
# Dimension, i.e., equivalent to the number of in arguments of the function 'f'
self.d = int(self.eng.nargin(func_name))
self._d = int(self.eng.nargin(func_name))
RootOracle.logger.debug('Function {0} with {1} parameters'.format(func_name, self.d))
RootOracle.logger.debug('Matlab function {0}'.format(self.f))
RootOracle.logger.debug('Function {0} with {1} parameters'.format(func_name, self._d))
RootOracle.logger.debug('Matlab function {0}'.format(self._f))
def __setattr__(self, name, value):
# type: (OracleMatlab, str, None) -> None
@property
def matlab_model_file(self):
# type: (OracleMatlab) -> str
"""
Getter of matlab_model_file class attribute.
"""
return self._matlab_model_file
@matlab_model_file.setter
def matlab_model_file(self, value):
# type: (OracleMatlab, str) -> None
"""
Assignation of a value to a class attribute.
Setter of matlab_model_file class attribute.
Args:
self (OracleMatlab): The OracleMatlab.
name (str): The attribute.
value (None): The value
value (str): The value
Returns:
None: self.name = value.
None: self.matlab_model_file = value.
Example:
>>> matlab_model_file = 'test1.m'
>>> ora = OracleMatlab()
>>> ora.matlab_model_file = matlab_model_file
"""
if value.strip() != '':
self._f = None
self._d = 0
self._matlab_model_file = value
# _matlab_model_file = property(getname, setname, delname)
@property
def f(self):
# type: (OracleMatlab) -> callable
"""
Getter of f class attribute.
"""
if self._f is None and self._d == 0:
self._load_function()
return self._f
object.__setattr__(self, name, value)
@f.setter
def f(self, value):
# type: (OracleMatlab, callable) -> None
"""
Setter of matlab_model_file class attribute.
Args:
self (OracleMatlab): The OracleMatlab.
value (object): The value
Returns:
None: self.f = value.
"""
self._f = value
# f = property(getname, setname, delname)
@property
def d(self):
# type: (OracleMatlab) -> int
"""
Getter of d class attribute.
"""
if self._f is None and self._d == 0:
self._load_function()
str_matlab_file = 'matlab_model_file'
if (name == str_matlab_file) and (value.strip() != ''):
self.f = None
self.d = None
return self._d
@d.setter
def d(self, value):
# type: (OracleMatlab, int) -> None
"""
Setter of d class attribute.
Args:
self (OracleMatlab): The OracleMatlab.
value (int): The value
Returns:
None: self.d = value.
Example:
>>> ora = OracleMatlab()
>>> ora.d = 2
"""
self._d = value
# d = property(getname, setname, delname)
@cython.returns(str)
def __repr__(self):
......@@ -142,7 +212,7 @@ class OracleMatlab(Oracle):
"""
Printer.
"""
return self.matlab_model_file
return self._matlab_model_file
@cython.returns(str)
def __str__(self):
......@@ -150,7 +220,7 @@ class OracleMatlab(Oracle):
"""
Printer.
"""
return self.matlab_model_file
return self._matlab_model_file
@cython.returns(cython.bint)
def __eq__(self, other):
......@@ -158,7 +228,7 @@ class OracleMatlab(Oracle):
"""
self == other
"""
return self.matlab_model_file == other.matlab_model_file
return self._matlab_model_file == other.matlab_model_file
@cython.returns(cython.bint)
def __ne__(self, other):
......@@ -174,8 +244,9 @@ class OracleMatlab(Oracle):
"""
Identity function (via hashing).
"""
return hash(tuple(self.matlab_model_file))
return hash(tuple(self._matlab_model_file))
@cython.returns(cython.void)
def __del__(self):
# type: (OracleMatlab) -> None
"""
......@@ -184,14 +255,16 @@ class OracleMatlab(Oracle):
if self.eng is not None:
self.eng.quit()
@cython.returns(object)
def __copy__(self):
# type: (OracleMatlab) -> OracleMatlab
"""
other = copy.copy(self)
"""
RootOracle.logger.debug('__copy__: {0}'.format(self))
return OracleMatlab(matlab_model_file=self.matlab_model_file)
return OracleMatlab(matlab_model_file=self._matlab_model_file)
@cython.returns(object)
def __deepcopy__(self, memo):
# type: (OracleMatlab, dict) -> OracleMatlab
"""
......@@ -200,37 +273,37 @@ class OracleMatlab(Oracle):
# deepcopy function is required for creating multiple instances of the Oracle in ParSearch.
# deepcopy cannot handle neither matlab.engine nor matlab.func
RootOracle.logger.debug('__deeopcopy__: {0}'.format(self))
return OracleMatlab(matlab_model_file=self.matlab_model_file)
def __getattr__(self, name):
# type: (OracleMatlab, str) -> _
"""
Returns:
self.name (object attribute)
"""
elem = object.__getattribute__(self, name)
if (elem is None) and (name in ['f', 'd']):
self._load_function()
elem = object.__getattribute__(self, name)
# elif (elem is None) and (name == 'eng'):
# self._load_matlab_engine()
# elem = object.__getattribute__(self, name)
RootOracle.logger.debug('__getattr__: {0}, {1}'.format(name, elem))