Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
N
numer-60-2
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Littichai Buddaken
numer-60-2
Commits
f7f9d76a
Commit
f7f9d76a
authored
Apr 19, 2018
by
Littichai Buddaken
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add week12(edited)&week13
parent
a67cb5bd
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
512 additions
and
0 deletions
+512
-0
Numerical Integration-checkpoint.ipynb
....ipynb_checkpoints/Numerical Integration-checkpoint.ipynb
+0
-0
Numerical Integration.ipynb
week12/Numerical Integration.ipynb
+0
-0
Initial-Value-checkpoint.ipynb
week13/.ipynb_checkpoints/Initial-Value-checkpoint.ipynb
+256
-0
Initial-Value.ipynb
week13/Initial-Value.ipynb
+256
-0
No files found.
week12/.ipynb_checkpoints/Numerical Integration-checkpoint.ipynb
0 → 100644
View file @
f7f9d76a
This diff is collapsed.
Click to expand it.
week12/Numerical Integration.ipynb
0 → 100644
View file @
f7f9d76a
This diff is collapsed.
Click to expand it.
week13/.ipynb_checkpoints/Initial-Value-checkpoint.ipynb
0 → 100644
View file @
f7f9d76a
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Optimization\n",
"\n",
"> หาค่า $x$ ที่ทำให้ $F(x)$ มีค่าน้อยที่สุด และทำให้ $g(x) = 0, h(x) \\ge 0$\n",
"\n",
"$F(x)$ - objective function\n",
"\n",
"$x$ - design variable\n",
"\n",
"$g(x), h(x)$ - constraint functions\n",
"\n",
"minimize $F(x)$ = maximize $-F(x)$\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"> จงหาค่า $x$ ในช่วง $[a,b]$ ที่ทำ $f(x)$ มีค่าน้อยที่สุด\n",
"\n",
"## Bracketing\n",
"- $x = a$\n",
"- เลือกค่า $x$ ใหม่ ตามทิศทาง $dx = +\\Delta$ หรือ $dx = -\\Delta$ ที่ทำให้ค่า $f(x+dx) \\lt f(x)$\n",
"- กำหนดให้ $x = x + dx$ ไปเรื่อยๆ จนกว่า $f(x+dx) > f(x)$"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Golden Section Search\n",
"\n",
"```python\n",
"def search(f,a,b,tol=1.0e-9):\n",
" nIter = int(math.ceil(-2.078087*math.log(tol/abs(b-a))))\n",
" R = 0.618033989\n",
" C = 1.0 - R\n",
" # First telescoping\n",
" x1 = R*a + C*b; x2 = C*a + R*b\n",
" f1 = f(x1); f2 = f(x2)\n",
" # Main loop\n",
" for i in range(nIter):\n",
" if f1 > f2:\n",
" a = x1\n",
" x1 = x2; f1 = f2\n",
" x2 = C*a + R*b; f2 = f(x2)\n",
" else:\n",
" b = x2\n",
" x2 = x1; f2 = f1\n",
" x1 = R*a + C*b; f1 = f(x1)\n",
"\n",
" if f1 < f2: return x1,f1\n",
" return x2,f2\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Powell's Method\n",
"\n",
"* Choose a point $x_0$ in the design space\n",
"* Choose the starting vectors $v_i$ , $i=1, 2, . . . , n$ (the usual choice is $v_i = e_i$ \n",
" * where $e_i$ is the unit vector in the $x_i$ -coordinate direction)\n",
"* Cycle:\n",
" * Loop over $i = 1, 2, . . . , n$:\n",
" * Minimize $F(x)$ along the line through $x_{i−1}$ in the direction of $v_i$\n",
" * Let the minimum point be $x_i$\n",
" * End loop.\n",
" * $v_{n+1} \\leftarrow x_0 − x_n$\n",
" * Minimize $F(x)$ along the line through $x_0$ in the direction of $v_{n+1}$\n",
" * Let the minimum point be $x_{n+1}$\n",
" * if $|x n+1 − x 0 | < ε$ exit loop\n",
" * Loop over $i = 1, 2, . . . , n$\n",
" * $v_i = v_{i+1}$ ($v_1$ is discarded; the other vectors are reused).\n",
" * End loop\n",
" * $x_0 = x_{n+1}$\n",
"* End cycle\n",
"\n",
"\n",
"```python\n",
"import numpy as np\n",
"import math\n",
"\n",
"def powell(F,x,h=0.1,tol=1.0e-6):\n",
"\n",
" def f(s): return F(x + s*v)\n",
"\n",
" n = len(x)\n",
" df = np.zeros(n)\n",
" u = np. identity(n)\n",
"\n",
" for j in range(30):\n",
" xOld = x.copy()\n",
" fOld = F(xOld)\n",
" \n",
" for i in range(n):\n",
" v = u[i]\n",
" a,b = bracket(f,0.0,h)\n",
" s,fMin = search(f,a,b)\n",
" df[i] = fOld - fMin\n",
" fOld = fMin\n",
" x = x + s*v\n",
"\n",
" v = x - xOld\n",
" a,b = bracket(f,0.0,h)\n",
" s,fLast = search(f,a,b)\n",
" x = x + s*v\n",
" if math.sqrt(np.dot(x-xOld,x-xOld)/n) < tol: return x,j+1\n",
" iMax = np.argmax(df)\n",
" for i in range(iMax,n-1):\n",
" u[i] = u[i+1]\n",
" u[n-1] = v\n",
"\n",
" print(\"Powell did not converge\")\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## example\n",
"1. จงเขียนโปรแกรมหา $x$ ที่ทำให้ $f(x) = 100(y-x^2)^2 + (1-x)^2$ ด้วยวิธี Powell โดยเริ่มจากจุด $(-1,1)$\n",
"\n",
"```python\n",
"from numpy import array\n",
"\n",
"def F(x): return 100.0*(x[1] - x[0]**2)**2 + (1 - x[0])**2\n",
"\n",
"xStart = array([-1.0, 1.0])\n",
"xMin,nIter = powell(F,xStart)\n",
"print(\"x =\",xMin)\n",
"print(\"F(x) =\",F(xMin))\n",
"print(\"Number of cycles =\",nIter)\n",
"input (\"Press return to exit\")\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Downhil Simplex Method (Nelder-Mead Method)\n",
"\n",
"* Choose a starting simplex\n",
"* Cycle until $d \\le \\epsilon$ \n",
" * Try reflection\n",
" * if new vertex $\\le$ old $Lo$: accept reflection\n",
" * Try expansion\n",
" * if new vertex $\\le$ old $Lo$: accept expansion\n",
" * else:\n",
" * if new vertex $>$ old $Hi$:\n",
" * Try contraction\n",
" * if new vertex $\\le$ old $Hi$: accept contraction\n",
" * else: use shrinkage\n",
" \n",
" \n",
"```python\n",
"import numpy as np\n",
"import math\n",
"\n",
"def downhill(F,xStart,side=0.1,tol=1.0e-6):\n",
" n = len(xStart)\n",
" x = np.zeros((n+1,n))\n",
" f = np.zeros(n+1)\n",
" x[0] = xStart\n",
" for i in range(1,n+1):\n",
" x[i] = xStart\n",
" x[i,i-1] = xStart[i-1] + side\n",
"\n",
" for i in range(n+1): f[i] = F(x[i])\n",
"\n",
" for k in range(500):\n",
" iLo = np.argmin(f)\n",
" iHi = np.argmax(f)\n",
" d = (-(n+1)*x[iHi] + np.sum(x,axis=0))/n\n",
" if math.sqrt(np.dot(d,d)/n) < tol: return x[iLo]\n",
"\n",
" # Try reflection\n",
" xNew = x[iHi] + 2.0*d\n",
" fNew = F(xNew)\n",
" if fNew <= f[iLo]:\n",
" # Accept reflection\n",
" x[iHi] = xNew\n",
" f[iHi] = fNew\n",
"\n",
" # Try expanding the reflection\n",
" xNew = x[iHi] + d\n",
" fNew = F(xNew)\n",
" if fNew <= f[iLo]:\n",
" # Accept expansion\n",
" x[iHi] = xNew\n",
" f[iHi] = fNew\n",
" else:\n",
" if fNew <= f[iHi]:\n",
" # Accept reflection\n",
" x[iHi] = xNew\n",
" f[iHi] = fNew\n",
" else:\n",
" # Try contraction\n",
" xNew = x[iHi] + 0.5*d\n",
" fNew = F(xNew)\n",
" if fNew <= f[iHi]: # Accept contraction\n",
" x[iHi] = xNew\n",
" f[iHi] = fNew\n",
" else:\n",
" # Use shrinkage\n",
" for i in range(len(x)):\n",
" if i != iLo:395\n",
" x[i] = (x[i] - x[iLo])*0.5\n",
" f[i] = F(x[i])\n",
"\n",
" print(\"Too many iterations in downhill\")\n",
" return x[iLo]\n",
"```"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.6.1"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
week13/Initial-Value.ipynb
0 → 100644
View file @
f7f9d76a
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Optimization\n",
"\n",
"> หาค่า $x$ ที่ทำให้ $F(x)$ มีค่าน้อยที่สุด และทำให้ $g(x) = 0, h(x) \\ge 0$\n",
"\n",
"$F(x)$ - objective function\n",
"\n",
"$x$ - design variable\n",
"\n",
"$g(x), h(x)$ - constraint functions\n",
"\n",
"minimize $F(x)$ = maximize $-F(x)$\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"> จงหาค่า $x$ ในช่วง $[a,b]$ ที่ทำ $f(x)$ มีค่าน้อยที่สุด\n",
"\n",
"## Bracketing\n",
"- $x = a$\n",
"- เลือกค่า $x$ ใหม่ ตามทิศทาง $dx = +\\Delta$ หรือ $dx = -\\Delta$ ที่ทำให้ค่า $f(x+dx) \\lt f(x)$\n",
"- กำหนดให้ $x = x + dx$ ไปเรื่อยๆ จนกว่า $f(x+dx) > f(x)$"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Golden Section Search\n",
"\n",
"```python\n",
"def search(f,a,b,tol=1.0e-9):\n",
" nIter = int(math.ceil(-2.078087*math.log(tol/abs(b-a))))\n",
" R = 0.618033989\n",
" C = 1.0 - R\n",
" # First telescoping\n",
" x1 = R*a + C*b; x2 = C*a + R*b\n",
" f1 = f(x1); f2 = f(x2)\n",
" # Main loop\n",
" for i in range(nIter):\n",
" if f1 > f2:\n",
" a = x1\n",
" x1 = x2; f1 = f2\n",
" x2 = C*a + R*b; f2 = f(x2)\n",
" else:\n",
" b = x2\n",
" x2 = x1; f2 = f1\n",
" x1 = R*a + C*b; f1 = f(x1)\n",
"\n",
" if f1 < f2: return x1,f1\n",
" return x2,f2\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Powell's Method\n",
"\n",
"* Choose a point $x_0$ in the design space\n",
"* Choose the starting vectors $v_i$ , $i=1, 2, . . . , n$ (the usual choice is $v_i = e_i$ \n",
" * where $e_i$ is the unit vector in the $x_i$ -coordinate direction)\n",
"* Cycle:\n",
" * Loop over $i = 1, 2, . . . , n$:\n",
" * Minimize $F(x)$ along the line through $x_{i−1}$ in the direction of $v_i$\n",
" * Let the minimum point be $x_i$\n",
" * End loop.\n",
" * $v_{n+1} \\leftarrow x_0 − x_n$\n",
" * Minimize $F(x)$ along the line through $x_0$ in the direction of $v_{n+1}$\n",
" * Let the minimum point be $x_{n+1}$\n",
" * if $|x n+1 − x 0 | < ε$ exit loop\n",
" * Loop over $i = 1, 2, . . . , n$\n",
" * $v_i = v_{i+1}$ ($v_1$ is discarded; the other vectors are reused).\n",
" * End loop\n",
" * $x_0 = x_{n+1}$\n",
"* End cycle\n",
"\n",
"\n",
"```python\n",
"import numpy as np\n",
"import math\n",
"\n",
"def powell(F,x,h=0.1,tol=1.0e-6):\n",
"\n",
" def f(s): return F(x + s*v)\n",
"\n",
" n = len(x)\n",
" df = np.zeros(n)\n",
" u = np. identity(n)\n",
"\n",
" for j in range(30):\n",
" xOld = x.copy()\n",
" fOld = F(xOld)\n",
" \n",
" for i in range(n):\n",
" v = u[i]\n",
" a,b = bracket(f,0.0,h)\n",
" s,fMin = search(f,a,b)\n",
" df[i] = fOld - fMin\n",
" fOld = fMin\n",
" x = x + s*v\n",
"\n",
" v = x - xOld\n",
" a,b = bracket(f,0.0,h)\n",
" s,fLast = search(f,a,b)\n",
" x = x + s*v\n",
" if math.sqrt(np.dot(x-xOld,x-xOld)/n) < tol: return x,j+1\n",
" iMax = np.argmax(df)\n",
" for i in range(iMax,n-1):\n",
" u[i] = u[i+1]\n",
" u[n-1] = v\n",
"\n",
" print(\"Powell did not converge\")\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## example\n",
"1. จงเขียนโปรแกรมหา $x$ ที่ทำให้ $f(x) = 100(y-x^2)^2 + (1-x)^2$ ด้วยวิธี Powell โดยเริ่มจากจุด $(-1,1)$\n",
"\n",
"```python\n",
"from numpy import array\n",
"\n",
"def F(x): return 100.0*(x[1] - x[0]**2)**2 + (1 - x[0])**2\n",
"\n",
"xStart = array([-1.0, 1.0])\n",
"xMin,nIter = powell(F,xStart)\n",
"print(\"x =\",xMin)\n",
"print(\"F(x) =\",F(xMin))\n",
"print(\"Number of cycles =\",nIter)\n",
"input (\"Press return to exit\")\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Downhil Simplex Method (Nelder-Mead Method)\n",
"\n",
"* Choose a starting simplex\n",
"* Cycle until $d \\le \\epsilon$ \n",
" * Try reflection\n",
" * if new vertex $\\le$ old $Lo$: accept reflection\n",
" * Try expansion\n",
" * if new vertex $\\le$ old $Lo$: accept expansion\n",
" * else:\n",
" * if new vertex $>$ old $Hi$:\n",
" * Try contraction\n",
" * if new vertex $\\le$ old $Hi$: accept contraction\n",
" * else: use shrinkage\n",
" \n",
" \n",
"```python\n",
"import numpy as np\n",
"import math\n",
"\n",
"def downhill(F,xStart,side=0.1,tol=1.0e-6):\n",
" n = len(xStart)\n",
" x = np.zeros((n+1,n))\n",
" f = np.zeros(n+1)\n",
" x[0] = xStart\n",
" for i in range(1,n+1):\n",
" x[i] = xStart\n",
" x[i,i-1] = xStart[i-1] + side\n",
"\n",
" for i in range(n+1): f[i] = F(x[i])\n",
"\n",
" for k in range(500):\n",
" iLo = np.argmin(f)\n",
" iHi = np.argmax(f)\n",
" d = (-(n+1)*x[iHi] + np.sum(x,axis=0))/n\n",
" if math.sqrt(np.dot(d,d)/n) < tol: return x[iLo]\n",
"\n",
" # Try reflection\n",
" xNew = x[iHi] + 2.0*d\n",
" fNew = F(xNew)\n",
" if fNew <= f[iLo]:\n",
" # Accept reflection\n",
" x[iHi] = xNew\n",
" f[iHi] = fNew\n",
"\n",
" # Try expanding the reflection\n",
" xNew = x[iHi] + d\n",
" fNew = F(xNew)\n",
" if fNew <= f[iLo]:\n",
" # Accept expansion\n",
" x[iHi] = xNew\n",
" f[iHi] = fNew\n",
" else:\n",
" if fNew <= f[iHi]:\n",
" # Accept reflection\n",
" x[iHi] = xNew\n",
" f[iHi] = fNew\n",
" else:\n",
" # Try contraction\n",
" xNew = x[iHi] + 0.5*d\n",
" fNew = F(xNew)\n",
" if fNew <= f[iHi]: # Accept contraction\n",
" x[iHi] = xNew\n",
" f[iHi] = fNew\n",
" else:\n",
" # Use shrinkage\n",
" for i in range(len(x)):\n",
" if i != iLo:395\n",
" x[i] = (x[i] - x[iLo])*0.5\n",
" f[i] = F(x[i])\n",
"\n",
" print(\"Too many iterations in downhill\")\n",
" return x[iLo]\n",
"```"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.6.1"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment