Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature parameterize obj #144

Merged
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
200 changes: 200 additions & 0 deletions examples/basic_optimization_with_arguments.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Basic Optimization with Arguments"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here, we will run a basic optimization using an objective function that needs parameterization. We will use the ``single.GBestPSO`` and a version of the rosenbrock function to demonstrate"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Running Python 3.5.2 |Anaconda custom (64-bit)| (default, Jul 2 2016, 17:53:06) \n",
"[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]\n"
]
}
],
"source": [
"import sys\n",
"# change directory to access pyswarms\n",
"sys.path.append('../')\n",
"\n",
"print(\"Running Python {}\".format(sys.version))"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [],
"source": [
"# import modules\n",
"import numpy as np\n",
"\n",
"# create a parameterized version of the classic Rosenbrock unconstrained optimzation function\n",
"def rosenbrock_with_args(x, a, b):\n",
"\n",
" f = (a - x[:, 0]) ** 2 + b * (x[:, 1] - x[:, 0] ** 2) ** 2\n",
" return f"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Using Arguments"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Arguments can either be passed in using a tuple or a dictionary, using the ``args=()`` and ``kwargs={}`` paradigm. First lets optimize the Rosenbrock function using args. Note in the definition of the Rosenbrock function above, there were two arguments that need to be passed other than the design variables, ``a``, and ``b``."
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"INFO:pyswarms.single.global_best:Iteration 1/1000, cost: 17849.1066764\n",
"INFO:pyswarms.single.global_best:Iteration 101/1000, cost: 0.0127720296794\n",
"INFO:pyswarms.single.global_best:Iteration 201/1000, cost: 0.00118393018803\n",
"INFO:pyswarms.single.global_best:Iteration 301/1000, cost: 1.93661349444e-06\n",
"INFO:pyswarms.single.global_best:Iteration 401/1000, cost: 7.54187359817e-08\n",
"INFO:pyswarms.single.global_best:Iteration 501/1000, cost: 1.56235319713e-10\n",
"INFO:pyswarms.single.global_best:Iteration 601/1000, cost: 7.2848456422e-11\n",
"INFO:pyswarms.single.global_best:Iteration 701/1000, cost: 5.93891975854e-11\n",
"INFO:pyswarms.single.global_best:Iteration 801/1000, cost: 4.24299200168e-11\n",
"INFO:pyswarms.single.global_best:Iteration 901/1000, cost: 3.70662128431e-11\n",
"INFO:pyswarms.single.global_best:================================\n",
"Optimization finished!\n",
"Final cost: 0.0000\n",
"Best value: [0.99999402891984257, 0.99998804008671482]\n",
"\n"
]
}
],
"source": [
"from pyswarms.single.global_best import GlobalBestPSO\n",
"\n",
"# instatiate the optimizer\n",
"x_max = 10 * np.ones(2)\n",
"x_min = -1 * x_max\n",
"bounds = (x_min, x_max)\n",
"options = {'c1': 0.5, 'c2': 0.3, 'w': 0.9}\n",
"optimizer = GlobalBestPSO(n_particles=10, dimensions=2, options=options, bounds=bounds)\n",
"\n",
"# now run the optimization, pass a=1 and b=100 as a tuple assigned to args\n",
"args = (1.0, 100.0)\n",
"cost, pos = optimizer.optimize(rosenbrock_with_args, iters=1000, args=args, print_step=100, verbose=3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now lets use kwargs to parameterize the model instead."
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"INFO:pyswarms.single.global_best:Iteration 1/1000, cost: 3.56818109307e-11\n",
"INFO:pyswarms.single.global_best:Iteration 101/1000, cost: 3.55379244278e-11\n",
"INFO:pyswarms.single.global_best:Iteration 201/1000, cost: 3.5520228268e-11\n",
"INFO:pyswarms.single.global_best:Iteration 301/1000, cost: 3.55181104999e-11\n",
"INFO:pyswarms.single.global_best:Iteration 401/1000, cost: 3.55171536074e-11\n",
"INFO:pyswarms.single.global_best:Iteration 501/1000, cost: 3.55165535881e-11\n",
"INFO:pyswarms.single.global_best:Iteration 601/1000, cost: 3.55163343941e-11\n",
"INFO:pyswarms.single.global_best:Iteration 701/1000, cost: 3.55162583716e-11\n",
"INFO:pyswarms.single.global_best:Iteration 801/1000, cost: 3.55162357208e-11\n",
"INFO:pyswarms.single.global_best:Iteration 901/1000, cost: 3.55162279771e-11\n",
"INFO:pyswarms.single.global_best:================================\n",
"Optimization finished!\n",
"Final cost: 0.0000\n",
"Best value: [0.99999404111206025, 0.99998807338372564]\n",
"\n"
]
}
],
"source": [
"kwargs={\"a\": 1.0, \"b\": 100.0}\n",
"cost, pos = optimizer.optimize(rosenbrock_with_args, iters=1000, kwargs=kwargs, print_step=100, verbose=3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [conda env:anaconda3]",
"language": "python",
"name": "conda-env-anaconda3-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
2 changes: 1 addition & 1 deletion pyswarms/base/base_discrete.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def _populate_history(self, hist):
self.pos_history.append(hist.position)
self.velocity_history.append(hist.velocity)

def optimize(self, objective_func, iters, print_step=1, verbose=1):
def optimize(self, objective_func, iters, args=(), kwargs={}, print_step=1, verbose=1):
"""Optimizes the swarm for a number of iterations.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe *args and **kwargs will work here instead of args=() and kwargs={}?


Performs the optimization to evaluate the objective
Expand Down
2 changes: 1 addition & 1 deletion pyswarms/base/base_single.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def _populate_history(self, hist):
self.pos_history.append(hist.position)
self.velocity_history.append(hist.velocity)

def optimize(self, objective_func, iters, print_step=1, verbose=1):
def optimize(self, objective_func, iters, args=(), kwargs={}, print_step=1, verbose=1):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe *args and **kwargs will work here instead of args=() and kwargs={}?

"""Optimizes the swarm for a number of iterations.

Performs the optimization to evaluate the objective
Expand Down
9 changes: 6 additions & 3 deletions pyswarms/discrete/binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def __init__(
# Initialize the topology
self.top = Ring()

def optimize(self, objective_func, iters, print_step=1, verbose=1):
def optimize(self, objective_func, iters, args=(), kwargs={}, print_step=1, verbose=1):
"""Optimizes the swarm for a number of iterations.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe *args and **kwargs will work here instead of args=() and kwargs={}?


Performs the optimization to evaluate the objective
Expand All @@ -172,10 +172,13 @@ def optimize(self, objective_func, iters, print_step=1, verbose=1):
the local best cost and the local best position among the
swarm.
"""
cli_print("Arguments Passed to Objective Function: \nargs: %s \nkwargs: %s\n" % (args, kwargs),
verbose, 2, logger=self.logger)

for i in range(iters):
# Compute cost for current position and personal best
self.swarm.current_cost = objective_func(self.swarm.position)
self.swarm.pbest_cost = objective_func(self.swarm.pbest_pos)
self.swarm.current_cost = objective_func(self.swarm.position, *args, **kwargs)
self.swarm.pbest_cost = objective_func(self.swarm.pbest_pos, *args, **kwargs)
self.swarm.pbest_pos, self.swarm.pbest_cost = compute_pbest(
self.swarm
)
Expand Down
14 changes: 11 additions & 3 deletions pyswarms/single/global_best.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def __init__(
# Initialize the topology
self.top = Star()

def optimize(self, objective_func, iters, print_step=1, verbose=1):
def optimize(self, objective_func, iters, args=(), kwargs={}, print_step=1, verbose=1):
"""Optimizes the swarm for a number of iterations.

Performs the optimization to evaluate the objective
Expand All @@ -143,6 +143,10 @@ def optimize(self, objective_func, iters, print_step=1, verbose=1):
objective function to be evaluated
iters : int
number of iterations
args : tuple
arguments for the objective function
kwargs : dict
arguments for the objective function
print_step : int (default is 1)
amount of steps for printing into console.
verbose : int (default is 1)
Expand All @@ -153,10 +157,14 @@ def optimize(self, objective_func, iters, print_step=1, verbose=1):
tuple
the global best cost and the global best position.
"""

cli_print("Arguments Passed to Objective Function: \nargs: %s \nkwargs: %s\n" % (args, kwargs),
verbose, 2, logger=self.logger)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good, keep this. I'd suggest using:

cli_print("Arguments passed to objective function: \nargs: %s \nkwargs: %s\n" % (*args, **kwargs),
                  verbose, 2, logger=self.logger)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears that using ** outside of a function call throws an IndexError. The Python documentation around this uses them without the decorators in the outside of the function call.
https://docs.python.org/dev/tutorial/controlflow.html#more-on-defining-functions
Thoughts?

Copy link
Owner

@ljvmiranda921 ljvmiranda921 Jun 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops. Yes, you are correct, just remove ** and it should print. One last Python 3.X thing, perhaps a .format() is better?

name = 'ljvmiranda921'
print("Hi my name is: {}".format(name))

I'd probably update my cli_print soon to make everything uniform.

for i in range(iters):
# Compute cost for current position and personal best
self.swarm.current_cost = objective_func(self.swarm.position)
self.swarm.pbest_cost = objective_func(self.swarm.pbest_pos)
self.swarm.current_cost = objective_func(self.swarm.position, *args, **kwargs)
self.swarm.pbest_cost = objective_func(self.swarm.pbest_pos, *args, **kwargs)
self.swarm.pbest_pos, self.swarm.pbest_cost = compute_pbest(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion, this is just the modification we need. That is, to provide flexibility in the objective_function by providing an optional *args or **kwargs argument. There's no need to put args = () or kwargs = {} in the main optimize() method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! I fixed the optimize prototypes. The only goofy thing there is the *args had to be listed before the other key word arguments i.e. def fun(foo, bar, *args, baz=3, **kwargs)

self.swarm
)
Expand Down
13 changes: 10 additions & 3 deletions pyswarms/single/local_best.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def __init__(
# Initialize the topology
self.top = Ring()

def optimize(self, objective_func, iters, print_step=1, verbose=1):
def optimize(self, objective_func, iters, args=(), kwargs={}, print_step=1, verbose=1):
"""Optimizes the swarm for a number of iterations.

Performs the optimization to evaluate the objective
Expand All @@ -186,6 +186,10 @@ def optimize(self, objective_func, iters, print_step=1, verbose=1):
objective function to be evaluated
iters : int
number of iterations
args : tuple
arguments for the objective function
kwargs : dict
arguments for the objective function
print_step : int (default is 1)
amount of steps for printing into console.
verbose : int (default is 1)
Expand All @@ -197,10 +201,13 @@ def optimize(self, objective_func, iters, print_step=1, verbose=1):
the local best cost and the local best position among the
swarm.
"""
cli_print("Arguments Passed to Objective Function: \nargs: %s \nkwargs: %s\n" % (args, kwargs),
verbose, 2, logger=self.logger)

for i in range(iters):
# Compute cost for current position and personal best
self.swarm.current_cost = objective_func(self.swarm.position)
self.swarm.pbest_cost = objective_func(self.swarm.pbest_pos)
self.swarm.current_cost = objective_func(self.swarm.position, *args, **kwargs)
self.swarm.pbest_cost = objective_func(self.swarm.pbest_pos, *args, **kwargs)
self.swarm.pbest_pos, self.swarm.pbest_cost = compute_pbest(
self.swarm
)
Expand Down
13 changes: 11 additions & 2 deletions pyswarms/utils/environments/plot_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def assertions(self):
"Missing methods in optimizer, check " "pyswarms.base module"
)

def __init__(self, optimizer, objective_func, iters):
def __init__(self, optimizer, objective_func, iters, obj_args=(), obj_kwargs={}):
"""Runs the optimizer against an objective function for a number
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to touch this 👍 This will be Deprecated soon. Simply revert this to the original to avoid merge conflicts down the road.

of iterations

Expand All @@ -125,17 +125,26 @@ def __init__(self, optimizer, objective_func, iters):
iters : int
The number of iterations to run the optimizer. This argument
is passed to the :code:`optimize` method of the :code:`optimizer`.
obj_args : tuple
Tuple of arguments to be passed into the objective function for
parameterization.
obj_kwargs : dict
dictionary of kwargs to be passed into the objective function for
parameterization.
"""
self.logger = logging.getLogger(__name__)
# Store the arguments
self.optimizer = optimizer
self.objective_func = objective_func
self.iters = iters
self.obj_args = obj_args
self.obj_kwargs = obj_kwargs
# Check assertions
self.assertions()
# Run the optimizer
self.optimizer.reset()
self.status = self.optimizer.optimize(objective_func, iters, 1, 0)
self.status = self.optimizer.optimize(objective_func, iters, args=obj_args,
kwargs=obj_kwargs, print_step=1, verbose=0)
# Initialize tuples for particle plotting
self.Index = namedtuple("Index", ["x", "y", "z"])
self.Limit = namedtuple("Limit", ["x", "y", "z"])
Expand Down
11 changes: 10 additions & 1 deletion pyswarms/utils/search/base_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ def __init__(
options,
objective_func,
iters,
obj_args=(),
obj_kwargs={},
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to avoid obj_args=() and obj_kwargs={}? If not, just revert this to the original. I'm planning to update its API soon

bounds=None,
velocity_clamp=(0, 1),
):
Expand Down Expand Up @@ -66,6 +68,10 @@ def __init__(
objective function to be evaluated
iters: int
number of iterations
obj_args : tuple
tuple of arguments for objective function parameterization.
obj_kwargs : dict
dict of arguments for objective function parameterization.
bounds : tuple of np.ndarray, optional (default is None)
a tuple of size 2 where the first entry is the minimum bound
while the second entry is the maximum bound. Each array must
Expand All @@ -85,6 +91,8 @@ def __init__(
self.vclamp = velocity_clamp
self.objective_func = objective_func
self.iters = iters
self.obj_args = obj_args
self.obj_kwargs = obj_kwargs
# Invoke assertions
self.assertions()

Expand All @@ -104,7 +112,8 @@ def generate_score(self, options):
)

# Return score
return f.optimize(self.objective_func, self.iters)[0]
return f.optimize(self.objective_func, self.iters,
args=self.obj_args, kwargs=self.obj_kwargs)[0]

def search(self, maximum=False):
"""Compares optimizer's objective function performance scores
Expand Down
Loading