Skip to content

Commit

Permalink
Minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
light-weaver committed Sep 25, 2023
1 parent 04ce71f commit a43aaec
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 48 deletions.
46 changes: 29 additions & 17 deletions desdeo_tools/scalarization/ASF.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ def __call__(self, objective_vectors: np.ndarray, reference_point: np.ndarray):

return max_term


class AspResASF(ASFBase):
"""Implementation of an ASF using both aspiration and reservation levels.
directly consider both upper and lower bounds of the preferred ranges within the solution

Check failure on line 392 in desdeo_tools/scalarization/ASF.py

View workflow job for this annotation

GitHub Actions / Flake8

desdeo_tools/scalarization/ASF.py#L392

Trailing whitespace (W291)
Expand All @@ -413,16 +413,28 @@ class AspResASF(ASFBase):
6th International Conference’, Proceedings, Springer-Verlag, Berlin, Heidelberg, 2011, pp. 212–225.
"""

def __init__(self, nadir: np.ndarray, ideal: np.ndarray, rho: float = 1e-6, rho_sum: float = 1e-6,
alpha: float = 1e-1, beta: float = 1e-1,):
def __init__(
self,
nadir: np.ndarray,
ideal: np.ndarray,
rho: float = 1e-6,
rho_sum: float = 1e-6,
alpha: float = 1e-1,
beta: float = 1e-1,
):
self.nadir = nadir
self.ideal = ideal
self.rho = rho
self.rho_sum = rho_sum
self.alpha = alpha
self.beta = beta

def __call__(self, objective_vectors: np.ndarray, reference_point: np.ndarray, reservation_point: np.ndarray):
def __call__(
self,
objective_vectors: np.ndarray,
reference_point: np.ndarray,
reservation_point: np.ndarray,
):
# assure this function works with single objective vectors
if objective_vectors.ndim == 1:
f = objective_vectors.reshape((1, -1))
Expand All @@ -435,24 +447,23 @@ def __call__(self, objective_vectors: np.ndarray, reference_point: np.ndarray, r
ide = self.ideal
uto = self.ideal - self.rho
phi = np.zeros((objective_vectors.ndim,))

for i in range(objective_vectors.ndim):
if ide[i] <= f[0][i] <= z[i]:
phi[i] = -1 + self.alpha * (1/(z-uto))[i] * (f[0][i] - z[i])
phi[i] = -1 + self.alpha * (1 / (z - uto))[i] * (f[0][i] - z[i])

if z[i] <= f[0][i] <= r[i]:
phi[i] = (1/(r-z))[i] * (f[0][i] - r[i])
phi[i] = (1 / (r - z))[i] * (f[0][i] - r[i])

if r[i] <= f[0][i] <= nad[i]:
phi[i] = self.beta * (1/(nad-r))[i] * (f[0][i] - r[i])


phi[i] = self.beta * (1 / (nad - r))[i] * (f[0][i] - r[i])

max_term = np.array([np.max(phi)])
sum_term = np.array([self.rho_sum * np.sum(phi)])

return max_term + sum_term


class STEM(ASFBase):
"""Implementation of the Step Method (STEM).

Check failure on line 469 in desdeo_tools/scalarization/ASF.py

View workflow job for this annotation

GitHub Actions / Flake8

desdeo_tools/scalarization/ASF.py#L469

Blank line contains whitespace (W293)
Expand Down Expand Up @@ -482,9 +493,10 @@ def __call__(self, objective_vectors: np.ndarray):

nad = self.nadir
uto = self.ideal - self.rho
# TODO: #50 Fix the following line
phi = np.zeros((objective_vectors.ndim,))

Check failure on line 497 in desdeo_tools/scalarization/ASF.py

View workflow job for this annotation

GitHub Actions / Flake8

desdeo_tools/scalarization/ASF.py#L497

Local variable 'phi' is assigned to but never used (F841)

Check failure on line 497 in desdeo_tools/scalarization/ASF.py

View workflow job for this annotation

GitHub Actions / Run linters

Ruff (F841)

desdeo_tools/scalarization/ASF.py:497:9: F841 Local variable `phi` is assigned to but never used
e = abs(nad-uto)/np.max((nad, uto), axis=0)
max_term = np.array([(e/e.sum() * (f - uto)).max()])
e = abs(nad - uto) / np.max((nad, uto), axis=0)

max_term = np.array([(e / e.sum() * (f - uto)).max()])

return max_term
return max_term
8 changes: 2 additions & 6 deletions desdeo_tools/utilities/lattice_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ def simplexLatticeDesign(num_dimensions: int, lattice_resolution: int) -> np.nda
"""

number_of_vectors = comb(
lattice_resolution + num_dimensions - 1,
num_dimensions - 1,
exact=True,
lattice_resolution + num_dimensions - 1, num_dimensions - 1, exact=True
)

temp1 = range(1, num_dimensions + lattice_resolution)
Expand Down Expand Up @@ -122,9 +120,7 @@ def simplexLatticefromNumPoints(
while True:
temp_lattice_resolution += 1
number_of_vectors = comb(
temp_lattice_resolution + num_dimensions - 1,
num_dimensions - 1,
exact=True,
temp_lattice_resolution + num_dimensions - 1, num_dimensions - 1, exact=True
)
if number_of_vectors > num_points:
break
Expand Down
63 changes: 38 additions & 25 deletions desdeo_tools/utilities/pmod.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import numpy as np
from math import sqrt
from math import sqrt


def mapping_p(obtainpop: np.ndarray, pref_point: list):
""""""
size = len(obtainpop)
mapping_point = np.array([None]*size, dtype=object)
mapping_point = np.array([None] * size, dtype=object)
fsize = 0
T = 0.0
for i in range(0, size):
Expand All @@ -15,20 +15,21 @@ def mapping_p(obtainpop: np.ndarray, pref_point: list):
for j in range(0, fsize):
t += obtainpop[i][j] * pref_point[j]
p += pref_point[j] * pref_point[j]
T = 1 - (t/p)
T = 1 - (t / p)
f = np.zeros(fsize)
for j in range(0, fsize):
f[j] = obtainpop[i][j] + pref_point[j]*T
f[j] = obtainpop[i][j] + pref_point[j] * T
mapping_point[i] = f
return mapping_point


def mapping_distance(mapping_point, pref_point):
"""Calculate D1 in the paper, which is distance between mapping points and reference point
Args:
mapping_point (np.ndarray): The points that we moved to the hyperplane.
pref_point (np.ndarray): The reference point that the DM provides.
Returns:
float: D1
Expand All @@ -39,23 +40,27 @@ def mapping_distance(mapping_point, pref_point):
pp_size = len(pref_point)
for j in range(0, size):
for i in range(0, pp_size):
distance += (mapping_point[j][i] - pref_point[i])*(mapping_point[j][i] - pref_point[i])
distance += (mapping_point[j][i] - pref_point[i]) * (
mapping_point[j][i] - pref_point[i]
)
sum_ += sqrt(distance)
return sum_ / distance


def dist_vector(vec1, vec2):
dim = len(vec1)
sum_ = 0
for i in range(0, dim):
sum_ += (vec1[i]- vec2[i])**2
sum_ += (vec1[i] - vec2[i]) ** 2
return sqrt(sum_)

def d2_spcing(df:np.ndarray):

def d2_spcing(df: np.ndarray):
"""Calculate D2 in the paper, which is the standard deviation of each mapping point
to the nearest point
Args:
df(np.ndarray): The points that we moved to the hyperplane.
Returns:
float: D2
Expand All @@ -65,20 +70,28 @@ def d2_spcing(df:np.ndarray):
dist = []
temp = 0
for i in range(0, size):
distance = 1.0e+30
distance = 1.0e30
for j in range(0, size):
if j != i:
temp = dist_vector(df[i], df[j])
if temp < distance:
distance = temp
dist.append(distance)
average = sum(dist)/len(dist)
average = sum(dist) / len(dist)
for i in range(0, size):
temp = average - dist[i]
sum_ += temp**2
return sqrt(sum_/(size-1))
sum_ += temp ** 2
return sqrt(sum_ / (size - 1))


def distan_d3(ref_point:np.ndarray, population:np.ndarray, r:float, k:float, inside_ROI=[], outside_ROI=[]):
def distan_d3(
ref_point: np.ndarray,
population: np.ndarray,
r: float,
k: float,
inside_ROI=[],
outside_ROI=[],
):
"""Calculate D3 in the paper, which is the distance between preferred solution
and origin.
Args:
Expand All @@ -88,7 +101,7 @@ def distan_d3(ref_point:np.ndarray, population:np.ndarray, r:float, k:float, ins
k(float): penalty coefficient
inside_ROI= (list): solutions inside ROI
outside_ROI (list): solutions outside ROI
Returns:
float: D3
Expand All @@ -97,7 +110,7 @@ def distan_d3(ref_point:np.ndarray, population:np.ndarray, r:float, k:float, ins
sum1 = 0
sum2 = 0
d = 0
origion = [0]*len(population[0])
origion = [0] * len(population[0])
map_point = mapping_p(population, ref_point)
for i in range(0, dim):
d = dist_vector(map_point[i], ref_point)
Expand All @@ -106,19 +119,19 @@ def distan_d3(ref_point:np.ndarray, population:np.ndarray, r:float, k:float, ins
sum2 += sqrt(sum1)
inside_ROI.append(i)
else:
sum2 += k*sqrt(sum1)
sum2 += k * sqrt(sum1)
outside_ROI.append(i)
return sum2 / dim

def get_pmod(ref_point:np.ndarray, population:np.ndarray, r:float, k:float):
"""computes the PMOD indicator based on the following paper:
"Hou, Zhanglu, et al. "A performance indicator for reference-point-based multiobjective evolutionary
optimization." 2018 IEEE Symposium Series on Computational Intelligence (SSCI). IEEE, 2018".
"""

def get_pmod(ref_point: np.ndarray, population: np.ndarray, r: float, k: float):
"""computes the PMOD indicator based on the following paper:
"Hou, Zhanglu, et al. "A performance indicator for reference-point-based multiobjective evolutionary
optimization." 2018 IEEE Symposium Series on Computational Intelligence (SSCI). IEEE, 2018".
"""
mapping_point = mapping_p(population, ref_point)
d1 = mapping_distance(population, ref_point)
d2 = d2_spcing(mapping_point)
d3 = distan_d3(ref_point, population, r, k)
size = len(population)
pmod_value = d1 + d2 + d3
return pmod_value
return pmod_value

0 comments on commit a43aaec

Please sign in to comment.