Skip to content

Commit

Permalink
Implemented all single-objective functions (#6)
Browse files Browse the repository at this point in the history
Please note that a merge conflict was resolved while committing
this change. Look at lines 283 to 308, specifically to the Matyas
function.
  • Loading branch information
Carl-K authored and ljvmiranda921 committed Aug 3, 2017
1 parent 108c50a commit 60df023
Showing 1 changed file with 48 additions and 45 deletions.
93 changes: 48 additions & 45 deletions pyswarms/utils/functions/single_obj.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ def beale_func(x):

x_ = x[:,0]
y_ = x[:,1]
j = (1.5 - x_ + x_ * y_)**2.0 + \
(2.25 - x_ + x_ * y_**2.0)**2.0 + \
(2.625 - x_ + x_ * y_**3.0)**2.0
j = ((1.5 - x_ + x_ * y_)**2.0
+ (2.25 - x_ + x_ * y_**2.0)**2.0
+ (2.625 - x_ + x_ * y_**3.0)**2.0)

return j

Expand Down Expand Up @@ -193,12 +193,14 @@ def goldstein_func(x):
if not np.logical_and(x >= -2, x <= 2).all():
raise ValueError('Input for Goldstein-Price function must be within [-2, 2].')

# TODO: Write actual function here

# TODO: Change this part by returning the actual value when
# you compute x.
dummy = np.array([3,3,3]) # defined just to not break the tests
return dummy
x_ = x[:,0]
y_ = x[:,1]
j = ((1 + (x_ + y_ + 1)**2.0
* (19 - 14*x_ + 3*x_**2.0 - 14*y_ + 6*x_*y_ + 3*y_**2.0))
* (30 + (2*x_ - 3 * y_)**2.0
* (18 - 32*x_ + 12*x_**2.0 + 48*y_ - 36*x_*y_ + 27*y_**2.0)))

return j

# TODO: Implement Booth's Function
def booth_func(x):
Expand Down Expand Up @@ -231,12 +233,11 @@ def booth_func(x):
if not np.logical_and(x >= -10, x <= 10).all():
raise ValueError('Input for Booth function must be within [-10, 10].')

# TODO: Write actual function here

# TODO: Change this part by returning the actual value when
# you compute x.
dummy = np.array([0,0,0]) # defined just to not break the tests
return dummy
x_ = x[:,0]
y_ = x[:,1]
j = (x_ + 2 * y_ - 7)**2.0 + (2 * x_ + y_ - 5)**2.0

return j

# TODO: Implement Bukin Function no. 6
def bukin6_func(x):
Expand Down Expand Up @@ -273,12 +274,11 @@ def bukin6_func(x):
if not np.logical_and(x[:,1] >= -3, x[:,1] <= 3).all():
raise ValueError('y-coord for Bukin N. 6 function must be within [-3, 3].')

# TODO: Write actual function here
x_ = x[:,0]
y_ = x[:,1]
j = 100 * np.sqrt(np.absolute(y_**2.0 - 0.01*x_**2.0)) + 0.01 * np.absolute(x_ + 10)

# TODO: Change this part by returning the actual value when
# you compute x.
dummy = np.array([0,0,0]) # defined just to not break the tests
return dummy
return j

def matyas_func(x):
"""Matyas objective function
Expand All @@ -295,27 +295,19 @@ def matyas_func(x):
-------
numpy.ndarray
computed cost of size (n_particles, )
Raises
------
IndexError
When the input dimensions is greater than what the function
allows
ValueError
When the input is out of bounds with respect to the function
domain
"""
<<<<<<< HEAD
if not x.shape[1] == 2:
raise IndexError('Matyas function only takes two-dimensional input.')
if not np.logical_and(x >= -10, x <= 10).all():
raise ValueError('Input for Matyas function must be within [-10, 10].')
=======
x_ = x[:,0]
y_ = x[:,1]
j = 0.26 * (x_**2.0 + y_**2.0) - 0.48 * x_ * y_
>>>>>>> f5649c5... implemented all single-objective functions

# TODO: Write actual function here

# TODO: Change this part by returning the actual value when
# you compute x.
dummy = np.array([0,0,0]) # defined just to not break the tests
return dummy
return j

def levi_func(x):
"""Levi objective function
Expand Down Expand Up @@ -347,12 +339,22 @@ def levi_func(x):
if not np.logical_and(x >= -10, x <= 10).all():
raise ValueError('Input for Levi function must be within [-10, 10].')

# TODO: Write actual function here
mask = np.full(x.shape, False)
mask[:,-1] = True
masked_x = np.ma.array(x, mask=mask)

w_ = 1 + (x - 1) / 4
masked_w_ = np.ma.array(w_, mask=mask)
d_ = x.shape[1] - 1

# TODO: Change this part by returning the actual value when
# you compute x.
dummy = np.array([0,0,0]) # defined just to not break the tests
return dummy
j = (np.sin(np.pi * w_[:,0])**2.0
+ ((masked_x - 1)**2.0).sum(axis=1)
* (1 + 10 * np.sin(np.pi * (masked_w_).sum(axis=1) + 1)**2.0)
+ (w_[:,d_] - 1)**2.0
* (1 + np.sin(2 * np.pi * w_[:,d_])**2.0 ))


return j

def schaffer2_func(x):
"""Schaffer N.2 objective function
Expand Down Expand Up @@ -384,9 +386,10 @@ def schaffer2_func(x):
if not np.logical_and(x >= -100, x <= 100).all():
raise ValueError('Input for Schaffer function must be within [-100, 100].')

# TODO: Write actual function here
x_ = x[:,0]
y_ = x[:,1]
j = (0.5
+ ((np.sin(x_**2.0 - y_**2.0)**2.0 - 0.5)
/ ((1 + 0.001 * (x_**2.0 + y_**2.0))**2.0)))

# TODO: Change this part by returning the actual value when
# you compute x.
dummy = np.array([0,0,0]) # defined just to not break the tests
return dummy
return j

0 comments on commit 60df023

Please sign in to comment.