-
Notifications
You must be signed in to change notification settings - Fork 335
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
Changes from 8 commits
47bd5d5
9a4c85a
1c9a065
971de8b
685f12d
35344e1
05ded93
31abd82
86f843d
aa71a24
227295e
f3bb96b
c9f3c42
3695eca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe |
||
"""Optimizes the swarm for a number of iterations. | ||
|
||
Performs the optimization to evaluate the objective | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe |
||
|
||
Performs the optimization to evaluate the objective | ||
|
@@ -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 | ||
) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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) | ||
|
@@ -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) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is good, keep this. I'd suggest using:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It appears that using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whoops. Yes, you are correct, just remove name = 'ljvmiranda921'
print("Hi my name is: {}".format(name)) I'd probably update my |
||
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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
self.swarm | ||
) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
||
|
@@ -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"]) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,8 @@ def __init__( | |
options, | ||
objective_func, | ||
iters, | ||
obj_args=(), | ||
obj_kwargs={}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to avoid |
||
bounds=None, | ||
velocity_clamp=(0, 1), | ||
): | ||
|
@@ -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 | ||
|
@@ -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() | ||
|
||
|
@@ -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 | ||
|
There was a problem hiding this comment.
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 ofargs=()
andkwargs={}
?