Skip to content

Commit

Permalink
Remove _func in single_obj function names (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
jarcelao authored and ljvmiranda921 committed Jan 29, 2019
1 parent 918b46e commit 251a9e1
Show file tree
Hide file tree
Showing 26 changed files with 152 additions and 152 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ import pyswarms as ps

Suppose we want to find the minima of `f(x) = x^2` using global best
PSO, simply import the built-in sphere function,
`pyswarms.utils.functions.sphere_func()`, and the necessary optimizer:
`pyswarms.utils.functions.sphere()`, and the necessary optimizer:

```python
import pyswarms as ps
Expand All @@ -111,7 +111,7 @@ options = {'c1': 0.5, 'c2': 0.3, 'w':0.9}
# Call instance of PSO
optimizer = ps.single.GlobalBestPSO(n_particles=10, dimensions=2, options=options)
# Perform optimization
best_cost, best_pos = optimizer.optimize(fx.sphere_func, iters=100, verbose=3, print_step=25)
best_cost, best_pos = optimizer.optimize(fx.sphere, iters=100, verbose=3, print_step=25)
```
```s
>>> 2017-10-03 10:12:33,859 - pyswarms.single.global_best - INFO - Iteration 1/100, cost: 0.131244226714
Expand Down Expand Up @@ -170,7 +170,7 @@ options = {
# n_selection_iters is the number of iterations to run the searcher
# iters is the number of iterations to run the optimizer
g = RandomSearch(ps.single.LocalBestPSO, n_particles=40,
dimensions=20, options=options, objective_func=fx.sphere_func,
dimensions=20, options=options, objective_func=fx.sphere,
iters=10, n_selection_iters=100)

best_score, best_options = g.search()
Expand Down Expand Up @@ -202,7 +202,7 @@ from pyswarms.utils.plotters import plot_cost_history
# Set-up optimizer
options = {'c1':0.5, 'c2':0.3, 'w':0.9}
optimizer = ps.single.GlobalBestPSO(n_particles=50, dimensions=2, options=options)
optimizer.optimize(fx.sphere_func, iters=100)
optimizer.optimize(fx.sphere, iters=100)
# Plot the cost
plot_cost_history(optimizer.cost_history)
plt.show()
Expand All @@ -216,7 +216,7 @@ We can also plot the animation...
from pyswarms.utils.plotters.formatters import Mesher
from pyswarms.utils.plotters.formatters import Designer
# Plot the sphere function's mesh for better plots
m = Mesher(func=fx.sphere_func)
m = Mesher(func=fx.sphere)
# Adjust figure limits
d = Designer(limits=[(-1,1), (-1,1), (-0.1,1)],
label=['x-axis', 'y-axis', 'z-axis'])
Expand Down
8 changes: 4 additions & 4 deletions docs/examples/basic_optimization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ several variables at once.
optimizer = ps.single.GlobalBestPSO(n_particles=10, dimensions=2, options=options)
# Perform optimization
cost, pos = optimizer.optimize(fx.sphere_func, print_step=100, iters=1000, verbose=3)
cost, pos = optimizer.optimize(fx.sphere, print_step=100, iters=1000, verbose=3)
.. parsed-literal::
Expand Down Expand Up @@ -116,7 +116,7 @@ Now, let’s try this one using local-best PSO:
optimizer = ps.single.LocalBestPSO(n_particles=10, dimensions=2, options=options)
# Perform optimization
cost, pos = optimizer.optimize(fx.sphere_func, print_step=100, iters=1000, verbose=3)
cost, pos = optimizer.optimize(fx.sphere, print_step=100, iters=1000, verbose=3)
.. parsed-literal::
Expand Down Expand Up @@ -151,7 +151,7 @@ Another thing that we can do is to set some bounds into our solution, so
as to contain our candidate solutions within a specific range. We can do
this simply by passing a ``bounds`` parameter, of type ``tuple``, when
creating an instance of our swarm. Let’s try this using the global-best
PSO with the Rastrigin function (``rastrigin_func`` in
PSO with the Rastrigin function (``rastrigin`` in
``pyswarms.utils.functions.single_obj``).

Recall that the Rastrigin function is bounded within ``[-5.12, 5.12]``.
Expand Down Expand Up @@ -191,7 +191,7 @@ constant.
optimizer = ps.single.GlobalBestPSO(n_particles=10, dimensions=2, options=options, bounds=bounds)
# Perform optimization
cost, pos = optimizer.optimize(fx.rastrigin_func, print_step=100, iters=1000, verbose=3)
cost, pos = optimizer.optimize(fx.rastrigin, print_step=100, iters=1000, verbose=3)
.. parsed-literal::
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/custom_optimization_loop.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ new attributes.
import numpy as np
# Import sphere function as objective function
from pyswarms.utils.functions.single_obj import sphere_func as f
from pyswarms.utils.functions.single_obj import sphere as f
# Import backend modules
import pyswarms.backend as P
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/visualization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ the ``optimize()`` method for 100 iterations.
options = {'c1':0.5, 'c2':0.3, 'w':0.9}
optimizer = ps.single.GlobalBestPSO(n_particles=50, dimensions=2, options=options)
cost, pos = optimizer.optimize(fx.sphere_func, iters=100)
cost, pos = optimizer.optimize(fx.sphere, iters=100)
.. code-block::
Expand Down Expand Up @@ -112,7 +112,7 @@ with respect to our objective function. We can accomplish that using the
from pyswarms.utils.plotters.formatters import Mesher
# Initialize mesher with sphere function
m = Mesher(func=fx.sphere_func)
m = Mesher(func=fx.sphere)
There are different formatters available in the
``pyswarms.utils.plotters.formatters`` module to customize your plots
Expand Down
8 changes: 4 additions & 4 deletions examples/basic_optimization.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
"optimizer = ps.single.GlobalBestPSO(n_particles=10, dimensions=2, options=options)\n",
"\n",
"# Perform optimization\n",
"cost, pos = optimizer.optimize(fx.sphere_func, print_step=100, iters=1000, verbose=3)"
"cost, pos = optimizer.optimize(fx.sphere, print_step=100, iters=1000, verbose=3)"
]
},
{
Expand Down Expand Up @@ -184,15 +184,15 @@
"optimizer = ps.single.LocalBestPSO(n_particles=10, dimensions=2, options=options)\n",
"\n",
"# Perform optimization\n",
"cost, pos = optimizer.optimize(fx.sphere_func, print_step=100, iters=1000, verbose=3)"
"cost, pos = optimizer.optimize(fx.sphere, print_step=100, iters=1000, verbose=3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Optimizing a function with bounds\n",
"Another thing that we can do is to set some bounds into our solution, so as to contain our candidate solutions within a specific range. We can do this simply by passing a `bounds` parameter, of type `tuple`, when creating an instance of our swarm. Let's try this using the global-best PSO with the Rastrigin function (`rastrigin_func` in `pyswarms.utils.functions.single_obj`)."
"Another thing that we can do is to set some bounds into our solution, so as to contain our candidate solutions within a specific range. We can do this simply by passing a `bounds` parameter, of type `tuple`, when creating an instance of our swarm. Let's try this using the global-best PSO with the Rastrigin function (`rastrigin` in `pyswarms.utils.functions.single_obj`)."
]
},
{
Expand Down Expand Up @@ -269,7 +269,7 @@
"optimizer = ps.single.GlobalBestPSO(n_particles=10, dimensions=2, options=options, bounds=bounds)\n",
"\n",
"# Perform optimization\n",
"cost, pos = optimizer.optimize(fx.rastrigin_func, print_step=100, iters=1000, verbose=3)"
"cost, pos = optimizer.optimize(fx.rastrigin, print_step=100, iters=1000, verbose=3)"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion examples/custom_optimization_loop.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"import numpy as np\n",
"\n",
"# Import sphere function as objective function\n",
"from pyswarms.utils.functions.single_obj import sphere_func as f\n",
"from pyswarms.utils.functions.single_obj import sphere as f\n",
"\n",
"# Import backend modules\n",
"import pyswarms.backend as P\n",
Expand Down
4 changes: 2 additions & 2 deletions examples/visualization.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
"source": [
"options = {'c1':0.5, 'c2':0.3, 'w':0.9}\n",
"optimizer = ps.single.GlobalBestPSO(n_particles=50, dimensions=2, options=options)\n",
"cost, pos = optimizer.optimize(fx.sphere_func, iters=100)"
"cost, pos = optimizer.optimize(fx.sphere, iters=100)"
]
},
{
Expand Down Expand Up @@ -184,7 +184,7 @@
"outputs": [],
"source": [
"# Initialize mesher with sphere function\n",
"m = Mesher(func=fx.sphere_func)"
"m = Mesher(func=fx.sphere)"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion pyswarms/single/general_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
options=options, topology=my_topology)
# Perform optimization
stats = optimizer.optimize(fx.sphere_func, iters=100)
stats = optimizer.optimize(fx.sphere, iters=100)
This algorithm was adapted from the earlier works of J. Kennedy and
R.C. Eberhart in Particle Swarm Optimization [IJCNN1995]_.
Expand Down
2 changes: 1 addition & 1 deletion pyswarms/single/global_best.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
options=options)
# Perform optimization
stats = optimizer.optimize(fx.sphere_func, iters=100)
stats = optimizer.optimize(fx.sphere, iters=100)
This algorithm was adapted from the earlier works of J. Kennedy and
R.C. Eberhart in Particle Swarm Optimization [IJCNN1995]_.
Expand Down
2 changes: 1 addition & 1 deletion pyswarms/single/local_best.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
options=options)
# Perform optimization
stats = optimizer.optimize(fx.sphere_func, iters=100)
stats = optimizer.optimize(fx.sphere, iters=100)
This algorithm was adapted from one of the earlier works of
J. Kennedy and R.C. Eberhart in Particle Swarm Optimization
Expand Down
68 changes: 34 additions & 34 deletions pyswarms/utils/functions/single_obj.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@
the design pattern stated above.
Function list:
- Ackley's, ackley_func
- Beale, beale_func
- Booth, booth_func
- Bukin's No 6, bukin6_func
- Cross-in-Tray, crossintray_func
- Easom, easom_func
- Eggholder, eggholder_func
- Goldstein, goldstein_func
- Himmelblau's, himmelblau_func
- Holder Table, holdertable_func
- Levi, levi_func
- Matyas, matyas_func
- Rastrigin, rastrigin_func
- Rosenbrock, rosenbrock_func
- Schaffer No 2, schaffer2_func
- Sphere, sphere_func
- Three Hump Camel, threehump_func
- Ackley's, ackley
- Beale, beale
- Booth, booth
- Bukin's No 6, bukin6
- Cross-in-Tray, crossintray
- Easom, easom
- Eggholder, eggholder
- Goldstein, goldstein
- Himmelblau's, himmelblau
- Holder Table, holdertable
- Levi, levi
- Matyas, matyas
- Rastrigin, rastrigin
- Rosenbrock, rosenbrock
- Schaffer No 2, schaffer2
- Sphere, sphere
- Three Hump Camel, threehump
"""

# Import from __future__
Expand All @@ -45,7 +45,7 @@
import numpy as np


def ackley_func(x):
def ackley(x):
"""Ackley's objective function.
Has a global minimum of `0` at :code:`f(0,0,...,0)` with a search
Expand Down Expand Up @@ -81,7 +81,7 @@ def ackley_func(x):
return j


def beale_func(x):
def beale(x):
"""Beale objective function.
Only takes two dimensions and has a global minimum of `0` at
Expand Down Expand Up @@ -124,7 +124,7 @@ def beale_func(x):
return j


def booth_func(x):
def booth(x):
"""Booth's objective function.
Only takes two dimensions and has a global minimum of `0` at
Expand Down Expand Up @@ -161,7 +161,7 @@ def booth_func(x):
return j


def bukin6_func(x):
def bukin6(x):
"""Bukin N. 6 Objective Function
Only takes two dimensions and has a global minimum of `0` at
Expand Down Expand Up @@ -210,7 +210,7 @@ def bukin6_func(x):
return j


def crossintray_func(x):
def crossintray(x):
"""Cross-in-tray objective function.
Only takes two dimensions and has a four equal global minimums
Expand Down Expand Up @@ -264,7 +264,7 @@ def crossintray_func(x):
return j


def easom_func(x):
def easom(x):
"""Easom objective function.
Only takes two dimensions and has a global minimum of
Expand Down Expand Up @@ -312,7 +312,7 @@ def easom_func(x):
return j


def eggholder_func(x):
def eggholder(x):
"""Eggholder objective function.
Only takes two dimensions and has a global minimum of
Expand Down Expand Up @@ -359,7 +359,7 @@ def eggholder_func(x):
return j


def goldstein_func(x):
def goldstein(x):
"""Goldstein-Price's objective function.
Only takes two dimensions and has a global minimum at
Expand Down Expand Up @@ -424,7 +424,7 @@ def goldstein_func(x):
return j


def himmelblau_func(x):
def himmelblau(x):
"""Himmelblau's objective function
Only takes two dimensions and has a four equal global minimums
Expand Down Expand Up @@ -470,7 +470,7 @@ def himmelblau_func(x):
return j


def holdertable_func(x):
def holdertable(x):
"""Holder Table objective function
Only takes two dimensions and has a four equal global minimums
Expand Down Expand Up @@ -520,7 +520,7 @@ def holdertable_func(x):
return j


def levi_func(x):
def levi(x):
"""Levi objective function
Only takes two dimensions and has a global minimum at
Expand Down Expand Up @@ -569,7 +569,7 @@ def levi_func(x):
return j


def matyas_func(x):
def matyas(x):
"""Matyas objective function
Only takes two dimensions and has a global minimum at
Expand Down Expand Up @@ -599,7 +599,7 @@ def matyas_func(x):
return j


def rastrigin_func(x):
def rastrigin(x):
"""Rastrigin objective function.
Has a global minimum at :code:`f(0,0,...,0)` with a search
Expand Down Expand Up @@ -632,7 +632,7 @@ def rastrigin_func(x):
return j


def rosenbrock_func(x):
def rosenbrock(x):
"""Rosenbrock objective function.
Also known as the Rosenbrock's valley or Rosenbrock's banana
Expand All @@ -658,7 +658,7 @@ def rosenbrock_func(x):
return r


def schaffer2_func(x):
def schaffer2(x):
"""Schaffer N.2 objective function
Only takes two dimensions and has a global minimum at
Expand Down Expand Up @@ -703,7 +703,7 @@ def schaffer2_func(x):
return j


def sphere_func(x):
def sphere(x):
"""Sphere objective function.
Has a global minimum at :code:`0` and with a search domain of
Expand All @@ -724,7 +724,7 @@ def sphere_func(x):
return j


def threehump_func(x):
def threehump(x):
"""Three-hump camel objective function
Only takes two dimensions and has a global minimum of `0` at
Expand Down
Loading

0 comments on commit 251a9e1

Please sign in to comment.