{"id":1867,"date":"2023-06-28T00:33:26","date_gmt":"2023-06-28T07:33:26","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=1867"},"modified":"2023-06-28T00:33:26","modified_gmt":"2023-06-28T07:33:26","slug":"351-optimization-using-scipy","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2023\/06\/351-optimization-using-scipy\/","title":{"rendered":"#351 Optimization using SciPy"},"content":{"rendered":"<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom scipy.optimize import linprog\r\n# set up cost list with cost function coefficient values\r\nc = &#x5B;-2,-3]\r\n# set up constraint coefficient matrix A\r\nA_ub = &#x5B;&#x5B;1,1],&#x5B;2,1]]\r\n# constraint list for upper bounds (less than or equal constraints)\r\nb_ub =&#x5B;10,15]\r\n# in addition, i need to prepare a bounds tuple for each\r\n# optimization variable and summarize them a list\r\nx1_bounds = (0,None)\r\nx2_bounds = (0,None)\r\n# now I use SciPy.optimize.linprog to model and solve the problem at hand\r\nmodel_linear = linprog(c=c,A_ub=A_ub,b_ub=b_ub,bounds=&#x5B;x1_bounds,x2_bounds])\r\n# output model solution\r\nprint(str(model_linear))\r\n#Results:\r\n# message: Optimization terminated successfully. (HiGHS Status 7: Optimal)\r\n# success: True\r\n# status: 0\r\n# fun: -30.0\r\n# x: &#x5B; 0.000e+00  1.000e+01]\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>from scipy.optimize import linprog # set up cost list with cost function coefficient values c = &#x5B;-2,-3] # set up constraint coefficient matrix A A_ub = &#x5B;&#x5B;1,1],&#x5B;2,1]] # constraint list for upper bounds (less than or equal constraints) b_ub =&#x5B;10,15] # in addition, i need to prepare a bounds tuple for each # optimization variable [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_et_pb_use_builder":"off","_et_pb_old_content":"","_et_gb_content_width":"","_lmt_disableupdate":"yes","_lmt_disable":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[2,71],"tags":[],"class_list":["post-1867","post","type-post","status-publish","format-standard","hentry","category-python","category-scipy"],"modified_by":"gantovnik","jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-u7","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":495,"url":"https:\/\/gantovnik.com\/bio-tips\/2020\/05\/optimization-with-constraints\/","url_meta":{"origin":1867,"position":0},"title":"#72 Optimization with constraints using scipy in python","author":"gantovnik","date":"2020-05-03","format":false,"excerpt":"[code language=\"python\"] import os from scipy.optimize import minimize import matplotlib.pyplot as plt import numpy as np os.chdir(r'D:\\projects\\wordpress\\ex72') os.getcwd() def f(X): x, y = X return (x - 1)2 + (y - 1)2 def func_X_Y_to_XY(f, X, Y): #Wrapper for f(X, Y) -> f([X, Y]) s = np.shape(X) return f(np.vstack([X.ravel(), Y.ravel()])).reshape(*s) x_opt\u2026","rel":"","context":"In &quot;optimization&quot;","block_context":{"text":"optimization","link":"https:\/\/gantovnik.com\/bio-tips\/category\/optimization\/"},"img":{"alt_text":"ex72","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2020\/05\/ex72.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2020\/05\/ex72.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2020\/05\/ex72.png?resize=525%2C300 1.5x"},"classes":[]},{"id":117,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/optimization-with-contraints\/","url_meta":{"origin":1867,"position":1},"title":"#22: Optimization with constraints using SciPy in python","author":"gantovnik","date":"2019-01-03","format":false,"excerpt":"[code language=\"python\"] import os import matplotlib.pyplot as plt import numpy as np from scipy.optimize import minimize os.chdir(r'D:\\data\\scripts\\web1\\ex22') os.getcwd() def f(X): return (X[0]-1)2 + (X[1]-1)2 def g(X): return X[1]-1.75-(X[0]-0.75)**4 def func_X_Y_to_XY(f, X, Y): s = np.shape(X) return f(np.vstack([X.ravel(), Y.ravel()])).reshape(*s) x_opt=minimize(f,(0,0),method='BFGS').x print(x_opt) constraints = [dict(type='ineq', fun=g)] x_cons_opt = minimize(f, (0, 0), method='SLSQP',\u2026","rel":"","context":"In &quot;optimization&quot;","block_context":{"text":"optimization","link":"https:\/\/gantovnik.com\/bio-tips\/category\/optimization\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example22.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example22.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example22.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1758,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/01\/335-solution-of-nonlinear-equation-using-brent-method-in-python\/","url_meta":{"origin":1867,"position":2},"title":"#335 Solution of nonlinear equation by Brent&#8217;s method in python","author":"gantovnik","date":"2023-01-06","format":false,"excerpt":"Brent's method is a hybrid root-finding algorithm combining the bisection method, the secant method, and inverse quadratic interpolation. ex335.py [code language=\"python\"] import numpy as np import matplotlib.pyplot as plt import scipy.optimize as optimize def f(x): return 2*x - 1 + 2*np.cos(np.pi*x) x = np.linspace(0.0,2.0,201) y=f(x) plt.plot(x,y,label='$2x - 1 + 2\\\\cos(\\\\pi\u2026","rel":"","context":"In &quot;optimization&quot;","block_context":{"text":"optimization","link":"https:\/\/gantovnik.com\/bio-tips\/category\/optimization\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/01\/ex335.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2634,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/07\/436-non-convex-univariate-function-optimization-using-brents-method-in-python\/","url_meta":{"origin":1867,"position":3},"title":"#436 Non-convex univariate function optimization using Brent&#8217;s method in python","author":"gantovnik","date":"2024-07-18","format":false,"excerpt":"Brent\u2019s method is an optimization algorithm that combines a bisecting algorithm (Dekker\u2019s method) and inverse quadratic interpolation. It can be used for constrained and unconstrained univariate function optimization. The Brent-Dekker method is an extension of the bisection method. It is a root-finding algorithm that combines elements of the secant method\u2026","rel":"","context":"In &quot;optimization&quot;","block_context":{"text":"optimization","link":"https:\/\/gantovnik.com\/bio-tips\/category\/optimization\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex436.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex436.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex436.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1874,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/06\/353-particle-swarm-optimization-using-python\/","url_meta":{"origin":1867,"position":4},"title":"#353 Particle swarm optimization using python","author":"gantovnik","date":"2023-06-28","format":false,"excerpt":"[code language=\"python\"] # pip install pyswarms from pyswarms.single.global_best import GlobalBestPSO import numpy as np # Define characteristics of variables: x_min = [0, 0] x_max = [1, 1] bounds = (x_min, x_max) dim = len(x_min) # Define settings of the algorithm: pop = 100 iterations = 250 options = {'c1': 0.5,\u2026","rel":"","context":"In &quot;optimization&quot;","block_context":{"text":"optimization","link":"https:\/\/gantovnik.com\/bio-tips\/category\/optimization\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":541,"url":"https:\/\/gantovnik.com\/bio-tips\/2020\/05\/doptprm-discrete\/","url_meta":{"origin":1867,"position":5},"title":"DOPTPRM, DISCRETE","author":"gantovnik","date":"2020-05-11","format":false,"excerpt":"DOPTPRM, DISCRETE Bulk Data Entry Discreteness parameter. Influences the tendency for elements in a topology optimization to converge to a material density of 0 or 1. Default DISCRETE=1. Recommended bounds are 0.0 and 2.0 for shells, or 3.0 for solids. Improving Discreteness. There are numerous ways to improve the discreteness\u2026","rel":"","context":"In &quot;HyperMesh&quot;","block_context":{"text":"HyperMesh","link":"https:\/\/gantovnik.com\/bio-tips\/category\/hypermesh\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1867","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/comments?post=1867"}],"version-history":[{"count":0,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1867\/revisions"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=1867"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=1867"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=1867"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}