{"id":495,"date":"2020-05-03T03:23:10","date_gmt":"2020-05-03T10:23:10","guid":{"rendered":"http:\/\/gantovnik.com\/bio-tips\/?p=495"},"modified":"2024-07-14T19:21:53","modified_gmt":"2024-07-15T02:21:53","slug":"optimization-with-constraints","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2020\/05\/optimization-with-constraints\/","title":{"rendered":"#72 Optimization with constraints using scipy in python"},"content":{"rendered":"<p><img data-recalc-dims=\"1\" decoding=\"async\" class=\"alignnone size-full wp-image-497\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2020\/05\/ex72.png?resize=600%2C400\" alt=\"ex72\" width=\"600\" height=\"400\" srcset=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2020\/05\/ex72.png?w=600&amp;ssl=1 600w, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2020\/05\/ex72.png?resize=300%2C200&amp;ssl=1 300w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport os\nfrom scipy.optimize import minimize\nimport matplotlib.pyplot as plt \nimport numpy as np\n\nos.chdir(r&amp;#039;D:\\projects\\wordpress\\ex72&amp;#039;) \nos.getcwd()\n\ndef f(X):\n    x, y = X\n    return (x - 1)&lt;strong&gt;2 + (y - 1)&lt;\/strong&gt;2\n\ndef func_X_Y_to_XY(f, X, Y):\n#Wrapper for f(X, Y) -&amp;gt; f(&#x5B;X, Y])\n    s = np.shape(X)\n    return f(np.vstack(&#x5B;X.ravel(), Y.ravel()])).reshape(*s)\n\nx_opt = minimize(f, &#x5B;1, 1], method=&amp;#039;BFGS&amp;#039;).x\nbnd_x1, bnd_x2 = (2, 3), (0, 2)\nx_cons_opt = minimize(f, &#x5B;1, 1], method=&amp;#039;L-BFGS-B&amp;#039;, bounds=&#x5B;bnd_x1, bnd_x2]).x\nfig, ax = plt.subplots(figsize=(6, 4))\nx_ = y_ = np.linspace(-1, 3, 100)\nX, Y = np.meshgrid(x_, y_)\nc = ax.contour(X, Y, func_X_Y_to_XY(f, X, Y), 50)\nax.plot(x_opt&#x5B;0], x_opt&#x5B;1], &amp;#039;b&lt;em&gt;&amp;#039;, markersize=15)\nax.plot(x_cons_opt&#x5B;0], x_cons_opt&#x5B;1], &amp;#039;r&lt;\/em&gt;&amp;#039;, markersize=15)\nbound_rect = plt.Rectangle((bnd_x1&#x5B;0], bnd_x2&#x5B;0]),\nbnd_x1&#x5B;1] - bnd_x1&#x5B;0], bnd_x2&#x5B;1] -\nbnd_x2&#x5B;0], facecolor=&amp;quot;grey&amp;quot;)\nax.add_patch(bound_rect)\nax.set_xlabel(r&amp;quot;$x_1$&amp;quot;, fontsize=14)\nax.set_ylabel(r&amp;quot;$x_2$&amp;quot;, fontsize=14)\nplt.colorbar(c, ax=ax)\nfig.tight_layout() \nplt.savefig(&amp;quot;ex72.png&amp;quot;, dpi=100) \nplt.show() \nplt.close()\n<\/pre>\n<p>\u00a0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>import os from scipy.optimize import minimize import matplotlib.pyplot as plt import numpy as np os.chdir(r&amp;#039;D:\\projects\\wordpress\\ex72&amp;#039;) os.getcwd() def f(X): x, y = X return (x &#8211; 1)&lt;strong&gt;2 + (y &#8211; 1)&lt;\/strong&gt;2 def func_X_Y_to_XY(f, X, Y): #Wrapper for f(X, Y) -&amp;gt; f(&#x5B;X, Y]) s = np.shape(X) return f(np.vstack(&#x5B;X.ravel(), Y.ravel()])).reshape(*s) x_opt = minimize(f, &#x5B;1, 1], method=&amp;#039;BFGS&amp;#039;).x bnd_x1, bnd_x2 [&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":"","_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":[9,2,71],"tags":[25,72],"class_list":["post-495","post","type-post","status-publish","format-standard","hentry","category-optimization","category-python","category-scipy","tag-optimization","tag-scipy"],"modified_by":"gantovnik","jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-7Z","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":117,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/optimization-with-contraints\/","url_meta":{"origin":495,"position":0},"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":104,"url":"https:\/\/gantovnik.com\/bio-tips\/2018\/12\/unconstrained-multivariate-optimization\/","url_meta":{"origin":495,"position":1},"title":"Unconstrained multivariate optimization","author":"gantovnik","date":"2018-12-31","format":false,"excerpt":"import os import matplotlib.pyplot as plt import numpy as np import scipy import sympy os.chdir('\/home\/vg\/Downloads\/projects\/ex18') os.getcwd() x1,x2=sympy.symbols(\"x_1,x_2\") f_sym=(x1-1)**4 + 5*(x2-1)**2 - 2*x1*x2 fprime_sym=[f_sym.diff(x_) for x_ in (x1,x2)] sympy.Matrix(fprime_sym) fhess_sym=[[f_sym.diff(x1_,x2_) for x1_ in (x1,x2)] for x2_ in (x1,x2)] sympy.Matrix(fhess_sym) f_lmbda=sympy.lambdify((x1,x2),f_sym,'numpy') fprime_lmbda=sympy.lambdify((x1,x2),fprime_sym,'numpy') fhess_lmbda=sympy.lambdify((x1,x2),fhess_sym,'numpy') def func_XY_to_xy(f): return lambda X: np.array(f(X[0],X[1])) f=func_XY_to_xy(f_lmbda) fprime=func_XY_to_xy(fprime_lmbda) fhess=func_XY_to_xy(fhess_lmbda)\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example18","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example18.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example18.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example18.png?resize=525%2C300 1.5x"},"classes":[]},{"id":101,"url":"https:\/\/gantovnik.com\/bio-tips\/2018\/12\/solution-of-system-of-nonlinear-equations\/","url_meta":{"origin":495,"position":2},"title":"Solution of system of nonlinear equations","author":"gantovnik","date":"2018-12-31","format":false,"excerpt":"import os import matplotlib.pyplot as plt import numpy as np import scipy os.chdir('\/home\/vg\/Downloads\/projects\/ex17') os.getcwd() def f(x): return [x[1]-x[0]**3-2*x[0]**2+1,x[1]+x[0]**2-1] tol=0.1 a,b=-2,2 x=np.linspace(-3,2,5000) y1=x**3+2*x**2-1 y2=-x**2+1 fig,ax=plt.subplots(figsize=(8,4)) ax.plot(x,y1,'k',lw=1.5) ax.plot(x,y2,'k',lw=1.5) sol1=scipy.optimize.fsolve(f,[-2,2]) sol2=scipy.optimize.fsolve(f,[1,-1]) sol3=scipy.optimize.fsolve(f,[-2,-5]) sols=[sol1,sol2,sol3] colors=['r','b','g'] for idx,s in enumerate(sols): ax.plot(s[0],s[1],colors[idx]+'*',markersize=15) for m in np.linspace(-4,3,80): for n in np.linspace(-20,20,40): x_guess=[m,n] sol=scipy.optimize.fsolve(f,x_guess) idx = (abs(sols-sol)**2).sum(axis=1).argmin() ax.plot(x_guess[0],x_guess[1],colors[idx]+'.')\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example17","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example17.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example17.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example17.png?resize=525%2C300 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":495,"position":3},"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":157,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/numerical-integration-of-odes-using-scipy\/","url_meta":{"origin":495,"position":4},"title":"Numerical integration of ODEs using SciPy","author":"gantovnik","date":"2019-01-09","format":false,"excerpt":"import os import numpy as np import matplotlib.pyplot as plt from scipy import integrate import sympy os.chdir(r'D:\\projects\\wordpress\\ex35') os.getcwd() def plot_direction_field(x, y_x, f_xy, x_lim=(-5, 5), y_lim=(-5, 5), ax=None): f_np = sympy.lambdify((x, y_x), f_xy, 'numpy') x_vec = np.linspace(x_lim[0], x_lim[1], 20) y_vec = np.linspace(y_lim[0], y_lim[1], 20) if ax is None: _, ax =\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example35","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example35.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":96,"url":"https:\/\/gantovnik.com\/bio-tips\/2018\/12\/bisection-method\/","url_meta":{"origin":495,"position":5},"title":"#16 Bisection method using python","author":"gantovnik","date":"2018-12-31","format":false,"excerpt":"[code language=\"python\"] import os import matplotlib.pyplot as plt import numpy as np os.chdir('\/home\/vg\/Downloads\/projects\/ex16') os.getcwd() f = lambda x: np.exp(x)-2 tol=0.1 a,b=-2,2 x=np.linspace(-2.1,2.1,1000) fig,ax=plt.subplots(1,1,figsize=(12,4)) ax.plot(x,f(x),lw=1.5) ax.axhline(0,ls=':',color='k') ax.set_xticks([-2,-1,0,1,2]) ax.set_xlabel(r'$x$',fontsize=18) ax.set_ylabel(r'$f(x)$',fontsize=18) fa,fb=f(a),f(b) ax.plot(a,fa,'ko') ax.plot(b,fb,'ko') ax.text(a,fa+0.5,r\"$a$\",ha='center',fontsize=18) ax.text(b,fb+0.5,r\"$b$\",ha='center',fontsize=18) n=1 while b-a &gt; tol: m=a+(b-a)\/2 fm=f(m) ax.plot(m,fm,'ko') ax.text(m,fm-0.5,r\"$m_%d$\" % n,ha='center') n=n+1 if (np.sign(fa)==np.sign(fm)): a,fa=m,fm else: b,fb=m,fm\u2026","rel":"","context":"In &quot;optimization&quot;","block_context":{"text":"optimization","link":"https:\/\/gantovnik.com\/bio-tips\/category\/optimization\/"},"img":{"alt_text":"example16","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example16.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example16.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example16.png?resize=525%2C300 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/495","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=495"}],"version-history":[{"count":2,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/495\/revisions"}],"predecessor-version":[{"id":2485,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/495\/revisions\/2485"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=495"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=495"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=495"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}