-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add feature to pass args to objective function (#144)
Resolves #143 This commit adds a `**kwargs` parameter to the `optimize()` method to pass arguments directly on the objective function. Additional tests and documentation were also provided. Committed with @bradahoward Signed-off-by: Lester James V. Miranda <[email protected]>
- Loading branch information
1 parent
6bd896d
commit 89f12cb
Showing
7 changed files
with
517 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,236 @@ | ||
{ | ||
"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": 2, | ||
"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": 8, | ||
"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, c=0):\n", | ||
"\n", | ||
" f = (a - x[:, 0]) ** 2 + b * (x[:, 1] - x[:, 0] ** 2) ** 2 + c\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 ``kwargs={}`` paradigm. First lets optimize the Rosenbrock function using keyword arguments. Note in the definition of the Rosenbrock function above, there were two arguments that need to be passed other than the design variables, and one optional keyword argument, ``a``, ``b``, and ``c``, respectively" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 12, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"INFO:pyswarms.single.global_best:Arguments Passed to Objective Function: {'c': 0, 'b': 100, 'a': 1}\n", | ||
"INFO:pyswarms.single.global_best:Iteration 1/1000, cost: 1022.9667801907804\n", | ||
"INFO:pyswarms.single.global_best:Iteration 101/1000, cost: 0.0011172801146408992\n", | ||
"INFO:pyswarms.single.global_best:Iteration 201/1000, cost: 7.845605970774126e-07\n", | ||
"INFO:pyswarms.single.global_best:Iteration 301/1000, cost: 1.313503109901238e-09\n", | ||
"INFO:pyswarms.single.global_best:Iteration 401/1000, cost: 5.187079604907219e-10\n", | ||
"INFO:pyswarms.single.global_best:Iteration 501/1000, cost: 1.0115283486088853e-10\n", | ||
"INFO:pyswarms.single.global_best:Iteration 601/1000, cost: 2.329870757208421e-13\n", | ||
"INFO:pyswarms.single.global_best:Iteration 701/1000, cost: 4.826176894160183e-15\n", | ||
"INFO:pyswarms.single.global_best:Iteration 801/1000, cost: 3.125715456651088e-17\n", | ||
"INFO:pyswarms.single.global_best:Iteration 901/1000, cost: 1.4236768129666014e-19\n", | ||
"INFO:pyswarms.single.global_best:================================\n", | ||
"Optimization finished!\n", | ||
"Final cost: 0.0000\n", | ||
"Best value: [0.99999999996210465, 0.9999999999218413]\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", | ||
"\n", | ||
"cost, pos = optimizer.optimize(rosenbrock_with_args, 1000, print_step=100, verbose=3, a=1, b=100, c=0)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"It is also possible to pass a dictionary of key word arguments by using ``**`` decorator when passing the dict" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 13, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"INFO:pyswarms.single.global_best:Arguments Passed to Objective Function: {'c': 0, 'b': 100.0, 'a': 1.0}\n", | ||
"INFO:pyswarms.single.global_best:Iteration 1/1000, cost: 1.996797703363527e-21\n", | ||
"INFO:pyswarms.single.global_best:Iteration 101/1000, cost: 1.0061676299213387e-24\n", | ||
"INFO:pyswarms.single.global_best:Iteration 201/1000, cost: 4.8140236741112245e-28\n", | ||
"INFO:pyswarms.single.global_best:Iteration 301/1000, cost: 2.879342304056693e-29\n", | ||
"INFO:pyswarms.single.global_best:Iteration 401/1000, cost: 0.0\n", | ||
"INFO:pyswarms.single.global_best:Iteration 501/1000, cost: 0.0\n", | ||
"INFO:pyswarms.single.global_best:Iteration 601/1000, cost: 0.0\n", | ||
"INFO:pyswarms.single.global_best:Iteration 701/1000, cost: 0.0\n", | ||
"INFO:pyswarms.single.global_best:Iteration 801/1000, cost: 0.0\n", | ||
"INFO:pyswarms.single.global_best:Iteration 901/1000, cost: 0.0\n", | ||
"INFO:pyswarms.single.global_best:================================\n", | ||
"Optimization finished!\n", | ||
"Final cost: 0.0000\n", | ||
"Best value: [1.0, 1.0]\n", | ||
"\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"kwargs={\"a\": 1.0, \"b\": 100.0, 'c':0}\n", | ||
"cost, pos = optimizer.optimize(rosenbrock_with_args, 1000, print_step=100, verbose=3, **kwargs)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"collapsed": true | ||
}, | ||
"source": [ | ||
"Any key word arguments in the objective function can be left out as they will be passed the default as defined in the prototype. Note here, ``c`` is not passed into the function." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 14, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"INFO:pyswarms.single.global_best:Arguments Passed to Objective Function: {'b': 100, 'a': 1}\n", | ||
"INFO:pyswarms.single.global_best:Iteration 1/1000, cost: 0.0\n", | ||
"INFO:pyswarms.single.global_best:Iteration 101/1000, cost: 0.0\n", | ||
"INFO:pyswarms.single.global_best:Iteration 201/1000, cost: 0.0\n", | ||
"INFO:pyswarms.single.global_best:Iteration 301/1000, cost: 0.0\n", | ||
"INFO:pyswarms.single.global_best:Iteration 401/1000, cost: 0.0\n", | ||
"INFO:pyswarms.single.global_best:Iteration 501/1000, cost: 0.0\n", | ||
"INFO:pyswarms.single.global_best:Iteration 601/1000, cost: 0.0\n", | ||
"INFO:pyswarms.single.global_best:Iteration 701/1000, cost: 0.0\n", | ||
"INFO:pyswarms.single.global_best:Iteration 801/1000, cost: 0.0\n", | ||
"INFO:pyswarms.single.global_best:Iteration 901/1000, cost: 0.0\n", | ||
"INFO:pyswarms.single.global_best:================================\n", | ||
"Optimization finished!\n", | ||
"Final cost: 0.0000\n", | ||
"Best value: [1.0, 1.0]\n", | ||
"\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"cost, pos = optimizer.optimize(rosenbrock_with_args, 1000, print_step=100, verbose=3, a=1, b=100)" | ||
] | ||
}, | ||
{ | ||
"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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.