Created
March 31, 2018 14:11
-
-
Save antonior92/3a4a62bd78a6cd60138cbfc549193e4c to your computer and use it in GitHub Desktop.
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2018-03-31T14:03:07.443287Z", | |
"start_time": "2018-03-31T14:03:07.329356Z" | |
}, | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"from scipy.optimize import (minimize, NonlinearConstraint, \n", | |
" LinearConstraint, Bounds, BFGS, SR1)\n", | |
"import pycutestmgr as cute\n", | |
"import time\n", | |
"import numpy as np\n", | |
"from numpy.linalg import norm\n", | |
"import scipy.sparse as spc\n", | |
"import warnings\n", | |
"cute.clearCache('HS71')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2018-03-31T14:03:07.483645Z", | |
"start_time": "2018-03-31T14:03:07.444795Z" | |
}, | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def print_header():\n", | |
" print(\"|{0:^10}|{1:^10}|{2:^10}|{3:^10}|{4:^10}|{5:^10}|\"\n", | |
" .format(\"name\", \"Analit\", \"SR1\", \"BFGS(skip)\", \"BFGS(damp)\", \"2-point\"))\n", | |
" s = \"-\"*9 + \":\"\n", | |
" print(\"|{0:^10}|{1:^10}|{2:^10}|{3:^10}|{4:^10}|{5:^10}|\"\n", | |
" .format(s, s, s, s, s, s))\n", | |
"\n", | |
"def print_problem_sol(name, \n", | |
" niters, opt, c_viol, \n", | |
" niters_sr1, opt_sr1, c_viol_sr1, \n", | |
" niters_bfgs_s, opt_bfgs_s, c_viol_bfgs_s, \n", | |
" niters_bfgs_d, opt_bfgs_d, c_viol_bfgs_d, \n", | |
" niters_fd, opt_fd, c_viol_fd):\n", | |
" if c_viol > 1e-6 or opt > 1e-6 or niters > 1000:\n", | |
" niters = 'x' \n", | |
" if c_viol_sr1 > 1e-6 or opt_sr1 > 1e-6 or niters_sr1 > 1000:\n", | |
" niters_sr1 = 'x'\n", | |
" if c_viol_bfgs_s > 1e-6 or opt_bfgs_s > 1e-6 or niters_bfgs_s > 1000:\n", | |
" niters_bfgs_s = 'x' \n", | |
" if c_viol_bfgs_d > 1e-6 or opt_bfgs_d > 1e-6 or niters_bfgs_d > 1000:\n", | |
" niters_bfgs_d = 'x' \n", | |
" if c_viol_fd > 1e-6 or opt_fd > 1e-6 or niters_fd > 1000:\n", | |
" niters_fd = 'x' \n", | |
" \n", | |
" print(\"|{0:^10}|{1:^10}|{2:^10}|{3:^10}|{4:^10}|{5:^10}|\"\n", | |
" .format(name, niters, niters_sr1, niters_bfgs_s, niters_bfgs_d, niters_fd))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2018-03-31T14:03:07.570261Z", | |
"start_time": "2018-03-31T14:03:07.485132Z" | |
}, | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"problems_si = [(\"HS7\", {}, {}),\n", | |
" (\"HS10\", {}, {}),\n", | |
" (\"HS11\", {}, {}),\n", | |
" (\"HS13\", {}, {}),\n", | |
" (\"HS14\", {}, {}),\n", | |
" (\"HS16\", {}, {}),\n", | |
" (\"HS17\", {}, {}),\n", | |
" (\"HS19\", {}, {}),\n", | |
" (\"HS20\", {}, {}),\n", | |
" (\"HS22\", {}, {}),\n", | |
" (\"HS24\", {}, {}),\n", | |
" (\"HS26\", {}, {}),\n", | |
" (\"HS28\", {}, {}),\n", | |
" (\"HS31\", {}, {}),\n", | |
" (\"HS32\", {}, {}),\n", | |
" (\"HS33\", {}, {}),\n", | |
" (\"HS39\", {}, {}),\n", | |
" (\"HS46\", {}, {}),\n", | |
" (\"HS51\", {}, {}),\n", | |
" (\"HS52\", {}, {}),\n", | |
" (\"HS53\", {}, {}),\n", | |
" (\"HS63\", {}, {}),\n", | |
" (\"HS64\", {}, {}),\n", | |
" (\"HS65\", {}, {}),\n", | |
" (\"HS70\", {}, {}),\n", | |
" (\"HS71\", {}, {}),\n", | |
" (\"HS72\", {}, {}),\n", | |
" (\"HS73\", {}, {}),\n", | |
" (\"HS74\", {}, {}),\n", | |
" (\"HS75\", {}, {}),\n", | |
" (\"HS77\", {}, {}),\n", | |
" (\"HS78\", {}, {}),\n", | |
" (\"HS79\", {}, {}),\n", | |
" (\"HS80\", {}, {}),\n", | |
" (\"HS81\", {}, {}),\n", | |
" (\"HS83\", {}, {}),\n", | |
" (\"HS84\", {}, {}),\n", | |
" (\"HS85\", {}, {\"initial_barrier_parameter\": 0.001}),\n", | |
" (\"HS86\", {}, {}),\n", | |
" (\"HS93\", {}, {}),\n", | |
" (\"HS95\", {}, {}),\n", | |
" (\"HS96\", {}, {}),\n", | |
" (\"HS97\", {}, {\"initial_barrier_parameter\": 100}),\n", | |
" (\"HS98\", {}, {\"initial_barrier_parameter\": 100}),\n", | |
" (\"HS99\", {}, {}), # Fails (As in the original paper)\n", | |
" (\"HS100\", {}, {}),\n", | |
" (\"HS104\", {}, {\"initial_barrier_parameter\": 100}),\n", | |
" (\"HS105\", {}, {}),\n", | |
" (\"HS106\", {}, {}),\n", | |
" (\"HS107\", {}, {}),\n", | |
" (\"HS108\", {}, {}),\n", | |
" (\"HS109\", {}, {}),\n", | |
" (\"HS111\", {}, {}),\n", | |
" (\"HS112\", {}, {\"initial_barrier_parameter\": 10}),\n", | |
" (\"HS113\", {}, {}),\n", | |
" (\"HS114\", {}, {\"initial_barrier_parameter\": 10}),\n", | |
" (\"HS116\", {}, {}),\n", | |
" (\"HS117\", {}, {}),\n", | |
" (\"HS118\", {}, {}),\n", | |
" (\"HS119\", {}, {})]\n", | |
"other = [(\"HS99\", {}, {}), ]\n", | |
"\n", | |
"list_feasible_box_constr = [\"HS13\", \"HS105\"]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2018-03-31T14:07:27.723552Z", | |
"start_time": "2018-03-31T14:03:07.571814Z" | |
} | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"| name | Analit | SR1 |BFGS(skip)|BFGS(damp)| 2-point |\n", | |
"|---------:|---------:|---------:|---------:|---------:|---------:|\n", | |
"| HS7 | 9 | 9 | 9 | 9 | 9 |\n", | |
"| HS10 | 15 | 36 | 36 | 36 | 15 |\n", | |
"| HS11 | 12 | 12 | 17 | 17 | 12 |\n", | |
"| HS13 | 1 | 1 | 1 | 1 | 1 |\n", | |
"| HS14 | 10 | 10 | 10 | 10 | 10 |\n", | |
"| HS16 | 13 | 12 | 15 | 14 | 13 |\n", | |
"| HS17 | 25 | 28 | 41 | 53 | 25 |\n", | |
"| HS19 | 36 | 37 | 39 | 38 | 36 |\n", | |
"| HS20 | 10 | 18 | 15 | 17 | 10 |\n", | |
"| HS22 | 14 | 14 | 14 | 14 | 14 |\n", | |
"| HS24 | 11 | 18 | 24 | 29 | 11 |\n", | |
"| HS26 | 22 | 32 | x | 676 | 22 |\n", | |
"| HS28 | 3 | 51 | 52 | 52 | 3 |\n", | |
"| HS31 | 12 | 12 | 24 | 30 | 12 |\n", | |
"| HS32 | 15 | 17 | 18 | 15 | 15 |\n", | |
"| HS33 | 17 | 22 | 62 | 49 | 17 |\n", | |
"| HS39 | 16 | 21 | 18 | 18 | 16 |\n", | |
"| HS46 | 29 | 31 | 415 | 50 | 29 |\n", | |
"| HS51 | 3 | 19 | 19 | 19 | 3 |\n", | |
"| HS52 | 3 | 21 | 23 | 23 | 4 |\n", | |
"| HS53 | 17 | 28 | 29 | 29 | 17 |\n", | |
"| HS63 | 13 | 14 | 83 | 30 | 13 |\n", | |
"| HS64 | 34 | 43 | 535 | 47 | 34 |\n", | |
"| HS65 | 22 | 20 | 28 | 33 | 22 |\n", | |
"| HS70 | 46 | 72 | 40 | 43 | 38 |\n", | |
"| HS71 | 17 | 20 | 161 | 31 | 17 |\n", | |
"| HS72 | 26 | x | x | x | 26 |\n", | |
"| HS73 | 18 | 18 | 21 | 21 | 18 |\n", | |
"| HS74 | 18 | 24 | 27 | 37 | 18 |\n", | |
"| HS75 | 102 | 95 | 115 | 22 | 102 |\n", | |
"| HS77 | 18 | 26 | 24 | 20 | 18 |\n", | |
"| HS78 | 5 | 8 | 10 | 10 | 5 |\n", | |
"| HS79 | 7 | 11 | 67 | 15 | 7 |\n", | |
"| HS80 | 17 | 17 | 16 | 15 | 17 |\n", | |
"| HS81 | 17 | 16 | 16 | 30 | 17 |\n", | |
"| HS83 | 42 | 30 | 27 | 27 | 43 |\n", | |
"| HS84 | 12 | 13 | x | 17 | 12 |\n", | |
"| HS85 | 133 | 121 | 173 | 150 | 133 |\n", | |
"| HS86 | 16 | 24 | 26 | 31 | 16 |\n", | |
"| HS93 | 22 | 42 | 236 | x | 22 |\n", | |
"| HS95 | 167 | 43 | 279 | 225 | 179 |\n", | |
"| HS96 | 213 | 99 | 112 | 59 | 208 |\n", | |
"| HS97 | 539 | 606 | 991 | x | 620 |\n", | |
"| HS98 | 385 | 396 | 409 | 900 | 425 |\n", | |
"| HS99 | x | x | x | x | x |\n", | |
"| HS100 | 20 | 22 | 33 | 33 | 20 |\n", | |
"| HS104 | 43 | 78 | 92 | 92 | 43 |\n", | |
"| HS105 | 56 | 493 | 700 | 762 | 64 |\n", | |
"| HS106 | 261 | x | x | x | 243 |\n", | |
"| HS107 | 12 | 17 | 22 | 31 | 12 |\n", | |
"| HS108 | 57 | 188 | x | x | 57 |\n", | |
"| HS109 | x | x | x | x | x |\n", | |
"| HS111 | 27 | x | x | x | 27 |\n", | |
"| HS112 | 37 | 42 | 43 | 52 | 37 |\n", | |
"| HS113 | 33 | 24 | 41 | 41 | 27 |\n", | |
"| HS114 | 86 | 110 | 94 | 74 | 93 |\n", | |
"| HS116 | 131 | x | x | x | 111 |\n", | |
"| HS117 | 28 | 72 | 505 | x | 33 |\n", | |
"| HS118 | 55 | 892 | 892 | 892 | 55 |\n", | |
"| HS119 | 24 | 56 | 48 | 51 | 24 |\n" | |
] | |
} | |
], | |
"source": [ | |
"print_header()\n", | |
"\n", | |
"default_options = {'sparse_jacobian':True, 'maxiter':1000, 'xtol':1e-7, 'gtol':1e-7}\n", | |
"\n", | |
"\n", | |
"for prob in problems_si:\n", | |
" name = prob[0]\n", | |
" sifParams = prob[1]\n", | |
" options = prob[2] \n", | |
" options = prob[2].copy()\n", | |
" options.update(default_options)\n", | |
" cute.clearCache(name)\n", | |
" cute.prepareProblem(name, sifParams=sifParams)\n", | |
" problem = cute.importProblem(name)\n", | |
" info = problem.getinfo()\n", | |
" x0 = info[\"x\"]\n", | |
" n = info[\"n\"]\n", | |
" m = info[\"m\"]\n", | |
" nnz = info[\"nnzj\"]\n", | |
" v0 = info[\"v\"]\n", | |
" eq_list = info[\"equatn\"]\n", | |
"\n", | |
" def convert_to_inf(bounds):\n", | |
" new_bounds = np.zeros_like(bounds)\n", | |
" i = 0\n", | |
" for b in bounds:\n", | |
" if b >= 1e20:\n", | |
" new_bounds[i] = np.inf\n", | |
" elif b <= -1e20:\n", | |
" new_bounds[i] = -np.inf\n", | |
" else:\n", | |
" new_bounds[i] = b\n", | |
" i += 1\n", | |
" return new_bounds\n", | |
"\n", | |
" # Define upper and lower bound\n", | |
" c_lb = convert_to_inf(info[\"cl\"])\n", | |
" c_ub = convert_to_inf(info[\"cu\"])\n", | |
" lb = convert_to_inf(info[\"bl\"])\n", | |
" ub = convert_to_inf(info[\"bu\"])\n", | |
" if name in list_feasible_box_constr:\n", | |
" x0 = np.maximum(lb+1e-10, np.minimum(ub-1e-10, x0))\n", | |
"\n", | |
" # Define function and constraints\n", | |
" def fun(x):\n", | |
" return problem.obj(x)[0]\n", | |
"\n", | |
" def grad(x):\n", | |
" _, g1 = problem.obj(x, True)\n", | |
" return g1\n", | |
"\n", | |
" def lagr_hess(x, v):\n", | |
" def matvec(p):\n", | |
" return problem.hprod(p, x, v)\n", | |
" return spc.linalg.LinearOperator((n, n), matvec=matvec)\n", | |
"\n", | |
"\n", | |
" def constr(x):\n", | |
" return problem.cons(x)\n", | |
"\n", | |
" def jac(x):\n", | |
" _, A1 = problem.cons(x, True)\n", | |
" return spc.csc_matrix(A1)\n", | |
" \n", | |
" def hess(x):\n", | |
" return spc.csc_matrix((n, n))\n", | |
" \n", | |
"\n", | |
" # Define constraints\n", | |
" box = Bounds(lb, ub, name in list_feasible_box_constr)\n", | |
" constr_analit = NonlinearConstraint(constr, c_lb, c_ub, jac, lagr_hess)\n", | |
" constr_sr1 = NonlinearConstraint(constr, c_lb, c_ub, jac, SR1())\n", | |
" constr_bfgs_s = NonlinearConstraint(constr, c_lb, c_ub, jac, BFGS(exception_strategy='skip_update'))\n", | |
" constr_bfgs_d = NonlinearConstraint(constr, c_lb, c_ub, jac, BFGS(exception_strategy='damp_update'))\n", | |
" constr_fd = NonlinearConstraint(constr, c_lb, c_ub, jac, '2-point')\n", | |
" \n", | |
" \n", | |
"\n", | |
" with warnings.catch_warnings():\n", | |
" warnings.simplefilter(\"ignore\")\n", | |
" result = minimize(fun, x0, method='trust-constr', jac=grad, hess=hess, constraints=constr_analit, \n", | |
" bounds=box, options=options)\n", | |
" result_sr1 = minimize(fun, x0, method='trust-constr', jac=grad, hess=SR1(), constraints=constr_sr1, \n", | |
" bounds=box, options=options)\n", | |
" result_bfgs_s = minimize(fun, x0, method='trust-constr', jac=grad, hess=BFGS(exception_strategy='skip_update'),\n", | |
" constraints=constr_bfgs_s, bounds=box, options=options)\n", | |
" result_bfgs_d = minimize(fun, x0, method='trust-constr', jac=grad, hess=BFGS(exception_strategy='damp_update'),\n", | |
" constraints=constr_bfgs_d, bounds=box, options=options)\n", | |
" result_fd = minimize(fun, x0, method='trust-constr', jac=grad, hess='2-point',\n", | |
" constraints=constr_fd, bounds=box, options=options)\n", | |
" \n", | |
"\n", | |
" # Print Results\n", | |
" print_problem_sol(name, \n", | |
" result.niter, result.optimality, result.constr_violation,\n", | |
" result_sr1.niter, result_sr1.optimality, result_sr1.constr_violation,\n", | |
" result_bfgs_s.niter, result_bfgs_s.optimality, result_bfgs_s.constr_violation,\n", | |
" result_bfgs_d.niter, result_bfgs_d.optimality, result_bfgs_d.constr_violation,\n", | |
" result_fd.niter, result_fd.optimality, result_fd.constr_violation)" | |
] | |
} | |
], | |
"metadata": { | |
"hide_input": false, | |
"kernelspec": { | |
"display_name": "scipy-devel", | |
"language": "python", | |
"name": "scipy-devel" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.13" | |
}, | |
"latex_envs": { | |
"bibliofile": "biblio.bib", | |
"cite_by": "apalike", | |
"current_citInitial": 1, | |
"eqLabelWithNumbers": true, | |
"eqNumInitial": 0 | |
}, | |
"toc": { | |
"colors": { | |
"hover_highlight": "#DAA520", | |
"navigate_num": "#000000", | |
"navigate_text": "#333333", | |
"running_highlight": "#FF0000", | |
"selected_highlight": "#FFD700", | |
"sidebar_border": "#EEEEEE", | |
"wrapper_background": "#FFFFFF" | |
}, | |
"moveMenuLeft": true, | |
"nav_menu": { | |
"height": "12px", | |
"width": "252px" | |
}, | |
"navigate_menu": true, | |
"number_sections": true, | |
"sideBar": true, | |
"threshold": 4, | |
"toc_cell": false, | |
"toc_section_display": "block", | |
"toc_window_display": true, | |
"widenNotebook": false | |
}, | |
"varInspector": { | |
"cols": { | |
"lenName": 16, | |
"lenType": 16, | |
"lenVar": 40 | |
}, | |
"kernels_config": { | |
"python": { | |
"delete_cmd_postfix": "", | |
"delete_cmd_prefix": "del ", | |
"library": "var_list.py", | |
"varRefreshCmd": "print(var_dic_list())" | |
}, | |
"r": { | |
"delete_cmd_postfix": ") ", | |
"delete_cmd_prefix": "rm(", | |
"library": "var_list.r", | |
"varRefreshCmd": "cat(var_dic_list()) " | |
} | |
}, | |
"types_to_exclude": [ | |
"module", | |
"function", | |
"builtin_function_or_method", | |
"instance", | |
"_Feature" | |
], | |
"window_display": false | |
}, | |
"widgets": { | |
"state": {}, | |
"version": "1.1.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment